Skip to content

Commit

Permalink
Add more field_norm() unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Feb 10, 2022
1 parent 95d80ef commit 6f64f2d
Show file tree
Hide file tree
Showing 21 changed files with 39 additions and 139 deletions.
17 changes: 10 additions & 7 deletions galois/_fields/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2021,14 +2021,17 @@ def field_norm(self) -> "FieldArray":
x = GF.Elements(); x
y = x.field_norm(); y
"""
if not type(self).is_extension_field:
raise TypeError(f"The Galois field must be an extension field to compute the field norm, not {type(self)}.")
field = type(self)
subfield = field.prime_subfield
p = field.characteristic
m = field.degree
norm = self**((p**m - 1) // (p - 1))
return subfield(norm)
x = self

if field.is_prime_field:
return x.copy()
else:
subfield = field.prime_subfield
p = field.characteristic
m = field.degree
norm = x**((p**m - 1) // (p - 1))
return subfield(norm)

def characteristic_poly(self) -> "Poly":
r"""
Expand Down
8 changes: 8 additions & 0 deletions scripts/generate_field_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ def make_luts(field, sub_folder, seed, sparse=False):
d = {"X": X, "Z": Z}
save_pickle(d, folder, "field_trace.pkl")

set_seed(seed + 17)
X, Z = io_1d(0, order, sparse=sparse)
for i in range(X.shape[0]):
z = F(X[i]).norm()
Z[i] = int(z)
d = {"X": X, "Z": Z}
save_pickle(d, folder, "field_norm.pkl")

###############################################################################
# Polynomial arithmetic
###############################################################################
Expand Down
12 changes: 12 additions & 0 deletions tests/fields/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,15 @@ def field_trace(field_folder):
d["X"] = GF(d["X"])
d["Z"] = GF.prime_subfield(d["Z"])
return d


@pytest.fixture(scope="session")
def field_norm(field_folder):
GF, folder = field_folder
with open(os.path.join(folder, "field_norm.pkl"), "rb") as f:
print(f"Loading {f}...")
d = pickle.load(f)
d["GF"] = GF
d["X"] = GF(d["X"])
d["Z"] = GF.prime_subfield(d["Z"])
return d
Binary file added tests/fields/data/GF(109987^4)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2147483647)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^100)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^2)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^3)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^32)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^8)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^8, 283, 19)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(31)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(3191)/field_norm.pkl
Binary file not shown.
Binary file not shown.
Binary file added tests/fields/data/GF(5)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(7)/field_norm.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(7^3)/field_norm.pkl
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/fields/test_arithmetic_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ def test_field_trace(field_trace):
z = x.field_trace()
assert np.array_equal(z, Z)
assert type(z) is GF.prime_subfield


def test_field_norm(field_norm):
GF, X, Z = field_norm["GF"], field_norm["X"], field_norm["Z"]
dtype = random.choice(GF.dtypes)
x = X.astype(dtype)
z = x.field_norm()
assert np.array_equal(z, Z)
assert type(z) is GF.prime_subfield
132 changes: 0 additions & 132 deletions tests/fields/test_field_norm.py

This file was deleted.

0 comments on commit 6f64f2d

Please sign in to comment.