From f29b0e8f17179a52a82957a130846158d75c1e01 Mon Sep 17 00:00:00 2001 From: mhostetter Date: Fri, 4 Feb 2022 11:01:12 -0500 Subject: [PATCH] Add more unit tests for integer square root --- scripts/generate_int_test_vectors.py | 12 +++++++++++- tests/conftest.py | 8 ++++++++ tests/data/isqrt.pkl | Bin 0 -> 285 bytes tests/test_math.py | 13 +++++++++++++ tests/test_number_theory.py | 21 --------------------- 5 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 tests/data/isqrt.pkl diff --git a/scripts/generate_int_test_vectors.py b/scripts/generate_int_test_vectors.py index f1199149e..451f0672b 100644 --- a/scripts/generate_int_test_vectors.py +++ b/scripts/generate_int_test_vectors.py @@ -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") @@ -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") diff --git a/tests/conftest.py b/tests/conftest.py index 976fe673f..87ec35bfa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 diff --git a/tests/data/isqrt.pkl b/tests/data/isqrt.pkl new file mode 100644 index 0000000000000000000000000000000000000000..f9ce97e71858ea608eb56fe58bbed8d47bcbdcfd GIT binary patch literal 285 zcmZo*nJUD{00y;FGLs@c!(*%DdioFQa$2x1_HLqpui~uRF8vS|(oyM(=mt z4}1feeX|&SLz#UKG5bm~`o3cHEobub>v|*Swe0p}6)#f(dt9j6zIsIRP7NzpAcub*Ke!l^xsQD6u1d-Hm8dy4`wtGA%HxVMbA6p-fk=JS^I z76xK@Zvk&Hpxb{&_#R2{?JV-0p6L5A#aCa%*SN~JJ=)hgz&9<>H_^&>UYYM|ZC||v a-^4864W_={hQ71Id>hJqAG@ZO>Hz@a7hGci literal 0 HcmV?d00001 diff --git a/tests/test_math.py b/tests/test_math.py index a06763ec7..3107c7a77 100644 --- a/tests/test_math.py +++ b/tests/test_math.py @@ -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] diff --git a/tests/test_number_theory.py b/tests/test_number_theory.py index 421dedec8..07470efbd 100644 --- a/tests/test_number_theory.py +++ b/tests/test_number_theory.py @@ -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)