Skip to content

Commit

Permalink
Add more multiplicative_order() unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Feb 10, 2022
1 parent a00e496 commit b6abfc2
Show file tree
Hide file tree
Showing 21 changed files with 38 additions and 148 deletions.
2 changes: 1 addition & 1 deletion galois/_fields/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,7 @@ def multiplicative_order(self) -> Union[np.integer, np.ndarray]:
else:
d = np.array(divisors(field.order - 1)) # Divisors d such that d | p^m - 1
y = np.power.outer(x, d) # x^d -- the first divisor d for which x^d == 1 is the order of x
idxs = np.argmin(np.abs(y.view(np.ndarray) - 1), axis=-1) # First index of divisors, which is the order of x
idxs = np.argmin(y, axis=-1) # First index of divisors, which is the order of x
order = d[idxs] # The order of each element of x

return order
Expand Down
7 changes: 7 additions & 0 deletions scripts/generate_field_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ def make_luts(field, sub_folder, seed, sparse=False):
d = {"X": X, "Z": Z}
save_pickle(d, folder, "additive_order.pkl")

set_seed(seed + 11)
X, Z = io_1d(1, order, sparse=sparse)
for i in range(X.shape[0]):
Z[i] = int(F(X[i]).multiplicative_order())
d = {"X": X, "Z": Z}
save_pickle(d, folder, "multiplicative_order.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 @@ -279,3 +279,15 @@ def field_additive_order(field_folder):
d["X"] = GF(d["X"])
d["Z"] = d["Z"]
return d


@pytest.fixture(scope="session")
def field_multiplicative_order(field_folder):
GF, folder = field_folder
with open(os.path.join(folder, "multiplicative_order.pkl"), "rb") as f:
print(f"Loading {f}...")
d = pickle.load(f)
d["GF"] = GF
d["X"] = GF(d["X"])
d["Z"] = d["Z"]
return d
Binary file not shown.
Binary file added tests/fields/data/GF(2)/multiplicative_order.pkl
Binary file not shown.
Binary file not shown.
Binary file added tests/fields/data/GF(2^100)/multiplicative_order.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^2)/multiplicative_order.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^3)/multiplicative_order.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^32)/multiplicative_order.pkl
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/fields/data/GF(31)/multiplicative_order.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(3191)/multiplicative_order.pkl
Binary file not shown.
Binary file not shown.
Binary file added tests/fields/data/GF(5)/multiplicative_order.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(7)/multiplicative_order.pkl
Binary file not shown.
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions tests/fields/test_arithmetic_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ def test_additive_order(field_additive_order):
z = x.additive_order()
assert np.array_equal(z, Z)
assert type(z) is np.ndarray


def test_multiplicative_order(field_multiplicative_order):
GF, X, Z = field_multiplicative_order["GF"], field_multiplicative_order["X"], field_multiplicative_order["Z"]
if GF.dtypes[-1] == np.object_:
# FIXME: Skipping large fields because they're too slow
return

dtype = random.choice(GF.dtypes)
x = X.astype(dtype)
z = x.multiplicative_order()
assert np.array_equal(z, Z)
assert type(z) is np.ndarray

with pytest.raises(ArithmeticError):
GF(0).multiplicative_order()
with pytest.raises(ArithmeticError):
GF.Range(0, 2).multiplicative_order()
147 changes: 0 additions & 147 deletions tests/fields/test_multiplicative_order.py

This file was deleted.

0 comments on commit b6abfc2

Please sign in to comment.