Skip to main content
This section describes the traits and implementations of hash functions used in the Merkle commitment scheme. S-two supports two hash functions: BLAKE2s-256 and Poseidon252. Here, we focus on the implementation for BLAKE2s-256; Poseidon252 is implemented similarly (see Poseidon reference).

MerkleHasher Trait

The MerkleHasher trait defines the interface for hash functions used in Merkle trees. Its main function, hash_node, computes the hash of a node from its children and (optionally) column values:

Implementation for BLAKE2s-256

The MerkleHasher implementation for BLAKE2s-256 uses a wrapper struct Blake2sHash:
The trait implementation is provided by Blake2sMerkleHasher:
In this Merkle tree implementation, node hashes are computed using both the children hashes and the column values. This differs from standard Merkle trees, where node hashes typically depend only on the children. More details are discussed in the next sections.

MerkleOps Trait

The MerkleOps trait defines Merkle tree operations for a commitment scheme, parameterized by a MerkleHasher. Its main function, commit_on_layer, takes the previous layer’s hashes and the current layer’s column values to generate the hashes for the next layer:

Implementation for BLAKE2s-256

The MerkleOps<Blake2sMerkleHasher> trait implementation for the CpuBackend is as follows:
In the next section, we will use these hash function implementations to describe the prover of the Merkle commitment scheme.