Skip to content

Commit

Permalink
Fix #378, int to bytes32, LATEST_RANDAO_MIXES, get_seed, get_crosslin…
Browse files Browse the repository at this point in the history
…k_committee, get_compact_committee_root (#380)
  • Loading branch information
mratsim authored and tersec committed Sep 4, 2019
1 parent 2520646 commit ad24095
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 20 deletions.
Binary file removed beacon_chain/spec/crypto
Binary file not shown.
Binary file removed beacon_chain/spec/digest
Binary file not shown.
16 changes: 6 additions & 10 deletions beacon_chain/spec/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# Uncategorized helper functions from the spec

import ./datatypes, ./digest, sequtils, math
import ./datatypes, ./digest, sequtils, math, endians

# https://github.com/ethereum/eth2.0-specs/blob/v0.8.3/specs/core/0_beacon-chain.md#integer_squareroot
func integer_squareroot*(n: SomeInteger): SomeInteger =
Expand Down Expand Up @@ -102,9 +102,7 @@ func get_current_epoch*(state: BeaconState): Epoch =
func get_randao_mix*(state: BeaconState,
epoch: Epoch): Eth2Digest =
## Returns the randao mix at a recent ``epoch``.
## ``epoch`` expected to be between (current_epoch -
## LATEST_RANDAO_MIXES_LENGTH, current_epoch].
state.randao_mixes[epoch mod LATEST_RANDAO_MIXES_LENGTH]
state.randao_mixes[epoch mod EPOCHS_PER_HISTORICAL_VECTOR]

func bytes_to_int*(data: openarray[byte]): uint64 =
doAssert data.len == 8
Expand All @@ -118,14 +116,12 @@ func bytes_to_int*(data: openarray[byte]): uint64 =
func int_to_bytes32*(x: uint64): array[32, byte] =
## Little-endian data representation
## TODO remove uint64 when those callers fade away
for i in 0 ..< 8:
result[24 + i] = byte((x shr i*8) and 0xff)
littleEndian64(result[0].addr, x.unsafeAddr)

func int_to_bytes32*(x: Epoch): array[32, byte] {.borrow.}

func int_to_bytes8*(x: uint64): array[8, byte] =
for i in 0 ..< 8:
result[i] = byte((x shr i*8) and 0xff)
littleEndian64(result[0].addr, x.unsafeAddr)

func int_to_bytes1*(x: int): array[1, byte] =
doAssert x >= 0
Expand Down Expand Up @@ -175,11 +171,11 @@ func get_seed*(state: BeaconState, epoch: Epoch): Eth2Digest =
var seed_input : array[32*3, byte]

# Detect potential underflow
doAssert LATEST_RANDAO_MIXES_LENGTH >= MIN_SEED_LOOKAHEAD
doAssert EPOCHS_PER_HISTORICAL_VECTOR >= MIN_SEED_LOOKAHEAD

seed_input[0..31] =
get_randao_mix(state,
epoch + LATEST_RANDAO_MIXES_LENGTH - MIN_SEED_LOOKAHEAD - 1).data
epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD - 1).data
seed_input[32..63] =
state.active_index_roots[epoch mod EPOCHS_PER_HISTORICAL_VECTOR].data
seed_input[64..95] = int_to_bytes32(epoch)
Expand Down
1 change: 0 additions & 1 deletion beacon_chain/spec/presets/mainnet.nim
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ const
# State list lengths
# ---------------------------------------------------------------
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#state-list-lengths
LATEST_RANDAO_MIXES_LENGTH* = 8192
EPOCHS_PER_HISTORICAL_VECTOR* = 65536
EPOCHS_PER_SLASHINGS_VECTOR* = 8192
HISTORICAL_ROOTS_LIMIT* = 16777216
Expand Down
1 change: 0 additions & 1 deletion beacon_chain/spec/presets/minimal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ const
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#state-list-lengths

# Changed
LATEST_RANDAO_MIXES_LENGTH* = 64
EPOCHS_PER_HISTORICAL_VECTOR* = 64
EPOCHS_PER_SLASHINGS_VECTOR* = 64
HISTORICAL_ROOTS_LIMIT* = 16777216
Expand Down
15 changes: 8 additions & 7 deletions beacon_chain/spec/state_transition_block.nim
Original file line number Diff line number Diff line change
Expand Up @@ -90,35 +90,36 @@ proc processBlockHeader(

true

# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#randao
proc processRandao(
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.3/specs/core/0_beacon-chain.md#randao
proc process_randao(
state: var BeaconState, body: BeaconBlockBody, flags: UpdateFlags,
stateCache: var StateCache): bool =
let
epoch = state.get_current_epoch()
proposer_index = get_beacon_proposer_index(state, stateCache)
proposer = addr state.validators[proposer_index]

# Verify that the provided randao value is valid
if skipValidation notin flags:
if not bls_verify(
proposer.pubkey,
hash_tree_root(get_current_epoch(state).uint64).data,
hash_tree_root(epoch.uint64).data,
body.randao_reveal,
get_domain(state, DOMAIN_RANDAO)):

notice "Randao mismatch", proposer_pubkey = proposer.pubkey,
message = get_current_epoch(state),
message = epoch,
signature = body.randao_reveal,
slot = state.slot
return false

# Mix it in
let
mix = get_current_epoch(state) mod LATEST_RANDAO_MIXES_LENGTH
mix = get_randao_mix(state, epoch)
rr = eth2hash(body.randao_reveal.getBytes()).data

for i, b in state.randao_mixes[mix].data:
state.randao_mixes[mix].data[i] = b xor rr[i]
for i in 0 ..< mix.data.len:
state.randao_mixes[epoch mod EPOCHS_PER_HISTORICAL_VECTOR].data[i] = mix.data[i] xor rr[i]

true

Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/spec/state_transition_epoch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ proc process_final_updates*(state: var BeaconState) =
state.slashings[next_epoch mod EPOCHS_PER_SLASHINGS_VECTOR] = 0.Gwei

# Set randao mix
state.randao_mixes[next_epoch mod LATEST_RANDAO_MIXES_LENGTH] =
state.randao_mixes[next_epoch mod EPOCHS_PER_HISTORICAL_VECTOR] =
get_randao_mix(state, current_epoch)

# Set historical root accumulator
Expand Down

0 comments on commit ad24095

Please sign in to comment.