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
1.1 KiB

  1. package merkletree
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/iden3/go-iden3-core/db"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestNewTree(t *testing.T) {
  9. mt, err := NewMerkleTree(db.NewMemoryStorage(), 10)
  10. assert.Nil(t, err)
  11. assert.Equal(t, "0", mt.Root().String())
  12. // test vectors generated using https://github.com/iden3/circomlib smt.js
  13. err = mt.Add(big.NewInt(1), big.NewInt(2))
  14. assert.Nil(t, err)
  15. assert.Equal(t, "4932297968297298434239270129193057052722409868268166443802652458940273154854", mt.Root().BigInt().String())
  16. err = mt.Add(big.NewInt(33), big.NewInt(44))
  17. assert.Nil(t, err)
  18. assert.Equal(t, "13563340744765267202993741297198970774200042973817962221376874695587906013050", mt.Root().BigInt().String())
  19. err = mt.Add(big.NewInt(1234), big.NewInt(9876))
  20. assert.Nil(t, err)
  21. assert.Equal(t, "16970503620176669663662021947486532860010370357132361783766545149750777353066", mt.Root().BigInt().String())
  22. proof, err := mt.GenerateProof(big.NewInt(33), nil)
  23. assert.Nil(t, err)
  24. assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(33), big.NewInt(44)))
  25. assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(33), big.NewInt(45)))
  26. }