Crate light_poseidon
source ·Expand description
light-poseidon is a Poseidon hash implementation in Rust created for Light Protocol.
Parameters
The library provides pre-generated parameters over the BN254 curve, however it can work with any parameters provided as long as developers take care of generating the round constants.
Parameters provided by the library are:
- x^5 S-boxes
- 3 prime fields (one zero prime field and two inputs from the caller)
- 8 full rounds and 57 partial rounds
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
// ]Implementation
The implementation is compatible with the original SageMath implementation, but it was also inspired by the following ones:
Performance
This repository contains a benchmark measuring the performance of this Poseidon implementation for given two random 32 bytes inputs.
To run them, simply use:
cargo bench
This is the result from a host with the following hardware:
- AMD Ryzen 9 5950x (base clock: 3.4 GHz, up to: 4.9 GHz)
- 4 x Corsair Vengeance DDR4 32GB 3600 MHz
poseidon_bn253_x5_3 time: [21.980 µs 21.997 µs 22.017 µs]
Found 9 outliers among 100 measurements (9.00%)
4 (4.00%) high mild
5 (5.00%) high severe