Source file mod_arith_gates.ml
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
open Kzg.Bls
open Identities
module L = Plompiler.LibCircuit
open Gates_common
module Make_ModAdd (MOD_ARITH : Plompiler__Gadget_mod_arith.MOD_ARITH) :
Base_sig = struct
module M = MOD_ARITH (L)
let q_label = "q_mod_add_" ^ M.label
let ( %! ) = Z.rem
let nb_used_wires =
let used_fst_row = 2 * M.nb_limbs in
let used_snd_row = M.nb_limbs + 1 + List.length M.moduli_add in
let nb_used_wires = Int.max used_fst_row used_snd_row in
assert (nb_used_wires <= Plompiler.Csir.nb_wires_arch) ;
nb_used_wires
let bs_mod_m =
List.init M.nb_limbs (fun i -> Z.pow M.base i %! M.modulus) |> List.rev
let (qm_shift, _), ts_bounds = M.bounds_add
let identity = (q_label, 1 + List.length M.moduli_add)
let index_com = None
let nb_advs = 0
let nb_buffers = 3
let gx_composition = true
let polynomials_degree =
(q_label, 2) :: List.init nb_used_wires (fun i -> (wire_name i, 2))
|> SMap.of_list
let get_values wires wires_g =
let xs = List.init M.nb_limbs (fun i -> wires.(i)) in
let ys = List.init M.nb_limbs (fun i -> wires.(M.nb_limbs + i)) in
let zs = List.init M.nb_limbs (fun i -> wires_g.(i)) in
let qm = wires_g.(M.nb_limbs) in
let ts = List.mapi (fun i _ -> wires_g.(M.nb_limbs + 1 + i)) M.moduli_add in
let t_infos =
List.map2 (fun tj (t_shift, _) -> Some (tj, t_shift)) ts ts_bounds
in
(xs, ys, zs, qm, t_infos)
let equations ~q:q_mod_add ~wires ~wires_g ?precomputed_advice:_ () =
let xs, ys, zs, qm, t_infos = get_values wires wires_g in
let sum = List.fold_left Scalar.add Scalar.zero in
List.map2
(fun mj t_info ->
let tj, tj_shift =
match t_info with
| Some (tj, tj_shift) -> (tj, tj_shift)
| None -> (Scalar.zero, Z.zero)
in
let id_mj =
let open Scalar in
sum
(List.map2
(fun bi_mod_m ((xi, yi), zi) ->
of_z (bi_mod_m %! mj) * (xi + yi + negate zi))
bs_mod_m
(List.combine (List.combine xs ys) zs))
+ negate (qm * of_z (M.modulus %! mj))
+ negate (of_z Z.(qm_shift * M.modulus %! mj))
+ negate ((tj + of_z tj_shift) * of_z mj)
in
Scalar.(q_mod_add * id_mj))
(Scalar.order :: M.moduli_add)
(None :: t_infos)
let prover_identities ~prefix_common ~prefix ~public:_ ~domain :
prover_identities =
fun evaluations ->
let domain_size = Domain.length domain in
let tmps, ids = get_buffers ~nb_buffers ~nb_ids:(snd identity) in
let ({q; wires} : witness) =
get_evaluations ~q_label ~prefix ~prefix_common evaluations
in
let q_mod_add = q in
let xs, ys, _zs, qm, t_infos = get_values wires wires in
List.mapi
(fun i (mj, t_info) ->
let id_mj_without_sum =
let tj, tj_coeff, tj_shift, tj_comp =
match t_info with
| Some (tj, tj_shift) ->
([tj], Scalar.[negate (of_z mj)], tj_shift, [1])
| None -> ([], [], Z.zero, [])
in
Evaluations.linear_c
~res:tmps.(0)
~evaluations:(qm :: tj)
~composition_gx:(1 :: tj_comp, domain_size)
~linear_coeffs:(Scalar.(negate (of_z (M.modulus %! mj))) :: tj_coeff)
~add_constant:
Scalar.(
negate (of_z Z.((qm_shift * M.modulus %! mj) + (tj_shift * mj))))
()
in
let id_mj =
List.fold_left2
(fun acc bi_mod_m (xi, yi) ->
let zi = xi in
let xi_plus_yi_minus_zi =
Evaluations.linear_c
~res:tmps.(2)
~evaluations:[xi; yi; zi]
~linear_coeffs:[one; one; mone]
~composition_gx:([0; 0; 1], domain_size)
()
in
let acc =
Evaluations.linear_c
~res:tmps.(1)
~evaluations:[acc; xi_plus_yi_minus_zi]
~linear_coeffs:[one; Scalar.of_z @@ (bi_mod_m %! mj)]
()
in
Evaluations.copy ~res:tmps.(0) acc)
id_mj_without_sum
bs_mod_m
(List.combine xs ys)
in
let identity =
Evaluations.mul_c ~res:ids.(i) ~evaluations:[q_mod_add; id_mj] ()
in
(prefix @@ q_label ^ "." ^ string_of_int i, identity))
((Scalar.order, None) :: List.combine M.moduli_add t_infos)
|> SMap.of_list
let verifier_identities ~prefix_common ~prefix ~public:_ ~generator:_
~size_domain:_ : verifier_identities =
fun _ answers ->
let {q; wires; wires_g} =
get_answers ~gx:true ~q_label ~prefix ~prefix_common answers
in
List.mapi
(fun i id -> (prefix @@ q_label ^ "." ^ string_of_int i, id))
(equations ~q ~wires ~wires_g ())
|> SMap.of_list
let cs ~q:q_mod_add ~wires ~wires_g ?precomputed_advice:_ () =
let open L in
let xs, ys, zs, qm, t_infos = get_values wires wires_g in
let* zero = Num.zero in
map2M
(fun mj t_info ->
let tj, tj_shift =
match t_info with
| Some (tj, tj_shift) -> (tj, tj_shift)
| None -> (zero, Z.zero)
in
let* id_mj =
let sum_terms =
List.map2
(fun bi_mod_m ((xi, yi), zi) ->
let c = Scalar.of_z (bi_mod_m %! mj) in
[(c, xi); (c, yi); (Scalar.negate c, zi)])
bs_mod_m
(List.combine (List.combine xs ys) zs)
|> List.concat
in
let qc =
Scalar.of_z Z.(-(qm_shift * M.modulus %! mj) - (tj_shift * mj))
in
let coeffs, vars =
List.split
@@ [
Scalar.(negate @@ of_z (M.modulus %! mj), qm);
Scalar.(negate @@ of_z mj, tj);
]
@ sum_terms
in
Num.add_list ~qc ~coeffs (to_list vars)
in
Num.mul q_mod_add id_mj)
(Scalar.order :: M.moduli_add)
(None :: t_infos)
end
module Make_ModMul (MOD_ARITH : Plompiler__Gadget_mod_arith.MOD_ARITH) :
Base_sig = struct
module M = MOD_ARITH (L)
let q_label = "q_mod_mul_" ^ M.label
let ( %! ) = Z.rem
let nb_used_wires =
let used_fst_row = 2 * M.nb_limbs in
let used_snd_row = M.nb_limbs + 1 + List.length M.moduli_mul in
let nb_used_wires = Int.max used_fst_row used_snd_row in
assert (nb_used_wires <= Plompiler.Csir.nb_wires_arch) ;
nb_used_wires
let bs_mod_m =
List.init M.nb_limbs (fun i -> Z.pow M.base i %! M.modulus) |> List.rev
let bij_mod_m =
List.init M.nb_limbs (fun i ->
List.init M.nb_limbs (fun j -> Z.pow M.base (i + j) %! M.modulus))
|> List.concat |> List.rev
let (qm_shift, _), ts_bounds = M.bounds_mul
let identity = (q_label, 1 + List.length M.moduli_mul)
let index_com = None
let nb_advs = 0
let nb_buffers = 3
let gx_composition = true
let polynomials_degree =
(q_label, 3) :: List.init nb_used_wires (fun i -> (wire_name i, 3))
|> SMap.of_list
let get_values wires wires_g =
let xs = List.init M.nb_limbs (fun i -> wires.(i)) in
let ys = List.init M.nb_limbs (fun i -> wires.(M.nb_limbs + i)) in
let zs = List.init M.nb_limbs (fun i -> wires_g.(i)) in
let qm = wires_g.(M.nb_limbs) in
let ts = List.mapi (fun i _ -> wires_g.(M.nb_limbs + 1 + i)) M.moduli_mul in
let t_infos =
List.map2 (fun tj (t_shift, _) -> Some (tj, t_shift)) ts ts_bounds
in
(xs, ys, zs, qm, t_infos)
let equations ~q:q_mod_mul ~wires ~wires_g ?precomputed_advice:_ () =
let xs, ys, zs, qm, t_infos = get_values wires wires_g in
let sum = List.fold_left Scalar.add Scalar.zero in
let x_times_y =
List.concat_map (fun xi -> List.map (fun yj -> Scalar.(xi * yj)) ys) xs
in
List.map2
(fun mj t_info ->
let tj, tj_shift =
match t_info with
| Some (tj, tj_shift) -> (tj, tj_shift)
| None -> (Scalar.zero, Z.zero)
in
let id_mj =
let open Scalar in
let mod_mj v = v %! mj |> of_z in
let bs_mod_m_mod_mj = List.map mod_mj bs_mod_m in
let bij_mod_m_mod_mj = List.map mod_mj bij_mod_m in
let sum_xy = sum @@ List.map2 Scalar.mul bij_mod_m_mod_mj x_times_y in
let sum_z = sum @@ List.map2 Scalar.mul bs_mod_m_mod_mj zs in
sum_xy + negate sum_z
+ negate (qm * mod_mj M.modulus)
+ negate (mod_mj Z.(qm_shift * M.modulus))
+ negate ((tj + of_z tj_shift) * of_z mj)
in
Scalar.(q_mod_mul * id_mj))
(Scalar.order :: M.moduli_mul)
(None :: t_infos)
let prover_identities ~prefix_common ~prefix ~public:_ ~domain :
prover_identities =
fun evaluations ->
let domain_size = Domain.length domain in
let tmps, ids = get_buffers ~nb_buffers ~nb_ids:(snd identity) in
let ({q; wires} : witness) =
get_evaluations ~q_label ~prefix ~prefix_common evaluations
in
let q_mod_mul = q in
let xs, ys, _zs, qm, t_infos = get_values wires wires in
let zs = xs in
let xy_pairs =
List.concat_map (fun xi -> List.map (fun yj -> (xi, yj)) ys) xs
in
List.mapi
(fun i (mj, t_info) ->
let id_mj_without_sums =
let tj, tj_coeff, tj_shift, tj_comp =
match t_info with
| Some (tj, tj_shift) ->
([tj], Scalar.[negate (of_z mj)], tj_shift, [1])
| None -> ([], [], Z.zero, [])
in
Evaluations.linear_c
~res:tmps.(0)
~evaluations:(qm :: tj)
~composition_gx:(1 :: tj_comp, domain_size)
~linear_coeffs:(Scalar.(negate (of_z (M.modulus %! mj))) :: tj_coeff)
~add_constant:
Scalar.(
negate (of_z Z.((qm_shift * M.modulus %! mj) + (tj_shift * mj))))
()
in
let id_mj_without_sum_z =
List.fold_left2
(fun acc bij_mod_m (xi, yj) ->
let xiyj =
Evaluations.mul_c ~res:tmps.(2) ~evaluations:[xi; yj] ()
in
let acc =
Evaluations.linear_c
~res:tmps.(1)
~evaluations:[acc; xiyj]
~linear_coeffs:[one; Scalar.of_z (bij_mod_m %! mj)]
()
in
Evaluations.copy ~res:tmps.(0) acc)
id_mj_without_sums
bij_mod_m
xy_pairs
in
let id_mj =
List.fold_left2
(fun acc bi_mod_m zi ->
let acc =
Evaluations.linear_c
~res:tmps.(1)
~evaluations:[acc; zi]
~linear_coeffs:[one; Scalar.(negate @@ of_z (bi_mod_m %! mj))]
~composition_gx:([0; 1], domain_size)
()
in
Evaluations.copy ~res:tmps.(0) acc)
id_mj_without_sum_z
bs_mod_m
zs
in
let identity =
Evaluations.mul_c ~res:ids.(i) ~evaluations:[q_mod_mul; id_mj] ()
in
(prefix @@ q_label ^ "." ^ string_of_int i, identity))
((Scalar.order, None) :: List.combine M.moduli_mul t_infos)
|> SMap.of_list
let verifier_identities ~prefix_common ~prefix ~public:_ ~generator:_
~size_domain:_ : verifier_identities =
fun _ answers ->
let {q; wires; wires_g} =
get_answers ~gx:true ~q_label ~prefix ~prefix_common answers
in
List.mapi
(fun i id -> (prefix @@ q_label ^ "." ^ string_of_int i, id))
(equations ~q ~wires ~wires_g ())
|> SMap.of_list
let cs ~q:q_mod_mul ~wires ~wires_g ?precomputed_advice:_ () =
let open L in
let xs, ys, zs, qm, t_infos = get_values wires wires_g in
let xy_pairs =
List.concat_map (fun xi -> List.map (fun yj -> (xi, yj)) ys) xs
in
let* xys = mapM (fun (xi, yj) -> Num.mul xi yj) xy_pairs in
let* zero = Num.constant Scalar.zero in
map2M
(fun mj t_info ->
let tj, tj_shift =
match t_info with
| Some (tj, tj_shift) -> (tj, tj_shift)
| None -> (zero, Z.zero)
in
let* id_mj =
let xy_terms =
List.map2
(fun bij_mod_m xiyj -> (Scalar.of_z (bij_mod_m %! mj), xiyj))
bij_mod_m
xys
in
let z_terms =
List.map2
(fun bi_mod_m zi ->
(Scalar.(negate @@ of_z (bi_mod_m %! mj)), zi))
bs_mod_m
zs
in
let sum_terms = xy_terms @ z_terms in
let qc =
Scalar.of_z Z.(-(qm_shift * M.modulus %! mj) - (tj_shift * mj))
in
let coeffs, vars =
List.split
@@ [
Scalar.(negate @@ of_z (M.modulus %! mj), qm);
Scalar.(negate @@ of_z mj, tj);
]
@ sum_terms
in
Num.add_list ~qc ~coeffs (to_list vars)
in
Num.mul q_mod_mul id_mj)
(Scalar.order :: M.moduli_mul)
(None :: t_infos)
end
module AddMod25519 = Make_ModAdd (Plompiler.ArithMod25519)
module MulMod25519 = Make_ModMul (Plompiler.ArithMod25519)
module AddMod64 = Make_ModAdd (Plompiler.ArithMod64)
module MulMod64 = Make_ModMul (Plompiler.ArithMod64)