-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpairing_circuit_test.go
151 lines (129 loc) · 4.49 KB
/
pairing_circuit_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"log"
"testing"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/backend/witness"
"github.com/consensys/gnark/constraint"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/consensys/gnark/std/algebra/emulated/sw_bn254"
"github.com/consensys/gnark/test"
)
func setupKeys() (groth16.ProvingKey, groth16.VerifyingKey, constraint.ConstraintSystem) {
circuit := BN256PairingCircuit{
G1Points: [2]*sw_bn254.G1Affine{new(sw_bn254.G1Affine), new(sw_bn254.G1Affine)},
G2Points: [2]*sw_bn254.G2Affine{new(sw_bn254.G2Affine), new(sw_bn254.G2Affine)},
}
r1cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
if err != nil {
log.Fatalf("Failed to compile circuit: %v", err)
}
//srs, srsLagrange, err := unsafekzg.NewSRS(r1cs)
//if err != nil {
// log.Fatalf("Failed to setup keys: %v", err)
//}
pk, vk, err := groth16.Setup(r1cs)
if err != nil {
log.Fatalf("Failed to setup keys: %v", err)
}
return pk, vk, r1cs
}
func createProof(pk groth16.ProvingKey, r1cs constraint.ConstraintSystem) (groth16.Proof, witness.Witness) {
witnessCircuit := BN256PairingCircuit{}
// Set the G1 and G2 points
witnessCircuit.FillVerifyCircuitsG1(
"2920616387084030925907755037226454382846345550621956833249622258647667607078",
"16502678157049327323910877548707266122319935523346850792311342099791838736912",
"7162082828732168937516361335135022403650719329242056518668518043784043198510",
"18768486256042060147567165227366772940689098806912976766549486364846889365307",
)
witnessCircuit.FillVerifyCircuitsG2()
witness, err := frontend.NewWitness(&witnessCircuit, ecc.BN254.ScalarField())
if err != nil {
panic(err)
}
proof, err := groth16.Prove(r1cs, pk, witness)
if err != nil {
log.Fatalf("Failed to create proof: %v", err)
}
public, err := witness.Public()
if err != nil {
log.Fatalf("Failed to Public: %v", err)
}
return proof, public
}
func verifyProof(proof groth16.Proof, vk groth16.VerifyingKey, witness witness.Witness) bool {
if err := groth16.Verify(proof, vk, witness); err != nil {
log.Fatalf("Failed to verify proof: %v", err)
return false
}
return true
}
func TestPairingCheckTestSolve(t *testing.T) {
witnessCircuit := BN256PairingCircuit{}
circuit := BN256PairingCircuit{
G1Points: [2]*sw_bn254.G1Affine{new(sw_bn254.G1Affine), new(sw_bn254.G1Affine)},
G2Points: [2]*sw_bn254.G2Affine{new(sw_bn254.G2Affine), new(sw_bn254.G2Affine)},
}
witnessCircuit.FillVerifyCircuitsG1(
"2920616387084030925907755037226454382846345550621956833249622258647667607078",
"16502678157049327323910877548707266122319935523346850792311342099791838736912",
"7162082828732168937516361335135022403650719329242056518668518043784043198510",
"18768486256042060147567165227366772940689098806912976766549486364846889365307",
)
witnessCircuit.FillVerifyCircuitsG2()
err := test.IsSolved(&circuit, &witnessCircuit, ecc.BN254.ScalarField())
assert := test.NewAssert(t)
assert.NoError(err)
}
func TestPairing(t *testing.T) {
log.Println("setupKeys start !")
pk, vk, r1cs := setupKeys()
log.Println("setupKeys end !")
log.Println("createProof start !")
proof, public := createProof(pk, r1cs)
log.Println("createProof end !")
if verifyProof(proof, vk, public) {
log.Println("Proof verified successfully!")
} else {
log.Println("Failed to verify proof.")
}
}
func TestOnCurve(t *testing.T) {
assert := test.NewAssert(t)
// proof[102]
var g10 = bn254.G1Jac{}
_, err := g10.X.SetString("2920616387084030925907755037226454382846345550621956833249622258647667607078")
if err != nil {
panic(err)
}
_, err = g10.Y.SetString("16502678157049327323910877548707266122319935523346850792311342099791838736912")
if err != nil {
panic(err)
}
g10.Z.SetOne()
assert.True(g10.IsOnCurve())
var g10GAff bn254.G1Affine
g10GAff.FromJacobian(&g10)
assert.True(g10GAff.IsOnCurve())
var g11 = bn254.G1Jac{}
_, err = g11.X.SetString("7162082828732168937516361335135022403650719329242056518668518043784043198510")
if err != nil {
panic(err)
}
_, err = g11.Y.SetString("18768486256042060147567165227366772940689098806912976766549486364846889365307")
if err != nil {
panic(err)
}
g11.Z.SetOne()
assert.True(g11.IsOnCurve())
var g11GAff bn254.G1Affine
g11GAff.FromJacobian(&g11)
assert.True(g11GAff.IsOnCurve())
g20, g21 := GetVerifyCircuitsG2Jac()
assert.True(g20.IsOnCurve())
assert.True(g21.IsOnCurve())
}