tersec 2024-02-17 09:02:50 +00:00 committed by GitHub
parent 7b5815358e
commit e410fe0052
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 2 deletions

View File

@ -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

View File

@ -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())