PoseidonHasher
Umbra SDK reference for Poseidon Hasher.
Defined in: src/utils/hasher.ts:196
Poseidon hash function compatible with Circom's Poseidon implementation.
Remarks
This class provides Poseidon hashing functionality using the same parameters as Circom's Poseidon implementation, ensuring compatibility with zero-knowledge proof circuits. The implementation uses the BN254 field (bn254_Fr) and supports input sizes from 1 to 17 elements.
The Poseidon hash is a zero-knowledge friendly hash function designed for use in cryptographic protocols and zero-knowledge proof systems. It uses the same parameters as Circom's Poseidon implementation, including:
- 8 full rounds
- Variable partial rounds (56-70 depending on input size)
- S-box power of 5
- Circom-compatible MDS matrices and round constants
Instances are cached per input size for performance optimization.
Example
// Hash a single value
const hash1 = PoseidonHasher.hash([100n]);
// Hash multiple values
const hash2 = PoseidonHasher.hash([1n, 2n, 3n, 4n, 5n]);
// The hash is compatible with Circom circuits
// poseidon([0, 1, 2, 3]) in Circom matches PoseidonHasher.hash([1n, 2n, 3n])Constructors
Constructor
new PoseidonHasher():
PoseidonHasher
Returns
PoseidonHasher
Methods
hash()
statichash(inputs):U256LeBytes
Defined in: src/utils/hasher.ts:280
Hashes an array of bigint values using Poseidon.
Parameters
inputs
bigint[]
Array of bigint values to hash (1-17 elements)
Returns
A 32-byte Poseidon hash in little-endian format
Throws
PoseidonInputError When the input array is empty or has more than 17 elements
Throws
PoseidonParameterError When the input size is not supported by available constants
Throws
PoseidonConstantError When Poseidon constants cannot be parsed
Remarks
This method computes a Poseidon hash over the input values. The hash is compatible with Circom's Poseidon implementation, ensuring it can be used in zero-knowledge proofs.
Hash Computation:
The implementation uses an initial state of 0n and prepends it to the inputs before hashing,
matching Circom's behavior. The computation is: poseidon([0n, ...inputs]).
Input Requirements:
- Must contain at least 1 element
- Must contain at most 17 elements
- All elements must be valid field elements in the BN254 field
Output Format:
The hash is returned as a 32-byte PoseidonHash in little-endian format, suitable
for use in zero-knowledge proof circuits and cryptographic protocols.
Example
// Hash a single value
const hash1 = PoseidonHasher.hash([100n]);
// Hash multiple values (up to 17)
const hash2 = PoseidonHasher.hash([1n, 2n, 3n, 4n, 5n]);
// The result is a 32-byte PoseidonHash
console.log(hash2.length); // 32