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.

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