#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Regression checks for the fail-closed release evidence gate.""" import copy import importlib.util import json from pathlib import Path from tempfile import TemporaryDirectory import unittest SCRIPT = Path(__file__).with_name("check-conformance.py") spec = importlib.util.spec_from_file_location("check_conformance", SCRIPT) gate = importlib.util.module_from_spec(spec) spec.loader.exec_module(gate) MANIFEST = json.loads(Path(__file__).with_name("conformance.json").read_text()) TARGETS = {(row["board"], row["chip"]): gate.REQUIRED_ALGORITHMS[row["chip"]] for row in MANIFEST["targets"]} class ConformanceGateTest(unittest.TestCase): def test_placeholder_matrix_cannot_pass_without_current_run_artifacts(self): manifest = copy.deepcopy(MANIFEST) for row in manifest["targets"]: row.update(sdk_image_sha256="0" * 64, stock_image_sha256="0" * 64, simulator_trace_sha256="0" * 64, stock_result="mines", alkane_share_accept_capture="does-not-exist.json", bench_validation="not-run") with TemporaryDirectory() as directory: errors = gate.validate(manifest, TARGETS, Path(directory), "123", "a" * 40, "b" * 64) self.assertEqual(len(errors), 64) self.assertTrue(all("evidence invalid" in error for error in errors)) def test_stale_source_identity_is_rejected(self): row = copy.deepcopy(MANIFEST["targets"][0]) row["stock_result"] = "mines" with TemporaryDirectory() as directory: root = Path(directory) report = root / "123" / row["board"] / f"{row['chip']}.json" report.parent.mkdir(parents=True) report.write_text(json.dumps({"schema": 1, "run_id": "123", "source_commit": "f" * 40, "os_binary_sha256": "b" * 64, "board": row["board"], "chip": row["chip"], "stock_firmware_modified": False})) with self.assertRaisesRegex(ValueError, "different run, source"): gate.verify_cell(row, root, "123", "a" * 40, "b" * 64, TARGETS[(row["board"], row["chip"])]) def test_share_accept_requires_real_difficulty_one_work(self): capture = {"nonce_source": "real_silicon_capture", "header80": "00" * 80, "pool_target_le": "ff" * 32, "nonce": 0, "submission": {"id": 4, "method": "mining.submit", "params": ["worker", "job", "", "00000000", "00000000"]}, "pool_response": {"id": 4, "result": True, "error": None}} with self.assertRaisesRegex(ValueError, "below difficulty one"): gate.verify_accept(capture, "Sha256") capture["submission"]["params"][4] = "00000001" with self.assertRaisesRegex(ValueError, "submitted nonce"): gate.verify_accept(capture, "Sha256") def test_scrypt_proof_matches_litecoin_and_independent_vectors(self): header = bytes.fromhex( "01000000f615f7ce3b4fc6b8f61e8f89aedb1d0852507650533a9e3b10b9bbcc30639f27" "9fcaa86746e1ef52d3edb3c4ad8259920d509bd073605c9bf1d59983752a6b06b817bb4" "ea78e011d012d59d4" ) self.assertEqual(len(header), 80) self.assertEqual( gate.proof_digest(header, "Scrypt")[::-1].hex(), "0000000110c8357966576df46f3b802ca897deb7ad18b12f1c24ecff6386ebd9", ) self.assertEqual( gate.proof_digest(bytes(range(80)), "Scrypt").hex(), "bc540a1a801df96e493005c71e010e2d387607fbf0fec416fd3c2645aa1ba9d2", ) def test_equihash_cells_still_need_an_algorithm_specific_verifier(self): with self.assertRaisesRegex(ValueError, "Equihash share verifier"): gate.verify_accept({}, "Equihash") def test_submitted_header_reconstruction_rejects_version_and_extranonce_changes(self): capture = { "subscription": {"extranonce1": "aa", "extranonce2_size": 1}, "notify": {"method": "mining.notify", "params": [ "job", "11" * 32, "01", "02", [], "20000000", "1d00ffff", "12345678", True]}, "configure_response": {"result": { "version-rolling": True, "version-rolling.mask": "1fffe000"}}, } params = ["worker", "job", "bb", "12345678", "00000001", "00002000"] header, _ = gate.reconstructed_header(capture, params) self.assertEqual(len(header), 80) self.assertEqual(header[:4], (0x20002000).to_bytes(4, "little")) self.assertEqual(header[4:8], b"\x11" * 4) changed = params.copy() changed[5] = "ffffffff" with self.assertRaisesRegex(ValueError, "version bits"): gate.reconstructed_header(capture, changed) changed[5] = params[5] changed[2] = "f" with self.assertRaisesRegex(ValueError, "Stratum hex"): gate.reconstructed_header(capture, changed) changed[2] = "cc" rebuilt, _ = gate.reconstructed_header(capture, changed) self.assertNotEqual(rebuilt, header) if __name__ == "__main__": unittest.main()