Source file lang_core.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
(*****************************************************************************)
(*                                                                           *)
(* MIT License                                                               *)
(* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

module S = Csir.Scalar

module type NUM = sig
  type scalar

  type 'a repr

  type 'a t

  val custom :
    ?qc:S.t ->
    ?ql:S.t ->
    ?qr:S.t ->
    ?qo:S.t ->
    ?qm:S.t ->
    ?qx2b:S.t ->
    ?qx5a:S.t ->
    scalar repr ->
    scalar repr ->
    scalar repr t

  val assert_custom :
    ?qc:S.t ->
    ?ql:S.t ->
    ?qr:S.t ->
    ?qo:S.t ->
    ?qm:S.t ->
    scalar repr ->
    scalar repr ->
    scalar repr ->
    unit repr t

  val add :
    ?qc:S.t -> ?ql:S.t -> ?qr:S.t -> scalar repr -> scalar repr -> scalar repr t

  val add_constant : ?ql:S.t -> S.t -> scalar repr -> scalar repr t

  val mul : ?qm:S.t -> scalar repr -> scalar repr -> scalar repr t

  val div : ?den_coeff:S.t -> scalar repr -> scalar repr -> scalar repr t

  val pow5 : scalar repr -> scalar repr t

  val is_zero : scalar repr -> bool repr t

  val is_not_zero : scalar repr -> bool repr t

  val assert_nonzero : scalar repr -> unit repr t

  val assert_bool : scalar repr -> unit repr t
end

module type BOOL = sig
  type scalar

  type 'a repr

  type 'a t

  val band : bool repr -> bool repr -> bool repr t

  val xor : bool repr -> bool repr -> bool repr t

  val bor : bool repr -> bool repr -> bool repr t

  val bor_lookup : bool repr -> bool repr -> bool repr t

  val bnot : bool repr -> bool repr t

  val ifthenelse : bool repr -> 'a repr -> 'a repr -> 'a repr t

  val swap : bool repr -> 'a repr -> 'a repr -> ('a * 'a) repr t

  val assert_true : bool repr -> unit repr t

  val assert_false : bool repr -> unit repr t

  val constant_bool : bool -> bool repr t

  val band_list : bool repr list -> bool repr t
end

module type COMMON = sig
  type scalar

  type input_kind = [`InputCom | `Public | `Private]

  type trace_kind = [input_kind | `NoInput]

  type 'a repr

  type 'a t

  val ret : 'a -> 'a t

  val ( let* ) : 'a t -> ('a -> 'b t) -> 'b t

  val ( >* ) : unit repr t -> 'a t -> 'a t

  (* Add a boolean check *)
  val with_bool_check : bool repr t -> unit repr t

  (* Retrieve the boolean representing the conjunction of all previous implicit
     checks.
     WARNING: This will "reset" the implicit check accumulator.
  *)
  val get_checks_wire : bool repr t

  module Input : sig
    type 'a input

    val scalar : S.t -> scalar input

    val to_scalar : scalar input -> S.t

    val bool : bool -> bool input

    val to_bool : bool input -> bool

    val unit : unit input

    val pair : 'a input -> 'b input -> ('a * 'b) input

    val to_pair : ('a * 'b) input -> 'a input * 'b input

    val list : 'a input list -> 'a list input

    val to_list : 'a list input -> 'a input list

    val with_implicit_bool_check :
      ('a repr -> bool repr t) -> 'a input -> 'a input

    val with_assertion : ('a repr -> unit repr t) -> 'a input -> 'a input

    type 'a t = 'a input
  end

  type 'b open_input_com

  val serialize : 'a Input.t -> S.t array

  val input : ?kind:input_kind -> 'a Input.t -> 'a repr t

  val new_input_com : unit repr t

  val begin_input_com : 'b -> 'b open_input_com

  val ( |: ) : ('c repr -> 'd) open_input_com -> 'c Input.t -> 'd open_input_com

  val end_input_com : 'a open_input_com -> 'a t

  val eq : 'a repr -> 'a repr -> bool

  val foldM : ('a -> 'b -> 'a t) -> 'a -> 'b list -> 'a t

  val pair : 'a repr -> 'b repr -> ('a * 'b) repr

  val of_pair : ('a * 'b) repr -> 'a repr * 'b repr

  val to_list : 'a repr list -> 'a list repr

  val of_list : 'a list repr -> 'a repr list

  val unit : unit repr

  val scalar_of_bool : bool repr -> scalar repr

  val unsafe_bool_of_scalar : scalar repr -> bool repr

  val assert_equal : 'a repr -> 'a repr -> unit repr t

  val equal : 'a repr -> 'a repr -> bool repr t

  val constant_scalar : S.t -> scalar repr t

  (** Returns a list of Boolean variables representing the little endian
    bit decomposition of the given scalar (with the least significant bit
    on the head). *)
  val bits_of_scalar :
    ?shift:Z.t -> nb_bits:int -> scalar repr -> bool list repr t

  val with_label : label:string -> 'a t -> 'a t

  module Num :
    NUM
      with type scalar = scalar
       and type 'a repr = 'a repr
       and type 'a t = 'a t

  module Bool :
    BOOL
      with type scalar = scalar
       and type 'a repr = 'a repr
       and type 'a t = 'a t

  module Ecc : sig
    val weierstrass_add :
      (scalar * scalar) repr ->
      (scalar * scalar) repr ->
      (scalar * scalar) repr t

    val edwards_add :
      (scalar * scalar) repr ->
      (scalar * scalar) repr ->
      (scalar * scalar) repr t

    val edwards_cond_add :
      (scalar * scalar) repr ->
      (scalar * scalar) repr ->
      bool repr ->
      (scalar * scalar) repr t
  end

  module Poseidon : sig
    val poseidon128_full_round :
      matrix:S.t array array ->
      k:S.t array ->
      variant:Variants.t ->
      scalar repr * scalar repr * scalar repr ->
      scalar list repr t

    val poseidon128_four_partial_rounds :
      matrix:S.t array array ->
      ks:S.t array array ->
      variant:Variants.t ->
      scalar repr * scalar repr * scalar repr ->
      scalar list repr t
  end

  module Anemoi : sig
    val anemoi_round :
      kx:S.t -> ky:S.t -> scalar repr * scalar repr -> (scalar * scalar) repr t

    val anemoi_double_round :
      kx1:S.t ->
      ky1:S.t ->
      kx2:S.t ->
      ky2:S.t ->
      scalar repr * scalar repr ->
      (scalar * scalar) repr t

    val anemoi_custom :
      kx1:S.t ->
      ky1:S.t ->
      kx2:S.t ->
      ky2:S.t ->
      scalar repr * scalar repr ->
      (scalar * scalar) repr t
  end

  val hd : 'a list repr -> 'a repr t
end