From 126f5ae6b9992f6200d9f0c228dcad03eee76c88 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 30 Sep 2024 11:44:12 +0800 Subject: [PATCH] test(mle2): improve and expand unit tests Enhance existing tests and add new ones for MLEPolynomial class. Verify correctness of NTT, evaluation, and decomposition methods. --- tests/test_mle2.py | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/tests/test_mle2.py b/tests/test_mle2.py index 72a11d2..121cf0d 100644 --- a/tests/test_mle2.py +++ b/tests/test_mle2.py @@ -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): @@ -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)]