@@ -62,6 +62,7 @@ module type Dsa = sig
62
62
module K_gen (H : Mirage_crypto.Hash .S ) : sig
63
63
val generate : key :priv -> Cstruct .t -> Cstruct .t
64
64
end
65
+ val force_precomputation : unit -> unit
65
66
end
66
67
67
68
module type Dh_dsa = sig
@@ -108,6 +109,8 @@ module type Foreign = sig
108
109
109
110
val double_c : out_point -> point -> unit
110
111
val add_c : out_point -> point -> point -> unit
112
+ val scalar_mult_base_c : out_point -> string -> unit
113
+ val force_precomputation_c : unit -> unit
111
114
end
112
115
113
116
module type Field_element = sig
@@ -125,6 +128,7 @@ module type Field_element = sig
125
128
val to_octets : field_element -> string
126
129
val double_point : point -> point
127
130
val add_point : point -> point -> point
131
+ val scalar_mult_base_point : scalar -> point
128
132
end
129
133
130
134
module Make_field_element (P : Parameters ) (F : Foreign ) : Field_element = struct
@@ -213,6 +217,11 @@ module Make_field_element (P : Parameters) (F : Foreign) : Field_element = struc
213
217
let tmp = out_point () in
214
218
F. add_c tmp a b;
215
219
out_p_to_p tmp
220
+
221
+ let scalar_mult_base_point (Scalar d ) =
222
+ let tmp = out_point () in
223
+ F. scalar_mult_base_c tmp d;
224
+ out_p_to_p tmp
216
225
end
217
226
218
227
module type Point = sig
@@ -226,6 +235,8 @@ module type Point = sig
226
235
val x_of_finite_point : point -> string
227
236
val params_g : point
228
237
val select : bool -> then_ :point -> else_ :point -> point
238
+ val scalar_mult_base : scalar -> point
239
+ val force_precomputation : unit -> unit
229
240
end
230
241
231
242
module Make_point (P : Parameters ) (F : Foreign ) : Point = struct
@@ -406,6 +417,9 @@ module Make_point (P : Parameters) (F : Foreign) : Point = struct
406
417
of_octets buf
407
418
| 0x00 | 0x04 -> Error `Invalid_length
408
419
| _ -> Error `Invalid_format
420
+
421
+ let scalar_mult_base = Fe. scalar_mult_base_point
422
+ let force_precomputation = F. force_precomputation_c
409
423
end
410
424
411
425
module type Scalar = sig
@@ -414,8 +428,8 @@ module type Scalar = sig
414
428
val of_octets : string -> (scalar , error ) result
415
429
val to_octets : scalar -> string
416
430
val scalar_mult : scalar -> point -> point
417
-
418
431
val scalar_mult_base : scalar -> point
432
+ val force_precomputation : unit -> unit
419
433
end
420
434
421
435
module Make_scalar (Param : Parameters ) (P : Point ) : Scalar = struct
@@ -435,62 +449,6 @@ module Make_scalar (Param : Parameters) (P : Point) : Scalar = struct
435
449
436
450
let to_octets (Scalar buf ) = rev_string buf
437
451
438
- (* Use a sliding window optimization method for scalar multiplication
439
- Hard-coded window size = 4
440
- Implementation inspired from Go's crypto library
441
- https://github.com/golang/go/blob/a5cd894318677359f6d07ee74f9004d28b4d164c/src/crypto/internal/nistec/p256.go#L317
442
- *)
443
- module Precomputed = struct
444
- (* Pre-compute multiples of the generator point *)
445
- let pre_compute_multiples () =
446
- let len = Param. fe_length * 2 in
447
- let one_table _ = Array. init 15 (fun _ -> P. at_infinity () ) in
448
- let table = Array. init len one_table in
449
- let base = ref P. params_g in
450
- for i = 0 to len - 1 do
451
- table.(i).(0 ) < - ! base;
452
- for j = 1 to 14 do
453
- table.(i).(j) < - P. add ! base table.(i).(j - 1 )
454
- done ;
455
- base := P. double ! base;
456
- base := P. double ! base;
457
- base := P. double ! base;
458
- base := P. double ! base
459
- done ;
460
- table
461
-
462
- (* Select the n-th element of the table
463
- without leaking information about [n] *)
464
- let table_select table n =
465
- let p = ref (P. at_infinity () ) in
466
- for i = 1 to 15 do
467
- let cond = not (Eqaf. bool_of_int (n - i)) in
468
- p := P. select cond ~then_: table.(i - 1 ) ~else_: ! p
469
- done ;
470
- ! p
471
-
472
- (* Returns [kG] by decomposing [k] in binary form, and adding
473
- [2^0G * k_0 + 2^1G * k_1 + ...] in constant time using
474
- pre-computed values of 2^iG *)
475
- let scalar_mult_base (Scalar k ) tables =
476
- let p = ref (P. at_infinity () ) in
477
- let index = ref 0 in (* Index increases since k is big-endian *)
478
- for i = 0 to String. length k - 1 do
479
- let byte = String. get_uint8 k i in
480
- let winValue = byte land 0b1111 in
481
- p := P. add ! p (table_select tables.(! index) winValue);
482
- incr index;
483
- let winValue = byte lsr 4 in
484
- p := P. add ! p (table_select tables.(! index) winValue);
485
- incr index
486
- done ;
487
- ! p
488
-
489
- let scalar_mult_base =
490
- let tables = pre_compute_multiples () in
491
- fun d -> scalar_mult_base d tables
492
- end
493
-
494
452
(* Branchless Montgomery ladder method *)
495
453
let scalar_mult (Scalar s ) p =
496
454
let r0 = ref (P. at_infinity () ) in
@@ -506,7 +464,9 @@ module Make_scalar (Param : Parameters) (P : Point) : Scalar = struct
506
464
! r0
507
465
508
466
(* Specialization of [scalar_mult d p] when [p] is the generator *)
509
- let scalar_mult_base = Precomputed. scalar_mult_base
467
+ let scalar_mult_base = P. scalar_mult_base
468
+
469
+ let force_precomputation = P. force_precomputation
510
470
end
511
471
512
472
module Make_dh (Param : Parameters ) (P : Point ) (S : Scalar ) : Dh = struct
@@ -818,6 +778,8 @@ module Make_dsa (Param : Parameters) (F : Fn) (P : Point) (S : Scalar) (H : Mira
818
778
819
779
let verify ~key (r , s ) digest =
820
780
verify_octets ~key (Cstruct. to_string r, Cstruct. to_string s) (Cstruct. to_string digest)
781
+
782
+ let force_precomputation = S. force_precomputation
821
783
end
822
784
823
785
module P224 : Dh_dsa = struct
@@ -849,6 +811,8 @@ module P224 : Dh_dsa = struct
849
811
external select_c : out_field_element -> bool -> field_element -> field_element -> unit = " mc_p224_select" [@@ noalloc]
850
812
external double_c : out_point -> point -> unit = " mc_p224_point_double" [@@ noalloc]
851
813
external add_c : out_point -> point -> point -> unit = " mc_p224_point_add" [@@ noalloc]
814
+ external scalar_mult_base_c : out_point -> string -> unit = " mc_p224_scalar_mult_base" [@@ noalloc]
815
+ external force_precomputation_c : unit -> unit = " mc_p224_force_precomputation" [@@ noalloc]
852
816
end
853
817
854
818
module Foreign_n = struct
@@ -898,6 +862,8 @@ module P256 : Dh_dsa = struct
898
862
external select_c : out_field_element -> bool -> field_element -> field_element -> unit = " mc_p256_select" [@@ noalloc]
899
863
external double_c : out_point -> point -> unit = " mc_p256_point_double" [@@ noalloc]
900
864
external add_c : out_point -> point -> point -> unit = " mc_p256_point_add" [@@ noalloc]
865
+ external scalar_mult_base_c : out_point -> string -> unit = " mc_p256_scalar_mult_base" [@@ noalloc]
866
+ external force_precomputation_c : unit -> unit = " mc_p256_force_precomputation" [@@ noalloc]
901
867
end
902
868
903
869
module Foreign_n = struct
@@ -948,6 +914,8 @@ module P384 : Dh_dsa = struct
948
914
external select_c : out_field_element -> bool -> field_element -> field_element -> unit = " mc_p384_select" [@@ noalloc]
949
915
external double_c : out_point -> point -> unit = " mc_p384_point_double" [@@ noalloc]
950
916
external add_c : out_point -> point -> point -> unit = " mc_p384_point_add" [@@ noalloc]
917
+ external scalar_mult_base_c : out_point -> string -> unit = " mc_p384_scalar_mult_base" [@@ noalloc]
918
+ external force_precomputation_c : unit -> unit = " mc_p384_force_precomputation" [@@ noalloc]
951
919
end
952
920
953
921
module Foreign_n = struct
@@ -999,6 +967,8 @@ module P521 : Dh_dsa = struct
999
967
external select_c : out_field_element -> bool -> field_element -> field_element -> unit = " mc_p521_select" [@@ noalloc]
1000
968
external double_c : out_point -> point -> unit = " mc_p521_point_double" [@@ noalloc]
1001
969
external add_c : out_point -> point -> point -> unit = " mc_p521_point_add" [@@ noalloc]
970
+ external scalar_mult_base_c : out_point -> string -> unit = " mc_p521_scalar_mult_base" [@@ noalloc]
971
+ external force_precomputation_c : unit -> unit = " mc_p521_force_precomputation" [@@ noalloc]
1002
972
end
1003
973
1004
974
module Foreign_n = struct
0 commit comments