-
Notifications
You must be signed in to change notification settings - Fork 2
/
lean_export.go
484 lines (432 loc) · 12.9 KB
/
lean_export.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package extractor
import (
"fmt"
"math/big"
"reflect"
"strings"
"github.com/reilabs/gnark-lean-extractor/abstractor"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/schema"
)
func ExportPrelude(name string, order *big.Int) string {
s := fmt.Sprintf(`import ProvenZk.Gates
import ProvenZk.Ext.Vector
namespace %s
def Order : ℕ := 0x%s
variable [Fact (Nat.Prime Order)]
abbrev F := ZMod Order`, name, order.Text(16))
return s
}
func ExportFooter(name string) string {
s := fmt.Sprintf(`end %s`, name)
return s
}
func ExportGadget(gadget ExGadget) string {
kArgsType := "F"
if len(gadget.Outputs) > 1 {
kArgsType = fmt.Sprintf("Vector F %d", len(gadget.Outputs))
}
inAssignment := gadget.Args
return fmt.Sprintf("def %s %s (k: %s -> Prop): Prop :=\n%s", gadget.Name, genArgs(inAssignment), kArgsType, genGadgetBody(inAssignment, gadget))
}
func ExportGadgets(exGadgets []ExGadget) string {
gadgets := make([]string, len(exGadgets))
for i, gadget := range exGadgets {
gadgets[i] = ExportGadget(gadget)
}
return strings.Join(gadgets, "\n\n")
}
func ExportCircuit(circuit ExCircuit) string {
gadgets := ExportGadgets(circuit.Gadgets)
circ := fmt.Sprintf("def circuit %s: Prop :=\n%s", genArgs(circuit.Inputs), genCircuitBody(circuit))
prelude := ExportPrelude(circuit.Name, circuit.Field.ScalarField())
footer := ExportFooter(circuit.Name)
return fmt.Sprintf("%s\n\n%s\n\n%s\n\n%s", prelude, gadgets, circ, footer)
}
func ArrayInit(f schema.Field, v reflect.Value, op Operand) error {
for i := 0; i < f.ArraySize; i++ {
op := Proj{op, i}
switch len(f.SubFields) {
case 1:
ArrayInit(f.SubFields[0], v.Index(i), op)
case 0:
value := reflect.ValueOf(op)
v.Index(i).Set(value)
default:
panic("Only nested arrays supported in SubFields")
}
}
return nil
}
func CircuitInit(class any, schema *schema.Schema) error {
// https://stackoverflow.com/a/49704408
// https://stackoverflow.com/a/14162161
// https://stackoverflow.com/a/63422049
// The purpose of this function is to initialise the
// struct fields with Operand interfaces for being
// processed by the Extractor.
v := reflect.ValueOf(class)
if v.Type().Kind() == reflect.Ptr {
ptr := v
v = ptr.Elem()
} else {
ptr := reflect.New(reflect.TypeOf(class))
temp := ptr.Elem()
temp.Set(v)
}
tmp_c := reflect.ValueOf(&class).Elem().Elem()
tmp := reflect.New(tmp_c.Type()).Elem()
tmp.Set(tmp_c)
for j, f := range schema.Fields {
field_name := f.Name
field := v.FieldByName(field_name)
field_type := field.Type()
// Can't assign an array to another array, therefore
// initialise each element in the array
if field_type.Kind() == reflect.Array {
ArrayInit(f, tmp.Elem().FieldByName(field_name), Input{j})
} else if field_type.Kind() == reflect.Slice {
// Recreate a zeroed array to remove overlapping pointers if input
// arguments are duplicated (i.e. `api.Call(SliceGadget{circuit.Path, circuit.Path})`)
zero_array := make([]frontend.Variable, f.ArraySize, f.ArraySize)
tmp.Elem().FieldByName(field_name).Set(reflect.ValueOf(&zero_array).Elem())
ArrayInit(f, tmp.Elem().FieldByName(field_name), Input{j})
} else if field_type.Kind() == reflect.Interface {
init := Input{j}
value := reflect.ValueOf(init)
tmp.Elem().FieldByName(field_name).Set(value)
} else {
fmt.Printf("Skipped type %s\n", field_type.Kind())
}
}
return nil
}
func KindOfField(a any, s string) reflect.Kind {
v := reflect.ValueOf(a).Elem()
f := v.FieldByName(s)
return f.Kind()
}
func CircuitArgs(field schema.Field) ExArgType {
// Handling only subfields which are nested arrays
switch len(field.SubFields) {
case 1:
subType := CircuitArgs(field.SubFields[0])
return ExArgType{field.ArraySize, &subType}
case 0:
return ExArgType{field.ArraySize, nil}
default:
panic("Only nested arrays supported in SubFields")
}
}
func GetExArgs(circuit any, fields []schema.Field) []ExArg {
args := []ExArg{}
for _, f := range fields {
kind := KindOfField(circuit, f.Name)
arg := ExArg{f.Name, kind, CircuitArgs(f)}
args = append(args, arg)
}
return args
}
// Cloned version of NewSchema without constraints
func GetSchema(circuit any) (*schema.Schema, error) {
tVariable := reflect.ValueOf(struct{ A frontend.Variable }{}).FieldByName("A").Type()
return schema.New(circuit, tVariable)
}
func getStructName(circuit any) string {
return reflect.TypeOf(circuit).Elem().Name()
}
func CircuitToLean(circuit abstractor.Circuit, field ecc.ID) (string, error) {
schema, err := GetSchema(circuit)
if err != nil {
return "", err
}
err = CircuitInit(circuit, schema)
if err != nil {
fmt.Println("CircuitInit error!")
fmt.Println(err.Error())
}
api := CodeExtractor{
Code: []App{},
Gadgets: []ExGadget{},
Field: field,
}
err = circuit.AbsDefine(&api)
if err != nil {
return "", err
}
name := getStructName(circuit)
extractorCircuit := ExCircuit{
Inputs: GetExArgs(circuit, schema.Fields),
Gadgets: api.Gadgets,
Code: api.Code,
Field: api.Field,
Name: name,
}
out := ExportCircuit(extractorCircuit)
return out, nil
}
func GadgetToLean(circuit abstractor.GadgetDefinition, field ecc.ID) (string, error) {
api := CodeExtractor{
Code: []App{},
Gadgets: []ExGadget{},
Field: field,
}
name := getStructName(circuit)
api.DefineGadget(circuit)
gadgets := ExportGadgets(api.Gadgets)
prelude := ExportPrelude(name, api.Field.ScalarField())
footer := ExportFooter(name)
return fmt.Sprintf("%s\n\n%s\n\n%s", prelude, gadgets, footer), nil
}
func genNestedArrays(a ExArgType) string {
if a.Type != nil {
return fmt.Sprintf("Vector (%s) %d", genNestedArrays(*a.Type), a.Size)
}
return fmt.Sprintf("Vector F %d", a.Size)
}
func genArgs(inAssignment []ExArg) string {
args := make([]string, len(inAssignment))
for i, in := range inAssignment {
switch in.Kind {
case reflect.Array, reflect.Slice:
args[i] = fmt.Sprintf("(%s: %s)", in.Name, genNestedArrays(in.Type))
default:
args[i] = fmt.Sprintf("(%s: F)", in.Name)
}
}
return strings.Join(args, " ")
}
func extractGateVars(arg Operand) []Operand {
switch arg.(type) {
case Proj:
return extractGateVars(arg.(Proj).Operand)
case ProjArray:
res := []Operand{}
for i := range arg.(ProjArray).Proj {
res = append(res, extractGateVars(arg.(ProjArray).Proj[i])...)
}
return res
default:
return []Operand{arg}
}
}
func assignGateVars(code []App, additional ...Operand) []string {
gateVars := make([]string, len(code))
for _, app := range code {
for _, arg := range app.Args {
bases := extractGateVars(arg)
for _, base := range bases {
switch base.(type) {
case Gate:
ix := base.(Gate).Index
if gateVars[ix] == "" {
gateVars[ix] = fmt.Sprintf("gate_%d", ix)
}
}
}
}
}
for _, out := range additional {
outBases := extractGateVars(out)
for _, outBase := range outBases {
switch outBase.(type) {
case Gate:
ix := outBase.(Gate).Index
if gateVars[ix] == "" {
gateVars[ix] = fmt.Sprintf("gate_%d", ix)
}
}
}
}
return gateVars
}
func genGadgetCall(gateVar string, inAssignment []ExArg, gateVars []string, gadget *ExGadget, args []Operand) string {
name := gadget.Name
operands := operandExprs(args, inAssignment, gateVars)
binder := "_"
if gateVar != "" {
binder = gateVar
}
return fmt.Sprintf(" %s %s fun %s =>\n", name, strings.Join(operands, " "), binder)
}
func genGateOp(op Op) string {
name := "unknown"
switch op {
case OpAdd:
name = "add"
case OpMulAcc:
name = "mul_acc"
case OpNegative:
name = "neg"
case OpSub:
name = "sub"
case OpMul:
name = "mul"
case OpDivUnchecked:
name = "div_unchecked"
case OpDiv:
name = "div"
case OpInverse:
name = "inv"
case OpXor:
name = "xor"
case OpOr:
name = "or"
case OpAnd:
name = "and"
case OpSelect:
name = "select"
case OpLookup:
name = "lookup"
case OpIsZero:
name = "is_zero"
case OpCmp:
name = "cmp"
case OpAssertEq:
name = "eq"
case OpAssertNotEq:
name = "ne"
case OpAssertIsBool:
name = "is_bool"
case OpAssertLessEqual:
name = "le"
case OpFromBinary:
name = "from_binary"
case OpToBinary:
name = "to_binary"
}
return fmt.Sprintf("Gates.%s", name)
}
func getGateName(gateVar string, explicit bool) string {
varName := "_ignored_"
if gateVar != "" {
varName = gateVar
}
if explicit {
return fmt.Sprintf("(%s : F)", varName)
}
return varName
}
func genGateBinder(gateVar string) string {
gateName := getGateName(gateVar, false)
return fmt.Sprintf("∃%s, %s = ", gateName, gateName)
}
func genFunctionalGate(gateVar string, op Op, operands []string) string {
return fmt.Sprintf(" %s%s %s ∧\n", genGateBinder(gateVar), genGateOp(op), strings.Join(operands, " "))
}
func genCallbackGate(gateVar string, op Op, operands []string, args []Operand) string {
gateName := getGateName(gateVar, false)
switch op {
case OpFromBinary:
is_gate := reflect.TypeOf(args[0]) == reflect.TypeOf(Gate{})
if len(args) == 1 && is_gate {
return fmt.Sprintf(" ∃%s, %s %s %s ∧\n", gateName, genGateOp(op), strings.Join(operands, " "), gateName)
}
return fmt.Sprintf(" ∃%s, %s vec![%s] %s ∧\n", gateName, genGateOp(op), strings.Join(operands, ", "), gateName)
case OpToBinary:
is_const := reflect.TypeOf(args[0]) == reflect.TypeOf(Const{})
if is_const {
operands[0] = fmt.Sprintf("(%s:F)", operands[0])
return fmt.Sprintf(" ∃%s, %s %s %s ∧\n", gateName, genGateOp(op), strings.Join(operands, " "), gateName)
}
return fmt.Sprintf(" ∃%s, %s %s %s ∧\n", gateName, genGateOp(op), strings.Join(operands, " "), gateName)
default:
return fmt.Sprintf(" ∃%s, %s %s %s ∧\n", gateName, genGateOp(op), strings.Join(operands, " "), gateName)
}
}
func genGenericGate(op Op, operands []string) string {
return fmt.Sprintf(" %s %s ∧\n", genGateOp(op), strings.Join(operands, " "))
}
func genOpCall(gateVar string, inAssignment []ExArg, gateVars []string, op Op, args []Operand) string {
// functional is set to true when the op returns a value
functional := false
callback := false
switch op {
case OpDivUnchecked, OpDiv, OpInverse, OpXor, OpOr, OpAnd, OpSelect, OpLookup, OpCmp, OpIsZero, OpToBinary, OpFromBinary:
callback = true
case OpAdd, OpMulAcc, OpNegative, OpSub, OpMul:
functional = true
}
operands := operandExprs(args, inAssignment, gateVars)
if functional {
// if an operation supports infinite length of arguments,
// turn it into a chain of operations
switch op {
case OpAdd, OpSub, OpMul:
{
finalStr := genFunctionalGate(gateVar, op, operands[0:2])
for len(operands) > 2 {
operands = operands[1:]
operands[0] = getGateName(gateVar, false)
finalStr += genFunctionalGate(gateVar, op, operands[0:2])
}
return finalStr
}
default:
return genFunctionalGate(gateVar, op, operands)
}
} else if callback {
return genCallbackGate(gateVar, op, operands, args)
} else {
return genGenericGate(op, operands)
}
}
func genLine(app App, gateVar string, inAssignment []ExArg, gateVars []string) string {
switch app.Op.(type) {
case *ExGadget:
return genGadgetCall(gateVar, inAssignment, gateVars, app.Op.(*ExGadget), app.Args)
case Op:
return genOpCall(gateVar, inAssignment, gateVars, app.Op.(Op), app.Args)
}
return ""
}
func genGadgetBody(inAssignment []ExArg, gadget ExGadget) string {
gateVars := assignGateVars(gadget.Code, gadget.Outputs...)
lines := make([]string, len(gadget.Code))
for i, app := range gadget.Code {
lines[i] = genLine(app, gateVars[i], inAssignment, gateVars)
}
outs := operandExprs(gadget.Outputs, inAssignment, gateVars)
result := outs[0]
if len(gadget.Outputs) > 1 {
result = fmt.Sprintf("vec![%s]", strings.Join(outs, ", "))
}
lastLine := fmt.Sprintf(" k %s", result)
return strings.Join(append(lines, lastLine), "")
}
func genCircuitBody(circuit ExCircuit) string {
gateVars := assignGateVars(circuit.Code)
lines := make([]string, len(circuit.Code))
for i, app := range circuit.Code {
lines[i] = genLine(app, gateVars[i], circuit.Inputs, gateVars)
}
lastLine := " True"
return strings.Join(append(lines, lastLine), "")
}
func operandExpr(operand Operand, inAssignment []ExArg, gateVars []string) string {
switch operand.(type) {
case Input:
return inAssignment[operand.(Input).Index].Name
case Gate:
return gateVars[operand.(Gate).Index]
case Proj:
return fmt.Sprintf("%s[%d]", operandExpr(operand.(Proj).Operand, inAssignment, gateVars), operand.(Proj).Index)
case ProjArray:
opArray := operandExprs(operand.(ProjArray).Proj, inAssignment, gateVars)
opArray = []string{strings.Join(opArray, ", ")}
return fmt.Sprintf("vec!%s", opArray)
case Const:
return operand.(Const).Value.Text(10)
default:
fmt.Printf("Type %T\n", operand)
panic("not yet supported")
}
}
func operandExprs(operands []Operand, inAssignment []ExArg, gateVars []string) []string {
exprs := make([]string, len(operands))
for i, operand := range operands {
exprs[i] = operandExpr(operand, inAssignment, gateVars)
}
return exprs
}