Hash functions in blockchain

1. Hash is an encryption algorithm

(1) Hash function (Hash Function), also known as hash function, hash algorithm.
The hash function is a public function that can map a message M of any length into a shorter and fixed-length value H(M) , and H(M) is called a hash value, hash value (Hash Value), hash Value or Message Digest.
(2) It is a one-way cryptographic system, that is, an irreversible mapping from plaintext to ciphertext, with only encryption and no decryption.
(3) Its function expression is: h=H(m) Function
description:
m: Any length message (different algorithm implementations have different length limits, some hash functions (SHA-3) do not limit the message length, some limit (SHA-2), but even if there is a limit, its length is very large, it can be considered as an arbitrary length message)
H: hash function
h: fixed-length hash value

Regardless of the digital format of the input or the size of the file, the output is a fixed-length bit string. Take the Sha256 algorithm used by Bitcoin as an example, no matter what data file is input, the output is 256bit

2. Characteristics of the Hash function

A good hash function must have three characteristics: variable-length input and fixed-length output, anti-collision, and irreversibility.
(1) Indefinite length input, fixed length output
The so-called indefinite length input and fixed length output, we have already mentioned it in the previous article. That is, no matter how long or how big the input data is, the length and format of the output data are fixed. For example, if you choose sha256, then your output is 256 bits.
(2) Anti-collision
If x is not equal to y, but H(x) is equal to H(y), then we say that the function H() is not anti-collision. On the contrary, we consider it to be anti-collision. A good hash function must be anti-collision.
(3) Irreversibility (single item )
given the hash function H() and the input data, the hash value can be easily solved, but given the hash value and the hash function, it is almost impossible to solve the input data. This is irreversibility, also known as one-way.

An excellent hash algorithm will be able to achieve:
(1) Forward fast: given plaintext and hash algorithm, the hash value can be calculated within limited time and limited resources.
(2) Reverse difficulty: given (several) hash values, it is difficult (basically impossible) to reverse the plaintext within a limited time.
(3) Input sensitivity: If the original input information is modified a little, the resulting hash values ​​should look very different.
(4) Conflict avoidance: It is difficult to find two pieces of plaintext with different contents so that their hash values ​​are consistent (collision occurs).

3. The role of hash function in blockchain

(1) Quick verification. In the blockchain, the hash function generates summaries of various data. When comparing two data for equality, you only need to compare their summaries. For example, to compare whether two transactions are equal, you only need to compare the hash values ​​of the two, which is fast and convenient.
(2) Prevent tampering. To transfer a piece of data, to ensure that it is not tampered with during the transfer process, you only need to transfer its digest at the same time. The person who receives the data regenerates a summary of the data, and then compares whether the passed summary and the generated summary are equal. If they are equal, it means that the data has not been tampered with during the transmission process.
(3) Proof of workload for the POW consensus algorithm. This is mainly used in the pow consensus algorithm. In detail, it is given certain data, and then let you find other data, and the hash value calculated by combining them is less than a certain value. Bitcoin and the current Ethereum all use the POW consensus.

POW Introduction Proof of Work, proof of work.
The POW consensus algorithm mainly determines who will generate blocks by calculating the difficulty value. The workload of POW refers to the solution of the equation. Whoever solves it first has the right to produce blocks. The equation is to calculate the hash value of the next block through the hash value of the previous block and the random value nonce. Whoever finds the nonce first can calculate the hash value of the next block first. This way The reason why it is called the calculation difficulty value is that there is no fixed solution to the equation, and we can only keep trying. This way of solving the equation is called hash collision, which is a probabilistic event. The more collisions, the more difficult it will be to solve the equation. big. Bitcoin uses POW consensus algorithm

Guess you like

Origin blog.csdn.net/weixin_42694422/article/details/119853859