diff --git a/src/lib_crypto_dal/trap.ml b/src/lib_crypto_dal/trap.ml index e16df6d9c819da4071e717fda5690cd5fde6c9d0..08e06a797dc2c0cb740eaadff44000600dcf9ad9 100644 --- a/src/lib_crypto_dal/trap.ml +++ b/src/lib_crypto_dal/trap.ml @@ -5,42 +5,26 @@ (* *) (*****************************************************************************) -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) - -(* 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 = - Dal_share_hash.(hash_bytes [pkh_bytes; share_bytes] |> to_bytes) - |> 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 06ce7207b3fbddc9ba3b6adae9e7c4ed825d7e9a..952faedb25acc2448df277577ae7da92188951da 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 ->