# Hash Algorithm Design of Blockchain

Hash algorithm is an important technology in the field of information technology, which is widely used in blockchain. After binary data of any length is mapped to the hash function, it is converted to fixed-length output, and the output value is the hash value of the data. There are many hash functions, and the hash algorithm is the most widely used in blockchain technology. Using this algorithm, binary data of any length can be converted into hash values of 256 bits and 32 bytes. The hash algorithm has the advantages of unidirectional, random, timing, fixed length, and so on.

* (1) Unidirectionality: the output data can be easily extracted from the input data, but the input data cannot be calculated from the output data.
* (2) Randomness: even if the input data is only slightly modified, the output data may change greatly, so different input data will not produce the same hash value.
* (3) Timing: any input data with different lengths will consume approximately the same time period through the same type of hash calculation.
* (4) Fixed length: any input data with different lengths can generate output data with the same length after the hash calculation of the same type.

Hash function is usually defined according to the generated hash value. There are two main methods.

Hash based on addition and multiplication is achieved by returning data elements and then adding each initial value associated with the data elements. Usually, the value of an element is multiplied by a prime number.

The following formulas provide a hash calculation method based on addition and multiplication:

![mathematical equation](https://onlinelibrary.wiley.com/cms/asset/e2ebed99-7367-4d14-97c2-5416c3f9c8f9/misy9943452-math-0004.png)(4)

![mathematical equation](https://onlinelibrary.wiley.com/cms/asset/6328124a-d897-4b36-be84-2c53f100e915/misy9943452-math-0005.png)(5)

![mathematical equation](https://onlinelibrary.wiley.com/cms/asset/6acafd01-7503-43b5-9781-d7292e912474/misy9943452-math-0006.png)(6)

![mathematical equation](https://onlinelibrary.wiley.com/cms/asset/d6b341f4-c9e5-441b-bfbe-aa65f235e8bf/misy9943452-math-0007.png)(7)

Like additive hashing, shift-based hashing also uses each element of the data string, but unlike additive hashing, shift-based hashing algorithms prefer to transmit data. It is usually a combination of left and right transfer operations, and the number of transfers is the quality. The result of each shift is only some additional aggregation operations, and the final result of shift calculation is the final result of change hash calculation. The formula of the shift hash algorithm is shown in the following formulas:

![mathematical equation](https://onlinelibrary.wiley.com/cms/asset/798bf549-b9e1-4b96-bc57-7824417ffb68/misy9943452-math-0008.png)(8)

![mathematical equation](https://onlinelibrary.wiley.com/cms/asset/6406f659-acff-4638-8936-9a3c49b6b269/misy9943452-math-0009.png)(9)

![mathematical equation](https://onlinelibrary.wiley.com/cms/asset/9cb22f65-238d-4746-84f2-48d977213408/misy9943452-math-0010.png)(10)

Merkel is a binary hash tree. In the Merkel tree, each leaf node is the hash value of a transaction. This article takes the Merkel tree with only four leaves as an example.

* (1) Use the sha256 hash algorithm twice to calculate the hash value of each transaction and calculate the hash value of each transaction. Here, four hash values of *HA*, *HB*, *HC*, and *HD* can be calculated, that is, the four-leaf nodes of the Merkel tree. article paper takes *HA* calculation as an example:

  ![mathematical equation](https://onlinelibrary.wiley.com/cms/asset/51590463-9727-4a4d-94bc-20bc93363a5c/misy9943452-math-0011.png)(11)
* (2) Combine the hash values of two leaf nodes *H*A and *H*B. This article takes HAB calculation as an example:

  ![mathematical equation](https://onlinelibrary.wiley.com/cms/asset/38a11e25-871f-4f7f-82c5-9b80a564568e/misy9943452-math-0012.png)(12)
* (3) Perform the same combined hash calculation as in step (2) on the existing two hash values HAB and HCD, and finally, get a new hash value HABCD. The calculation of HABCD is as follows:

  ![mathematical equation](https://onlinelibrary.wiley.com/cms/asset/242e8e38-8221-498b-80e2-8d4befb6315a/misy9943452-math-0013.png)(13)

At present, the workload proof mechanism is considered to be the most efficient blockchain consensus algorithm, but it takes a long time to reach a consensus, which will consume a lot of computing power and resources, so it is not suitable for large-scale promotion and application. The characteristics of various consensus algorithms widely used in blockchain

1. Comparison of common consensus algorithms.

<table><thead><tr><th width="322">Feature	</th><th> PoS</th><th>PoW	</th><th>DPoS</th><th>PBFT</th></tr></thead><tbody><tr><td>Transaction throughput</td><td>General</td><td>Low</td><td>High</td><td>High</td></tr><tr><td>Fault tolerance</td><td>&#x3C;50%</td><td>&#x3C;50%</td><td>&#x3C;50%</td><td>&#x3C;50%</td></tr><tr><td>Single transaction speed</td><td>min</td><td>10 min</td><td>s</td><td>ms</td></tr><tr><td>Number of supported nodes</td><td>Many</td><td>Many</td><td>Many</td><td>Less</td></tr><tr><td>Byzantine fault tolerance</td><td>Support</td><td>Support</td><td>Support</td><td>Support</td></tr><tr><td>Computing resource consumption</td><td>General</td><td>High</td><td>General</td><td>Low</td></tr><tr><td>Network resource consumption</td><td>General</td><td>Lower</td><td>General</td><td>High</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.extremebtc.io/basics/hash-algorithm-design-of-blockchain.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
