-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
421 lines (371 loc) · 10 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package main
import (
"bufio"
"fmt"
"log"
"math/big"
"os"
"strconv"
"strings"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
)
func main() {
stderr := log.New(os.Stderr, "", 0)
if len(os.Args) < 2 {
stderr.Println("Usage: xjsnark-gnark-prover <command> <args>")
os.Exit(1)
}
command := os.Args[1]
if command == "compile" {
if len(os.Args) < 4 {
stderr.Println("Usage: xjsnark-gnark-prover compile <xjsnark-arith-file> <output-gnark-circuit-file>")
os.Exit(1)
}
log.Print("Start Loading and compiling Xjsnark arith file")
circuit := newCircuitFromXjsnark(os.Args[2])
r1cs, err := frontend.Compile(ecc.BN254, r1cs.NewBuilder, circuit)
if err != nil {
panic(err)
}
log.Print("Load and compile Xjsnark arith file done")
f, err := os.Create(os.Args[3])
if err != nil {
panic(err)
}
defer f.Close()
fWriter := bufio.NewWriter(f)
fmt.Fprintf(fWriter, "%d\n", circuit.outputEnd)
defer fWriter.Flush()
r1cs.WriteTo(fWriter)
log.Print("Write gnark circuit done")
} else if command == "keygen" {
if len(os.Args) < 5 {
stderr.Println("Usage: xjsnark-gnark-prover keygen <gnark-circuit-file> <output-pkey-file> <output-vkey-file>")
os.Exit(1)
}
log.Print("Loading circuit start")
r1cs := groth16.NewCS(ecc.BN254)
f, err := os.Open(os.Args[2])
if err != nil {
panic(err)
}
defer f.Close()
// fReader := bufio.NewReader(f)
var unused string
_, _ = fmt.Fscanf(f, "%s\n", &unused)
_, err = r1cs.ReadFrom(f)
if err != nil {
panic(err)
}
log.Print("Loading circuit done")
log.Print("Generating pk and vk start")
pk, vk, err := groth16.Setup(r1cs)
log.Print("Generate pk and vk done")
log.Print("Writing pk")
fpk, err := os.Create(os.Args[3])
if err != nil {
panic(err)
}
defer fpk.Close()
fpkwriter := bufio.NewWriter(fpk)
pk.WriteRawTo(fpkwriter)
log.Print("Writing pk done")
log.Print("Writing vk")
fvk, err := os.Create(os.Args[4])
if err != nil {
panic(err)
}
defer fvk.Close()
vk.WriteRawTo(fvk)
log.Print("Writing vk done")
} else if command == "prove" {
if len(os.Args) < 6 {
stderr.Println("Usage: xjsnark-gnark-prover prove <gnark-circuit-file> <pkey-file> <witness> <output-proof>")
os.Exit(1)
}
log.Print("Loading circuit start")
r1cs := groth16.NewCS(ecc.BN254)
f, err := os.Open(os.Args[2])
if err != nil {
panic(err)
}
defer f.Close()
fReader := bufio.NewReaderSize(f, 4096000000)
var outputEnd uint
_, _ = fmt.Fscanf(fReader, "%d\n", &outputEnd)
_, err = r1cs.ReadFrom(fReader)
if err != nil {
panic(err)
}
log.Print("Loading circuit done")
log.Print("Loading pk")
fpk, err := os.Open(os.Args[3])
defer fpk.Close()
fpkReader := bufio.NewReader(fpk)
pk := groth16.NewProvingKey(ecc.BN254)
_, err = pk.UnsafeReadFrom(fpkReader)
log.Print("Loading pk done")
log.Print("Loading witness")
assignment := loadAssignment(os.Args[4], r1cs, outputEnd)
witness, err := frontend.NewWitness(assignment, ecc.BN254)
log.Print("Loading witness done")
log.Print("Proving")
proof, err := groth16.Prove(r1cs, pk, witness)
if err != nil {
panic(err)
}
log.Print("Prove done")
log.Print("Writing proof")
fp, err := os.Create(os.Args[5])
if err != nil {
panic(err)
}
defer fp.Close()
proof.WriteTo(fp)
log.Print("Writing proof done")
} else if command == "verify" {
if len(os.Args) < 5 {
stderr.Println("Usage: xjsnark-gnark-prover verify <proof> <vk-file> <public-witness>")
os.Exit(1)
}
log.Print("Loading proof")
fp, err := os.Open(os.Args[2])
defer fp.Close()
proof := groth16.NewProof(ecc.BN254)
proof.ReadFrom(fp)
log.Print("Loading proof done")
log.Print("Loading vk")
fvk, err := os.Open(os.Args[3])
defer fvk.Close()
vk := groth16.NewVerifyingKey(ecc.BN254)
_, err = vk.ReadFrom(fvk)
log.Print("Loading vk done")
log.Print("Loading public assignment")
assignment := loadPublicAssignment(os.Args[4])
witness, err := frontend.NewWitness(assignment, ecc.BN254)
log.Print("Loading public assignment done")
publicWitness, _ := witness.Public()
err = groth16.Verify(proof, vk, publicWitness)
if err != nil {
panic(err)
}
log.Print("Verify done")
} else {
stderr.Println("Expected one of the following command: compile, keygen, prove, verify")
os.Exit(1)
}
}
type Circuit struct {
P []frontend.Variable `gnark:",public"`
S []frontend.Variable
outputEnd uint
Scanner *bufio.Scanner
totalVars uint
}
func (circuit *Circuit) Define(api frontend.API) error {
parseLibsnarkArith(circuit, api)
return nil
}
func sliceAtoi(sa []string) ([]int, error) {
si := make([]int, 0, len(sa))
for _, a := range sa {
i, err := strconv.Atoi(a)
if err != nil {
return si, err
}
si = append(si, i)
}
return si, nil
}
func newCircuitFromXjsnark(xjsnarkArithPath string) (circuit *Circuit) {
circuit = new(Circuit)
f, err := os.Open(xjsnarkArithPath)
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(f)
circuit.Scanner = scanner
scanner.Scan()
line := scanner.Text()
n, _ := fmt.Sscanf(line, "total %d", &circuit.totalVars)
if n != 1 {
log.Fatal("File Format Does not Match, expect total n")
}
var nPublicInput, nSecretInput uint
for scanner.Scan() {
line = scanner.Text()
var id uint
n, _ = fmt.Sscanf(line, "input %d", &id)
if n == 1 {
nPublicInput++
continue
}
n, _ = fmt.Sscanf(line, "nizkinput %d", &id)
if n == 1 {
nSecretInput++
continue
}
// gnark does not support output, we record the ids and just log them in the end
n, _ = fmt.Sscanf(line, "output %d", &id)
if n == 1 {
circuit.outputEnd = id
continue
}
break
}
circuit.P = make([]frontend.Variable, nPublicInput)
circuit.S = make([]frontend.Variable, nSecretInput)
return
}
func parseLibsnarkArith(circuit *Circuit, api frontend.API) {
Vars := make([]frontend.Variable, circuit.totalVars)
scanner := circuit.Scanner
for i, p := range circuit.P {
Vars[i] = p
}
for i, s := range circuit.S {
Vars[i+len(circuit.P)] = s
}
for {
line := scanner.Text()
var t, inStr, outStr string
n, _ := fmt.Sscanf(line, "%s in %s out %s", &t, &inStr, &outStr)
if n == 3 {
inValues, err := sliceAtoi(strings.Split(inStr, "_"))
if err != nil {
log.Fatal(err)
}
outValues, err := sliceAtoi(strings.Split(outStr, "_"))
if err != nil {
log.Fatal(err)
}
if t == "add" {
var in []frontend.Variable
if len(inValues) > 2 {
in = make([]frontend.Variable, len(inValues)-2)
for i := 2; i < len(inValues); i++ {
in[i-2] = Vars[inValues[i]]
}
} else {
in = make([]frontend.Variable, 0)
}
Vars[outValues[0]] = api.Add(Vars[inValues[0]], Vars[inValues[1]], in...)
} else if t == "mul" {
Vars[outValues[0]] = api.Mul(Vars[inValues[0]], Vars[inValues[1]])
} else if strings.Contains(t, "const-mul-neg-") {
constStr := t[len("const-mul-neg-"):]
bi, success := new(big.Int).SetString(constStr, 16)
if !success {
log.Fatal("not a valid hex number")
}
Vars[outValues[0]] = api.Mul(api.Neg(bi), Vars[inValues[0]])
} else if strings.Contains(t, "const-mul-") {
constStr := t[len("const-mul-"):]
bi, success := new(big.Int).SetString(constStr, 16)
if !success {
log.Fatal("not a valid hex number. line:", line)
}
Vars[outValues[0]] = api.Mul(bi, Vars[inValues[0]])
} else if t == "assert" {
api.AssertIsEqual(api.Mul(Vars[inValues[0]], Vars[inValues[1]]), Vars[outValues[0]])
} else if t == "xor" {
Vars[outValues[0]] = api.Xor(api.IsZero(Vars[inValues[0]]), api.IsZero(Vars[inValues[1]]))
} else if t == "or" {
Vars[outValues[0]] = api.Or(api.IsZero(Vars[inValues[0]]), api.IsZero(Vars[inValues[1]]))
} else if t == "zerop" {
Vars[outValues[1]] = api.Sub(big.NewInt(1), api.IsZero(Vars[inValues[0]]))
} else if t == "split" {
l := len(outValues)
if l == 1 {
Vars[outValues[0]] = Vars[inValues[0]]
} else {
bits := api.ToBinary(Vars[inValues[0]], l)
for i, e := range bits {
Vars[outValues[i]] = e
}
}
} else if t == "pack" {
in := make([]frontend.Variable, len(inValues))
for i := 0; i < len(inValues); i++ {
in[i] = Vars[inValues[i]]
}
Vars[outValues[0]] = api.FromBinary(in...)
} else {
log.Fatal("Unknown opcode:", t)
}
} else {
log.Fatal("Arith file format invalid line:", line, "expected <opcode> in <input vars> out <output vars>")
}
if !scanner.Scan() {
break
}
}
if circuit.outputEnd != 0 {
outputStart := len(circuit.P) + len(circuit.S)
for i := outputStart; i <= int(circuit.outputEnd); i++ {
api.Println("Output", i, "=", Vars[i])
}
}
// For debug purpose, log all var
// for i := 0; i < int(circuit.totalVars); i++ {
// api.Println(i, Vars[i])
// }
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func loadAssignment(filename string, r1cs frontend.CompiledConstraintSystem, outputEnd uint) (ret *Circuit) {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
ret = new(Circuit)
ret.outputEnd = outputEnd
_, nSecret, nPublic := r1cs.GetNbVariables()
log.Print(nPublic, nSecret)
ret.P = make([]frontend.Variable, nPublic-1)
ret.S = make([]frontend.Variable, nSecret)
var id int
var hex string
for {
n, _ := fmt.Fscanf(f, "%d %s\n", &id, &hex)
if n != 2 {
break
}
bi, success := new(big.Int).SetString(hex, 16)
if !success {
log.Fatal("not a valid hex number")
}
if id < len(ret.P) {
ret.P[id] = bi
} else if id < len(ret.P)+len(ret.S) {
ret.S[id-len(ret.P)] = bi
}
}
return
}
func loadPublicAssignment(filename string) (ret *Circuit) {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
ret = new(Circuit)
ret.P = make([]frontend.Variable, 0)
var id int
var hex string
for {
n, _ := fmt.Fscanf(f, "%d %s\n", &id, &hex)
if n != 2 {
break
}
bi, success := new(big.Int).SetString(hex, 16)
if !success {
log.Fatal("not a valid hex number")
}
ret.P = append(ret.P, bi)
}
return
}