| Crates.io | poseidon-hash |
| lib.rs | poseidon-hash |
| version | 0.1.3 |
| created_at | 2025-11-05 09:12:56.036731+00 |
| updated_at | 2025-11-05 15:10:03.725902+00 |
| description | Rust port of Poseidon2 hash function and Goldilocks field arithmetic, ported from lighter-go (Lighter Protocol) |
| homepage | |
| repository | https://github.com/bvvvp009/lighter-rust |
| max_upload_size | |
| id | 1917630 |
| size | 46,189 |
Rust port of Poseidon2 hash function and Goldilocks field arithmetic, ported from lighter-go (Lighter Protocol).
This library has NOT been audited and is provided as-is. Use with caution.
p = 2^64 - 2^32 + 1no_std environments (with alloc)This crate provides essential cryptographic primitives for Zero-Knowledge proof systems:
Add to your Cargo.toml:
[dependencies]
poseidon-hash = "0.1"
Or use the latest version from git (until published):
[dependencies]
poseidon-hash = { git = "https://github.com/bvvvp009/lighter-rust", path = "rust-signer/poseidon-hash" }
use poseidon_hash::Goldilocks;
let a = Goldilocks::from(42);
let b = Goldilocks::from(10);
let sum = a.add(&b);
let product = a.mul(&b);
use poseidon_hash::{hash_to_quintic_extension, Goldilocks};
let elements = vec![
Goldilocks::from(1),
Goldilocks::from(2),
Goldilocks::from(3),
];
let hash = hash_to_quintic_extension(&elements);
use poseidon_hash::Fp5Element;
let a = Fp5Element::from_uint64_array([1, 2, 3, 4, 5]);
let b = Fp5Element::one();
let product = a.mul(&b);
let bytes = product.to_bytes_le(); // Returns [u8; 40]
serde: Enable serialization/deserialization support[dependencies]
poseidon-hash = { version = "0.1", features = ["serde"] }
Add the dependency to your Cargo.toml (see Installation above)
Import the types you need:
use poseidon_hash::{Goldilocks, Fp5Element, hash_to_quintic_extension};
// Create field elements
let a = Goldilocks::from_canonical_u64(42);
let b = Goldilocks::from_canonical_u64(10);
// Perform operations
let sum = a.add(&b);
let product = a.mul(&b);
let inverse = a.inverse();
// Check properties
assert!(!a.is_zero());
assert_eq!(Goldilocks::zero().is_zero(), true);
// Prepare input elements
let inputs = vec![
Goldilocks::from_canonical_u64(1),
Goldilocks::from_canonical_u64(2),
Goldilocks::from_canonical_u64(3),
];
// Hash to Fp5Element (40 bytes)
let hash = hash_to_quintic_extension(&inputs);
let hash_bytes = hash.to_bytes_le();
// Create Fp5 elements
let elem1 = Fp5Element::from_uint64_array([1, 2, 3, 4, 5]);
let elem2 = Fp5Element::one();
// Operations
let sum = elem1.add(&elem2);
let product = elem1.mul(&elem2);
let square = elem1.square();
let inverse = elem1.inverse();
// Serialization
let bytes = elem1.to_bytes_le(); // [u8; 40]
Merkle Tree Construction:
use poseidon_hash::{Goldilocks, hash_to_quintic_extension};
fn merkle_hash(left: &[u8; 40], right: &[u8; 40]) -> [u8; 40] {
// Convert bytes to Goldilocks elements
let left_elems: Vec<Goldilocks> = left.chunks(8)
.map(|chunk| {
let mut arr = [0u8; 8];
arr.copy_from_slice(chunk);
Goldilocks::from_canonical_u64(u64::from_le_bytes(arr))
})
.collect();
let right_elems: Vec<Goldilocks> = right.chunks(8)
.map(|chunk| {
let mut arr = [0u8; 8];
arr.copy_from_slice(chunk);
Goldilocks::from_canonical_u64(u64::from_le_bytes(arr))
})
.collect();
// Combine and hash
let combined: Vec<Goldilocks> = left_elems.into_iter()
.chain(right_elems.into_iter())
.collect();
hash_to_quintic_extension(&combined).to_bytes_le()
}
Converting Integers to Field Elements:
use poseidon_hash::Goldilocks;
// From u64
let elem = Goldilocks::from_canonical_u64(12345);
// From i64 (handles negatives)
let neg_elem = Goldilocks::from_i64(-10);
// From u64 using From trait
let elem: Goldilocks = 42u64.into();
The implementation is optimized for:
⚠️ Important: This library has NOT been security audited. Use with caution in production systems.
Full API documentation is available at docs.rs.
Licensed under either of:
at your option.
This library is ported from GO-SDK of the Lighter Protocol project. Feel free to open a PR or an issue.