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.

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