Struct light_poseidon::PoseidonHasher
source · pub struct PoseidonHasher<F: PrimeField> { /* private fields */ }Expand description
A stateful sponge performing Poseidon hash computation.
Implementations§
source§impl<F: PrimeField> PoseidonHasher<F>
impl<F: PrimeField> PoseidonHasher<F>
sourcepub fn new(params: PoseidonParameters<F>) -> Self
pub fn new(params: PoseidonParameters<F>) -> Self
Returns a new Poseidon hasher based on the given parameters.
sourcepub fn hash(&mut self, inputs: &[F]) -> Result<F, PoseidonError>
pub fn hash(&mut self, inputs: &[F]) -> Result<F, PoseidonError>
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
// ]