Hash

The Hash operation computes the root hash of the entire trie. This root hash uniquely represents the current state of the trie. Any change, no matter how small, results in a completely different root hash. This property is vital for ensuring data integrity and is a fundamental aspect of Merkle trees.

🪜 Steps to Compute the Root Hash:

  1. Fetch the Current Root: Start the hashing process from the current root of the trie.

  2. Leaf Node Hashing: If the current node is a leaf node:

    • Serialize the node's key and value (RLP + Raw Data).

    • Hash the serialized data using a cryptographic hash function Keccak256.

  3. Branch Node Hashing:

    • If the current node is a branch node:

      • Recursively compute the hash for each of its children.

      • Serialize the hashes of the child nodes along with any value stored in the branch node itself (RLP + Raw Data).

      • Hash the serialized data using a cryptographic hash function Keccak256.

  4. Extension Node Hashing:

    • If the current node is an extension node:

      • Compute the hash for its child node.

      • Serialize the node's nibble sequence along with the child hash (RLP + Raw Data).

      • Hash the serialized data using a cryptographic hash function Keccak256.

  5. HashNode Handling:

    • If you encounter a HashNode:

      • Fetch the actual node it references.

      • Proceed with computing the hash for the fetched node.

  6. Return the Root Hash:

    • After hashing the root node (and recursively hashing its children), the resultant hash value represents the root hash of the trie.

Last updated