Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve generic polynomial coverage #63

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/kyber_py/polynomials/polynomials_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def __sub__(self, other):
return self.parent(new_coeffs)

def __rsub__(self, other):
return self.__sub__(other)
return -self.__sub__(other)

def __isub__(self, other):
self = self - other
Expand Down
67 changes: 67 additions & 0 deletions tests/test_polynomial_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,27 @@ def test_random_element(self):
self.assertEqual(len(f.coeffs), self.R.n)
self.assertTrue(all([c < self.R.q for c in f.coeffs]))

def test_non_list_error(self):
self.assertRaises(TypeError, lambda: self.R("1"))

def test_long_list_error(self):
self.assertRaises(ValueError, lambda: self.R([0] * (self.R.n + 1)))

def test_string_format(self):
self.assertEqual(
str(self.R),
"Univariate Polynomial Ring in x over Finite Field of size 11 with modulus x^5 + 1",
)


class TestPolynomial(unittest.TestCase):
R = PolynomialRing(11, 5)

def test_getitem(self):
x = self.R.gen()
self.assertEqual(x[0], 0)
self.assertEqual(x[1], 1)

def test_is_zero(self):
self.assertTrue(self.R(0).is_zero())
self.assertFalse(self.R(1).is_zero())
Expand All @@ -38,6 +55,32 @@ def test_reduce_coefficients(self):
f = self.R(coeffs).reduce_coefficients()
self.assertTrue(all([c < self.R.q for c in f.coeffs]))

def test_equality(self):
for _ in range(100):
f1 = self.R.random_element()
f2 = -f1
self.assertEqual(f1, f1)
GiacomoPope marked this conversation as resolved.
Show resolved Hide resolved
if f1.is_zero():
self.assertTrue(f1 == f2)
else:
self.assertFalse(f1 == f2)

self.assertTrue(self.R(0) == 0)
self.assertTrue(self.R(1) == self.R.q + 1)
self.assertTrue(self.R(self.R.q - 1) == -1)

def test_add_failure(self):
f1 = self.R.random_element()
self.assertRaises(NotImplementedError, lambda: f1 + "a")

def test_sub_failure(self):
f1 = self.R.random_element()
self.assertRaises(NotImplementedError, lambda: f1 - "a")

def test_pow_failure(self):
f1 = self.R.random_element()
self.assertRaises(TypeError, lambda: f1 ** "a")

def test_add_polynomials(self):
zero = self.R(0)
for _ in range(100):
Expand All @@ -49,6 +92,10 @@ def test_add_polynomials(self):
self.assertEqual(f1 + f2, f2 + f1)
self.assertEqual(f1 + (f2 + f3), (f1 + f2) + f3)

f2 = f1
f2 += zero
self.assertEqual(f1, f2)

def test_sub_polynomials(self):
zero = self.R(0)
for _ in range(100):
Expand All @@ -58,9 +105,15 @@ def test_sub_polynomials(self):

self.assertEqual(f1 - zero, f1)
self.assertEqual(f3 - f3, zero)
self.assertEqual(f3 - 0, f3)
self.assertEqual(0 - f3, -f3)
self.assertEqual(f1 - f2, -(f2 - f1))
self.assertEqual(f1 - (f2 - f3), (f1 - f2) + f3)

f2 = f1
f2 -= zero
self.assertEqual(f1, f2)

def test_mul_polynomials(self):
zero = self.R(0)
one = self.R(1)
Expand All @@ -73,6 +126,12 @@ def test_mul_polynomials(self):
self.assertEqual(f1 * one, f1)
self.assertEqual(f1 * f2, f2 * f1)
self.assertEqual(f1 * (f2 * f3), (f1 * f2) * f3)
self.assertEqual(2 * f1, f1 + f1)
self.assertEqual(2 * f1, f1 * 2)

f2 = f1
f2 *= one
self.assertEqual(f1, f2)

def test_pow_polynomials(self):
one = self.R(1)
Expand All @@ -85,6 +144,14 @@ def test_pow_polynomials(self):
self.assertEqual(f1 * f1 * f1, f1**3)
self.assertRaises(ValueError, lambda: f1 ** (-1))

def test_print(self):
self.assertEqual(str(self.R(0)), "0")
self.assertEqual(str(self.R(1)), "1")
self.assertEqual(str(self.R.gen()), "x")
self.assertEqual(
str(self.R([1, 2, 3, 4, 5])), "1 + 2*x + 3*x^2 + 4*x^3 + 5*x^4"
)


if __name__ == "__main__":
unittest.main()