Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Poseidon Hash (Goldilocks)
Rust port of Poseidon2 hash function and Goldilocks field arithmetic, ported from lighter-go (Lighter Protocol).
⚠️ Security Warning
This library has NOT been audited and is provided as-is. Use with caution.
- This is a prototype port from the Go SDK (lighter-go)
- Not security audited - do not use in production without proper security review
- While the implementation appears to work correctly, cryptographic software requires careful auditing
- This is an open-source contribution and not an official Lighter Protocol library
- Use at your own risk
Features
- Goldilocks Field Arithmetic: Fast field operations with prime
p = 2^64 - 2^32 + 1 - Poseidon2 Hash Function: ZK-friendly hash function optimized for Zero-Knowledge proof systems
- Fp5 Quintic Extension Field: 40-byte field elements for cryptographic operations
- Optimized Performance: Efficient implementations for production use
- No Standard Library: Can be used in
no_stdenvironments (withalloc)
Overview
This crate provides essential cryptographic primitives for Zero-Knowledge proof systems:
- Goldilocks Field: A special prime field optimized for 64-bit CPU operations and ZK systems like Plonky2
- Poseidon2: A hash function designed specifically for ZK circuits with low constraint counts
- Fp5 Extension Field: Quintic extension field (GF(p^5)) for elliptic curve operations
Installation
Add to your Cargo.toml:
[]
= "0.1"
Or use the latest version from git (until published):
[]
= { = "https://github.com/bvvvp009/lighter-rust", = "rust-signer/poseidon-hash" }
Usage
Basic Field Arithmetic
use Goldilocks;
let a = from;
let b = from;
let sum = a.add;
let product = a.mul;
Poseidon2 Hashing
use ;
let elements = vec!;
let hash = hash_to_quintic_extension;
Fp5 Extension Field
use Fp5Element;
let a = from_uint64_array;
let b = one;
let product = a.mul;
let bytes = product.to_bytes_le; // Returns [u8; 40]
Optional Features
serde: Enable serialization/deserialization support
[]
= { = "0.1", = ["serde"] }
Integration Guide
Using in Your Project
-
Add the dependency to your
Cargo.toml(see Installation above) -
Import the types you need:
use ;
- Use field arithmetic for ZK circuit operations:
// Create field elements
let a = from_canonical_u64;
let b = from_canonical_u64;
// Perform operations
let sum = a.add;
let product = a.mul;
let inverse = a.inverse;
// Check properties
assert!;
assert_eq!;
- Hash data for ZK proofs:
// Prepare input elements
let inputs = vec!;
// Hash to Fp5Element (40 bytes)
let hash = hash_to_quintic_extension;
let hash_bytes = hash.to_bytes_le;
- Work with extension fields:
// Create Fp5 elements
let elem1 = from_uint64_array;
let elem2 = one;
// Operations
let sum = elem1.add;
let product = elem1.mul;
let square = elem1.square;
let inverse = elem1.inverse;
// Serialization
let bytes = elem1.to_bytes_le; // [u8; 40]
Common Patterns
Merkle Tree Construction:
use ;
Converting Integers to Field Elements:
use Goldilocks;
// From u64
let elem = from_canonical_u64;
// From i64 (handles negatives)
let neg_elem = from_i64;
// From u64 using From trait
let elem: Goldilocks = 42u64.into;
Use Cases
- Zero-Knowledge proof systems (Plonky2, STARKs)
- Cryptographic research and protocol development
- Blockchain protocols requiring ZK-friendly hashing
- Merkle tree construction for ZK systems
- Commitment schemes and hash-based signatures
- Elliptic curve cryptography over extension fields
Performance
The implementation is optimized for:
- Fast modular reduction using Goldilocks prime properties
- Efficient field arithmetic operations
- Low memory allocations
- Production-grade performance
Security Considerations
⚠️ Important: This library has NOT been security audited. Use with caution in production systems.
- Audit Status: This is a prototype port from lighter-go and requires security review before production use
- Hash Function: Poseidon2 is designed for ZK-proof systems but this implementation needs auditing
- Field Operations: Ensure proper input validation and bounds checking in your application
Documentation
Full API documentation is available at docs.rs.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
This library is ported from GO-SDK of the Lighter Protocol project. Feel free to open a PR or an issue.