pub trait PoseidonBytesHasher {
    // Required methods
    fn hash_bytes_be(
        &mut self,
        inputs: &[&[u8]]
    ) -> Result<[u8; 32], PoseidonError>;
    fn hash_bytes_le(
        &mut self,
        inputs: &[&[u8]]
    ) -> Result<[u8; 32], PoseidonError>;
}

Required Methods§

source

fn hash_bytes_be(&mut self, inputs: &[&[u8]]) -> Result<[u8; 32], PoseidonError>

Calculates a Poseidon hash for the given input of big-endian byte slices and returns the result as a byte array.

Examples

Example with two simple big-endian byte inputs and BN254-based parameters provided by the library.

use light_poseidon::{Poseidon, PoseidonBytesHasher, parameters::bn254_x5};
use ark_bn254::Fr;
use ark_ff::{BigInteger, PrimeField};

let mut poseidon = Poseidon::<Fr>::new_circom(2).unwrap();

let hash = poseidon.hash_bytes_be(&[&[1u8; 32], &[2u8; 32]]).unwrap();

println!("{:?}", hash);
// Should print:
// [
//     13, 84, 225, 147, 143, 138, 140, 28, 125, 235, 94, 3, 85, 242, 99, 25, 32, 123, 132,
//     254, 156, 162, 206, 27, 38, 231, 53, 200, 41, 130, 25, 144
// ]
Safety

Unlike the PrimeField::from_be_bytes_mod_order and Field::from_random_bytes methods, this function ensures that the input byte slice’s length exactly matches the modulus size of the prime field. If the size doesn’t match, an error is returned.

This strict check is designed to prevent unexpected behaviors and collisions that might occur when using from_be_bytes_mod_order or from_random_bytes, which simply take a subslice of the input if it’s too large, potentially leading to collisions.

source

fn hash_bytes_le(&mut self, inputs: &[&[u8]]) -> Result<[u8; 32], PoseidonError>

Calculates a Poseidon hash for the given input of little-endian byte slices and returns the result as a byte array.

Examples

Example with two simple little-endian byte inputs and BN254-based parameters provided by the library.

use light_poseidon::{Poseidon, PoseidonBytesHasher, parameters::bn254_x5};
use ark_bn254::Fr;
use ark_ff::{BigInteger, PrimeField};

let mut poseidon = Poseidon::<Fr>::new_circom(2).unwrap();

let hash = poseidon.hash_bytes_le(&[&[1u8; 32], &[2u8; 32]]).unwrap();

println!("{:?}", hash);
// Should print:
// [
//     144, 25, 130, 41, 200, 53, 231, 38, 27, 206, 162, 156, 254, 132, 123, 32, 25, 99, 242,
//     85, 3, 94, 235, 125, 28, 140, 138, 143, 147, 225, 84, 13
// ]
Safety

Unlike the PrimeField::from_le_bytes_mod_order and Field::from_random_bytes methods, this function ensures that the input byte slice’s length exactly matches the modulus size of the prime field. If the size doesn’t match, an error is returned.

This strict check is designed to prevent unexpected behaviors and collisions that might occur when using from_be_bytes_mod_order or from_random_bytes, which simply take a subslice of the input if it’s too large, potentially leading to collisions.

Implementors§