Skip to main content

Overview

The crypto_hash module provides cryptographic hash functions designed for efficient use in zero-knowledge proof systems. The primary hash function is Poseidon, an algebraic hash optimized for ZK circuits.

Poseidon Hash

Poseidon Struct

The Poseidon hash function with fixed output size.
PrimeField
The prime field over which the hash function operates
const usize
The rate of the sponge (number of field elements absorbed per permutation)

Methods

setup

Initializes a new Poseidon hash function with default parameters.
Poseidon<F, RATE>
A new Poseidon instance with default parameters for the field
Example:

evaluate

Evaluates the hash function over a list of field elements.
&[F]
Slice of field elements to hash
F
The hash output as a single field element
Example:

evaluate_many

Evaluates the hash function and returns multiple output elements.
&[F]
Slice of field elements to hash
usize
Number of field elements to output
Vec<F>
Vector of hash output field elements
Example:

evaluate_with_len

Evaluates the hash function, including the input length in the hash.
&[F]
Slice of field elements to hash
F
The hash output including length commitment
Note: This method prepends the length to prevent length-extension attacks. Example:

PoseidonSponge

PoseidonSponge Struct

A duplex sponge construction using the Poseidon permutation.
const usize
Number of field elements absorbed/squeezed per permutation
const usize
Number of field elements in the capacity (typically 1 for 128-bit security)

AlgebraicSponge Implementation

PoseidonSponge implements the AlgebraicSponge trait for Fiat-Shamir transformations.

absorb_native_field_elements

Absorbs field elements into the sponge state.
&[T]
Elements to absorb (automatically converted to field elements)
Example:

squeeze_native_field_elements

Squeezes field elements from the sponge state.
usize
Number of field elements to squeeze
SmallVec<[F; 10]>
Squeezed field elements
Example:

absorb_nonnative_field_elements

Absorbs non-native field elements (from a different field).
impl IntoIterator<Item = Target>
Non-native field elements to absorb
Example:

squeeze_nonnative_field_elements

Squeezes non-native field elements.
usize
Number of non-native field elements to squeeze
SmallVec<[Target; 10]>
Squeezed non-native field elements

Sponge State Management

State Struct

Internal state of the Poseidon sponge.
The state is split into:
  • Capacity: Hidden state providing security
  • Rate: Public state for absorbing/squeezing

DuplexSpongeMode Enum

Tracks the current mode of the sponge.

Advanced Methods

get_limbs_representations

Converts a non-native field element to limb representation.
&TargetField
The field element to convert
OptimizationType
Whether to optimize for weight or constraints
SmallVec<[F; 10]>
Limb representation in the base field

get_bits

Obtains random bits from the sponge.
usize
Number of random bits to generate
Vec<bool>
Random bits derived from the sponge state
Note: Not uniformly distributed; use for specific applications only.

Implementation Details

Permutation

The Poseidon permutation consists of:
  1. Full rounds: S-box applied to all state elements
  2. Partial rounds: S-box applied to only the first state element
  3. MDS matrix multiplication: Mixing layer

Parameters

Poseidon parameters include:
  • Alpha: S-box exponent (typically 5 or 17)
  • Full rounds: Number of full S-box rounds
  • Partial rounds: Number of partial S-box rounds
  • ARK: Round constants for domain separation
  • MDS: Maximum distance separable matrix

Security

Poseidon provides:
  • 128-bit security with CAPACITY = 1
  • Collision resistance via sponge construction
  • Preimage resistance via one-way permutation

Usage in Fiat-Shamir

PoseidonSponge is used for Fiat-Shamir transformations in proof systems:

See Also