Source file gadget_poseidon.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
open Lang_core
module type P_HASH = sig
type scalar = S.t
type ctxt
val init : ?input_length:int -> unit -> ctxt
val digest : ctxt -> scalar array -> ctxt
val get : ctxt -> scalar
val direct : ?input_length:int -> scalar array -> scalar
end
open Lang_stdlib
module type PARAMETERS = sig
val variant : Variants.t
val width : int
val nb_full_rounds : int
val nb_partial_rounds : int
val nb_skips : int
val mds_matrix : string array array
val round_constants : string array
val partial_round_idx_to_permute : int
end
module Make (PP : PARAMETERS) (L : LIB) = struct
open PP
open L
module Poly = Polynomial.Univariate.Make (S)
type poly = Poly.polynomial
module Map : sig
val init : unit -> unit
val degree_of_wire : scalar repr -> int
val wire_of_degree : int -> scalar repr
end = struct
let assoc : ((scalar repr * int) list * int) ref = ref ([], 1)
let init () = assoc := ([], 1)
let degree_of_wire w =
List.find_opt (fun (w', _d) -> eq w w') (fst !assoc) |> function
| Some (_, d) -> d
| None ->
let l, d = !assoc in
let l = (w, d) :: l in
assoc := (l, d + 1);
d
let wire_of_degree d =
List.find_opt (fun (_w, d') -> d = d') (fst !assoc) |> function
| Some (w, _) -> w
| None -> assert false
end
type polys = poly array
let initial_state = Array.init width (fun _ -> Poly.zero)
let print_state state =
Array.iter (fun p -> Format.printf "%s\n" (Poly.to_string p)) state;
Format.printf "\n"
let mds_matrix = mds_matrix |> Array.map (Array.map S.of_string)
let round_constants = round_constants |> Array.map S.of_string
let poly_of_wire w = Poly.of_coefficients [ (S.one, Map.degree_of_wire w) ]
let wire_of_poly p =
with_label ~label:"Poseidon.wire_of_poly"
@@
let coeffs = Poly.get_list_coefficients p |> List.rev in
let qc, ws =
match coeffs with (qc, 0) :: l -> (qc, l) | l -> (S.zero, l)
in
Num.add_n ~qc @@ List.map (fun (q, d) -> (q, Map.wire_of_degree d)) ws
let s_box p : poly t =
with_label ~label:"Poseidon.s_box"
@@
if Poly.is_constant p then ret Poly.(p * p * p * p * p)
else
let open Num in
let* x = wire_of_poly p in
let* x5 = pow5 x in
ret @@ poly_of_wire x5
let rec repeat : n:int -> ('a -> 'a t) -> 'a -> 'a t =
fun ~n f e ->
if n <= 0 then ret e
else
let* x = f e in
repeat ~n:(n - 1) f x
let state_map : polys -> (poly -> poly t) -> polys t =
fun state f ->
with_label ~label:"Poseidon.state_map"
@@
let rec aux : polys -> int -> polys t =
fun state j ->
if j = width then ret state
else
let* p = f state.(j) in
state.(j) <- p;
aux state (j + 1)
in
aux state 0
let checkpoint : polys -> polys t =
fun state ->
with_label ~label:"Poseidon.checkpoint"
@@
let f p =
if Poly.is_constant p then ret p
else
let* w = wire_of_poly p in
ret @@ poly_of_wire w
in
state_map state f
let apply_matrix state =
let x = Array.copy state in
for j = 0 to width - 1 do
state.(j) <- Poly.zero;
for i = 0 to width - 1 do
state.(j) <- Poly.(state.(j) + mult_by_scalar mds_matrix.(j).(i) x.(i))
done
done;
state
let apply_round_key (state, i_round_key) =
for i = 0 to width - 1 do
state.(i) <-
Poly.add state.(i) (Poly.constants @@ round_constants.(i_round_key + i))
done;
(state, i_round_key + width)
let full_round : ?skip_ark:bool -> polys * int -> (polys * int) t =
fun ?(skip_ark = false) (state, i_round_key) ->
with_label ~label:"Poseidon.full_round"
@@ let* state = state_map state s_box in
let state = apply_matrix state in
if skip_ark then ret (state, i_round_key)
else ret @@ apply_round_key (state, i_round_key)
let full_round128 : ?skip_ark:bool -> polys * int -> (polys * int) t =
fun ?(skip_ark = false) (state, i_round_key) ->
assert (width = 3);
with_label ~label:"Poseidon.full_round128"
@@ let* state = checkpoint state in
let* x0 = wire_of_poly state.(0) in
let* x1 = wire_of_poly state.(1) in
let* x2 = wire_of_poly state.(2) in
let k = [| S.zero; S.zero; S.zero |] in
if not skip_ark then
for i = 0 to width - 1 do
k.(i) <- round_constants.(i_round_key + i)
done;
let* output =
Poseidon.poseidon128_full_round ~matrix:mds_matrix ~k ~variant
(x0, x1, x2)
in
(match of_list output with
| [ y0; y1; y2 ] ->
state.(0) <- poly_of_wire y0;
state.(1) <- poly_of_wire y1;
state.(2) <- poly_of_wire y2
| _ -> assert false);
ret @@ (state, i_round_key + width)
let partial_round : batch:int -> polys * int -> (polys * int) t =
fun ~batch (state, i_round_key) ->
with_label ~label:"Poseidon.partial_round"
@@
let f (state, i_round_key) =
let* p = s_box state.(partial_round_idx_to_permute) in
state.(partial_round_idx_to_permute) <- p;
let state = apply_matrix state in
ret @@ apply_round_key (state, i_round_key)
in
let* state, i_round_key = repeat ~n:batch f (state, i_round_key) in
let* state = checkpoint state in
ret (state, i_round_key)
let partial_round128 : batch:int -> polys * int -> (polys * int) t =
fun ~batch (state, i_round_key) ->
assert (width = 3);
assert (batch = 4);
with_label ~label:"Poseidon.partial_round128"
@@ let* state = checkpoint state in
let* x0 = wire_of_poly state.(0) in
let* x1 = wire_of_poly state.(1) in
let* x2 = wire_of_poly state.(2) in
let ks = Array.make_matrix width batch S.zero in
for j = 0 to batch - 1 do
for i = 0 to width - 1 do
ks.(i).(j) <- round_constants.(i_round_key + (width * j) + i)
done
done;
let* output =
Poseidon.poseidon128_four_partial_rounds ~matrix:mds_matrix ~ks
~variant (x0, x1, x2)
in
(match of_list output with
| [ y0; y1; y2 ] ->
state.(0) <- poly_of_wire y0;
state.(1) <- poly_of_wire y1;
state.(2) <- poly_of_wire y2
| _ -> assert false);
ret @@ (state, i_round_key + (batch * width))
let apply_permutation (state, _) =
with_label ~label:"Poseidon.apply_permutation"
@@
let batch = nb_skips + 1 in
let full = if width = 3 then full_round128 else full_round in
let partial =
if width = 3 && batch = 4 then partial_round128 else partial_round
in
let state, i_round_key = apply_round_key (state, 0) in
let* state = repeat ~n:(nb_full_rounds / 2) full (state, i_round_key) in
let* state = repeat ~n:(nb_partial_rounds / batch) (partial ~batch) state in
let* state = partial_round ~batch:(nb_partial_rounds mod batch) state in
let* state = repeat ~n:((nb_full_rounds / 2) - 1) full state in
full ~skip_ark:true state
let prepare_block with_padding (state, blocks_read) r nb_chunks inputs =
let block_size =
if blocks_read < nb_chunks - 1 then width - 1
else if with_padding then r
else Array.length inputs - (blocks_read * (width - 1))
in
let offset = blocks_read * (width - 1) in
for j = 0 to block_size - 1 do
let p = Poly.add state.(j + 1) @@ poly_of_wire inputs.(offset + j) in
state.(j + 1) <- p
done;
if blocks_read = nb_chunks - 1 && with_padding then
state.(r + 1) <- Poly.add state.(r + 1) Poly.one;
(state, blocks_read + 1)
let digest : ?input_length:int -> scalar list repr -> scalar repr t =
fun ?input_length inputs ->
Map.init ();
let inputs = Array.of_list @@ of_list inputs in
let l = Array.length inputs in
let assert_length expected =
let error_msg =
Format.sprintf "digest expects data of length %d, %d given" expected l
in
if l <> expected then raise @@ Invalid_argument error_msg
in
Option.iter assert_length input_length;
let with_padding = Option.is_none input_length in
let polys = Array.init width (fun _ -> Poly.zero) in
let nb_blocks = ((l - if with_padding then 0 else 1) / (width - 1)) + 1 in
let r = l mod (width - 1) in
with_label ~label:"Poseidon.digest"
@@
let block_iteration (state, blocks_read, i_round_key) =
let state, blocks_read =
prepare_block with_padding (state, blocks_read) r nb_blocks inputs
in
let* state, i_round_key = apply_permutation (state, i_round_key) in
ret (state, blocks_read, i_round_key)
in
let* state, _, _ = repeat ~n:nb_blocks block_iteration (polys, 0, 0) in
wire_of_poly state.(1)
end
module type HASH = functor (L : LIB) -> sig
open L
val digest : ?input_length:int -> scalar list repr -> scalar repr t
end
module Poseidon128 = struct
module P : P_HASH = struct
module H = Mec.Hash.Poseidon128.Make (S)
include H.Hash
let direct ?input_length inputs =
let ctx = init ?input_length () in
let ctx = digest ctx inputs in
get ctx
end
module V : HASH = Make (struct
let variant = Variants.P128
let width = 3
let nb_full_rounds = 8
let nb_partial_rounds = 56
let nb_skips = 3
let mds_matrix = Mds_128.v
let round_constants = Ark_128.v
let partial_round_idx_to_permute = 2
end)
end
module Poseidon252 = struct
module P : P_HASH = struct
module H = Mec.Hash.Poseidon252.Make (S)
include H.Hash
let direct ?input_length inputs =
let ctx = init ?input_length () in
let ctx = digest ctx inputs in
get ctx
end
module V : HASH = Make (struct
let variant = Variants.P252
let width = 5
let nb_full_rounds = 8
let nb_partial_rounds = 59
let nb_skips = 4
let mds_matrix = Mds_252.v
let round_constants = Ark_252.v
let partial_round_idx_to_permute = 4
end)
end
module PoseidonFull = struct
module P : P_HASH = struct
module H = Mec.Hash.Neptunus.Make (S)
include H.Hash
let direct ?input_length inputs =
let ctx = init ?input_length () in
let ctx = digest ctx inputs in
get ctx
end
module V : HASH = Make (struct
let variant = Variants.PFull128
let width = 3
let nb_full_rounds = 60
let nb_partial_rounds = 0
let nb_skips = 0
let mds_matrix = Mds_full.v
let round_constants = Ark_full.v
let partial_round_idx_to_permute = 0
end)
end