-
Notifications
You must be signed in to change notification settings - Fork 18
/
app_test.go
60 lines (53 loc) · 1.22 KB
/
app_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
package zksec_gkr
import (
"bytes"
"encoding/hex"
"fmt"
"testing"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/constraint/solver"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
)
func TestProveAndVerify(t *testing.T) {
circuit := Circuit{
// fixed
AliceBalance: aliceBalance,
// fixed
BobBalance: bobBalance,
// given by the user
NewBobBalance: 500,
// given by the user
NewAliceBalance: 0,
// private
Transfer: 500,
}
witness, err := frontend.NewWitness(&circuit, ecc.BN254.ScalarField())
if err != nil {
t.Fatal(err)
}
oR1cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
if err != nil {
fmt.Println("error occured ", err)
}
// create a proof
proof, err := groth16.Prove(
oR1cs, pk, witness, backend.WithSolverOptions(solver.WithHints(TransferHint)),
)
if err != nil {
t.Fatal(err)
}
// serialize proof verify
var buf bytes.Buffer
_, err = proof.WriteTo(&buf)
if err != nil {
t.Fatal(err)
}
proofHex := hex.EncodeToString(buf.Bytes())
err = VerifyProof("500", proofHex)
if err != nil {
t.Fatal(err)
}
}