From 731795aaa95dd54c5b9bea0aa9bd126a4b3b6faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 30 Nov 2023 12:46:00 -0300 Subject: [PATCH] Add `SszEx.hash` function --- .../state_transition/accessors.ex | 7 ++++--- lib/lambda_ethereum_consensus/state_transition/misc.ex | 7 ++++--- .../state_transition/operations.ex | 5 +++-- .../state_transition/predicates.ex | 5 +++-- lib/ssz_ex.ex | 5 +++++ 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/lambda_ethereum_consensus/state_transition/accessors.ex b/lib/lambda_ethereum_consensus/state_transition/accessors.ex index 465385c61..b2f2d04f5 100644 --- a/lib/lambda_ethereum_consensus/state_transition/accessors.ex +++ b/lib/lambda_ethereum_consensus/state_transition/accessors.ex @@ -3,6 +3,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do Functions accessing the current `BeaconState` """ + alias LambdaEthereumConsensus.SszEx alias LambdaEthereumConsensus.StateTransition.{Math, Misc, Predicates} alias SszTypes.{Attestation, BeaconState, IndexedAttestation, SyncCommittee, Validator} @@ -86,7 +87,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do candidate_index = active_validator_indices |> Enum.fetch!(shuffled_index) <<_::binary-size(rem(index, 32)), random_byte, _::binary>> = - :crypto.hash(:sha256, seed <> Misc.uint64_to_bytes(div(index, 32))) + SszEx.hash(seed <> Misc.uint64_to_bytes(div(index, 32))) effective_balance = Enum.fetch!(validators, candidate_index).effective_balance @@ -240,7 +241,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do state |> get_seed(epoch, Constants.domain_beacon_proposer()) - |> then(&:crypto.hash(:sha256, &1 <> Misc.uint64_to_bytes(state.slot))) + |> then(&SszEx.hash(&1 <> Misc.uint64_to_bytes(state.slot))) |> then(&Misc.compute_proposer_index(state, indices, &1)) end @@ -410,7 +411,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do ChainSpec.get("MIN_SEED_LOOKAHEAD") - 1 ) - :crypto.hash(:sha256, domain_type <> Misc.uint64_to_bytes(epoch) <> mix) + SszEx.hash(domain_type <> Misc.uint64_to_bytes(epoch) <> mix) end @doc """ diff --git a/lib/lambda_ethereum_consensus/state_transition/misc.ex b/lib/lambda_ethereum_consensus/state_transition/misc.ex index 0cade1233..dd029d20e 100644 --- a/lib/lambda_ethereum_consensus/state_transition/misc.ex +++ b/lib/lambda_ethereum_consensus/state_transition/misc.ex @@ -3,6 +3,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Misc do Misc functions """ + alias LambdaEthereumConsensus.SszEx alias SszTypes.BeaconState import Bitwise alias SszTypes.BeaconState @@ -55,7 +56,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Misc do Enum.reduce(0..(shuffle_round_count - 1), index, fn round, current_index -> round_as_bytes = <> - hash_of_seed_round = :crypto.hash(:sha256, seed <> round_as_bytes) + hash_of_seed_round = SszEx.hash(seed <> round_as_bytes) pivot = rem(bytes_to_uint64(hash_of_seed_round), index_count) @@ -65,7 +66,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Misc do position_div_256 = uint_to_bytes4(div(position, 256)) source = - :crypto.hash(:sha256, seed <> round_as_bytes <> position_div_256) + SszEx.hash(seed <> round_as_bytes <> position_div_256) byte_index = div(rem(position, 256), 8) <<_::binary-size(byte_index), byte, _::binary>> = source @@ -146,7 +147,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Misc do total = length(indices) {:ok, i} = compute_shuffled_index(rem(i, total), total, seed) candidate_index = Enum.at(indices, i) - random_byte = :crypto.hash(:sha256, seed <> uint_to_bytes4(div(i, 32))) + random_byte = SszEx.hash(seed <> uint_to_bytes4(div(i, 32))) <<_::binary-size(rem(i, 32)), byte, _::binary>> = random_byte effective_balance = Enum.at(state.validators, candidate_index).effective_balance diff --git a/lib/lambda_ethereum_consensus/state_transition/operations.ex b/lib/lambda_ethereum_consensus/state_transition/operations.ex index 3beec47ef..3d0a0766d 100644 --- a/lib/lambda_ethereum_consensus/state_transition/operations.ex +++ b/lib/lambda_ethereum_consensus/state_transition/operations.ex @@ -3,6 +3,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Operations do This module contains functions for handling state transition """ + alias LambdaEthereumConsensus.SszEx alias LambdaEthereumConsensus.StateTransition.{Accessors, Math, Misc, Mutators, Predicates} alias LambdaEthereumConsensus.Utils.BitVector @@ -782,7 +783,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Operations do if Bls.valid?(proposer.pubkey, signing_root, randao_reveal) do randao_mix = Accessors.get_randao_mix(state, epoch) - hash = :crypto.hash(:sha256, randao_reveal) + hash = SszEx.hash(randao_reveal) # Mix in RANDAO reveal mix = :crypto.exor(randao_mix, hash) @@ -963,7 +964,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Operations do defp validate_withdrawal_credentials(validator, address_change) do <> = validator.withdrawal_credentials - <<_, hash::binary-size(31)>> = :crypto.hash(:sha256, address_change.from_bls_pubkey) + <<_, hash::binary-size(31)>> = SszEx.hash(address_change.from_bls_pubkey) if prefix == Constants.bls_withdrawal_prefix() and address == hash do {:ok} diff --git a/lib/lambda_ethereum_consensus/state_transition/predicates.ex b/lib/lambda_ethereum_consensus/state_transition/predicates.ex index 3a38ff0db..73ffe288e 100644 --- a/lib/lambda_ethereum_consensus/state_transition/predicates.ex +++ b/lib/lambda_ethereum_consensus/state_transition/predicates.ex @@ -3,6 +3,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Predicates do Range of predicates enabling verification of state """ + alias LambdaEthereumConsensus.SszEx alias LambdaEthereumConsensus.StateTransition.{Accessors, Misc} alias SszTypes.BeaconState alias SszTypes.Validator @@ -131,9 +132,9 @@ defmodule LambdaEthereumConsensus.StateTransition.Predicates do defp hash_merkle_node(value_1, value_2, index, i) do if rem(div(index, 2 ** i), 2) == 1 do - :crypto.hash(:sha256, value_1 <> value_2) + SszEx.hash(value_1 <> value_2) else - :crypto.hash(:sha256, value_2 <> value_1) + SszEx.hash(value_2 <> value_1) end end diff --git a/lib/ssz_ex.ex b/lib/ssz_ex.ex index 85acb55eb..e46db263c 100644 --- a/lib/ssz_ex.ex +++ b/lib/ssz_ex.ex @@ -2,9 +2,14 @@ defmodule LambdaEthereumConsensus.SszEx do @moduledoc """ SSZ library in Elixir """ + ################# ### Public API ################# + + @spec hash(iodata()) :: binary() + def hash(data), do: :crypto.hash(:sha256, data) + def encode(value, {:int, size}), do: encode_int(value, size) def encode(value, :bool), do: encode_bool(value)