Skip to content

Commit

Permalink
Add more unit tests for integer square root
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Feb 10, 2022
1 parent f477eae commit f29b0e8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
12 changes: 11 additions & 1 deletion scripts/generate_int_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import sage
import numpy as np
from sage.all import xgcd, lcm, prod
from sage.all import xgcd, lcm, prod, isqrt

PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "tests")
FOLDER = os.path.join(PATH, "data")
Expand Down Expand Up @@ -86,3 +86,13 @@ def save_pickle(d, folder, name):
Z[i] = int(z)
d = {"X": X, "E": E, "M": M, "Z": Z}
save_pickle(d, FOLDER, "power.pkl")

set_seed(SEED + 105)
X = [random.randint(0, 1000) for _ in range(20)] + [random.randint(1000, 1_000_000_000) for _ in range(20)]
Z = [0,]*len(X)
for i in range(len(X)):
x = X[i]
z = isqrt(x)
Z[i] = int(z)
d = {"X": X, "Z": Z}
save_pickle(d, FOLDER, "isqrt.pkl")
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ def power():
print(f"Loading {f}...")
d = pickle.load(f)
return d


@pytest.fixture(scope="session")
def isqrt():
with open(os.path.join(FOLDER, "isqrt.pkl"), "rb") as f:
print(f"Loading {f}...")
d = pickle.load(f)
return d
Binary file added tests/data/isqrt.pkl
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,16 @@ def test_pow(power):
X, E, M, Z = power["X"], power["E"], power["M"], power["Z"]
for i in range(len(X)):
assert galois.pow(X[i], E[i], M[i]) == Z[i]


def test_isqrt_exceptions():
with pytest.raises(TypeError):
galois.isqrt(3.0)
with pytest.raises(ValueError):
galois.isqrt(-3)


def test_isqrt(isqrt):
X, Z = isqrt["X"], isqrt["Z"]
for i in range(len(X)):
assert galois.isqrt(X[i]) == Z[i]
21 changes: 0 additions & 21 deletions tests/test_number_theory.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,27 +209,6 @@ def test_is_cyclic():
# Integer arithmetic
###############################################################################

def test_isqrt_exceptions():
with pytest.raises(TypeError):
galois.isqrt(3.0)
with pytest.raises(ValueError):
galois.isqrt(-3)


def test_isqrt():
"""
Sage:
N = 20
n = [randint(0, 1_000_000) for _ in range(N)]
lut = [(ni, isqrt(ni)) for ni in n]
print(lut)
"""
LUT = [(681987, 825), (533875, 730), (743346, 862), (966298, 983), (983657, 991), (208532, 456), (658520, 811), (735666, 857), (155024, 393), (470463, 685), (71083, 266), (706821, 840), (628141, 792), (45582, 213), (460761, 678), (511644, 715), (719018, 847), (596428, 772), (821551, 906), (27234, 165)]
for item in LUT:
n, x = item
assert galois.isqrt(n) == x


def test_iroot_exceptions():
with pytest.raises(TypeError):
galois.iroot(9.0, 3)
Expand Down

0 comments on commit f29b0e8

Please sign in to comment.