soroban-poseidon
Poseidon and Poseidon2 cryptographic hash functions for Soroban smart contracts.
Features
- Poseidon: Matches circom's implementation
- Poseidon2: Matches noir's implementation
- Support for BN254 and BLS12-381 fields
Installation
Add to your Cargo.toml:
[]
= { = "https://github.com/stellar/rs-soroban-poseidon" }
Usage
Poseidon Hash
use poseidon_hash;
use ;
let env = default;
let inputs = vec!;
// Hash 2 inputs with t=3 (rate=2, capacity=1)
let hash = ;
Poseidon2 Hash
use poseidon2_hash;
use ;
let env = default;
let inputs = vec!;
// Hash 3 inputs with t=4 (rate=3, capacity=1)
let hash = ;
Reusing Sponge for Multiple Hashes
For repeated hashing, create a sponge once to reuse the pre-initialized parameters:
use PoseidonSponge;
use ;
let env = default;
let mut sponge = new;
let inputs1 = vec!;
let inputs2 = vec!;
// Each call computes a fresh hash (state is reset between calls)
let hash1 = sponge.compute_hash;
let hash2 = sponge.compute_hash;
Supported Configurations
Poseidon
| Field | State Size (T) | Rate | Inputs |
|---|---|---|---|
| BN254 | 2, 3, 4, 5, 6 | T-1 | 1–5 |
| BLS12-381 | 2, 3, 4, 5, 6 | T-1 | 1–5 |
Poseidon2
| Field | State Size (T) | Rate | Inputs |
|---|---|---|---|
| BN254 | 2, 3, 4 | T-1 | 1–3 |
| BLS12-381 | 2, 3, 4 | T-1 | 1–3 |
Limitations / Future Work
-
Multi-round absorption: Currently, for Poseidon, inputs must exactly fill the rate (i.e.,
inputs.len() == T - 1), matching circom's behavior wherenInputsdeterminesT = nInputs + 1. Poseidon2 requires inputs to fit within a single rate (i.e.,inputs.len() <= T - 1). Future versions will support absorbing inputs larger than the state size across multiple permutation rounds. -
Persistent parameters: Make
PoseidonParams/Poseidon2Paramsa#[contracttype]so they can be stored as contract data and reduce the contract size. -
Additional sponge modes: Support more sponge operation modes such as full duplex mode for streaming absorb/squeeze operations.
Development
# Format code
# Build test contract WASMs
# Run all tests (fmt + build-test-wasms + unit tests)
# Clean build artifacts
License
Apache-2.0