diff --git a/kyber/entities/polring.py b/kyber/entities/polring.py index a1c69f2..11ab967 100644 --- a/kyber/entities/polring.py +++ b/kyber/entities/polring.py @@ -34,7 +34,7 @@ def _apply_polynomial_modulo_limit(self) -> None: while coef_count >= n+1: self._coefs[-n-1] -= self._coefs[-1] self._coefs[-1] = 0 - while self._coefs[-1] == 0: + while coef_count > 0 and self._coefs[-1] == 0: self._coefs.pop() coef_count -= 1 diff --git a/tests/test_polring.py b/tests/test_polring.py index 64e04fb..f86d506 100644 --- a/tests/test_polring.py +++ b/tests/test_polring.py @@ -22,6 +22,11 @@ def test_init_with_numpy_array(self): self.assertEqual(type(pol.coefs), list) self.assertEqual(type(pol.coefs[0]), int) + def test_init_with_remainder_zero(self): + # when taken modulo x^n+1, this polynomial should result 0 + pol = PolynomialRing([1] + [0]*255 + [1]) + self.assertEqual(pol.coefs, []) + def test_init_with_random_inputs(self): # test with 1000 samples that randomily initialized polring matches expected seed(42)