-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier_bn254_test.go
66 lines (61 loc) · 2.01 KB
/
verifier_bn254_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package arbo
import (
"fmt"
"math/big"
"testing"
"time"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/consensys/gnark/profile"
"github.com/consensys/gnark/test"
qt "github.com/frankban/quicktest"
arbotree "github.com/vocdoni/arbo"
"github.com/vocdoni/gnark-crypto-primitives/poseidon"
internaltest "github.com/vocdoni/gnark-crypto-primitives/test"
"go.vocdoni.io/dvote/util"
)
type testVerifierBN254 struct {
Root frontend.Variable
Key frontend.Variable
Value frontend.Variable
Siblings [160]frontend.Variable
}
func (circuit *testVerifierBN254) Define(api frontend.API) error {
// use poseidon hash function
return CheckInclusionProof(api, poseidon.Hash, circuit.Key, circuit.Value, circuit.Root, circuit.Siblings[:])
}
func TestVerifierBN254(t *testing.T) {
c := qt.New(t)
// profile the circuit compilation
p := profile.Start()
now := time.Now()
_, _ = frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &testVerifierBN254{})
fmt.Println("elapsed", time.Since(now))
p.Stop()
fmt.Println("constrains", p.NbConstraints())
// generate census proof
testCensus, err := internaltest.GenerateCensusProofForTest(internaltest.CensusTestConfig{
Dir: t.TempDir() + "/bn254",
ValidSiblings: v_siblings,
TotalSiblings: n_siblings,
KeyLen: k_len,
Hash: arbotree.HashFunctionPoseidon,
BaseFiled: arbotree.BN254BaseField,
}, util.RandomBytes(k_len), big.NewInt(10).Bytes())
c.Assert(err, qt.IsNil)
// init and print inputs
fSiblings := [n_siblings]frontend.Variable{}
for i := 0; i < n_siblings; i++ {
fSiblings[i] = testCensus.Siblings[i]
}
inputs := testVerifierBN254{
Root: testCensus.Root,
Key: testCensus.Key,
Value: testCensus.Value,
Siblings: fSiblings,
}
assert := test.NewAssert(t)
assert.SolvingSucceeded(&testVerifierBN254{}, &inputs, test.WithCurves(ecc.BN254), test.WithBackends(backend.GROTH16))
}