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.

56 lines
1.5 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "math/big"
  6. "testing"
  7. qt "github.com/frankban/quicktest"
  8. "github.com/vocdoni/arbo"
  9. "go.vocdoni.io/dvote/db"
  10. "go.vocdoni.io/dvote/db/badgerdb"
  11. )
  12. func TestGenerator(t *testing.T) {
  13. c := qt.New(t)
  14. database, err := badgerdb.New(db.Options{Path: c.TempDir()})
  15. c.Assert(err, qt.IsNil)
  16. tree, err := arbo.NewTree(database, 4, arbo.HashFunctionPoseidon)
  17. c.Assert(err, qt.IsNil)
  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 := arbo.BigIntToBytes(bLen, big.NewInt(testVector[i][0]))
  27. v := arbo.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 := arbo.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. // store the data into a file that will be used at the circom test
  39. err = ioutil.WriteFile("go-smt-verifier-inputs.json", jCvp, 0600)
  40. c.Assert(err, qt.IsNil)
  41. // proof of non-existence
  42. k = arbo.BigIntToBytes(bLen, big.NewInt(int64(5)))
  43. cvp, err = tree.GenerateCircomVerifierProof(k)
  44. c.Assert(err, qt.IsNil)
  45. jCvp, err = json.Marshal(cvp)
  46. c.Assert(err, qt.IsNil)
  47. // store the data into a file that will be used at the circom test
  48. err = ioutil.WriteFile("go-smt-verifier-non-existence-inputs.json", jCvp, 0600)
  49. c.Assert(err, qt.IsNil)
  50. }