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
|
(** Finite field. *)
(** Module signature for a finite field. *)
module type S = sig
type t
(** A type representing an element in the finite field. *)
val p : Z.t
(** The prime modulus of the field. *)
val zero : t
(** The zero value, i.e. the additive identity. *)
val one : t
(** The one value, i.e. the multiplicative identity. *)
val of_string : string -> t
(** [of_string s] returns the field element represented by [s]. *)
val to_string : t -> string
(** [to_string a] returns the string representation of [a]. *)
val equal : t -> t -> bool
(** [equal a b] returns [true] if [a] is equal to [b]. *)
val neg : t -> t
(** [neg a] is the additive inverse of [a]. [add a (neg a) = zero] for all
[a]. *)
val inv : t -> t
(** [inv a] is the multiplicative inverse of [a]. [mul a (inv a) = one] for
all non-zero [a]. Furthermore, we define [inv zero = zero]. *)
val add : t -> t -> t
(** [add a b] is the sum of [a] plus [b] *)
val sub : t -> t -> t
(** [sub a b] is the difference of [a] minus [b]. It is equivalent to
[add a (neg b)]. *)
val mul : t -> t -> t
(** [add a b] is the product of [a] times [b] *)
val div : t -> t -> t
(** [div a b] is the field division of [a] by [b]. It is equivalent to
[mul a (inv b)]. Furthermore, we define [div a zero = zero]. *)
val pow : t -> Z.t -> t
(** [pow a exp] is [a] to the power of [exp]. [pow zero zero] is defined to be
[zero]. *)
val ( ~- ) : t -> t
(** [neg]. *)
val ( ~/ ) : t -> t
(** [inv]. *)
val ( + ) : t -> t -> t
(** [add]. *)
val ( - ) : t -> t -> t
(** [sub]. *)
val ( * ) : t -> t -> t
(** [mul]. *)
val ( / ) : t -> t -> t
(** [div]. *)
end
(** Module signature for a polynomial extension field. *)
module type SX = sig
include S
type f
(** The field over which the polynomial is defined. *)
val of_f : f -> t
(** [of_f a] maps an underlying field element to the extension field. *)
val of_coeffs : f array -> t
(** [of_coeffs c] is the field element with polynomial coefficients [c]. *)
val to_coeffs : t -> f array
(** [to_coeffs a] are the polynomial coefficients of [a]. *)
end
module Make (PRIME : sig
val p : Z.t
end) : S = struct
type t = Z.t
let p = PRIME.p
let mod_p x = Z.erem x p
let zero = Z.zero
let one = Z.one
let of_string s = Z.of_string s |> mod_p
let to_string = Z.to_string
let equal = Z.equal
let neg a = Z.neg a |> mod_p
let add a b = Z.add a b |> mod_p
let mul a b = Z.mul a b |> mod_p
let pow a exp = if equal a zero then zero else Z.powm a exp p
let inv a = pow a Z.minus_one
let sub a b = add a (neg b)
let div a b = mul a (inv b)
let ( ~- ) = neg
let ( ~/ ) = inv
let ( + ) = add
let ( - ) = sub
let ( * ) = mul
let ( / ) = div
end
module MakeExtension (POLY : sig
module F : S
val q : F.t array
end) : SX with type f = POLY.F.t = struct
module F = POLY.F
type f = F.t
let q = POLY.q
let degree = Array.length q
let () = assert (degree >= 2)
let of_f a = Array.init degree (function 0 -> a | _ -> F.zero)
let of_coeffs a =
if Array.length a == degree then Array.copy a
else invalid_arg "wrong number of elements"
let to_coeffs = Array.copy
type t = f array
let p = F.p
let zero = Array.make degree F.zero
let one = Array.init degree (function 0 -> F.one | _ -> F.zero)
let of_string s =
let strip_parens s =
if String.starts_with ~prefix:"(" s && String.ends_with ~suffix:")" s then
String.sub s 1 (String.length s - 2)
else invalid_arg "field element missing parentheses"
in
let a =
strip_parens s |> String.split_on_char ',' |> List.map F.of_string
|> Array.of_list
in
if Array.length a = degree then a
else invalid_arg "wrong number of elements"
let to_string a =
Array.map F.to_string a |> Array.to_list |> String.concat ","
|> Printf.sprintf "(%s)"
let equal = Array.for_all2 F.equal
let neg a = Array.map F.neg a
let add a b = Array.map2 F.add a b
let mul2 a b =
let a0, a1 = (a.(0), a.(1)) in
let b0, b1 = (b.(0), b.(1)) in
let t1 = F.mul a0 b0 in
let t2 = F.mul a1 b1 in
let r0 = F.sub t1 t2 in
let r1 = F.(sub (mul (add a0 a1) (add b0 b1)) (add t1 t2)) in
[| r0; r1 |]
let muln a b =
let r = Array.make ((degree * 2) - 1) F.zero in
for i = 0 to degree - 1 do
for j = 0 to degree - 1 do
r.(i + j) <- F.add r.(i + j) (F.mul a.(i) b.(j))
done
done;
for i = Array.length r - 1 downto degree do
let qi = r.(i) in
if not (F.equal qi F.zero) then
for j = 0 to degree - 1 do
let j' = i - degree + j in
r.(j') <- F.sub r.(j') (F.mul qi q.(j))
done
else ()
done;
Array.sub r 0 degree
let inv2 a =
let a0, a1 = (a.(0), a.(1)) in
let factor = F.(inv (add (mul a0 a0) (mul a1 a1))) in
F.[| mul factor a0; mul factor (neg a1) |]
let invn a =
let deg a =
let rec deg' = function
| 0 -> 0
| i -> if F.equal a.(i) F.zero then deg' (i - 1) else i
in
deg' (Array.length a - 1)
in
let ediv a b =
let da = deg a in
let db = deg b in
let q = Array.make (da + 1) F.zero in
let r = Array.sub a 0 (da + 1) in
let c = b.(db) in
for i = da - db downto 0 do
let qd = F.div r.(db + i) c in
q.(i) <- qd;
for j = 0 to db do
r.(i + j) <- F.sub r.(i + j) (F.mul qd b.(j))
done
done;
q
in
let submulq a b q =
let r = Array.copy a in
for i = 0 to deg b do
for j = 0 to deg q do
r.(i + j) <- F.sub r.(i + j) (F.mul b.(i) q.(j))
done
done;
r
in
let t = ref (Array.append zero [| F.zero |]) in
let newt = ref (Array.append one [| F.zero |]) in
let r = ref (Array.append q [| F.one |]) in
let newr = ref (Array.append a [| F.zero |]) in
while Array.for_all F.(equal zero) !newr = false do
let q = ediv !r !newr in
let newr' = submulq !r !newr q in
let newt' = submulq !t !newt q in
r := !newr;
newr := newr';
t := !newt;
newt := newt'
done;
let a0 = F.inv !r.(0) in
Array.map (F.mul a0) (Array.sub !t 0 degree)
let mul, inv = if degree = 2 then (mul2, inv2) else (muln, invn)
let pow a exp =
if Array.to_seq a |> Seq.drop 1 |> Seq.for_all F.(equal zero) then
let f = a.(0) in
if F.(equal zero f) then zero
else if F.(equal one f) then one
else if Z.(equal exp zero) then one
else of_f @@ F.pow a.(0) exp
else if Z.(equal exp zero) then one
else if Z.(equal exp one) then a
else
let abs = Z.abs exp in
let nbits = Z.numbits abs in
let rec loop pow bit res =
let res' = if Z.testbit abs bit then mul res pow else res in
let bit' = bit + 1 in
if bit' = nbits then res'
else
let pow' = mul pow pow in
loop pow' bit' res'
in
let res = loop a 0 one in
if Z.sign exp >= 0 then res else inv res
let sub a b = add a (neg b)
let div a b = mul a (inv b)
let ( ~- ) = neg
let ( ~/ ) = inv
let ( + ) = add
let ( - ) = sub
let ( * ) = mul
let ( / ) = div
end
|