Source file polynomial.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
module Fr = Bls12_381.Fr
module Stubs = struct
type fr = Fr.t
type fr_array = Fr_carray.t
(** [of_sparse res p n] converts the sparse representation of a polynomial [p] to
the dense representation, from an OCaml array [p] of size [n] to a C array [res] of
size [degree p + 1]
requires:
- degree of each coeff [d_i >= 0] and [d_i] are unique
- the result must be initialized with zero (as done by {!Fr_carray.allocate})
- [size res = degree p + 1]
- [size p = n] *)
external of_sparse : fr_array -> (fr * int) array -> int -> unit
= "caml_bls12_381_polynomial_polynomial_of_sparse_stubs"
[@@noalloc]
(** [add res a b size_a size_b] writes the result of polynomial addition of [a] and [b]
in [res]
requires:
- [size a = size_a]
- [size b = size_b]
- [size res = max (size_a, size_b)]
- [res], [a] and [b] are either pairwise disjoint or equal *)
external add : fr_array -> fr_array -> fr_array -> int -> int -> unit
= "caml_bls12_381_polynomial_polynomial_add_stubs"
[@@noalloc]
(** [sub res a b size_a size_b] writes the result of polynomial subtraction of [b] from [a]
in [res]
requires:
- [size a = size_a]
- [size b = size_b]
- [size res = max (size_a, size_b)]
- [res], [a] and [b] are either pairwise disjoint or equal *)
external sub : fr_array -> fr_array -> fr_array -> int -> int -> unit
= "caml_bls12_381_polynomial_polynomial_sub_stubs"
[@@noalloc]
(** [mul res a b size_a size_b] writes the result of polynomial multiplication of [a] by [b]
in [res]
requires:
- the result must be initialized with zero (as done by {!Fr_carray.allocate})
- [size a = size_a]
- [size b = size_b]
- [size res = size_a + size_b - 1] *)
external mul : fr_array -> fr_array -> fr_array -> int -> int -> unit
= "caml_bls12_381_polynomial_polynomial_mul_stubs"
[@@noalloc]
(** [mul_by_scalar res b a size_a] writes the result of multiplying a polynomial [a]
by a blst_fr element [b] in [res]
requires:
- [size a = size_a]
- [size res = size_a]
- [res] and [a] either disjoint or equal *)
external mul_by_scalar : fr_array -> fr -> fr_array -> int -> unit
= "caml_bls12_381_polynomial_polynomial_mul_by_scalar_stubs"
[@@noalloc]
(** [linear res poly_polylen_coeff nb_polys] writes the result of
computing [λ₁·p₁(x) + λ₂·p₂(x) + … + λₖ·pₖ(x)] in [res], where
- [poly_polylen_coeff.[i] = (pᵢ, size_p_i, λᵢ)]
- [nb_polys] is a number of polynomials, i.e., [i = 1..nb_polys]
requires:
- the result must be initialized with zero (as done by {!Fr_carray.allocate})
- [size res = max (size_p_i)]
- [size poly_polylen_coeff = nb_polys]
- [size p_i = size_p_i] *)
external linear : fr_array -> (fr_array * int * fr) array -> int -> unit
= "caml_bls12_381_polynomial_polynomial_linear_stubs"
[@@noalloc]
(** [linear_with_powers res c poly_polylen nb_polys] writes the result of
computing [c⁰·p₀(x) + c¹·p₁(x) + … + cᵏ·pₖ(x)] in [res], where
- [poly_polylen.[i] = (pᵢ, size_p_i)]
- [nb_polys] is a number of polynomials
requires:
- the result must be initialized with zero (as done by {!Fr_carray.allocate})
- [size res = max (size_p_i)]
- [size poly_polylen = nb_polys]
- [size p_i = size_p_i] *)
external linear_with_powers :
fr_array -> fr -> (fr_array * int) array -> int -> unit
= "caml_bls12_381_polynomial_polynomial_linear_with_powers_stubs"
[@@noalloc]
(** [negate res p n] writes the result of negating a polynomial [p] in [res]
requires:
- [size p = n]
- [size res = n]
- [res] and [p] either disjoint or equal *)
external negate : fr_array -> fr_array -> int -> unit
= "caml_bls12_381_polynomial_polynomial_negate_stubs"
[@@noalloc]
(** [evaluate res p n x] writes the result of evaluating a polynomial [p] at [x]
in [res]
- requires: [size p = n] and [n > 0] *)
external evaluate : fr -> fr_array -> int -> fr -> unit
= "caml_bls12_381_polynomial_polynomial_evaluate_stubs"
[@@noalloc]
(** [division_xn res_q res_r p size_p (n, c)] writes the quotient and remainder of
the division of a polynomial [p] by [(X^n + c)] in [res]
requires:
- [size p = size_p] and [size_p > n]
- [size res_q = size_p - n]
- [size res_r = n] *)
external division_xn :
fr_array -> fr_array -> fr_array -> int -> int * fr -> unit
= "caml_bls12_381_polynomial_polynomial_division_xn_stubs"
[@@noalloc]
(** [mul_xn res p size_p n c] writes the result of multiplying a polynomial [p]
by [(X^n + c)] in [res]
requires:
- [res] is initialized with bls-fr zeros
- [size p = size_p]
- [size res = size_p + n] *)
external mul_xn : fr_array -> fr_array -> int -> int -> fr -> unit
= "caml_bls12_381_polynomial_polynomial_mul_xn_stubs"
[@@noalloc]
external derivative : fr_array -> fr_array -> int -> unit
= "caml_bls12_381_polynomial_polynomial_derivative_stubs"
[@@noalloc]
end
module Polynomial_impl = struct
type scalar = Fr.t
type t = Fr_carray.t [@@deriving repr]
let of_carray p = p
let to_carray p = p
let length = Fr_carray.length
let erase p = Fr_carray.erase p (length p)
let allocate = Fr_carray.allocate
let copy p = Fr_carray.copy ~offset:0 ~len:(length p) p
let copy_carray = Fr_carray.copy
let get = Fr_carray.get
let degree = Fr_carray.degree
let init = Fr_carray.init
let equal p1 p2 =
let n1 = length p1 in
let n2 = length p2 in
let short_n, long_p, long_n =
if n1 <= n2 then (n1, p2, n2) else (n2, p1, n1)
in
if Fr_carray.equal p1 ~offset1:0 p2 ~offset2:0 ~len:short_n then
let rec stop_at_first_non_zero i =
if i = long_n then true
else if Fr.eq (get long_p i) Fr.zero then stop_at_first_non_zero (i + 1)
else false
in
stop_at_first_non_zero short_n
else false
let to_string p =
String.concat
" ; "
(List.map Fr.to_string (Array.to_list @@ Fr_carray.to_array p))
let of_coefficients coefficients =
let coefficients = Array.of_list coefficients in
let degree =
Array.fold_left
(fun max_degree (_coeff, d) ->
assert (d >= 0) ;
max d max_degree)
0
coefficients
in
let polynomial = allocate (degree + 1) in
Stubs.of_sparse polynomial coefficients (Array.length coefficients) ;
polynomial
let of_dense = Fr_carray.of_array
let zero = of_coefficients []
let one = of_coefficients [(Fr.one, 0)]
let generate_biased_random_polynomial n =
assert (n >= 0) ;
if Random.int 10 = 0 || n = 0 then zero
else
let poly =
Array.init n (fun _ ->
if Random.bool () then Fr.random () else Fr.copy Fr.zero)
in
Array.set poly (n - 1) Fr.one ;
Fr_carray.of_array poly
let random n = List.init n (fun i -> (Fr.random (), i)) |> of_coefficients
let to_dense_coefficients p =
let len = 1 + max 0 (degree p) in
Fr_carray.to_array ~len p
let to_sparse_coefficients poly =
let poly = to_dense_coefficients poly in
let res = ref [] in
for deg = Array.length poly - 1 downto 0 do
let coef = poly.(deg) in
if not (Fr.is_zero coef) then res := (Fr.copy coef, deg) :: !res
done ;
!res
let add p1 p2 =
let n1 = length p1 in
let n2 = length p2 in
let res_size = max n1 n2 in
let res = allocate res_size in
Stubs.add res p1 p2 n1 n2 ;
res
let add_inplace res p1 p2 =
let n1 = length p1 in
let n2 = length p2 in
let n_res = length res in
assert (n_res = max n1 n2) ;
Stubs.add res p1 p2 n1 n2
let sub p1 p2 =
let n1 = length p1 in
let n2 = length p2 in
let max_size = max n1 n2 in
let res = allocate max_size in
Stubs.sub res p1 p2 n1 n2 ;
res
let sub_inplace res p1 p2 =
let n1 = length p1 in
let n2 = length p2 in
let n_res = length res in
assert (n_res >= max n1 n2) ;
Stubs.sub res p1 p2 n1 n2
let mul p1 p2 =
let n1 = length p1 in
let n2 = length p2 in
let res_size = n1 + n2 - 1 in
let res = allocate res_size in
Stubs.mul res p1 p2 n1 n2 ;
res
let mul_by_scalar scalar p =
let n = length p in
let res = allocate n in
Stubs.mul_by_scalar res scalar p n ;
res
let mul_by_scalar_inplace res scalar p =
let n = length p in
let n_res = length res in
assert (n_res >= n) ;
Stubs.mul_by_scalar res scalar p n
let linear polys coeffs =
let nb_polys = List.length polys in
assert (List.compare_length_with coeffs nb_polys = 0) ;
let res_size =
List.fold_left (fun res_size p -> max (length p) res_size) 0 polys
in
if res_size = 0 then zero
else
let res = allocate res_size in
let poly_polylen_coeff =
List.map2 (fun p coeff -> (p, length p, coeff)) polys coeffs
in
Stubs.linear res (Array.of_list poly_polylen_coeff) nb_polys ;
res
let linear_with_powers polys coeff =
let nb_polys = List.length polys in
let polys = List.map (fun p -> (p, length p)) polys in
let res_size =
List.fold_left (fun res_size (_p, size) -> max size res_size) 0 polys
in
let res = allocate res_size in
Stubs.linear_with_powers res coeff (Array.of_list polys) nb_polys ;
res
let opposite p =
let n = length p in
let res = allocate n in
Stubs.negate res p n ;
res
let opposite_inplace p =
let n = length p in
Stubs.negate p p n
let is_zero p = if degree p = -1 then true else false
let truncate ~len p =
if len < 0 then
raise (Invalid_argument "truncate: expected positive length.")
else
let min_len_capacity p =
if is_zero p then 1 else min len (degree p + 1)
in
Fr_carray.copy ~len:(min_len_capacity p) p
let evaluate p scalar =
let n = length p in
let res = Fr.copy scalar in
Stubs.evaluate res p n scalar ;
res
exception Rest_not_null of string
let division_xn p n c =
assert (n > 0) ;
let poly_degree = degree p in
let poly_size = poly_degree + 1 in
if poly_degree = -1 || poly_degree < n then (zero, p)
else
let res_q = allocate (poly_size - n) in
let res_r = allocate n in
Stubs.division_xn res_q res_r p poly_size (n, c) ;
let poly_q = res_q in
let poly_r = res_r in
(poly_q, poly_r)
let mul_xn p n c =
let l = length p in
let res = allocate (l + n) in
Stubs.mul_xn res p l n c ;
res
let derivative p =
let n = length p in
if is_zero p || n = 1 then zero
else
let res = allocate (n - 1) in
Stubs.derivative res p n ;
res
let split ~nb_chunks size_chunks p =
let poly_degree = degree p in
let nb_coeff_P = 1 + poly_degree in
if poly_degree = -1 then List.init nb_chunks (fun _ -> zero)
else
List.init nb_chunks (fun i ->
if (i + 1) * size_chunks <= nb_coeff_P then
if i = nb_chunks - 1 then Fr_carray.copy ~offset:(i * size_chunks) p
else Fr_carray.copy ~offset:(i * size_chunks) ~len:size_chunks p
else if i * size_chunks < nb_coeff_P then
Fr_carray.copy
~offset:(i * size_chunks)
~len:(nb_coeff_P - (i * size_chunks))
p
else zero)
let blind ~nb_blinds n p =
let blinding_factor = random nb_blinds in
(add p (mul_xn blinding_factor n Fr.(negate one)), blinding_factor)
let ( = ) = equal
let ( + ) = add
let ( - ) = sub
let ( * ) = mul
let constant c = of_coefficients [(c, 0)]
let fold_left_map = Fr_carray.fold_left_map
end
module type Polynomial_sig = sig
(**
This library implements polynomials of Bls12_381.Fr as arrays of contiguous
memory in C, allowing much better performances for algorithms that scan the
polynomials.
An array [a] of size [n] represents the polynomial $\sum_i^(n-1) a[i] X^i$
The length of [a] is always greater or equal than the degree+1 of its
corresponding polynomial, if greater it padded with zeros. As a consequence a
polynomial has many representations, namely all arrays with trailing zeros.
*)
type scalar
type t [@@deriving repr]
(** [init n f] returns a fresh polynomial of length [n], with element number [i]
initialized to the result of [f i]. *)
val init : int -> (int -> scalar) -> t
(** [allocate len] creates a zero polynomial of size [len] *)
val allocate : int -> t
(** [erase p] overwrites a polynomial [p] with a zero polynomial of
the same size as the polynomial [p] *)
val erase : t -> unit
(** [generate_biased_random_polynomial n] generates a random polynomial of
degree strictly lower than [n], the distribution is NOT uniform, it is
biased towards sparse polynomials and particularly towards the zero
polynomial *)
val generate_biased_random_polynomial : int -> t
(** [random n] generates a uniformly sampled polynomial among the set of all
polynomials of degree strictly lower than [n] *)
val random : int -> t
(** [degree p] returns the degree of a polynomial [p]. Returns [-1] for the
zero polynomial *)
val degree : t -> int
(** [get p i] returns the [i]-th element of a given array [p], a coefficient of [X^i]
in [p] *)
val get : t -> int -> scalar
(** [to_string p] returns the string representation of a polynomial [p] *)
val to_string : t -> string
(** [copy p] returns a copy of a polynomial [p] *)
val copy : t -> t
(** [truncate ~len p] returns a new polynomial made of the first [len]
coefficients of [p]. If [degree p + 1] is less than [len] then
[copy p] is returned.
@raise [Invalid_argument] if [len] is negative. *)
val truncate : len:int -> t -> t
(** [to_dense_coefficients p] returns the dense representation of
a polynomial [p], i.e., it converts a C array to an OCaml array *)
val to_dense_coefficients : t -> scalar array
(** [of_dense p] creates a value of type [t] from the dense representation of
a polynomial [p], i.e., it converts an OCaml array to a C array *)
val of_dense : scalar array -> t
(** [of_coefficients p] creates a value of type [t] from the sparse representation of
a polynomial [p], i.e., it converts an OCaml array to a C array *)
val of_coefficients : (scalar * int) list -> t
(** [equal a b] checks whether a polynomial [a] is equal to a polynomial [b] *)
val equal : t -> t -> bool
(** [is_zero p] checks whether a polynomial [p] is the zero polynomial *)
val is_zero : t -> bool
(** [zero] is the zero polynomial, the neutral element for polynomial addition *)
val zero : t
(** [one] is the constant polynomial one, the neutral element for polynomial
multiplication *)
val one : t
(** [add] computes polynomial addition *)
val add : t -> t -> t
(** [add_inplace res a b] computes polynomial addition of [a] and [b] and
writes the result in [res]
Note: [res] can be equal to either [a] or [b] *)
val add_inplace : t -> t -> t -> unit
(** [sub] computes polynomial subtraction *)
val sub : t -> t -> t
(** [sub_inplace res a b] computes polynomial subtraction of [a] and [b] and
writes the result in [res]
Note: [res] can be equal to either [a] or [b] *)
val sub_inplace : t -> t -> t -> unit
(** [mul] computes polynomial multiplication
Note: naive quadratic algorithm, result's size is the sum of arguments' size *)
val mul : t -> t -> t
(** [mul_by_scalar] computes multiplication of a polynomial by a blst_fr element *)
val mul_by_scalar : scalar -> t -> t
(** [mul_by_scalar_inplace res s p] computes multiplication of a polynomial [p]
by a blst_fr element [s] and stores it in [res] *)
val mul_by_scalar_inplace : t -> scalar -> t -> unit
(** [linear p s] computes [∑ᵢ s.(i)·p.(i)] *)
val linear : t list -> scalar list -> t
(** [linear_with_powers p s] computes [∑ᵢ sⁱ·p.(i)]. This function is more efficient
than [linear] + [powers] *)
val linear_with_powers : t list -> scalar -> t
(** [opposite] computes polynomial negation *)
val opposite : t -> t
(** [opposite_inplace p] computes polynomial negation
Note: The argument [p] is overwritten *)
val opposite_inplace : t -> unit
(** [evaluate p x] evaluates a polynomial [p] at [x] *)
val evaluate : t -> scalar -> scalar
exception Rest_not_null of string
(** [division_xn p n c] returns the quotient and remainder of the division of
[p] by [(X^n + c)] *)
val division_xn : t -> int -> scalar -> t * t
(** [mul_xn p n c] returns the product of [p] and [(X^n + c)] *)
val mul_xn : t -> int -> scalar -> t
(** [derivative p] returns the formal derivative of [p] *)
val derivative : t -> t
val split : nb_chunks:int -> int -> t -> t list
(** [blind ~nb_blinds n p] adds to polynomial [p] a random multiple of
polynomial [(X^n - 1)], chosen by uniformly sampling a polynomial [b]
of degree strictly lower than [nb_blinds] and multiplying it by
[(X^n - 1)], [b] is returned as the second argument *)
val blind : nb_blinds:int -> int -> t -> t * t
(** Infix operator for {!equal} *)
val ( = ) : t -> t -> bool
(** Infix operator for {!add} *)
val ( + ) : t -> t -> t
(** Infix operator for {!sub} *)
val ( - ) : t -> t -> t
(** Infix operator for {!mul} *)
val ( * ) : t -> t -> t
(** [constant s] creates a value of type [t] from a blst_fr element [s] *)
val constant : scalar -> t
(** [fold_left_map] is a combination of fold_left and map that threads an
accumulator through calls to [f]. *)
val fold_left_map : ('acc -> scalar -> 'acc * scalar) -> 'acc -> t -> 'acc * t
end
module type Polynomial_unsafe_sig = sig
include Polynomial_sig
(** [to_carray p] converts [p] from type {!t} to type {!Fr_carray.t}
Note: [to_carray p] doesn't create a copy of [p] *)
val to_carray : t -> Fr_carray.t
(** [of_carray p] converts [p] from type {!Fr_carray.t} to type {!t}
Note: [of_carray p] doesn't create a copy of [p] *)
val of_carray : Fr_carray.t -> t
(** [copy_carray ?offset ?len p] returns a polynomial made of [len] contiguous
coefficients starting from the coefficient of index [offset].
By default, [offset = 0] and [len = length p - offset].
@raise [Invalid_argument] if [offset] is not in the range 0 to [(length p - 1)],
or if [len] is not positive, or if [offset + length] is not in the range 0 to
[(length p - 1)]. *)
val copy_carray : ?offset:int -> ?len:int -> t -> t
(** [length p] returns the length of the underlying {!Fr_carray.t}. *)
val length : t -> int
end
module Polynomial_unsafe :
Polynomial_unsafe_sig with type scalar = Bls12_381.Fr.t =
Polynomial_impl
include (
Polynomial_unsafe :
Polynomial_sig
with type scalar = Polynomial_unsafe.scalar
and type t = Polynomial_unsafe.t)