-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
270 lines (256 loc) · 7.61 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Welcome to the gnark playground!
package main
import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"math/big"
"time"
"github.com/auti-project/four-way-matching/fwmcircuit"
"github.com/consensys/gnark-crypto/accumulator/merkletree"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc"
"github.com/consensys/gnark-crypto/ecc/bn254/twistededwards"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/consensys/gnark/std/accumulator/merkle"
twistededwards2 "github.com/consensys/gnark/std/algebra/twistededwards"
)
func hashFunc(data []byte) ([]byte, error) {
mimcHash := mimc.NewMiMC()
mimcHash.Write(data)
return mimcHash.Sum(nil), nil
}
func main() {
var (
amount1 int64 = 100
amount4 int64 = 200
aux1 int64 = 1
aux2 int64 = 2
aux3 int64 = 3
aux4 int64 = 4
counter int64 = 10
)
timestampRangeLeft := time.Now().UnixNano()
time.Sleep(200 * time.Millisecond)
timestamp1 := time.Now().UnixNano()
time.Sleep(200 * time.Millisecond)
timestamp2 := time.Now().UnixNano()
time.Sleep(200 * time.Millisecond)
timestamp3 := time.Now().UnixNano()
time.Sleep(200 * time.Millisecond)
timestamp4 := time.Now().UnixNano()
time.Sleep(200 * time.Millisecond)
timestampRangeRight := time.Now().UnixNano()
privateG, privateH, err := fwmcircuit.KeyGen()
if err != nil {
panic(err)
}
g := &privateG.PublicKey.A // point affine
h := &privateH.PublicKey.A // point affine
assignG := twistededwards2.Point{
X: g.X.String(),
Y: g.Y.String(),
}
assignH := twistededwards2.Point{
X: h.X.String(),
Y: h.Y.String(),
}
assignCommitAmount1, _ := assignCommit(amount1, timestamp1, counter, g, h)
assignCommitAmount4, _ := assignCommit(amount4, timestamp4, counter, g, h)
assignCommitAux1, commitAux1 := assignCommit(aux1, timestamp1, counter, g, h)
assignCommitAux2, commitAux2 := assignCommit(aux2, timestamp2, counter, g, h)
assignCommitAux3, commitAux3 := assignCommit(aux3, timestamp3, counter, g, h)
assignCommitAux4, commitAux4 := assignCommit(aux4, timestamp4, counter, g, h)
const bufLen = 1000000
var buf bytes.Buffer
for i := 0; i < bufLen; i++ {
if i == 100 {
buf.Write(commitAux1)
} else if i == 200 {
buf.Write(commitAux2)
} else if i == 300 {
buf.Write(commitAux3)
} else if i == 400 {
buf.Write(commitAux4)
} else {
randBuf := make([]byte, 32)
_, err := rand.Read(randBuf)
if err != nil {
panic(err)
}
buf.Write(randBuf)
}
}
root1, proof1, numLeaves, err := merkletree.BuildReaderProof(&buf, mimc.NewMiMC(), 32, 100)
proofHelper1 := merkle.GenerateProofHelper(proof1, 100, numLeaves)
buf.Reset()
for i := 0; i < bufLen; i++ {
if i == 100 {
buf.Write(commitAux1)
} else if i == 200 {
buf.Write(commitAux2)
} else if i == 300 {
buf.Write(commitAux3)
} else if i == 400 {
buf.Write(commitAux4)
} else {
randBuf := make([]byte, 32)
_, err := rand.Read(randBuf)
if err != nil {
panic(err)
}
buf.Write(randBuf)
}
}
root2, proof2, numLeaves, err := merkletree.BuildReaderProof(&buf, mimc.NewMiMC(), 32, 200)
proofHelper2 := merkle.GenerateProofHelper(proof2, 200, numLeaves)
buf.Reset()
for i := 0; i < bufLen; i++ {
if i == 100 {
buf.Write(commitAux1)
} else if i == 200 {
buf.Write(commitAux2)
} else if i == 300 {
buf.Write(commitAux3)
} else if i == 400 {
buf.Write(commitAux4)
} else {
randBuf := make([]byte, 32)
_, err := rand.Read(randBuf)
if err != nil {
panic(err)
}
buf.Write(randBuf)
}
}
root3, proof3, numLeaves, err := merkletree.BuildReaderProof(&buf, mimc.NewMiMC(), 32, 300)
proofHelper3 := merkle.GenerateProofHelper(proof3, 300, numLeaves)
buf.Reset()
for i := 0; i < bufLen; i++ {
if i == 100 {
buf.Write(commitAux1)
} else if i == 200 {
buf.Write(commitAux2)
} else if i == 300 {
buf.Write(commitAux3)
} else if i == 400 {
buf.Write(commitAux4)
} else {
randBuf := make([]byte, 32)
_, err := rand.Read(randBuf)
if err != nil {
panic(err)
}
buf.Write(randBuf)
}
}
root4, proof4, numLeaves, err := merkletree.BuildReaderProof(&buf, mimc.NewMiMC(), 32, 400)
proofHelper4 := merkle.GenerateProofHelper(proof4, 400, numLeaves)
// compiles our circuit into a R1CS
var circuit fwmcircuit.FWMCircuit = fwmcircuit.FWMCircuit{
Path1: make([]frontend.Variable, len(proof1)),
Path2: make([]frontend.Variable, len(proof2)),
Path3: make([]frontend.Variable, len(proof3)),
Path4: make([]frontend.Variable, len(proof4)),
Helper1: make([]frontend.Variable, len(proofHelper1)),
Helper2: make([]frontend.Variable, len(proofHelper2)),
Helper3: make([]frontend.Variable, len(proofHelper3)),
Helper4: make([]frontend.Variable, len(proofHelper4)),
}
ccs, err := frontend.Compile(ecc.BN254, r1cs.NewBuilder, &circuit)
handleErr(err)
// groth16 zkSNARK: Setup
pk, vk, err := groth16.Setup(ccs)
handleErr(err)
// witness definition
assignment := fwmcircuit.FWMCircuit{
// statements
Commitment1: assignCommitAux1,
Commitment2: assignCommitAux2,
Commitment3: assignCommitAux3,
Commitment4: assignCommitAux4,
AmountCommitment1: assignCommitAmount1,
AmountCommitment4: assignCommitAmount4,
TimestampRangeLeft: timestampRangeLeft,
TimestampRangeRight: timestampRangeRight,
Root1: root1,
Root2: root2,
Root3: root3,
Root4: root4,
// witnesses
G: assignG,
H: assignH,
Counter: counter,
Aux1: aux1,
Aux2: aux2,
Aux3: aux3,
Aux4: aux4,
Amount1: amount1,
Amount4: amount4,
Timestamp1: timestamp1,
Timestamp2: timestamp2,
Timestamp3: timestamp3,
Timestamp4: timestamp4,
Path1: make([]frontend.Variable, len(proof1)),
Path2: make([]frontend.Variable, len(proof2)),
Path3: make([]frontend.Variable, len(proof3)),
Path4: make([]frontend.Variable, len(proof4)),
Helper1: make([]frontend.Variable, len(proofHelper1)),
Helper2: make([]frontend.Variable, len(proofHelper2)),
Helper3: make([]frontend.Variable, len(proofHelper3)),
Helper4: make([]frontend.Variable, len(proofHelper4)),
}
for i := range proof1 {
assignment.Path1[i] = proof1[i]
}
for i := range proof2 {
assignment.Path2[i] = proof2[i]
}
for i := range proof3 {
assignment.Path3[i] = proof3[i]
}
for i := range proof4 {
assignment.Path4[i] = proof4[i]
}
for i := range proofHelper1 {
assignment.Helper1[i] = proofHelper1[i]
}
for i := range proofHelper2 {
assignment.Helper2[i] = proofHelper2[i]
}
for i := range proofHelper3 {
assignment.Helper3[i] = proofHelper3[i]
}
for i := range proofHelper4 {
assignment.Helper4[i] = proofHelper4[i]
}
witness, err := frontend.NewWitness(&assignment, ecc.BN254)
handleErr(err)
publicWitness, _ := witness.Public()
// groth16: Prove & Verify
proof, err := groth16.Prove(ccs, pk, witness)
handleErr(err)
err = groth16.Verify(proof, vk, publicWitness)
handleErr(err)
// measure the proof size
proofBytes, err := json.Marshal(proof)
handleErr(err)
fmt.Printf("Proof size: %d bytes", len(proofBytes))
}
func assignCommit(num, timestamp, counter int64, g, h *twistededwards.PointAffine) (twistededwards2.Point, []byte) {
commit := new(twistededwards.PointAffine).ScalarMul(g, big.NewInt(num))
tmp := new(twistededwards.PointAffine).ScalarMul(h, fwmcircuit.MiMCHashTimestampCounter(timestamp, counter))
commit.Add(commit, tmp)
return twistededwards2.Point{
X: commit.X.String(),
Y: commit.Y.String(),
}, commit.Marshal()
}
func handleErr(err error) {
if err != nil {
panic(err)
}
}