You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
871 B

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