"""Independent structural, primitive, negative and retained-result controls.""" import hashlib import json from pathlib import Path import unittest import model class ModelTests(unittest.TestCase): def test_sha256_known_vector(self): self.assertEqual(hashlib.sha256(b"abc").hexdigest(), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") self.assertEqual(model.split_tree([]).hex(), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") def test_prefixes_are_raw_bytes(self): self.assertEqual(model.leaf(b"A"), hashlib.sha256(b"\x00A").digest()) self.assertNotEqual(model.leaf(b"A"), hashlib.sha256(b"00A").digest()) self.assertEqual(model.parent(b"x" * 32, b"y" * 32), hashlib.sha256(b"\x01" + b"x" * 32 + b"y" * 32).digest()) def test_leaf_ambiguity(self): a, b = (b"A", b"B", b"C"), (b"A", b"B", b"C", b"C") self.assertEqual(model.duplicate_last(a)[0], model.duplicate_last(b)[0]) self.assertEqual(model.symbolic(a, True), model.symbolic(b, True)) self.assertNotEqual(model.split_tree(a), model.split_tree(b)) def test_internal_ambiguity_not_leaf_only(self): a, b = tuple(bytes([x]) for x in b"ABCDEF"), tuple(bytes([x]) for x in b"ABCDEFEF") self.assertEqual(model.duplicate_last(a)[0], model.duplicate_last(b)[0]) self.assertEqual(model.duplicate_last(a)[1], []) self.assertEqual(model.duplicate_last(b)[1], [1]) self.assertTrue(all(b[i] != b[i + 1] for i in range(len(b) - 1))) self.assertNotEqual(model.split_tree(a), model.split_tree(b)) def test_padding_is_not_observed_duplicate(self): self.assertEqual(model.duplicate_last((b"A", b"B", b"C"))[1], []) self.assertEqual(model.duplicate_last((b"A", b"B", b"C", b"C"))[1], [0]) def test_repetition_can_be_legitimate(self): entries = (b"A", b"A") root, levels = model.duplicate_last(entries) self.assertEqual(levels, [0]) self.assertTrue(model.root_and_count_match(entries, root, 2)) def test_expected_count_and_root_both_matter(self): entries = (b"A", b"B", b"C") root = model.duplicate_last(entries)[0] self.assertFalse(model.root_and_count_match(entries + (b"C",), root, 3)) self.assertTrue(model.root_and_count_match(entries + (b"C",), root, 4)) self.assertFalse(model.root_and_count_match((b"A", b"B", b"D"), root, 3)) self.assertFalse(model.root_and_count_match(entries, bytes(32), 3)) def test_empty_duplicate_tree_is_rejected(self): with self.assertRaises(ValueError): model.duplicate_last(()) self.assertFalse(model.root_and_count_match((), bytes(32), 0)) def test_exact_three_leaf_equations(self): a, b, c = [model.leaf(x) for x in (b"A", b"B", b"C")] self.assertEqual(model.duplicate_last((b"A", b"B", b"C"))[0], model.parent(model.parent(a, b), model.parent(c, c))) self.assertEqual(model.split_tree((b"A", b"B", b"C")), model.parent(model.parent(a, b), c)) def test_distinct_lengths_oracle_up_to_65(self): for n in range(1, 66): entries = tuple(bytes([x]) for x in range(n)) for padded in (False, True): actual = model.duplicate_last(entries)[0] if padded else model.split_tree(entries) self.assertEqual(model.evaluate(model.symbolic(entries, padded)), actual) def test_split_stack_oracle(self): for n in range(66): entries = [bytes([x]) for x in range(n)] stack = [] for i, data in enumerate(entries): stack.append(model.leaf(data)) j = i while j & 1: right, left = stack.pop(), stack.pop() stack.append(model.parent(left, right)) j >>= 1 while len(stack) > 1: right, left = stack.pop(), stack.pop() stack.append(model.parent(left, right)) root = stack[0] if stack else hashlib.sha256(b"").digest() self.assertEqual(root, model.split_tree(entries)) def test_finite_exhaustion(self): report = model.exhaustive() self.assertEqual(report["lists"], 2046) self.assertEqual(report["rootOracleComparisons"], 4092) self.assertGreater(report["duplicateLast"]["ambiguousGroups"], 0) self.assertEqual(report["splitTree"]["ambiguousGroups"], 0) self.assertEqual(report["sameCountDistinctListAliases"], 0) def test_controls_and_case_membership(self): rows = model.generate()["rows"] self.assertEqual([r["case"] for r in rows], [ "repeat-last-leaf", "repeat-last-subtree", "odd-five-to-six", "changed-entry-control", "reordered-control", "legitimate-repeat-control"]) self.assertEqual([r["duplicateLast"]["sameRoot"] for r in rows], [True, True, True, False, False, True]) self.assertEqual([r["splitTree"]["sameRoot"] for r in rows], [False, False, False, False, False, True]) self.assertEqual([r["trustedRootAndCountAccepts"] for r in rows], [False, False, False, False, False, True]) def test_retained_result_exact(self): path = Path(__file__).with_name("results.json") self.assertEqual(path.read_text(), model.serialized()) self.assertEqual(json.loads(path.read_text())["schemaVersion"], model.SCHEMA) if __name__ == "__main__": unittest.main()