From fa1cf30746bf6cf207e0e1d6d11a6aceb02ae5d3 Mon Sep 17 00:00:00 2001 From: Antonio Locascio Date: Mon, 22 May 2023 16:49:21 +0200 Subject: [PATCH 1/2] Lib_plompiler/Lang_core: remove useless primitive from interface --- src/lib_plompiler/lang_core.ml | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib_plompiler/lang_core.ml b/src/lib_plompiler/lang_core.ml index 5ea5798ece30..2b2374748a98 100644 --- a/src/lib_plompiler/lang_core.ml +++ b/src/lib_plompiler/lang_core.ml @@ -166,8 +166,6 @@ module type COMMON = sig 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 -- GitLab From da083c086c22c63566f3ca6e2a2edb392089f461 Mon Sep 17 00:00:00 2001 From: ambrona Date: Mon, 22 May 2023 18:32:31 +0200 Subject: [PATCH 2/2] lib_plompiler: remove redundant argument [variant] in Poseidon --- src/lib_plompiler/circuit.ml | 9 ++++----- src/lib_plompiler/gadget_poseidon.ml | 15 +-------------- src/lib_plompiler/lang_core.ml | 2 -- src/lib_plompiler/result.ml | 4 ++-- src/lib_plompiler/solver.ml | 28 ++++++++++------------------ src/lib_plompiler/solver.mli | 4 ++-- src/lib_plompiler/variants.ml | 26 -------------------------- 7 files changed, 19 insertions(+), 69 deletions(-) delete mode 100644 src/lib_plompiler/variants.ml diff --git a/src/lib_plompiler/circuit.ml b/src/lib_plompiler/circuit.ml index 0b0dd0e5bcb8..4b85eec0c592 100644 --- a/src/lib_plompiler/circuit.ml +++ b/src/lib_plompiler/circuit.ml @@ -971,12 +971,11 @@ module Poseidon = struct let mul = Poly.( * ) end) - let poseidon128_full_round ~matrix ~k ~variant - (Scalar x0, Scalar x1, Scalar x2) = + let poseidon128_full_round ~matrix ~k (Scalar x0, Scalar x1, Scalar x2) = let*& y0 = fresh Dummy.scalar in let*& y1 = fresh Dummy.scalar in let*& y2 = fresh Dummy.scalar in - let solver = Poseidon128Full {x0; y0; x1; y1; x2; y2; k; variant} in + let solver = Poseidon128Full {x0; y0; x1; y1; x2; y2; k; matrix} in let minv = VS.inverse matrix in let k_vec = VS.(mul minv (transpose [|k|])) in @@ -1022,7 +1021,7 @@ module Poseidon = struct ~solver >* ret @@ to_list [Scalar y0; Scalar y1; Scalar y2] - let poseidon128_four_partial_rounds ~matrix ~ks ~variant + let poseidon128_four_partial_rounds ~matrix ~ks (Scalar x0, Scalar x1, Scalar x2) = let*& a = fresh Dummy.scalar in let*& a_5 = fresh Dummy.scalar in @@ -1036,7 +1035,7 @@ module Poseidon = struct let k_cols = Array.init 4 (fun i -> VS.filter_cols (Int.equal i) ks) in let solver = Poseidon128Partial - {a; b; c; a_5; b_5; c_5; x0; y0; x1; y1; x2; y2; k_cols; variant} + {a; b; c; a_5; b_5; c_5; x0; y0; x1; y1; x2; y2; k_cols; matrix} in (* We represent variables x0, x1, x2_5, a, a_5, b, b_5, c, c_5, y0, y1, y2 diff --git a/src/lib_plompiler/gadget_poseidon.ml b/src/lib_plompiler/gadget_poseidon.ml index abbd9ea2cd2d..22f1303903e1 100644 --- a/src/lib_plompiler/gadget_poseidon.ml +++ b/src/lib_plompiler/gadget_poseidon.ml @@ -27,8 +27,6 @@ open Lang_core open Lang_stdlib module type PARAMETERS = sig - val variant : Variants.t - val width : int val nb_full_rounds : int @@ -210,11 +208,7 @@ module Make (PP : PARAMETERS) (L : LIB) = struct k.(i) <- round_constants.(i_round_key + i) done ; let* output = - Poseidon.poseidon128_full_round - ~matrix:mds_matrix - ~k - ~variant - (x0, x1, x2) + Poseidon.poseidon128_full_round ~matrix:mds_matrix ~k (x0, x1, x2) in (match of_list output with | [y0; y1; y2] -> @@ -257,7 +251,6 @@ module Make (PP : PARAMETERS) (L : LIB) = struct Poseidon.poseidon128_four_partial_rounds ~matrix:mds_matrix ~ks - ~variant (x0, x1, x2) in (match of_list output with @@ -344,8 +337,6 @@ module Poseidon128 = struct end module V : Hash_sig.HASH = Make (struct - let variant = Variants.P128 - let width = 3 let nb_full_rounds = 8 @@ -374,8 +365,6 @@ module Poseidon252 = struct end module V : Hash_sig.HASH = Make (struct - let variant = Variants.P252 - let width = 5 let nb_full_rounds = 8 @@ -404,8 +393,6 @@ module PoseidonFull = struct end module V : Hash_sig.HASH = Make (struct - let variant = Variants.PFull128 - let width = 3 let nb_full_rounds = 60 diff --git a/src/lib_plompiler/lang_core.ml b/src/lib_plompiler/lang_core.ml index 2b2374748a98..571fad10a8cd 100644 --- a/src/lib_plompiler/lang_core.ml +++ b/src/lib_plompiler/lang_core.ml @@ -238,14 +238,12 @@ module type COMMON = 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 diff --git a/src/lib_plompiler/result.ml b/src/lib_plompiler/result.ml index bde8bdaa0906..53727c3f343b 100644 --- a/src/lib_plompiler/result.ml +++ b/src/lib_plompiler/result.ml @@ -343,7 +343,7 @@ end module Poseidon = struct module VS = Linear_algebra.Make_VectorSpace (S) - let poseidon128_full_round ~matrix ~k ~variant:_ (x0, x1, x2) = + let poseidon128_full_round ~matrix ~k (x0, x1, x2) = let pow5 x = S.pow (of_s x) (Z.of_int 5) in let x_vec = [|Array.map pow5 [|x0; x1; x2|]|] |> VS.transpose in let y_vec = VS.mul matrix x_vec in @@ -352,7 +352,7 @@ module Poseidon = struct let y2 = S.add k.(2) @@ y_vec.(2).(0) in ret @@ to_list [S (X y0); S (X y1); S (X y2)] - let poseidon128_four_partial_rounds ~matrix ~ks ~variant:_ (x0, x1, x2) = + let poseidon128_four_partial_rounds ~matrix ~ks (x0, x1, x2) = let k0 = VS.filter_cols (Int.equal 0) ks in let k1 = VS.filter_cols (Int.equal 1) ks in let k2 = VS.filter_cols (Int.equal 2) ks in diff --git a/src/lib_plompiler/solver.ml b/src/lib_plompiler/solver.ml index 500e8f563a48..ab7388639ced 100644 --- a/src/lib_plompiler/solver.ml +++ b/src/lib_plompiler/solver.ml @@ -90,7 +90,7 @@ type pos128full_desc = { x2 : int; y2 : int; k : VS.t array; - variant : Variants.t; + matrix : VS.matrix; } type swap_desc = {b : int; x : int; y : int; u : int; v : int} [@@deriving repr] @@ -99,8 +99,8 @@ type swap_desc = {b : int; x : int; y : int; u : int; v : int} [@@deriving repr] S.t but for some reason Repr does not see this equality. *) let pos128full_desc_t = let open Repr in - record "pos128full_desc" (fun x0 y0 x1 y1 x2 y2 k variant -> - {x0; y0; x1; y1; x2; y2; k; variant}) + record "pos128full_desc" (fun x0 y0 x1 y1 x2 y2 k matrix -> + {x0; y0; x1; y1; x2; y2; k; matrix}) |+ field "x0" int (fun t -> t.x0) |+ field "y0" int (fun t -> t.y0) |+ field "x1" int (fun t -> t.x1) @@ -108,7 +108,7 @@ let pos128full_desc_t = |+ field "x2" int (fun t -> t.x2) |+ field "y2" int (fun t -> t.y2) |+ field "k" (array S.t) (fun t -> t.k) - |+ field "variant" Variants.t (fun t -> t.variant) + |+ field "matrix" (array (array S.t)) (fun t -> t.matrix) |> sealr type pos128partial_desc = { @@ -126,15 +126,15 @@ type pos128partial_desc = { y2 : int; (* Can we share these? *) k_cols : VS.matrix array; - variant : Variants.t; + matrix : VS.matrix; } let pos128partial_desc_t = let open Repr in record "pos128partial_desc" - (fun a b c a_5 b_5 c_5 x0 y0 x1 y1 x2 y2 k_cols variant -> - {a; b; c; a_5; b_5; c_5; x0; y0; x1; y1; x2; y2; k_cols; variant}) + (fun a b c a_5 b_5 c_5 x0 y0 x1 y1 x2 y2 k_cols matrix -> + {a; b; c; a_5; b_5; c_5; x0; y0; x1; y1; x2; y2; k_cols; matrix}) |+ field "a" int (fun t -> t.a) |+ field "b" int (fun t -> t.b) |+ field "c" int (fun t -> t.c) @@ -148,7 +148,7 @@ let pos128partial_desc_t = |+ field "x2" int (fun t -> t.x2) |+ field "y2" int (fun t -> t.y2) |+ field "k_cols" (array (array (array S.t))) (fun t -> t.k_cols) - |+ field "variant" Variants.t (fun t -> t.variant) + |+ field "matrix" (array (array S.t)) (fun t -> t.matrix) |> sealr type anemoi_desc = { @@ -328,11 +328,7 @@ let solve_one trace solver = let x_res, y_res = if S.is_zero b then (x, y) else (y, x) in trace.(u) <- x_res ; trace.(v) <- y_res - | Poseidon128Full {x0; y0; x1; y1; x2; y2; k; variant} -> - let matrix = - Array.map (Array.map S.of_string) - @@ if variant = PFull128 then Mds_full.v else Mds_128.v - in + | Poseidon128Full {x0; y0; x1; y1; x2; y2; k; matrix} -> let pow5 x = S.pow trace.(x) (Z.of_int 5) in let x_vec = [|Array.map pow5 [|x0; x1; x2|]|] |> VS.transpose in let y_vec = VS.mul matrix x_vec in @@ -340,11 +336,7 @@ let solve_one trace solver = (fun i yi -> trace.(yi) <- S.add k.(i) @@ y_vec.(i).(0)) [y0; y1; y2] | Poseidon128Partial - {a; b; c; a_5; b_5; c_5; x0; y0; x1; y1; x2; y2; k_cols; variant} -> - let matrix = - Array.map (Array.map S.of_string) - @@ if variant = PFull128 then Mds_full.v else Mds_128.v - in + {a; b; c; a_5; b_5; c_5; x0; y0; x1; y1; x2; y2; k_cols; matrix} -> let pow5 x = S.pow x (Z.of_int 5) in let ppow5 v = [|v.(0); v.(1); [|pow5 v.(2).(0)|]|] in let x_vec = [|[|trace.(x0)|]; [|trace.(x1)|]; [|trace.(x2)|]|] in diff --git a/src/lib_plompiler/solver.mli b/src/lib_plompiler/solver.mli index 208c170ae4d1..a6ff797d78ad 100644 --- a/src/lib_plompiler/solver.mli +++ b/src/lib_plompiler/solver.mli @@ -89,7 +89,7 @@ type pos128full_desc = { x2 : int; y2 : int; k : S.t array; - variant : Variants.t; + matrix : matrix; } type pos128partial_desc = { @@ -106,7 +106,7 @@ type pos128partial_desc = { x2 : int; y2 : int; k_cols : matrix array; - variant : Variants.t; + matrix : matrix; } type anemoi_desc = { diff --git a/src/lib_plompiler/variants.ml b/src/lib_plompiler/variants.ml deleted file mode 100644 index 86db06a66ba0..000000000000 --- a/src/lib_plompiler/variants.ml +++ /dev/null @@ -1,26 +0,0 @@ -(*****************************************************************************) -(* *) -(* MIT License *) -(* Copyright (c) 2022 Nomadic Labs *) -(* *) -(* 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. *) -(* *) -(*****************************************************************************) - -type t = P128 | P252 | PFull128 [@@deriving repr] -- GitLab