Skip to main content

Crate poseidon_hash

Crate poseidon_hash 

Source
Expand description

§Poseidon Hash (Goldilocks)

Rust implementation of Poseidon2 hash function and Goldilocks field arithmetic.

§⚠️ Security Warning

This library has NOT been audited and is provided as-is. Use with caution.

  • Prototype implementation focused on correctness
  • 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

§Overview

This crate provides essential cryptographic primitives for Zero-Knowledge proof systems:

  • Goldilocks Field: A special prime field (p = 2^64 - 2^32 + 1) optimized for 64-bit CPU operations
  • Poseidon2 Hash: A ZK-friendly hash function designed for low constraint counts in ZK circuits
  • Fp5 Extension Field: Quintic extension field (GF(p^5)) for elliptic curve operations

§Features

  • Fast field arithmetic with optimized modular reduction
  • Efficient Poseidon2 hash implementation
  • 40-byte field elements for cryptographic operations
  • Production-grade performance and security

§Example

use poseidon_hash::{Goldilocks, hash_to_quintic_extension};

// Field arithmetic
let a = Goldilocks::from_canonical_u64(42);
let b = Goldilocks::from_canonical_u64(10);
let sum = a.add(&b);
let product = a.mul(&b);

// Poseidon2 hashing
let elements = vec![
    Goldilocks::from_canonical_u64(1),
    Goldilocks::from_canonical_u64(2),
    Goldilocks::from_canonical_u64(3),
];
let hash = hash_to_quintic_extension(&elements);

Modules§

merkle
Binary Merkle tree built with the Poseidon2 hash over the Goldilocks field.

Structs§

Fp5Element
Fp5 extension field element.
Goldilocks
Goldilocks field element.

Functions§

empty_hash_out
Returns an empty hash output (all zeros). Equivalent to Go’s EmptyHashOut function.
hash_n_to_one
Combines multiple hash outputs into a single hash output. Equivalent to Go’s HashNToOne function.
hash_no_pad
Hashes a slice of Goldilocks field elements, producing exactly 4 output elements. Equivalent to Go’s HashNoPad function.
hash_out_from_bytes_le
Deserialises a HashOut from 32 little-endian bytes.
hash_out_to_bytes_le
Serialises a HashOut to 32 little-endian bytes.
hash_to_quintic_extension
Hashes a slice of Goldilocks field elements to a single Fp5Element.
permute
Applies the Poseidon2 permutation to a 12-element state array.

Type Aliases§

HashOut
Hash output type: 4 Goldilocks elements (32 bytes) Equivalent to Go’s HashOut type