Skip to content

Commit

Permalink
Add unit tests for galois.prod() with polynomials
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Feb 10, 2022
1 parent a5d9dd2 commit 5344660
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 1 deletion.
15 changes: 14 additions & 1 deletion scripts/generate_field_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import sage
import numpy as np
from sage.all import GF, PolynomialRing, log, matrix, vector, xgcd, lcm
from sage.all import GF, PolynomialRing, log, matrix, vector, xgcd, lcm, prod

FIELD = None
RING = None
Expand Down Expand Up @@ -684,6 +684,19 @@ def make_luts(field, sub_folder, seed, sparse=False):
d = {"X": X, "Z": Z}
save_pickle(d, folder, "lcm.pkl")

set_seed(seed + 403)
X = []
Z = []
for i in range(20):
XX = [random_coeffs(0, order, MIN_COEFFS, MAX_COEFFS) for i in range(random.randint(2, 5))]
X.append(XX)
xx = [list_to_poly(XXi) for XXi in XX]
z = prod(xx)
z = poly_to_list(z)
Z.append(z)
d = {"X": X, "Z": Z}
save_pickle(d, folder, "prod.pkl")


if __name__ == "__main__":
field = GF(2, modulus="primitive", repr="int")
Expand Down
13 changes: 13 additions & 0 deletions tests/polys/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,16 @@ def poly_lcm(field_folder):
d["X"] = [[galois.Poly(p, field=GF) for p in X] for X in d["X"]]
d["Z"] = [galois.Poly(p, field=GF) for p in d["Z"]]
return d


@pytest.fixture(scope="session")
def poly_prod(field_folder):
GF, folder = field_folder
folder = convert_folder(folder)
with open(os.path.join(folder, "prod.pkl"), "rb") as f:
print(f"Loading {f}...")
d = pickle.load(f)
d["GF"] = GF
d["X"] = [[galois.Poly(p, field=GF) for p in X] for X in d["X"]]
d["Z"] = [galois.Poly(p, field=GF) for p in d["Z"]]
return d
Binary file added tests/polys/data/GF(109987^4)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2147483647)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^100)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^2)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^3)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^32)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^8)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^8, 283, 19)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(31)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(3191)/prod.pkl
Binary file not shown.
Binary file not shown.
Binary file added tests/polys/data/GF(5)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(7)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(7^3)/prod.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(7^3, 643, 244)/prod.pkl
Binary file not shown.
18 changes: 18 additions & 0 deletions tests/polys/test_arithmetic_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,21 @@ def test_lcm(poly_lcm):

assert z == Z[i]
assert isinstance(z, galois.Poly)


def test_prod_exceptions():
with pytest.raises(ValueError):
a = galois.Poly.Random(5)
b = galois.Poly.Random(4, field=galois.GF(3))
c = galois.Poly.Random(3)
galois.prod(a, b, c)


def test_prod(poly_prod):
GF, X, Z = poly_prod["GF"], poly_prod["X"], poly_prod["Z"]
for i in range(len(X)):
x = X[i]
z = galois.prod(*x)

assert z == Z[i]
assert isinstance(z, galois.Poly)

0 comments on commit 5344660

Please sign in to comment.