pub struct PoseidonHasher<F: PrimeField> { /* private fields */ }
Expand description

A stateful sponge performing Poseidon hash computation.

Implementations§

Returns a new Poseidon hasher based on the given parameters.

Calculates a Poseidon hash for the given input of prime fields.

Poseidon prepends a zero prime field at the beginning of the state, appends the given input and then, if the length of the state is still smaller than the width of the state, it appends zero prime fields at the end of the state until they are equal.

Therefore inputs argument cannot be larger than the number of prime fields in the state - 1. To be precise, the undesirable condition is inputs.len() > self.params.width - 1. Providing such input will result in an error.

Examples

Example with two simple big-endian byte inputs (converted to prime fields) and BN254-based parameters provided by the library.

use light_poseidon::{PoseidonHasher, parameters::bn254_x5_3::poseidon_parameters};
use ark_bn254::Fq;
use ark_ff::{BigInteger, PrimeField};

let params = poseidon_parameters();
let mut poseidon = PoseidonHasher::new(params);

let input1 = Fq::from_be_bytes_mod_order(&[1u8; 32]);
let input2 = Fq::from_be_bytes_mod_order(&[2u8; 32]);

let hash = poseidon.hash(&[input1, input2]).unwrap();

// Do something with `hash`.
println!("{:?}", hash.into_repr().to_bytes_be());
// Should print:
// [
//     40, 7, 251, 60, 51, 30, 115, 141, 251, 200, 13, 46, 134, 91, 113, 170, 131, 90, 53,
//     175, 9, 61, 242, 164, 127, 33, 249, 65, 253, 131, 35, 116
// ]

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.