Skip to content

Commit

Permalink
https://github.com/ethereum/consensus-specs/pull/3600
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec committed Feb 16, 2024
1 parent 7b58153 commit 673cc05
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion beacon_chain/spec/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ func shortLog*(v: FinalityCheckpoints): auto =

chronicles.formatIt FinalityCheckpoints: it.shortLog

# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#integer_squareroot
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/phase0/beacon-chain.md#integer_squareroot
func integer_squareroot*(n: SomeInteger): SomeInteger =
## Return the largest integer ``x`` such that ``x**2 <= n``.
doAssert n >= 0'u64

if n == high(uint64):
return 4294967295'u64

var
x = n
y = (x + 1) div 2
Expand Down
7 changes: 6 additions & 1 deletion tests/test_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ import
suite "Spec helpers":
test "integer_squareroot":
check:
integer_squareroot(0'u64) == 0'u64
integer_squareroot(1'u64) == 1'u64
integer_squareroot(2'u64) == 1'u64
integer_squareroot(3'u64) == 1'u64
integer_squareroot(4'u64) == 2'u64
integer_squareroot(5'u64) == 2'u64

# https://github.com/ethereum/consensus-specs/pull/3600
integer_squareroot(0'u64) == 0'u64
integer_squareroot(100'u64) == 10'u64
integer_squareroot(18446744073709551614'u64) == 4294967295'u64
integer_squareroot(18446744073709551615'u64) == 4294967295'u64

test "build_proof - BeaconState":
var
forked = newClone(initGenesisState())
Expand Down

0 comments on commit 673cc05

Please sign in to comment.