Source file permutation_gate.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
module Permutation_gate_impl (PP : Polynomial_protocol.Polynomial_protocol_sig) =
struct
module PP = PP
module MP = PP.MP
module Domain = PP.PC.Polynomial.Domain
module Poly = PP.PC.Polynomial.Polynomial
module Scalar = PP.PC.Scalar
module Commitment = PP.PC.Commitment
module Fr_generation = PP.PC.Fr_generation
module Evaluations = PP.Evaluations
let monomial_of_list = SMap.monomial_of_list
let z = "Z"
type public_parameters = {
g_map_perm_PP : Poly.t SMap.t;
cm_g_map_perm_PP : Commitment.t SMap.t;
s_poly_map : Poly.t SMap.t;
cm_s_poly_map : Commitment.t SMap.t;
permutation : int array;
}
let srs_size ~zero_knowledge ~n = if zero_knowledge then n + 9 else n
let one = Scalar.one
let zero = Scalar.zero
let mone = Scalar.negate one
let quadratic_non_residues = Fr_generation.build_quadratic_non_residues 8
let get_k k =
if k < 8 then quadratic_non_residues.(k)
else raise (Invalid_argument "Permutation.get_k : k must be lower than 8.")
let gate_identity ~prefix wires_name beta gamma =
let wires_name = Array.map (fun x -> prefix ^ x) wires_name in
let choose_list wires_name nb n =
let index = string_of_int (n + 1) in
match nb with
| 0 ->
let f = wires_name.(n) in
(true, f, f, 0, 0)
| 1 -> (true, "Si" ^ index, prefix ^ "Ss" ^ index, 1, 0)
| 2 -> (false, "", "", 0, 1)
| _ -> raise (Failure "choose_list: Error in ternary decomposition")
in
let ternary nb length_array =
let rec cpt_three acc length n =
match n with
| 0 -> (acc, length)
| _ -> cpt_three ((n mod 3) :: acc) (length + 1) (n / 3)
in
let suffix, suffix_length = cpt_three [] 0 nb in
List.rev_append
(List.init (length_array - suffix_length) (fun _ -> 0))
suffix
in
let nb_wires = Array.length wires_name in
let array_betas = Fr_generation.powers (nb_wires + 1) beta in
let array_gammas = Fr_generation.powers (nb_wires + 1) gamma in
let size_identity = Z.(to_int (pow (of_int 3) nb_wires)) in
let rec compute_identity_zfg acc size =
let rec cpt_mnmls (acc_z, acc_zg, acc_b, acc_g) base3 ctr =
match base3 with
| t :: sub3 ->
let add, item_z, item_zg, b, g = choose_list wires_name t ctr in
let acc_b = acc_b + b in
let acc_g = acc_g + g in
let acc_z = if add then item_z :: acc_z else acc_z in
let acc_zg = if add then item_zg :: acc_zg else acc_zg in
cpt_mnmls (acc_z, acc_zg, acc_b, acc_g) sub3 (ctr + 1)
| [] ->
let coeff = Scalar.mul array_betas.(acc_b) array_gammas.(acc_g) in
( (SMap.monomial_of_list acc_z, coeff),
(SMap.monomial_of_list acc_zg, Scalar.negate coeff) )
in
if size = size_identity then acc
else
let i_base3 = ternary size nb_wires in
let mnml_z, mnml_zg =
cpt_mnmls ([ prefix ^ "Z" ], [ prefix ^ "Zg" ], 0, 0) i_base3 0
in
compute_identity_zfg (mnml_z :: mnml_zg :: acc) (size + 1)
in
let identity_zfg = MP.Polynomial.of_list (compute_identity_zfg [] 0) in
let identity_l1_z =
MP.Polynomial.of_list
[
(SMap.monomial_of_list [ "L1"; prefix ^ "Z" ], one);
(SMap.monomial_of_list [ "L1" ], mone);
]
in
SMap.of_list
[ (prefix ^ "Perm.a", identity_l1_z); (prefix ^ "Perm.b", identity_zfg) ]
let v_map ~prefix generator =
let g_poly = Poly.of_coefficients [ (generator, 1) ] in
SMap.singleton (prefix ^ "Zg") (prefix ^ "Z", g_poly)
module Preprocessing = struct
let l1 domain =
let size_domain = Domain.length domain in
let scalar_list =
Array.append
Fr_generation.[| fr_of_int_safe 0; fr_of_int_safe 1 |]
Array.(init (size_domain - 2) (fun _ -> zero))
in
Evaluations.interpolation_fft2 domain scalar_list
let sid_list_non_quadratic_residues size =
if size > 8 then
raise (Failure "sid_list_non_quadratic_residues: sid list too long")
else List.init size (fun i -> Poly.of_coefficients [ (get_k i, 1) ])
let sid_map_non_quadratic_residues_prover size =
if size > 8 then
raise (Failure "sid_map_non_quadratic_residues: sid map too long")
else
SMap.of_list
(List.init size (fun i ->
let k = get_k i in
("Si" ^ string_of_int (i + 1), Poly.of_coefficients [ (k, 1) ])))
type PP.not_committed += MulK of Evaluations.scalar
let () =
let open Encodings in
PP.register_nc_eval_and_encoding
(function
| MulK k -> Some (fun x_array -> Scalar.mul k x_array.(0)) | _ -> None)
~title:"permutation" ~tag:1
Data_encoding.(obj1 (req "permutation" fr_encoding))
(function MulK k -> Some k | _ -> None)
(fun k -> MulK k)
let sid_map_non_quadratic_residues_verifier size =
if size > 8 then
raise (Failure "sid_map_non_quadratic_residues: sid map too long")
else
SMap.of_list
(List.init size (fun i ->
let k = get_k i in
("Si" ^ string_of_int (i + 1), MulK k)))
let evaluations_sid size domain_evals =
SMap.of_list
(List.init size (fun i ->
let k = get_k i in
let evals =
Evaluations.mul_by_scalar k (Evaluations.of_domain domain_evals)
in
("Si" ^ string_of_int (i + 1), evals)))
let ssigma_map_non_quadratic_residues ~prefix permutation domain size =
let n = Domain.length domain in
let ssigma_map =
SMap.of_list
(List.init size (fun i ->
let offset = i * n in
let coeff_list =
Array.init n (fun j ->
let s_ij = permutation.(offset + j) in
let coeff = get_k (s_ij / n) in
let index = s_ij mod n in
Scalar.mul coeff (Domain.get domain index))
in
( prefix ^ "Ss" ^ string_of_int (i + 1),
Evaluations.interpolation_fft2 domain coeff_list )))
in
ssigma_map
end
module Permutation_poly = struct
let compute_prime res_evaluation tmp_evaluation beta gamma evaluations
wires_names s_names z_name n =
let wires_names = Array.to_list wires_names in
let z_evaluation = Evaluations.find_evaluation evaluations "Z" in
let _i, res_evaluation =
let f_fold (i, acc_evaluation) wire_name s_name =
let comp = if i = 0 && z_name = "Zg" then 1 else 0 in
let res_evaluation =
let evaluation_linear_i =
Evaluations.linear ~res:tmp_evaluation ~evaluations
~poly_names:[ wire_name; s_name ] ~linear_coeffs:[ one; beta ]
~add_constant:gamma ()
in
Evaluations.mul_c ~res:res_evaluation
~evaluations:[ evaluation_linear_i; acc_evaluation ]
~composition_gx:([ 0; comp ], n)
()
in
(i + 1, res_evaluation)
in
List.fold_left2 f_fold (0, z_evaluation) wires_names s_names
in
res_evaluation
let precompute_perm_identity_poly wires_name beta gamma evaluations n =
let z_evaluation = Evaluations.find_evaluation evaluations "Z" in
let z_evaluation_len = Evaluations.length z_evaluation in
let tmp_evaluation = Evaluations.find_evaluation evaluations "tmp_eval" in
let id1_evaluation = Evaluations.create z_evaluation_len in
let id2_evaluation = Evaluations.create z_evaluation_len in
let identity_zfg =
let nb_wires = Array.length wires_name in
let f_evaluation =
let sid_names =
List.init nb_wires (fun i -> "Si" ^ string_of_int (i + 1))
in
compute_prime tmp_evaluation id2_evaluation beta gamma evaluations
wires_name sid_names "Z" n
in
let g_evaluation =
let ss_names =
List.init nb_wires (fun i -> "Ss" ^ string_of_int (i + 1))
in
compute_prime id2_evaluation id1_evaluation beta gamma evaluations
wires_name ss_names "Zg" n
in
Evaluations.linear_c ~res:id1_evaluation
~evaluations:[ f_evaluation; g_evaluation ]
~linear_coeffs:[ one; mone ] ()
in
let identity_l1_z =
let l1_evaluation = Evaluations.find_evaluation evaluations "L1" in
let z_mone_evaluation =
Evaluations.linear_c ~res:tmp_evaluation ~evaluations:[ z_evaluation ]
~add_constant:mone ()
in
Evaluations.mul_c ~res:id2_evaluation
~evaluations:[ l1_evaluation; z_mone_evaluation ]
()
in
SMap.of_list [ ("Perm.a", identity_l1_z); ("Perm.b", identity_zfg) ]
let compute_Z s domain beta gamma values indices blinds =
let size_domain = Domain.length domain in
let scalar_array_Z =
let indices = Array.of_list (List.map snd (SMap.bindings indices)) in
let size_res = Array.length indices.(0) in
assert (size_res = size_domain);
let g_res = Array.init size_res (fun _ -> Scalar.zero) in
let f_prev = ref Scalar.one in
let f_res = ref Scalar.one in
let tmp = Bls12_381.Fr.(copy one) in
for i = 1 to size_res - 1 do
for j = 0 to Array.length indices - 1 do
let indices_list_j_i = indices.(j).(i) in
let v_gamma = Scalar.(values.(indices_list_j_i) + gamma) in
let f_coeff =
let gi = Domain.get domain i in
Bls12_381.Fr.(
mul_inplace tmp gi (get_k j);
mul_inplace gi tmp beta;
add_inplace gi gi v_gamma;
gi)
in
let g_coeff =
let sj = s.((j * size_domain) + i) in
let gj = Domain.get domain (sj mod size_domain) in
Bls12_381.Fr.(
mul_inplace tmp gj (get_k (Int.div sj size_domain));
mul_inplace gj tmp beta;
add_inplace gj gj v_gamma;
gj)
in
if j = 0 then (
f_res := f_coeff;
g_res.(i) <- g_coeff)
else
Bls12_381.Fr.(
mul_inplace !f_res !f_res f_coeff;
mul_inplace g_res.(i) g_res.(i) g_coeff)
done;
let f_over_g = Scalar.div_exn !f_res g_res.(i) in
Bls12_381.Fr.(
mul_inplace f_over_g f_over_g !f_prev;
g_res.(i) <- !f_prev;
f_prev := f_over_g)
done;
g_res.(0) <- !f_prev;
g_res
in
let z_poly = Evaluations.interpolation_fft2 domain scalar_array_Z in
match blinds with
| None -> z_poly
| Some b ->
let p_poly =
Poly.of_coefficients [ (b.(0), 2); (b.(1), 1); (b.(2), 0) ]
in
let zs_poly =
Poly.of_coefficients [ (one, size_domain); (mone, 0) ]
in
Poly.(add (mul zs_poly p_poly) z_poly)
end
let polynomials_degree ~nb_wires = nb_wires + 1
let preprocessing ?(prefix = "") ~domain ~permutation ~nb_wires () =
Preprocessing.ssigma_map_non_quadratic_residues ~prefix permutation domain
nb_wires
let sid_not_committed = Preprocessing.sid_map_non_quadratic_residues_verifier
let common_preprocessing ~compute_l1 ~domain ~nb_wires ~domain_evals =
let sid_evals = Preprocessing.evaluations_sid nb_wires domain_evals in
let sid_query =
let sid_func =
Preprocessing.sid_map_non_quadratic_residues_verifier nb_wires
in
PP.{ empty_verifier_query with not_committed = sid_func }
in
let g_map_perm_PP =
if not compute_l1 then SMap.empty
else SMap.singleton "L1" (Preprocessing.l1 domain)
in
(g_map_perm_PP, sid_evals, sid_query)
let prover_query ?(prefix = "") ~wires_name ~generator ~beta ~gamma
~evaluations ~n () =
PP.
{
v_map = v_map ~prefix generator;
precomputed_polys =
Permutation_poly.precompute_perm_identity_poly wires_name beta gamma
evaluations n;
}
let verifier_query ?(compute_sid = true) ?(prefix = "") ~wires_name ~generator
~beta ~gamma ~nb_wires () =
PP.
{
v_map = v_map ~prefix generator;
identities = gate_identity ~prefix wires_name beta gamma;
not_committed =
(if compute_sid then
Preprocessing.sid_map_non_quadratic_residues_verifier nb_wires
else SMap.empty);
}
let f_map_contribution ~permutation ~values ~indices ~blinds ~beta ~gamma
~domain =
let z_poly =
Permutation_poly.compute_Z permutation domain beta gamma values indices
blinds
in
SMap.of_list [ (z, z_poly) ]
end
module type Permutation_gate_sig = sig
module PP : Polynomial_protocol.Polynomial_protocol_sig
val srs_size : zero_knowledge:bool -> n:int -> int
val polynomials_degree : nb_wires:int -> int
val common_preprocessing :
compute_l1:bool ->
domain:PP.PC.Polynomial.Domain.t ->
nb_wires:int ->
domain_evals:PP.Evaluations.domain ->
PP.PC.Polynomial.Polynomial.t SMap.t
* PP.Evaluations.t SMap.t
* PP.verifier_query
val preprocessing :
?prefix:string ->
domain:PP.PC.Polynomial.Domain.t ->
permutation:int array ->
nb_wires:int ->
unit ->
PP.PC.Polynomial.Polynomial.t SMap.t
val prover_query :
?prefix:string ->
wires_name:string array ->
generator:PP.PC.Scalar.t ->
beta:PP.PC.Scalar.t ->
gamma:PP.PC.Scalar.t ->
evaluations:PP.Evaluations.t SMap.t ->
n:int ->
unit ->
PP.prover_query
val verifier_query :
?compute_sid:bool ->
?prefix:string ->
wires_name:string array ->
generator:PP.PC.Scalar.t ->
beta:PP.PC.Scalar.t ->
gamma:PP.PC.Scalar.t ->
nb_wires:int ->
unit ->
PP.verifier_query
val f_map_contribution :
permutation:int array ->
values:PP.PC.Scalar.t array ->
indices:int array SMap.t ->
blinds:PP.PC.Scalar.t array option ->
beta:PP.PC.Scalar.t ->
gamma:PP.PC.Scalar.t ->
domain:PP.PC.Polynomial.Domain.t ->
PP.PC.Polynomial.Polynomial.t SMap.t
end
module Permutation_gate (PP : Polynomial_protocol.Polynomial_protocol_sig) :
Permutation_gate_sig with module PP = PP =
Permutation_gate_impl (PP)