mirror of
https://github.com/allaunthefox/Research-Stack.git
synced 2026-08-13 17:10:34 +00:00
Create 4-Infrastructure/lib/ with canonical implementations of:
- hashing.py: sha256_bytes, sha256_text, sha256_file
- q16.py: Q16_16 fixed-point constants and arithmetic
- jsonl.py: load_json, load_jsonl, write_jsonl, stable_json, canonical_json_bytes
- fraction_utils.py: Fraction serialization helpers for hardware probes
Refactor 66 files across 4-Infrastructure/{hardware,shim,infra} and
5-Applications/{scripts,tools-scripts,hutter_prize,text-to-cad} to import
from the shared library instead of maintaining local copies.
4-Infrastructure/auto/lib/q16.py now re-exports from lib.q16.
Net: -743 lines (490 added, 1233 removed)
Build: not applicable (Python-only change, py_compile verified on all 71 files)
Co-Authored-By: Allaun Silverfox <bigdataiscoming+9i37y6j2@protonmail.com>
83 lines
2.3 KiB
Python
83 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Derive a deterministic trinary VM program from an input file."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
|
|
from lib.hashing import sha256_file
|
|
|
|
|
|
WIDTH = 6
|
|
TRIT_MAP = {0: -1, 1: 0, 2: 1}
|
|
SCHEMA = "trinary_vm_derivation_v1"
|
|
def byte_to_trits(value: int) -> list[int]:
|
|
digits = [0] * WIDTH
|
|
remaining = value
|
|
for index in range(WIDTH - 1, -1, -1):
|
|
remaining, digit = divmod(remaining, 3)
|
|
digits[index] = TRIT_MAP[digit]
|
|
return digits
|
|
|
|
|
|
def derive_program_bytes(data: bytes) -> list[dict[str, object]]:
|
|
program: list[dict[str, object]] = []
|
|
for value in data:
|
|
for index, trit in enumerate(byte_to_trits(value)):
|
|
program.append(
|
|
{
|
|
"op": "SET",
|
|
"target": "alpha",
|
|
"index": index,
|
|
"value": trit,
|
|
}
|
|
)
|
|
program.append(
|
|
{
|
|
"op": "SHIFT",
|
|
"target": "alpha",
|
|
"direction": "right",
|
|
}
|
|
)
|
|
return program
|
|
|
|
|
|
def derive_payload(input_path: Path) -> dict[str, object]:
|
|
data = input_path.read_bytes()
|
|
return {
|
|
"schema": SCHEMA,
|
|
"input_path": str(input_path),
|
|
"input_size_bytes": len(data),
|
|
"input_sha256": sha256_file(input_path),
|
|
"subregisters": {
|
|
"alpha": [0] * WIDTH,
|
|
"beta": [0] * WIDTH,
|
|
},
|
|
"program": derive_program_bytes(data),
|
|
}
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--input", required=True, help="Input file to derive from.")
|
|
parser.add_argument("--output", required=True, help="Path to write the JSON program.")
|
|
return parser.parse_args()
|
|
|
|
|
|
def main() -> int:
|
|
args = parse_args()
|
|
input_path = Path(args.input).resolve()
|
|
output_path = Path(args.output).resolve()
|
|
payload = derive_payload(input_path)
|
|
|
|
output_path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
|
|
print(f"Wrote derived program to {output_path}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|