package secp256k1-internal

  1. Overview
  2. Docs

Source file internal.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
module Scalar = struct
  type t = Cstruct.buffer

  let size = 32

  external const :
    t ->
    int32 ->
    int32 ->
    int32 ->
    int32 ->
    int32 ->
    int32 ->
    int32 ->
    int32 ->
    unit = "ml_secp256k1_scalar_const_bytecode" "ml_secp256k1_scalar_const"
    [@@noalloc]

  let const ?(d7 = 0l) ?(d6 = 0l) ?(d5 = 0l) ?(d4 = 0l) ?(d3 = 0l) ?(d2 = 0l)
      ?(d1 = 0l) ?(d0 = 0l) () =
    let buf = Cstruct.create size in
    const buf.buffer d7 d6 d5 d4 d3 d2 d1 d0 ;
    buf.buffer

  let zero () = const ()

  let one () = const ~d0:1l ()

  let copy t =
    let ret = Cstruct.create size in
    Cstruct.(blit (of_bigarray t) 0 ret 0 size) ;
    ret.buffer

  external clear : t -> unit = "ml_secp256k1_scalar_clear" [@@noalloc]

  external get_bits : t -> int -> int -> int = "ml_secp256k1_scalar_get_bits"
    [@@noalloc]

  external get_bits_var : t -> int -> int -> int
    = "ml_secp256k1_scalar_get_bits_var"
    [@@noalloc]

  external set_b32 : t -> Cstruct.buffer -> bool = "ml_secp256k1_scalar_set_b32"
    [@@noalloc]

  external set_int : Cstruct.buffer -> int -> unit
    = "ml_secp256k1_scalar_set_int"
    [@@noalloc]

  external get_b32 : Cstruct.buffer -> t -> unit = "ml_secp256k1_scalar_get_b32"
    [@@noalloc]

  external add : t -> t -> t -> bool = "ml_secp256k1_scalar_add" [@@noalloc]

  external cadd_bit : t -> int -> bool -> unit = "ml_secp256k1_scalar_cadd_bit"
    [@@noalloc]

  external mul : t -> t -> t -> unit = "ml_secp256k1_scalar_mul" [@@noalloc]

  external shr_int : t -> int -> int = "ml_secp256k1_scalar_shr_int" [@@noalloc]

  external inverse : t -> t -> unit = "ml_secp256k1_scalar_inverse" [@@noalloc]

  external inverse_var : t -> t -> unit = "ml_secp256k1_scalar_inverse_var"
    [@@noalloc]

  external negate : t -> t -> unit = "ml_secp256k1_scalar_negate" [@@noalloc]

  external is_zero : t -> bool = "ml_secp256k1_scalar_is_zero" [@@noalloc]

  external is_one : t -> bool = "ml_secp256k1_scalar_is_one" [@@noalloc]

  external is_even : t -> bool = "ml_secp256k1_scalar_is_even" [@@noalloc]

  external is_high : t -> bool = "ml_secp256k1_scalar_is_high" [@@noalloc]

  external cond_negate : t -> bool -> bool = "ml_secp256k1_scalar_cond_negate"
    [@@noalloc]

  external equal : t -> t -> bool = "ml_secp256k1_scalar_eq" [@@noalloc]

  external mul_shift_var : t -> t -> t -> int -> unit
    = "ml_secp256k1_mul_shift_var"
    [@@noalloc]

  let set_b32 t buf = set_b32 t (Cstruct.to_bigarray buf)

  let get_b32 buf t = get_b32 (Cstruct.to_bigarray buf) t
end

module Field = struct
  type t = Cstruct.buffer

  module Storage = struct
    type t = Cstruct.buffer

    let size = 32

    let to_cstruct t = Cstruct.of_bigarray t

    let of_cstruct cs =
      let res = Cstruct.create size in
      try
        Cstruct.blit cs 0 res 0 size ;
        Some res.buffer
      with _ -> None

    let of_cstruct_exn cs =
      match of_cstruct cs with
      | Some t -> t
      | None -> invalid_arg "Field.Storage.of_cstruct_exn"

    external const :
      t ->
      int32 ->
      int32 ->
      int32 ->
      int32 ->
      int32 ->
      int32 ->
      int32 ->
      int32 ->
      unit
      = "ml_secp256k1_fe_storage_const_bytecode" "ml_secp256k1_fe_storage_const"
      [@@noalloc]

    let const ?(d7 = 0l) ?(d6 = 0l) ?(d5 = 0l) ?(d4 = 0l) ?(d3 = 0l) ?(d2 = 0l)
        ?(d1 = 0l) ?(d0 = 0l) () =
      let buf = Cstruct.create size in
      const buf.buffer d7 d6 d5 d4 d3 d2 d1 d0 ;
      buf.buffer

    external cmov : t -> t -> bool -> unit = "ml_secp256k1_fe_storage_cmov"
      [@@noalloc]
  end

  let size = 40

  external const :
    t ->
    int32 ->
    int32 ->
    int32 ->
    int32 ->
    int32 ->
    int32 ->
    int32 ->
    int32 ->
    unit = "ml_secp256k1_fe_const_bytecode" "ml_secp256k1_fe_const"
    [@@noalloc]

  let const ?(d7 = 0l) ?(d6 = 0l) ?(d5 = 0l) ?(d4 = 0l) ?(d3 = 0l) ?(d2 = 0l)
      ?(d1 = 0l) ?(d0 = 0l) () =
    let buf = Cstruct.create size in
    const buf.buffer d7 d6 d5 d4 d3 d2 d1 d0 ;
    buf.buffer

  external normalize : t -> unit = "ml_secp256k1_fe_normalize" [@@noalloc]

  external normalize_weak : t -> unit = "ml_secp256k1_fe_normalize_weak"
    [@@noalloc]

  external normalize_var : t -> unit = "ml_secp256k1_fe_normalize_var"
    [@@noalloc]

  external normalizes_to_zero : t -> bool = "ml_secp256k1_fe_normalizes_to_zero"
    [@@noalloc]

  external normalizes_to_zero_var : t -> bool
    = "ml_secp256k1_fe_normalizes_to_zero_var"
    [@@noalloc]

  external set_int : t -> int -> unit = "ml_secp256k1_fe_set_int" [@@noalloc]

  external clear : t -> unit = "ml_secp256k1_fe_clear" [@@noalloc]

  external is_zero : t -> bool = "ml_secp256k1_fe_is_zero" [@@noalloc]

  external is_odd : t -> bool = "ml_secp256k1_fe_is_odd" [@@noalloc]

  external equal : t -> t -> bool = "ml_secp256k1_fe_equal" [@@noalloc]

  external equal_var : t -> t -> bool = "ml_secp256k1_fe_equal_var" [@@noalloc]

  external cmp_var : t -> t -> int = "ml_secp256k1_fe_cmp_var" [@@noalloc]

  external set_b32 : t -> Cstruct.buffer -> bool = "ml_secp256k1_fe_set_b32"
    [@@noalloc]

  external get_b32 : Cstruct.buffer -> t -> unit = "ml_secp256k1_fe_get_b32"
    [@@noalloc]

  external negate : t -> t -> int -> unit = "ml_secp256k1_fe_negate" [@@noalloc]

  external mul_int : t -> int -> unit = "ml_secp256k1_fe_mul_int" [@@noalloc]

  external add : t -> t -> unit = "ml_secp256k1_fe_add" [@@noalloc]

  external mul : t -> t -> t -> unit = "ml_secp256k1_fe_mul" [@@noalloc]

  external sqrt : t -> t -> int = "ml_secp256k1_fe_sqrt" [@@noalloc]

  external inv : t -> t -> unit = "ml_secp256k1_fe_inv" [@@noalloc]

  external inv_var : t -> t -> unit = "ml_secp256k1_fe_inv_var" [@@noalloc]

  external to_storage : Storage.t -> t -> unit = "ml_secp256k1_fe_to_storage"
    [@@noalloc]

  external from_storage : t -> Storage.t -> unit
    = "ml_secp256k1_fe_from_storage"
    [@@noalloc]

  external cmov : t -> t -> bool -> unit = "ml_secp256k1_fe_cmov" [@@noalloc]

  let set_b32 t buf = set_b32 t (Cstruct.to_bigarray buf)

  let get_b32 buf t = get_b32 (Cstruct.to_bigarray buf) t

  let compare = cmp_var
end

module Group = struct
  type t = Cstruct.buffer

  type ge = t

  let size = (2 * Field.size) + 8

  module Storage = struct
    type t = Cstruct.buffer

    let size = 2 * Field.Storage.size

    let to_cstruct t = Cstruct.of_bigarray t

    let of_cstruct cs =
      let res = Cstruct.create size in
      try
        Cstruct.blit cs 0 res 0 size ;
        Some res.buffer
      with _ -> None

    let of_cstruct_exn cs =
      match of_cstruct cs with
      | Some t -> t
      | None -> invalid_arg "Group.Storage.of_cstruct_exn"

    external of_fields : t -> Field.Storage.t -> Field.Storage.t -> unit
      = "ml_secp256k1_ge_storage_of_fields"
      [@@noalloc]

    let of_fields ?(x = Field.const ()) ?(y = Field.const ()) () =
      let cs = Cstruct.create size in
      of_fields cs.buffer x y ;
      cs.buffer

    external cmov : t -> t -> bool -> unit = "ml_secp256k1_ge_storage_cmov"
      [@@noalloc]
  end

  module Jacobian = struct
    type t = Cstruct.buffer

    let size = (3 * Field.size) + 8

    external of_fields : t -> Field.t -> Field.t -> Field.t -> bool -> unit
      = "ml_secp256k1_gej_of_fields"
      [@@noalloc]

    external set_infinity : t -> unit = "ml_secp256k1_gej_set_infinity"
      [@@noalloc]

    external set_ge : t -> ge -> unit = "ml_secp256k1_gej_set_ge" [@@noalloc]

    external get_ge : ge -> t -> unit = "ml_secp256k1_ge_set_gej" [@@noalloc]

    external eq_x_var : Field.t -> t -> int = "ml_secp256k1_gej_eq_x_var"
      [@@noalloc]

    external neg : t -> t -> unit = "ml_secp256k1_gej_neg" [@@noalloc]

    external is_infinity : t -> bool = "ml_secp256k1_gej_is_infinity"
      [@@noalloc]

    external double_var : t -> t -> Field.t option -> unit
      = "ml_secp256k1_gej_double_var"
      [@@noalloc]

    external add_var : t -> t -> t -> Field.t option -> unit
      = "ml_secp256k1_gej_add_var"
      [@@noalloc]

    external add_ge : t -> t -> ge -> unit = "ml_secp256k1_gej_add_ge"
      [@@noalloc]

    external add_ge_var : t -> t -> ge -> Field.t option -> unit
      = "ml_secp256k1_gej_add_ge_var"
      [@@noalloc]

    external add_zinv_var : t -> t -> ge -> Field.t -> unit
      = "ml_secp256k1_gej_add_zinv_var"
      [@@noalloc]

    external mul : t -> ge -> Scalar.t -> unit = "ml_secp256k1_ecmult_const"
      [@@noalloc]

    external clear : t -> unit = "ml_secp256k1_gej_clear" [@@noalloc]

    external rescale : t -> Field.t -> unit = "ml_secp256k1_gej_rescale"
      [@@noalloc]

    let of_fields ?(x = Field.const ()) ?(y = Field.const ())
        ?(z = Field.const ()) ?(infinity = false) () =
      let cs = Cstruct.create size in
      of_fields cs.buffer x y z infinity ;
      cs.buffer

    let double_var ?rzr r a = double_var r a rzr

    let add_var ?rzr r a b = add_var r a b rzr

    let add_ge_var ?rzr r a b = add_ge_var r a b rzr
  end

  external of_fields : t -> Field.t -> Field.t -> bool -> unit
    = "ml_secp256k1_ge_of_fields"
    [@@noalloc]

  external set_xy : t -> Field.t -> Field.t -> unit = "ml_secp256k1_ge_set_xy"
    [@@noalloc]

  external is_infinity : t -> bool = "ml_secp256k1_ge_is_infinity" [@@noalloc]

  external is_valid_var : t -> bool = "ml_secp256k1_ge_is_valid_var" [@@noalloc]

  external neg : t -> t -> unit = "ml_secp256k1_ge_neg" [@@noalloc]

  external clear : t -> unit = "ml_secp256k1_ge_clear" [@@noalloc]

  external to_storage : Storage.t -> t -> unit = "ml_secp256k1_ge_to_storage"
    [@@noalloc]

  external from_storage : t -> Storage.t -> unit
    = "ml_secp256k1_ge_from_storage"
    [@@noalloc]

  let of_fields ?(x = Field.const ()) ?(y = Field.const ()) ?(infinity = false)
      () =
    let cs = Cstruct.create size in
    of_fields cs.buffer x y infinity ;
    cs.buffer

  let g =
    let x =
      Field.const
        ~d7:0x79BE667El
        ~d6:0xF9DCBBACl
        ~d5:0x55A06295l
        ~d4:0xCE870B07l
        ~d3:0x029BFCDBl
        ~d2:0x2DCE28D9l
        ~d1:0x59F2815Bl
        ~d0:0x16F81798l
        ()
    in
    let y =
      Field.const
        ~d7:0x483ADA77l
        ~d6:0x26A3C465l
        ~d5:0x5DA4FBFCl
        ~d4:0x0E1108A8l
        ~d3:0xFD17B448l
        ~d2:0xA6855419l
        ~d1:0x9C47D08Fl
        ~d0:0xFB10D4B8l
        ()
    in
    of_fields ~x ~y ~infinity:false ()

  external serialize : t -> Cstruct.buffer -> int -> bool -> int
    = "ml_secp256k1_eckey_pubkey_serialize"
    [@@noalloc]

  external parse : t -> Cstruct.buffer -> int -> bool
    = "ml_secp256k1_eckey_pubkey_parse"
    [@@noalloc]

  let to_pubkey ?(compress = true) cs e =
    match serialize e cs.Cstruct.buffer cs.len compress with
    | 0 -> failwith "Group.to_pubkey"
    | len -> Cstruct.sub cs 0 len

  let from_pubkey t cs =
    match parse t cs.Cstruct.buffer cs.len with
    | false -> failwith "Group.from_pubkey"
    | true -> ()
end