Skip to content

Commit

Permalink
fixed a bug that could have crashed when initing polring with specifi…
Browse files Browse the repository at this point in the history
…c coefficients
  • Loading branch information
PyryL committed Dec 18, 2023
1 parent 9546c7f commit d363224
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kyber/entities/polring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions tests/test_polring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d363224

Please sign in to comment.