Skip to content

Commit

Permalink
Correct number of limbs in gen tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Firobe committed Feb 15, 2024
1 parent 810bace commit fb7b87c
Show file tree
Hide file tree
Showing 5 changed files with 9,378 additions and 9,377 deletions.
17 changes: 10 additions & 7 deletions ec/gen_tables/gen_tables.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ let pp_array elem_fmt fmt arr =
done;
fout "@]@,}"

let pp_string_words ~wordsize fmt str =
let limbs = String.length str * 8 / wordsize in
let div_round_up a b =
a / b + (if a mod b = 0 then 0 else 1)

let pp_string_words ~wordsize ~byte_length fmt str =
let limbs = div_round_up (byte_length * 8) wordsize in
assert (String.length str * 8 mod wordsize = 0);
let bytes = Bytes.unsafe_of_string str in
fprintf fmt "@[<2>{@\n";
Expand Down Expand Up @@ -51,22 +54,22 @@ let check_shape tables =
x)
tables

let print_tables tables ~wordsize =
let print_tables tables ~wordsize ~byte_length =
let fe_len = String.length tables.(0).(0).(0) in
printf "@[<2>static WORD generator_table[%d][15][3][LIMBS] = @," (fe_len * 2);
pp_array
(pp_array (pp_array (pp_string_words ~wordsize)))
(pp_array (pp_array (pp_string_words ~wordsize ~byte_length)))
std_formatter tables;
printf "@];@,"

let print_toplevel name (module P : Mirage_crypto_ec.Dh_dsa) =
let tables = P.Dsa.Precompute.generator_tables () in
let tables, byte_length = P.Dsa.Precompute.generator_tables () in
check_shape tables;
print_header name;
printf "@[<v>#ifdef ARCH_64BIT@,";
print_tables ~wordsize:64 tables;
print_tables ~wordsize:64 ~byte_length tables;
printf "#else // 32-bit@,";
print_tables ~wordsize:32 tables;
print_tables ~wordsize:32 ~byte_length tables;
printf "@]#endif@."

let curves =
Expand Down
9 changes: 5 additions & 4 deletions ec/mirage_crypto_ec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module type Dsa = sig
val generate : key:priv -> Cstruct.t -> Cstruct.t
end
module Precompute : sig
val generator_tables : unit -> string array array array
val generator_tables : unit -> (string array array array * int)
end
end

Expand Down Expand Up @@ -428,7 +428,7 @@ module type Scalar = sig
val to_octets : scalar -> string
val scalar_mult : scalar -> point -> point
val scalar_mult_base : scalar -> point
val generator_tables : unit -> field_element array array array
val generator_tables : unit -> (field_element array array array * int)
end

module Make_scalar (Param : Parameters) (P : Point) : Scalar = struct
Expand Down Expand Up @@ -465,7 +465,8 @@ module Make_scalar (Param : Parameters) (P : Point) : Scalar = struct
(* Specialization of [scalar_mult d p] when [p] is the generator *)
let scalar_mult_base = P.scalar_mult_base

(* Pre-compute multiples of the generator point *)
(* Pre-compute multiples of the generator point
returns the tables along with the number of significant bytes *)
let generator_tables () =
let len = Param.fe_length * 2 in
let one_table _ = Array.init 15 (fun _ -> P.at_infinity ()) in
Expand All @@ -482,7 +483,7 @@ module Make_scalar (Param : Parameters) (P : Point) : Scalar = struct
base := P.double !base
done;
let convert {f_x; f_y; f_z} = [|f_x; f_y; f_z|] in
Array.map (Array.map convert) table
(Array.map (Array.map convert) table, Param.byte_length)
end

module Make_dh (Param : Parameters) (P : Point) (S : Scalar) : Dh = struct
Expand Down
5 changes: 3 additions & 2 deletions ec/mirage_crypto_ec.mli
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ module type Dsa = sig
(** Operations to precompute useful data meant to be hardcoded in
[mirage-crypto-ec] before compilation *)
module Precompute : sig
val generator_tables : unit -> string array array array
val generator_tables : unit -> (string array array array * int)
(** Return an array of shape (Fe_length * 2, 15, 3) containing multiples of
the generator point for the curve. Useful only to bootstrap tables
necessary for scalar multiplication. *)
necessary for scalar multiplication. Returns the tables and the number
of significant bytes for each element.*)
end
end

Expand Down
Loading

0 comments on commit fb7b87c

Please sign in to comment.