Blockchain across Oracle
上QQ阅读APP看书,第一时间看更新

The types of hash functions used by blockchain

In the world of hash functions, there are a lot of different types, as can be found at http://www.wikiwand.com/en/List_of_hash_functions. In the context of blockchain, the main hash functions involved include RIPEMD-160, SHA(2)-256, and KECCAK-256. Both RIPEMD-160 and SHA(2)-256 are used by the original blockchain, Bitcoin core code for hashing, but with a slight twist; that is, it defines its own hash functions as follows:

hash160(privateKey) = RIPEMD-160(SHA-256(privateKey))
hash256(byteArray) = SHA-256(SHA-256(byteArray))

The first function is used for identifying a destination, otherwise called the address, and it is computed by hashing your private key first with SHA-256 and then immediately after with the RIPEMD-160 hash function. The second function is used for hashing two core entities: transactions and blocks. The hash function is used to generate a transactionId or a blockId by hashing the data using SHA-256 and then hashing the 256-bit output again using SHA-256. We will explore the concept behind blocks later in this chapter.

How do SHA2/Merkle-Damg?rd hash functions work?

Hash functions, such as MD5, RIPEMD-160, and SHA-256, fall under a class of hash algorithms called Merkle–Damg?rd. These form a construction or method of building cryptographic hash functions that are collision resistant from collision-resistant, one-way compression functions.



In the preceding diagram, the function receives an input, which first applies a left-padding function to create a result with a size that is a multiple of a fixed number (such as 512 or 1024). The function then breaks the result up into blocks of that fixed number and processes one block of data at a time with the compression function. Each time the new block inputted, it is combined with the result of the previous round. To make the result more secure, the last block is padded with zeros as needed and bits that represent the length of the entire message. Sometimes, the function serves as a finalization function to harden the hash.

As I mentioned, other, often newer, blockchains use different hashing algorithms. For example, Ethereum uses KECCAK-256, a precursor of SHA3-256, to identify an account by hashing the private key. The main reason that Ethereum changed its algorithm is because previously with SHA-2, there were a number of attacks discovered. Thus, there was a growing fear that the secure SHA-2 algorithm would soon be broken. The creators chose KECCAK-256, as it was a totally different algorithm to SHA and AES.

How do SHA3/Sponge hash functions work?

Hash algorithms such as KECCAK-256 and SHA3-x fall into a class of hash functions called Sponge. This is a construction or method of building cryptographic hash functions with a limited internal state. It takes an input stream of bits of any length and produces an output stream of bits of any desired length. 

In the following diagram, the function receives an input. When executed, the state memory ( S) is initialized with 0 bits ( b). The state memory is divided into two parts (bitrate r and capacity c). The capacity ( c) part is always a whole multiple of bitrate ( r) by padding enough bits. The input is split into blocks P i of a fixed length, and put into a function ( f) that permutes or transforms the state memory. After the transformation, the output blocks Z i are hashed and are combined into an output string:

Digital signatures, such as the transactionId, blockId, and address are a fundamental part of blockchains and provide identity validation and hashes, which allows us to keep the integrity of the blockchain, and represent its current (potentially infinite) state. They are more powerful and credible, compared to physically signing a check or contract, because this kind of signature can be highly inconsistent and easily forged, imitated, or supplanted as opposed to the immutable nature of a blockchain hash.

These kinds of signatures are also used to identify transactions. Instead of describing a transaction, for example, as "Packt sends Developer X a total of Y units of currency Z at date and time DT", it is referenced by the unique digital signature or hash. By referring to a hash, it can be used directly to track and trace the transaction, for example, copy and paste the hash in a UI (such as blockchain explorer) to see details of the transaction. Usually, a user interface hides any kind of IDs or serial numbers, but as the use of technologies, such as blockchain, which use cryptographic hash functions become more widespread, showing a hash is often better than displaying a long description.