Skip to content

Commit

Permalink
test(mle2): improve and expand unit tests
Browse files Browse the repository at this point in the history
Enhance existing tests and add new ones for MLEPolynomial class.
Verify correctness of NTT, evaluation, and decomposition methods.
  • Loading branch information
ahy231 committed Sep 30, 2024
1 parent 564e09b commit 126f5ae
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions tests/test_mle2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
sys.path.append('../src')
sys.path.append('src')

from mle2 import MLEPolynomial, pow_2
from mle2 import MLEPolynomial
from utils import bits_le_with_width

# Assuming Fp is defined in your original file, if not, you may need to import or define it here
Fp = GF(2**64 - 2**32 + 1)

class TestMLEPolynomial(unittest.TestCase):
Expand All @@ -21,53 +21,54 @@ def test_init(self):
def test_from_coeffs(self):
coeffs = [Fp(1), Fp(2), Fp(3), Fp(4)]
mle = MLEPolynomial.from_coeffs(coeffs, 2)
self.assertEqual(len(mle.evals), 4)
self.assertEqual(mle.evals, MLEPolynomial.compute_evals_from_coeffs(coeffs))

def test_ntt_core(self):
vs = [Fp(1), Fp(2), Fp(3), Fp(4)]
result = MLEPolynomial.ntt_core(vs, Fp(1))
self.assertEqual(len(result), 4)
self.assertEqual(result, [MLEPolynomial(vs, 2).evaluate(bits_le_with_width(x, 2)) for x in range(4)])
self.assertEqual(vs, MLEPolynomial.ntt_core(result, -1))

def test_compute_evals_from_coeffs(self):
coeffs = [Fp(1), Fp(2), Fp(3), Fp(4)]
evals = MLEPolynomial.compute_evals_from_coeffs(coeffs)
self.assertEqual(len(evals), 4)
self.assertEqual(evals, [MLEPolynomial(coeffs, 2).evaluate(bits_le_with_width(x, 2)) for x in range(4)])

def test_compute_coeffs_from_evals(self):
evals = [Fp(1), Fp(2), Fp(3), Fp(4)]
coeffs = MLEPolynomial.compute_coeffs_from_evals(evals)
self.assertEqual(len(coeffs), 4)
self.assertEqual(evals, [MLEPolynomial(coeffs, 2).evaluate(bits_le_with_width(x, 2)) for x in range(4)])

def test_evaluate(self):
evals = [Fp(1), Fp(2), Fp(3), Fp(4)]
mle = MLEPolynomial(evals, 2)
result = mle.evaluate([Fp(1), Fp(2)])
# self.assertIsInstance(result, Fp)
self.assertEqual(result, MLEPolynomial.evaluate_from_evals(evals, [Fp(1), Fp(2)]))

def test_evaluate_from_coeffs(self):
coeffs = [Fp(1), Fp(2), Fp(3), Fp(4)]
result = MLEPolynomial.evaluate_from_coeffs(coeffs, [Fp(1), Fp(2)])
# self.assertIsInstance(result, Fp)

def test_eval_from_coeffs(self):
coeffs = [Fp(1), Fp(2), Fp(3), Fp(4)]
result = MLEPolynomial.eval_from_coeffs(coeffs, Fp(2))
# self.assertIsInstance(result, Fp)
self.assertEqual(result, MLEPolynomial.evaluate_from_evals(MLEPolynomial.compute_evals_from_coeffs(coeffs), [Fp(1), Fp(2)]))

def test_decompose_by_div(self):
evals = [Fp(i) for i in range(8)]
evals = [i for i in range(8)]
mle = MLEPolynomial(evals, 3)
point = [Fp(1), Fp(2), Fp(3)]
point = [1, 2, 3]
k = len(point)
quotients, remainder = mle.decompose_by_div(point)
self.assertEqual(len(quotients), 3)
# self.assertIsInstance(remainder, Fp)
remainder = MLEPolynomial([remainder], 0)
for i in range(k):
remainder = MLEPolynomial.mul_quotients(quotients[i], remainder, point[i])
self.assertEqual(remainder.evals, mle.evals)

def test_decompose_by_div_from_coeffs(self):
coeffs = [Fp(i) for i in range(8)]
point = [Fp(1), Fp(2), Fp(3)]
quotients, remainder = MLEPolynomial.decompose_by_div_from_coeffs(coeffs, point)
self.assertEqual(len(quotients), 3)
# self.assertIsInstance(remainder, Fp)
evals = MLEPolynomial.compute_evals_from_coeffs(coeffs[:])
self.assertEqual(evals, [0, 1, 2, 6, 4, 10, 12, 28])
quotients, remainder = MLEPolynomial(evals, 3).decompose_by_div(point)
quotients = [MLEPolynomial.compute_coeffs_from_evals(q.evals) for q in quotients]
self.assertEqual((quotients, remainder), MLEPolynomial.decompose_by_div_from_coeffs(coeffs, point))

def test_eq_poly_vec(self):
point = [Fp(i) for i in range(4)]
Expand Down

0 comments on commit 126f5ae

Please sign in to comment.