Skip to content

Commit

Permalink
Make lagrange_poly() unit tests more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Nov 10, 2022
1 parent 8b8a40e commit 7e8a688
Show file tree
Hide file tree
Showing 19 changed files with 38 additions and 40 deletions.
19 changes: 19 additions & 0 deletions scripts/generate_field_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,25 @@ def make_luts(field, sub_folder, seed, sparse=False):
d = {"X": X, "Y": Y, "Z": Z}
save_pickle(d, folder, "crt.pkl")

set_seed(seed + 406)
X = [0,]*3 # The x values
Y = [0,]*3 # The y values
Z = [0,]*3 # The lagrange polynomial
for i in range(3):
N = min(10, order)
if dtype == object:
x = randint_matrix(0, order, (N,))
else:
x = np.array(random.sample(range(0, order), N), dtype=dtype)
y = randint_matrix(0, order, (N,))
points = [(F(x[i]), F(y[i])) for i in range(N)]
z = RING.lagrange_polynomial(points)
X[i] = x
Y[i] = y
Z[i] = poly_to_list(z)
d = {"X": X, "Y": Y, "Z": Z}
save_pickle(d, folder, "lagrange_poly.pkl")

###############################################################################
# Special polynomials
###############################################################################
Expand Down
10 changes: 10 additions & 0 deletions tests/polys/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ def poly_modular_power(field_folder):
return d


@pytest.fixture(scope="session")
def poly_lagrange_poly(field_folder):
GF, d = read_pickle(field_folder, "lagrange_poly.pkl")
d["GF"] = GF
d["X"] = [GF(X) for X in d["X"]]
d["Y"] = [GF(Y) for Y in d["Y"]]
d["Z"] = [galois.Poly(p, field=GF) for p in d["Z"]]
return d


###############################################################################
# Fixtures for special polynomials
###############################################################################
Expand Down
Binary file added tests/polys/data/GF(109987^4)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2147483647)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^100)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^2)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^3)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^32)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^8)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^8, 283, 19)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(31)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(3191)/lagrange_poly.pkl
Binary file not shown.
Binary file not shown.
Binary file added tests/polys/data/GF(5)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(7)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(7^3)/lagrange_poly.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(7^3, 643, 244)/lagrange_poly.pkl
Binary file not shown.
49 changes: 9 additions & 40 deletions tests/polys/test_lagrange_poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,12 @@ def test_exceptions():
galois.lagrange_poly(GF([0, 1, 2, 0]), y)


def test_binary_field():
GF = galois.GF(2)
x = GF([0, 1])
y = GF([0, 0])
assert galois.lagrange_poly(x, y) == galois.Poly.Str("0", field=GF)

GF = galois.GF(2)
x = GF([0, 1])
y = GF([0, 1])
assert galois.lagrange_poly(x, y) == galois.Poly.Str("x", field=GF)

GF = galois.GF(2)
x = GF([0, 1])
y = GF([1, 0])
assert galois.lagrange_poly(x, y) == galois.Poly.Str("x + 1", field=GF)

x = GF([0, 1])
y = GF([1, 1])
assert galois.lagrange_poly(x, y) == galois.Poly.Str("1", field=GF)


def test_prime_field():
GF = galois.GF(251)
x = GF([10, 117, 142, 120, 163, 13, 37, 67, 135, 55])
y = GF([197, 191, 31, 24, 65, 222, 224, 58, 47, 178])
assert galois.lagrange_poly(x, y) == galois.Poly.Str("207*x^9 + 222*x^8 + 227*x^7 + 88*x^6 + 6*x^5 + 52*x^4 + 82*x^3 + 198*x^2 + 27*x + 78", field=GF)


def test_binary_extension_field():
GF = galois.GF(2**8)
x = GF([141, 144, 248, 59, 208, 32, 254, 46, 230, 15])
y = GF([250, 27, 81, 177, 62, 208, 221, 76, 182, 6])
assert galois.lagrange_poly(x, y) == galois.Poly.Str("104*x^9 + 130*x^8 + 92*x^7 + 199*x^6 + 64*x^5 + 211*x^4 + 130*x^3 + 38*x^2 + 140*x + 114", field=GF)


def test_prime_extension_field():
GF = galois.GF(3**5)
x = GF([114, 151, 235, 198, 129, 192, 73, 184, 186, 78])
y = GF([152, 50, 232, 129, 212, 226, 152, 26, 148, 239])
assert galois.lagrange_poly(x, y) == galois.Poly.Str("42*x^9 + 200*x^8 + 109*x^7 + 82*x^6 + x^5 + 241*x^4 + 65*x^3 + 153*x^2 + 143*x + 88", field=GF)
def test_lagrange_poly(poly_lagrange_poly):
GF, X, Y, Z = poly_lagrange_poly["GF"], poly_lagrange_poly["X"], poly_lagrange_poly["Y"], poly_lagrange_poly["Z"]
for i in range(len(X)):
x = X[i]
y = Y[i]
poly = galois.lagrange_poly(x, y)
assert type(poly) == galois.Poly
assert poly.field == GF
assert poly == Z[i]

0 comments on commit 7e8a688

Please sign in to comment.