light-poseidon is a Poseidon hash implementation in Rust created for Light Protocol.
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:
Example with two simple big-endian byte inputs (converted to prime fields) and BN254-based parameters provided by the library:
```rust use lightposeidon::{PoseidonHasher, parameters::bn254x53::poseidonparameters}; use arkbn254::Fq; use arkff::{BigInteger, PrimeField};
let params = poseidon_parameters(); let mut poseidon = PoseidonHasher::new(params);
let input1 = Fq::frombebytesmodorder(&[1u8; 32]); let input2 = Fq::frombebytesmodorder(&[2u8; 32]);
let hash = poseidon.hash(&[input1, input2]).unwrap();
// Do something with hash
.
println!("{:?}", hash.intorepr().tobytes_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
// ]
```
The implementation is compatible with the original SageMath implementation, but it was also inspired by the following ones:
This repository contains a benchmark measuring the performance of this Poseidon implementation for given two random 32 bytes inputs.
To run them, simply use:
bash
cargo bench
This is the result from a host with the following hardware:
norust
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
Licensed under Apache License, Version 2.0.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.