Skip to content

Commit f82a3af

Browse files
authored
Merge pull request #3600 from ethereum/integer_squareroot
Handle `integer_squareroot` bound case
2 parents 2faa44b + e3d91d8 commit f82a3af

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

specs/phase0/beacon-chain.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ The following values are (non-configurable) constants used throughout the specif
178178

179179
| Name | Value |
180180
| - | - |
181+
| `UINT64_MAX` | `uint64(2**64 - 1)` |
181182
| `GENESIS_SLOT` | `Slot(0)` |
182183
| `GENESIS_EPOCH` | `Epoch(0)` |
183184
| `FAR_FUTURE_EPOCH` | `Epoch(2**64 - 1)` |
@@ -599,6 +600,8 @@ def integer_squareroot(n: uint64) -> uint64:
599600
"""
600601
Return the largest integer ``x`` such that ``x**2 <= n``.
601602
"""
603+
if n == UINT64_MAX:
604+
return uint64(4294967295)
602605
x = n
603606
y = (x + 1) // 2
604607
while y < x:

tests/core/pyspec/eth2spec/test/helpers/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@
8989
#
9090
# Number
9191
#
92-
MAX_UINT_64 = 2**64 - 1
92+
UINT64_MAX = 2**64 - 1

tests/core/pyspec/eth2spec/test/phase0/unittests/math/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import random
2+
from math import isqrt
3+
from eth2spec.test.context import (
4+
spec_test,
5+
single_phase,
6+
with_all_phases,
7+
)
8+
9+
10+
@with_all_phases
11+
@spec_test
12+
@single_phase
13+
def test_integer_squareroot(spec):
14+
values = [0, 100, 2**64 - 2, 2**64 - 1]
15+
for n in values:
16+
uint64_n = spec.uint64(n)
17+
assert spec.integer_squareroot(uint64_n) == isqrt(n)
18+
19+
rng = random.Random(5566)
20+
for _ in range(10):
21+
n = rng.randint(0, 2**64 - 1)
22+
uint64_n = spec.uint64(n)
23+
assert spec.integer_squareroot(uint64_n) == isqrt(n)
24+
25+
try:
26+
spec.integer_squareroot(spec.uint64(2**64))
27+
assert False
28+
except ValueError:
29+
pass

tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
spec_state_test,
33
with_all_phases,
44
)
5-
from eth2spec.test.helpers.constants import MAX_UINT_64
5+
from eth2spec.test.helpers.constants import UINT64_MAX
66
from eth2spec.test.helpers.forks import (
77
is_post_altair, is_post_bellatrix,
88
)
@@ -16,9 +16,9 @@ def check_bound(value, lower_bound, upper_bound):
1616
@with_all_phases
1717
@spec_state_test
1818
def test_validators(spec, state):
19-
check_bound(spec.VALIDATOR_REGISTRY_LIMIT, 1, MAX_UINT_64)
20-
check_bound(spec.MAX_COMMITTEES_PER_SLOT, 1, MAX_UINT_64)
21-
check_bound(spec.TARGET_COMMITTEE_SIZE, 1, MAX_UINT_64)
19+
check_bound(spec.VALIDATOR_REGISTRY_LIMIT, 1, UINT64_MAX)
20+
check_bound(spec.MAX_COMMITTEES_PER_SLOT, 1, UINT64_MAX)
21+
check_bound(spec.TARGET_COMMITTEE_SIZE, 1, UINT64_MAX)
2222

2323
# Note: can be less if you assume stricters bounds on validator set based on total ETH supply
2424
maximum_validators_per_committee = (
@@ -30,24 +30,24 @@ def test_validators(spec, state):
3030
check_bound(spec.config.MIN_PER_EPOCH_CHURN_LIMIT, 1, spec.VALIDATOR_REGISTRY_LIMIT)
3131
check_bound(spec.config.CHURN_LIMIT_QUOTIENT, 1, spec.VALIDATOR_REGISTRY_LIMIT)
3232

33-
check_bound(spec.config.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, spec.TARGET_COMMITTEE_SIZE, MAX_UINT_64)
33+
check_bound(spec.config.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT, spec.TARGET_COMMITTEE_SIZE, UINT64_MAX)
3434

3535

3636
@with_all_phases
3737
@spec_state_test
3838
def test_balances(spec, state):
3939
assert spec.MAX_EFFECTIVE_BALANCE % spec.EFFECTIVE_BALANCE_INCREMENT == 0
40-
check_bound(spec.MIN_DEPOSIT_AMOUNT, 1, MAX_UINT_64)
41-
check_bound(spec.MAX_EFFECTIVE_BALANCE, spec.MIN_DEPOSIT_AMOUNT, MAX_UINT_64)
42-
check_bound(spec.MAX_EFFECTIVE_BALANCE, spec.EFFECTIVE_BALANCE_INCREMENT, MAX_UINT_64)
40+
check_bound(spec.MIN_DEPOSIT_AMOUNT, 1, UINT64_MAX)
41+
check_bound(spec.MAX_EFFECTIVE_BALANCE, spec.MIN_DEPOSIT_AMOUNT, UINT64_MAX)
42+
check_bound(spec.MAX_EFFECTIVE_BALANCE, spec.EFFECTIVE_BALANCE_INCREMENT, UINT64_MAX)
4343

4444

4545
@with_all_phases
4646
@spec_state_test
4747
def test_hysteresis_quotient(spec, state):
48-
check_bound(spec.HYSTERESIS_QUOTIENT, 1, MAX_UINT_64)
48+
check_bound(spec.HYSTERESIS_QUOTIENT, 1, UINT64_MAX)
4949
check_bound(spec.HYSTERESIS_DOWNWARD_MULTIPLIER, 1, spec.HYSTERESIS_QUOTIENT)
50-
check_bound(spec.HYSTERESIS_UPWARD_MULTIPLIER, spec.HYSTERESIS_QUOTIENT, MAX_UINT_64)
50+
check_bound(spec.HYSTERESIS_UPWARD_MULTIPLIER, spec.HYSTERESIS_QUOTIENT, UINT64_MAX)
5151

5252

5353
@with_all_phases
@@ -68,7 +68,7 @@ def test_time(spec, state):
6868
assert spec.SLOTS_PER_EPOCH <= spec.SLOTS_PER_HISTORICAL_ROOT
6969
assert spec.MIN_SEED_LOOKAHEAD < spec.MAX_SEED_LOOKAHEAD
7070
assert spec.SLOTS_PER_HISTORICAL_ROOT % spec.SLOTS_PER_EPOCH == 0
71-
check_bound(spec.SLOTS_PER_HISTORICAL_ROOT, spec.SLOTS_PER_EPOCH, MAX_UINT_64)
71+
check_bound(spec.SLOTS_PER_HISTORICAL_ROOT, spec.SLOTS_PER_EPOCH, UINT64_MAX)
7272
check_bound(spec.MIN_ATTESTATION_INCLUSION_DELAY, 1, spec.SLOTS_PER_EPOCH)
7373

7474

0 commit comments

Comments
 (0)