ToolszkEVMArchitectureZkproverHashing state machines

Hashing state machines

For hashing, the zkEVM utilizes two state machines: the Keccak state machine and the Poseidon state machine. The Keccak-256 hash function is used for seamless E

For hashing, the zkEVM utilizes two state machines: the Keccak state machine and the Poseidon state machine. The Keccak-256 hash function is used for seamless EVM compatibility, whereas Poseidon is best suited for the zkProver context because it is a STARK-friendly hash (SFH) function.

The sponge construction

By design, Keccak and Poseidon are both sponge constructions. A generic sponge construction is a simple iterated construction for building a function:

F:ZZlF: \mathbb{Z}^* \to \mathbb{Z}^l

with an input of variable-length and arbitrary output length based on a fixed-length permutation:

f:ZbZbf: \mathbb{Z}^b \to \mathbb{Z}^b

operating on a fixed number bb of bits.

The array of bb bits that ff keeps transforming is called the state, and bb is called the width of the state.

The state array is split into two chunks, one with rr bits and the other with cc bits. The width b=r+cb = r + c, where rr is called the bitrate (or simply rate) and cc is called the capacity.

Sponge construction phases

The elements that completely describe a single instance of a sponge construction are: the fixed-length permutation ff, the padding rule pad, the bitrate value rr, and the capacity cc.

A schema of the sponge construction is shown in the below figure.

A Sponge Function Construction

Initializing phase

The input string is either padded to reach the rr-bit length (if it was shorter than rr bits) or split into rr-bit long chunks, with the last one padded to reach the rr-bit length (if it was longer than rr bits). A hash function-specific reversible padding rule is used.

The state of the hash function is initialized to a bb-bit vector (or array) of zeros.

Absorbing phase

During this phase, the rr-bit input blocks are XOR-ed sequentially with the first rr bits of the state, intermixed with permutation function ff applications. This process is repeated until all input blocks have been XOR-ed with the state.

Take note that the last cc bits, which correspond to the capacity value, do not absorb any external input.

Squeezing phase

The first rr bits of the state are returned as output blocks in this phase, intermixed with applications of the function ff. The number of output blocks is entirely up to the user.

Keep in mind that the last cc bits, which correspond to the capacity value, are never output during this phase. Actually, if the output is longer than the specified length, it is truncated to the required size.

Edit on GitHub

Last updated on