Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle integer_squareroot bound case #3600

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions specs/phase0/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ The following values are (non-configurable) constants used throughout the specif

| Name | Value |
| - | - |
| `MAX_UINT_64` | `uint64(2**64 - 1)` |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nit-picky, but I think this would be better as UINT64_MAX. Where the second _ is removed and MAX is moved to the end. This aligns more with how other libraries do things:

Suggested change
| `MAX_UINT_64` | `uint64(2**64 - 1)` |
| `UINT64_MAX` | `uint64(2**64 - 1)` |

| `GENESIS_SLOT` | `Slot(0)` |
| `GENESIS_EPOCH` | `Epoch(0)` |
| `FAR_FUTURE_EPOCH` | `Epoch(2**64 - 1)` |
Expand Down Expand Up @@ -599,6 +600,8 @@ def integer_squareroot(n: uint64) -> uint64:
"""
Return the largest integer ``x`` such that ``x**2 <= n``.
"""
if n == MAX_UINT_64:
return uint64(4294967295)
x = n
y = (x + 1) // 2
while y < x:
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import random
from math import isqrt
from eth2spec.test.context import (
spec_test,
single_phase,
with_all_phases,
)


@with_all_phases
@spec_test
@single_phase
def test_integer_squareroot(spec):
values = [0, 100, 2**64 - 2, 2**64 - 1]
for n in values:
uint64_n = spec.uint64(n)
assert spec.integer_squareroot(uint64_n) == isqrt(n)

rng = random.Random(5566)
for _ in range(10):
n = rng.randint(0, 2**64 - 1)
uint64_n = spec.uint64(n)
assert spec.integer_squareroot(uint64_n) == isqrt(n)

try:
spec.integer_squareroot(spec.uint64(2**64))
assert False
except ValueError:
pass
Loading