From a7a0e02df31de8092a6bb604a16b82452e911e73 Mon Sep 17 00:00:00 2001 From: Eugen Zalinescu Date: Thu, 12 Dec 2024 12:11:53 +0100 Subject: [PATCH 1/2] DAL/Trap: use Blake2B directly --- src/lib_crypto_dal/trap.ml | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/src/lib_crypto_dal/trap.ml b/src/lib_crypto_dal/trap.ml index e16df6d9c819..8cdb3aab8147 100644 --- a/src/lib_crypto_dal/trap.ml +++ b/src/lib_crypto_dal/trap.ml @@ -5,24 +5,7 @@ (* *) (*****************************************************************************) -module Dal_share_hash = struct - module H = - Tezos_crypto.Blake2B.Make - (Tezos_crypto.Base58) - (struct - let name = "pkh_and_dal_share_hash" - - let title = "A hash of a pkh and a DAL share" - - let b58check_prefix = "\077\167\043" (* shh(53) for "share hash" *) - - let size = Some 32 - end) - - include H -end - -let two_to_hash_size = Z.(shift_left one Dal_share_hash.size) +let two_to_hash_size = Z.(shift_left one Tezos_crypto.Blake2B.size) (* This function checks that `hash(delegate . share) < trap_rate * 2^|hash|`, where the dot denotes concatenation and |v| the length of the bitstring v. *) @@ -37,8 +20,8 @@ let share_is_trap delegate share ~(traps_fraction : Q.t) = Data_encoding.Binary.to_bytes Cryptobox.share_encoding share in let hash = - Dal_share_hash.(hash_bytes [pkh_bytes; share_bytes] |> to_bytes) - |> Bytes.to_string |> Z.of_bits + Tezos_crypto.Blake2B.(hash_bytes [pkh_bytes; share_bytes] |> to_string) + |> Z.of_bits in let threshold = two_to_hash_size |> Q.of_bigint |> Q.mul traps_fraction |> Q.to_bigint -- GitLab From e5c9ddca48448c30b3971664249278eb40eed537 Mon Sep 17 00:00:00 2001 From: Eugen Zalinescu Date: Tue, 17 Dec 2024 15:58:48 +0100 Subject: [PATCH 2/2] DAL/Trap: clean up share_is_trap --- src/lib_crypto_dal/trap.ml | 43 +++++++++++++++++++------------------ src/lib_crypto_dal/trap.mli | 8 +++---- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/lib_crypto_dal/trap.ml b/src/lib_crypto_dal/trap.ml index 8cdb3aab8147..08e06a797dc2 100644 --- a/src/lib_crypto_dal/trap.ml +++ b/src/lib_crypto_dal/trap.ml @@ -5,25 +5,26 @@ (* *) (*****************************************************************************) -let two_to_hash_size = Z.(shift_left one Tezos_crypto.Blake2B.size) - -(* This function checks that `hash(delegate . share) < trap_rate * 2^|hash|`, +(* [share_is_trap pkh share ~traps_fraction] checks that + `hash(pkh . share) < trap_rate * 2^hash_size`, where the dot denotes concatenation and |v| the length of the bitstring v. *) -let share_is_trap delegate share ~(traps_fraction : Q.t) = - let open Error_monad.Result_syntax in - let* pkh_bytes = - Data_encoding.Binary.to_bytes - Tezos_crypto.Signature.Public_key_hash.encoding - delegate - in - let+ share_bytes = - Data_encoding.Binary.to_bytes Cryptobox.share_encoding share - in - let hash = - Tezos_crypto.Blake2B.(hash_bytes [pkh_bytes; share_bytes] |> to_string) - |> Z.of_bits - in - let threshold = - two_to_hash_size |> Q.of_bigint |> Q.mul traps_fraction |> Q.to_bigint - in - Z.leq hash threshold +let share_is_trap = + let two_to_hash_size = Z.(shift_left one Tezos_crypto.Blake2B.size) in + fun pkh share ~traps_fraction -> + let open Error_monad.Result_syntax in + let* pkh_bytes = + Data_encoding.Binary.to_bytes + Tezos_crypto.Signature.Public_key_hash.encoding + pkh + in + let+ share_bytes = + Data_encoding.Binary.to_bytes Cryptobox.share_encoding share + in + let hash = + Tezos_crypto.Blake2B.(hash_bytes [pkh_bytes; share_bytes] |> to_string) + |> Z.of_bits + in + let threshold = + two_to_hash_size |> Q.of_bigint |> Q.mul traps_fraction |> Q.to_bigint + in + Z.leq hash threshold diff --git a/src/lib_crypto_dal/trap.mli b/src/lib_crypto_dal/trap.mli index 06ce7207b3fb..952faedb25ac 100644 --- a/src/lib_crypto_dal/trap.mli +++ b/src/lib_crypto_dal/trap.mli @@ -10,8 +10,8 @@ based on the fraction [traps_fraction] of shards that should be traps. The function computes the hash of the concatenation of [pkh] and [share], - denoted as `hash(pkh . share)`, where the dot represents concatenation. - It then checks if this hash value is less than `trap_rate * 2^n`, where + denoted as `hash(pkh . share)`, where the dot represents concatenation. It + then checks if this hash value is less than `traps_fraction * 2^n`, where `n` is the (fixed) bit size of the hash. The function returns: @@ -22,8 +22,8 @@ - [Error write_error] if there is an issue encoding [pkh] or [share]. - This function assumes [trap_rate] is valid (i.e., a rational number within - [0, 1]). + This function assumes [traps_fraction] is valid (i.e., a rational number + within [0, 1]). *) val share_is_trap : Tezos_crypto.Signature.Public_key_hash.t -> -- GitLab