arnaucube
arnaucube/go-merkletree-iden3Public Self-hosted repository
go-merkletree-iden3/utils.go
utils.goRaw
 1package merkletree
 2
 3import (
 4	"math/big"
 5
 6	"github.com/iden3/go-iden3-crypto/poseidon"
 7)
 8
 9// HashElems performs a poseidon hash over the array of ElemBytes, currently we
10// are using 2 elements.  Uses poseidon.Hash to be compatible with the circom
11// circuits implementations.
12func HashElems(elems ...*big.Int) (*Hash, error) {
13	poseidonHash, err := poseidon.Hash(elems)
14	if err != nil {
15		return nil, err
16	}
17	return NewHashFromBigInt(poseidonHash), nil
18}
19
20// HashElemsKey performs a poseidon hash over the array of ElemBytes, currently
21// we are using 2 elements.
22func HashElemsKey(key *big.Int, elems ...*big.Int) (*Hash, error) {
23	if key == nil {
24		key = new(big.Int).SetInt64(0)
25	}
26	bi := make([]*big.Int, 3)
27	copy(bi[:], elems)
28	bi[2] = key
29	poseidonHash, err := poseidon.Hash(bi)
30	if err != nil {
31		return nil, err
32	}
33	return NewHashFromBigInt(poseidonHash), nil
34}