Documentation
¶
Index ¶
- func BigIntDecoder(data []byte) (*big.Int, error)
- func BigIntEncoder(n *big.Int) ([]byte, error)
- func BigIntEqual(a, b *big.Int) bool
- func Blake2bHasher(a, b *big.Int) *big.Int
- func MiMC7Hasher(a, b *big.Int) *big.Int
- func MiMCBLS12377Hasher(a, b *big.Int) *big.Int
- func MiMCBN254Hasher(a, b *big.Int) *big.Int
- func MultiPoseidonHasher(a, b *big.Int) *big.Int
- func PoseidonHasher(a, b *big.Int) *big.Int
- func SHA256Hasher(a, b *big.Int) *big.Int
- func VerifyProofWith[N any](proof MerkleProof[N], hash Hasher[N], eq Equal[N]) bool
- type Equal
- type Hasher
- type LeanIMT
- func Import[N any](hash Hasher[N], nodesJSON string, eq Equal[N], mapFn func(string) (N, error)) (*LeanIMT[N], error)
- func New[N any](hash Hasher[N], eq Equal[N], storage db.Database, ...) (*LeanIMT[N], error)
- func NewWithPebble[N any](hash Hasher[N], eq Equal[N], encoder func(N) ([]byte, error), ...) (*LeanIMT[N], error)
- func (t *LeanIMT[N]) Close() error
- func (t *LeanIMT[N]) Depth() int
- func (t *LeanIMT[N]) Export() (string, error)
- func (t *LeanIMT[N]) GenerateProof(index int) (MerkleProof[N], error)
- func (t *LeanIMT[N]) Has(leaf N) bool
- func (t *LeanIMT[N]) IndexOf(leaf N) int
- func (t *LeanIMT[N]) Insert(leaf N) int
- func (t *LeanIMT[N]) InsertMany(leaves []N) error
- func (t *LeanIMT[N]) Leaves() []N
- func (t *LeanIMT[N]) Load() error
- func (t *LeanIMT[N]) Root() (N, bool)
- func (t *LeanIMT[N]) Size() int
- func (t *LeanIMT[N]) Sync() error
- func (t *LeanIMT[N]) Update(index int, newLeaf N) error
- func (t *LeanIMT[N]) UpdateMany(indices []int, leaves []N) error
- func (t *LeanIMT[N]) VerifyProof(proof MerkleProof[N]) bool
- type MerkleProof
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BigIntDecoder ¶
BigIntDecoder decodes bytes to a *big.Int. This function is used by the LeanIMT for persistence operations. It explicitly handles zero values to ensure they are properly decoded.
Parameters:
- data: Byte slice to decode
Returns: Decoded big.Int value, or error if decoding fails
func BigIntEncoder ¶
BigIntEncoder encodes a *big.Int to bytes using big-endian format. This function is used by the LeanIMT for persistence operations. It explicitly handles zero values to ensure they are properly encoded.
Parameters:
- n: The big.Int value to encode
Returns: Byte slice representation of the value, or error if encoding fails
func BigIntEqual ¶
BigIntEqual is an equality function for *big.Int values. This function is used by the LeanIMT to compare values for equality.
Parameters:
- a: First value to compare
- b: Second value to compare
Returns: true if a equals b, false otherwise
func Blake2bHasher ¶
Blake2bHasher performs BLAKE2b-256 hash on two big.Int values. BLAKE2b is a cryptographic hash function that is faster than SHA-256 while providing similar security guarantees. It's optimized for 64-bit platforms and is widely used in modern cryptographic applications.
This hasher is suitable for:
- High-performance hashing requirements
- Modern cryptographic systems
- Applications requiring fast, secure hashing
The function converts both inputs to bytes, writes them to a BLAKE2b hasher, and returns the 256-bit hash result.
Parameters:
- a: First input value
- b: Second input value
Returns: Hash result as *big.Int Panics if the BLAKE2b initialization fails
func MiMC7Hasher ¶
MiMC7Hasher performs MiMC-7 hash on two big.Int values using the iden3 implementation. MiMC-7 is a variant of the MiMC hash function with 7 rounds per block, optimized for zero-knowledge proof systems. This implementation is compatible with iden3's circom circuits and other iden3 tooling.
This hasher is suitable for:
- Compatibility with iden3 ecosystem (circom, snarkjs)
- ZK applications using iden3 libraries
- Systems requiring MiMC-7 specifically
The function operates over the BN254 scalar field and is compatible with iden3's circom MiMC7 implementation.
Parameters:
- a: First input value
- b: Second input value
Returns: Hash result as *big.Int Panics if the hash operation fails
func MiMCBLS12377Hasher ¶
MiMCBLS12377Hasher performs MiMC hash on two big.Int values over the BLS12-377 curve. MiMC (Minimal Multiplicative Complexity) is a family of block ciphers and hash functions designed to be efficient in zero-knowledge proof systems. This variant operates over the scalar field of the BLS12-377 elliptic curve.
This hasher is suitable for:
- ZK circuits using the BLS12-377 curve
- Applications requiring BLS12-377 compatibility
- Systems built with gnark using BLS12-377
The function ensures inputs are reduced modulo the BLS12-377 field order before hashing.
Parameters:
- a: First input value
- b: Second input value
Returns: Hash result as *big.Int Panics if the hash operation fails
func MiMCBN254Hasher ¶
MiMCBN254Hasher performs MiMC hash on two big.Int values over the BN254 curve. MiMC (Minimal Multiplicative Complexity) is a family of block ciphers and hash functions designed to be efficient in zero-knowledge proof systems. This variant operates over the scalar field of the BN254 (also known as BN128 or alt_bn128) elliptic curve.
This hasher is suitable for:
- ZK circuits using the BN254 curve (most common in Ethereum)
- Applications requiring BN254 compatibility
- Systems built with gnark using BN254
- Ethereum-compatible ZK applications
The function ensures inputs are reduced modulo the BN254 field order before hashing.
Parameters:
- a: First input value
- b: Second input value
Returns: Hash result as *big.Int Panics if the hash operation fails
func MultiPoseidonHasher ¶
MultiPoseidonHasher performs MultiPoseidon hash on two big.Int values. MultiPoseidon is Vocdoni's implementation of the Poseidon hash function that can efficiently handle variable-length inputs by automatically chunking them into field elements. This makes it particularly useful for hashing arbitrary-length data in ZK circuits.
This hasher is suitable for:
- Vocdoni ecosystem applications
- Variable-length input hashing in ZK circuits
- Applications requiring efficient multi-element Poseidon hashing
The function operates over the BN254 scalar field and is optimized for use in gnark circuits.
Parameters:
- a: First input value
- b: Second input value
Returns: Hash result as *big.Int Panics if the hash operation fails
func PoseidonHasher ¶
PoseidonHasher performs Poseidon hash on two big.Int values using the iden3 implementation. Poseidon is a ZK-friendly cryptographic hash function optimized for use in zero-knowledge proof systems, particularly over the BN254 curve. It's significantly more efficient in circuits compared to traditional hash functions like SHA-256.
This hasher is suitable for:
- Merkle tree constructions in ZK circuits
- Privacy-preserving applications
- Blockchain applications requiring ZK proofs
Parameters:
- a: First input value
- b: Second input value
Returns: Hash result as *big.Int Panics if the hash operation fails (should not happen with valid inputs)
func SHA256Hasher ¶
SHA256Hasher performs SHA-256 hash on two big.Int values. SHA-256 is a widely-used cryptographic hash function from the SHA-2 family. While not optimized for zero-knowledge circuits, it provides strong security guarantees and is well-tested in production systems.
This hasher is suitable for:
- General-purpose cryptographic hashing
- Systems requiring NIST-approved algorithms
- Compatibility with existing SHA-256 based systems
The function converts both inputs to bytes (big-endian), concatenates them, and computes the SHA-256 hash. The result is interpreted as a big.Int.
Parameters:
- a: First input value
- b: Second input value
Returns: Hash result as *big.Int
func VerifyProofWith ¶
func VerifyProofWith[N any](proof MerkleProof[N], hash Hasher[N], eq Equal[N]) bool
VerifyProofWith verifies a proof using the provided hash and equality functions.
Types ¶
type Equal ¶
Equal is an optional equality comparator used for leaf lookups and proofs. If nil, reflect.DeepEqual is used.
type LeanIMT ¶
type LeanIMT[N any] struct { // contains filtered or unexported fields }
LeanIMT is a binary Lean Incremental Merkle Tree.
- dynamic depth (ceil(log2(size)))
- no zero nodes; if a right child is missing, parent = left child
- proofs omit missing siblings and encode the path as an index integer.
LeanIMT is safe for concurrent use by multiple goroutines.
func Import ¶
func Import[N any](hash Hasher[N], nodesJSON string, eq Equal[N], mapFn func(string) (N, error)) (*LeanIMT[N], error)
Import parses a JSON-encoded nodes matrix and returns a new tree. If mapFn is provided, every JSON scalar value that is encoded as a string will be passed through mapFn to build values of type N. If mapFn is nil, Import attempts to unmarshal directly into [][]N.
func New ¶
func New[N any](hash Hasher[N], eq Equal[N], storage db.Database, encoder func(N) ([]byte, error), decoder func([]byte) (N, error)) (*LeanIMT[N], error)
New creates a new empty LeanIMT with the provided hash function. If eq is nil, reflect.DeepEqual is used for equality. If storage is nil, the tree operates in memory-only mode. If storage is provided, encoder and decoder functions must also be provided.
Example usage:
tree, err := New(BigIntHasher, BigIntEqual, nil, nil, nil) // in-memory tree, err := New(BigIntHasher, BigIntEqual, db, BigIntEncoder, BigIntDecoder) // persistent
func NewWithPebble ¶
func NewWithPebble[N any](hash Hasher[N], eq Equal[N], encoder func(N) ([]byte, error), decoder func([]byte) (N, error), datadir string) (*LeanIMT[N], error)
NewWithPebble is a wrapper around New. Creates a new LeanIMT using a persistent Pebble DB at the specified directory.
func (*LeanIMT[N]) Close ¶
Close ensures all changes are synced and closes the database connection.
func (*LeanIMT[N]) Export ¶
Export encodes the internal matrix as JSON. For *big.Int values, this results in JSON strings (via TextMarshaler), matching the TS behavior that stringifies bigints.
func (*LeanIMT[N]) GenerateProof ¶
func (t *LeanIMT[N]) GenerateProof(index int) (MerkleProof[N], error)
GenerateProof builds a LeanIMT proof for the leaf at index.
func (*LeanIMT[N]) Insert ¶
Insert inserts a single leaf at the end, updating path to root bottom-up.
func (*LeanIMT[N]) InsertMany ¶
InsertMany inserts m leaves in batch (more efficient than m x Insert).
func (*LeanIMT[N]) Leaves ¶
func (t *LeanIMT[N]) Leaves() []N
Leaves returns a copy of the leaves array.
func (*LeanIMT[N]) Load ¶
Load restores the tree from persistent storage. It reads all leaves from the database and rebuilds the tree structure.
func (*LeanIMT[N]) Sync ¶
Sync persists the current tree state to disk atomically. Only the leaves are stored; intermediate nodes are computed on load.
func (*LeanIMT[N]) Update ¶
Update replaces the leaf at index with newLeaf and updates path to root.
func (*LeanIMT[N]) UpdateMany ¶
UpdateMany updates multiple leaves efficiently in O(n). It validates indices (range and duplicates).
func (*LeanIMT[N]) VerifyProof ¶
func (t *LeanIMT[N]) VerifyProof(proof MerkleProof[N]) bool
VerifyProof verifies a proof against the current tree hash function.
type MerkleProof ¶
MerkleProof contains the fields needed to verify membership: - Root: root at the time of proof - Leaf: the leaf value - PathBits: packed path bits (LSB is first sibling combined) - LeafIndex: absolute leaf position in the tree - Siblings: the sibling nodes included (missing siblings are omitted)