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.

57 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(arbo.Config{Database: database, MaxLevels: 4,
  17. HashFunction: arbo.HashFunctionPoseidon})
  18. c.Assert(err, qt.IsNil)
  19. testVector := [][]int64{
  20. {1, 11},
  21. {2, 22},
  22. {3, 33},
  23. {4, 44},
  24. }
  25. bLen := 1
  26. for i := 0; i < len(testVector); i++ {
  27. k := arbo.BigIntToBytes(bLen, big.NewInt(testVector[i][0]))
  28. v := arbo.BigIntToBytes(bLen, big.NewInt(testVector[i][1]))
  29. if err := tree.Add(k, v); err != nil {
  30. t.Fatal(err)
  31. }
  32. }
  33. // proof of existence
  34. k := arbo.BigIntToBytes(bLen, big.NewInt(int64(2)))
  35. cvp, err := tree.GenerateCircomVerifierProof(k)
  36. c.Assert(err, qt.IsNil)
  37. jCvp, err := json.Marshal(cvp)
  38. c.Assert(err, qt.IsNil)
  39. // store the data into a file that will be used at the circom test
  40. err = ioutil.WriteFile("go-smt-verifier-inputs.json", jCvp, 0600)
  41. c.Assert(err, qt.IsNil)
  42. // proof of non-existence
  43. k = arbo.BigIntToBytes(bLen, big.NewInt(int64(5)))
  44. cvp, err = tree.GenerateCircomVerifierProof(k)
  45. c.Assert(err, qt.IsNil)
  46. jCvp, err = json.Marshal(cvp)
  47. c.Assert(err, qt.IsNil)
  48. // store the data into a file that will be used at the circom test
  49. err = ioutil.WriteFile("go-smt-verifier-non-existence-inputs.json", jCvp, 0600)
  50. c.Assert(err, qt.IsNil)
  51. }