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.

64 lines
2.1 KiB

  1. package arbo
  2. import (
  3. "encoding/json"
  4. "math/big"
  5. "testing"
  6. qt "github.com/frankban/quicktest"
  7. "go.vocdoni.io/dvote/db"
  8. "go.vocdoni.io/dvote/db/badgerdb"
  9. )
  10. func TestCircomVerifierProof(t *testing.T) {
  11. c := qt.New(t)
  12. database, err := badgerdb.New(db.Options{Path: c.TempDir()})
  13. c.Assert(err, qt.IsNil)
  14. tree, err := NewTree(Config{Database: database, MaxLevels: 4,
  15. HashFunction: HashFunctionPoseidon})
  16. c.Assert(err, qt.IsNil)
  17. defer tree.db.Close() //nolint:errcheck
  18. testVector := [][]int64{
  19. {1, 11},
  20. {2, 22},
  21. {3, 33},
  22. {4, 44},
  23. }
  24. bLen := 1
  25. for i := 0; i < len(testVector); i++ {
  26. k := BigIntToBytes(bLen, big.NewInt(testVector[i][0]))
  27. v := BigIntToBytes(bLen, big.NewInt(testVector[i][1]))
  28. if err := tree.Add(k, v); err != nil {
  29. t.Fatal(err)
  30. }
  31. }
  32. // proof of existence
  33. k := BigIntToBytes(bLen, big.NewInt(int64(2)))
  34. cvp, err := tree.GenerateCircomVerifierProof(k)
  35. c.Assert(err, qt.IsNil)
  36. jCvp, err := json.Marshal(cvp)
  37. c.Assert(err, qt.IsNil)
  38. // test vector checked with a circom circuit (arbo/testvectors/circom)
  39. c.Assert(string(jCvp), qt.Equals, `{"fnc":0,"isOld0":"0","key":"2","oldK`+
  40. `ey":"0","oldValue":"0","root":"1355816845522055904274785395894906304622`+
  41. `6645447188878859760119761585093422436","siblings":["1162013050763544193`+
  42. `2056895853942898236773847390796721536119314875877874016518","5158240518`+
  43. `874928563648144881543092238925265313977134167935552944620041388700","0"`+
  44. `,"0"],"value":"22"}`)
  45. // proof of non-existence
  46. k = BigIntToBytes(bLen, big.NewInt(int64(5)))
  47. cvp, err = tree.GenerateCircomVerifierProof(k)
  48. c.Assert(err, qt.IsNil)
  49. jCvp, err = json.Marshal(cvp)
  50. c.Assert(err, qt.IsNil)
  51. // test vector checked with a circom circuit (arbo/testvectors/circom)
  52. c.Assert(string(jCvp), qt.Equals, `{"fnc":1,"isOld0":"0","key":"5","oldK`+
  53. `ey":"1","oldValue":"11","root":"135581684552205590427478539589490630462`+
  54. `26645447188878859760119761585093422436","siblings":["756056982086999933`+
  55. `1905412009838015295115276841209205575174464206730109811365","1276103081`+
  56. `3800436751877086580591648324911598798716611088294049841213649313596","0`+
  57. `","0"],"value":"11"}`)
  58. }