Skip to content

Commit

Permalink
Add SszEx.hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed Nov 30, 2023
1 parent ced1cb8 commit 731795a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lib/lambda_ethereum_consensus/state_transition/accessors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 """
Expand Down
7 changes: 4 additions & 3 deletions lib/lambda_ethereum_consensus/state_transition/misc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Misc do
Misc functions
"""

alias LambdaEthereumConsensus.SszEx
alias SszTypes.BeaconState
import Bitwise
alias SszTypes.BeaconState
Expand Down Expand Up @@ -55,7 +56,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Misc do
Enum.reduce(0..(shuffle_round_count - 1), index, fn round, current_index ->
round_as_bytes = <<round>>

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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/lambda_ethereum_consensus/state_transition/operations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -963,7 +964,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Operations do

defp validate_withdrawal_credentials(validator, address_change) do
<<prefix::binary-size(1), address::binary-size(31)>> = 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}
Expand Down
5 changes: 3 additions & 2 deletions lib/lambda_ethereum_consensus/state_transition/predicates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions lib/ssz_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 731795a

Please sign in to comment.