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.

55 lines
1.4 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/badgerdb"
  10. )
  11. func TestGenerator(t *testing.T) {
  12. c := qt.New(t)
  13. database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
  14. c.Assert(err, qt.IsNil)
  15. tree, err := arbo.NewTree(database, 4, arbo.HashFunctionPoseidon)
  16. c.Assert(err, qt.IsNil)
  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 := arbo.BigIntToBytes(bLen, big.NewInt(testVector[i][0]))
  26. v := arbo.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 := arbo.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. // store the data into a file that will be used at the circom test
  38. err = ioutil.WriteFile("go-smt-verifier-inputs.json", jCvp, 0600)
  39. c.Assert(err, qt.IsNil)
  40. // proof of non-existence
  41. k = arbo.BigIntToBytes(bLen, big.NewInt(int64(5)))
  42. cvp, err = tree.GenerateCircomVerifierProof(k)
  43. c.Assert(err, qt.IsNil)
  44. jCvp, err = json.Marshal(cvp)
  45. c.Assert(err, qt.IsNil)
  46. // store the data into a file that will be used at the circom test
  47. err = ioutil.WriteFile("go-smt-verifier-non-existence-inputs.json", jCvp, 0600)
  48. c.Assert(err, qt.IsNil)
  49. }