import importlib.util import itertools import json import hashlib import unittest from pathlib import Path HERE = Path(__file__).resolve().parent spec = importlib.util.spec_from_file_location("correction", HERE / "rs-correction.py") m = importlib.util.module_from_spec(spec) spec.loader.exec_module(m) class ReconstructionTests(unittest.TestCase): def test_all_scalar_products_against_polynomial_oracle(self): for a, b in itertools.product(range(16), repeat=2): self.assertEqual(m.mul(a, b), m.polynomial_mul(a, b)) self.assertEqual(m.mul(6, 7), 1) self.assertEqual(m.mul(4, 4), 3) self.assertNotEqual(m.mul(4, 4), (4 * 4) % 16) def test_exact_published_matrix(self): v, g, _ = m.constructions() self.assertEqual(g, [list(row) for row in m.EXPECTED]) self.assertEqual(v, [[1, 0, 0], [1, 1, 1], [1, 2, 4], [1, 3, 5], [1, 4, 3], [1, 5, 2]]) def test_all_81920_roundtrips_and_independent_codebook(self): _, g, _ = m.constructions() book = [(list(data), m.encode(g, data, m.polynomial_mul)) for data in itertools.product(range(16), repeat=3)] count = 0 for ids in itertools.combinations(range(6), 3): inv = m.inverse([g[i] for i in ids]) self.assertIsNotNone(inv) observations = set() for data, encoded in book: selected = [encoded[i] for i in ids] observations.add(tuple(selected)) self.assertEqual(m.encode(inv, selected), data) count += 1 self.assertEqual(len(observations), 4096) self.assertEqual(count, 81920) def test_row_mixing_negative_control(self): _, g, mixed = m.constructions() self.assertEqual(g[:3], mixed[:3]) self.assertEqual(mixed[5], [1, 1, 0]) self.assertIsNone(m.inverse([mixed[i] for i in (0, 1, 5)])) n = m.results()["negativeControl"] self.assertNotEqual(n["dataA"], n["dataB"]) self.assertEqual(n["observedA"], n["observedB"]) self.assertGreater(len(m.results()["rows"][1]["singular"]), 0) def test_negative_control_survivors_with_independent_codebook(self): _, _, mixed = m.constructions() book = [m.encode(mixed, data, m.polynomial_mul) for data in itertools.product(range(16), repeat=3)] singular = [] for ids in itertools.combinations(range(6), 3): count = len({tuple(word[i] for i in ids) for word in book}) self.assertIn(count, (256, 4096)) if count != 4096: singular.append(list(ids)) self.assertEqual(singular, [[0, 1, 5], [2, 3, 5]]) self.assertEqual(m.results()["rows"][1]["recoverable"], 18) def test_inverse_both_orders_and_nonmutation(self): a = [[1, 2, 4], [1, 3, 5], [1, 4, 3]] before = [row[:] for row in a] b = m.inverse(a) ident = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] self.assertEqual(m.multiply(a, b), ident) self.assertEqual(m.multiply(b, a), ident) self.assertEqual(a, before) self.assertIsNone(m.inverse([[1, 1], [1, 1]])) def test_invalid_inputs_fail_under_optimized_python_too(self): for value in [None, True, False, -1, 16, 1.0, "1"]: for fn in (m.mul, m.polynomial_mul): with self.assertRaises(ValueError): fn(value, 1) for matrix in [None, [], [[]], [[1], [2, 3]], [[True]], [[16]], [[1, 2]]]: with self.assertRaises(ValueError): m.inverse(matrix) with self.assertRaises(ValueError): m.multiply([[1]], [[1], [2]]) for data in [None, [1], [True, 2, 3], [1, 2, 16]]: with self.assertRaises(ValueError): m.encode(m.EXPECTED, data) def test_generated_evidence_and_source_hashes(self): output = json.loads((HERE / "rs-correction.json").read_text()) for key, value in m.results().items(): self.assertEqual(output[key], value, key) for name, digest in output["sourceSha256"].items(): self.assertEqual(hashlib.sha256((HERE / name).read_bytes()).hexdigest(), digest) self.assertIn("T", output["generatedAt"]) if __name__ == "__main__": unittest.main()