""" Resonance Word — Python port of j-matrix-twin/resonance_word.ijs 64-bit field elements over GF(p), p = 2^64 - 2^32 + 1 (Goldilocks prime). Format: 8-bit class tag in high 8 bits | 56-bit payload. Bug fix: the original J rw_pack formula had `(payload * 2^56 <. payload)` where `<.` is minimum — when payload < 2^56 (always for 56-bit payloads), the second term evaluates to payload*payload. This port corrects it to just `(cls << 56) | payload`. """ from __future__ import annotations from dataclasses import dataclass # Goldilocks prime: 2^64 - 2^32 + 1 P_GOLD = (1 << 64) - (1 << 32) + 1 # Class tags (8-bit) CLASS = { "PRIME": 0x01, "LATTICE": 0x02, "ORBIT": 0x03, "SEAL": 0x04, "TRANSITION": 0x05, "INVOLUTION": 0x06, "ANCHOR": 0x07, "CERTIFICATE": 0x08, "WORM": 0x09, "SOVEREIGN": 0x0A, } CLASS_NAMES = {v: k for k, v in CLASS.items()} LATTICE_ORDER = 12288 # |G| = P × B = 48 × 256 # UREF 11 involution generators UREF_GEN = [1 << i for i in range(11)] # [1, 2, 4, ..., 1024] # ── Field arithmetic ────────────────────────────────────────────────────────── def gf_add(a: int, b: int) -> int: return (a + b) % P_GOLD def gf_mul(a: int, b: int) -> int: return (a * b) % P_GOLD def gf_sub(a: int, b: int) -> int: return (a - b) % P_GOLD # ── Packing / unpacking ─────────────────────────────────────────────────────── def rw_pack(cls: int, payload: int) -> int: """ Pack class + payload into a 64-bit Goldilocks field element. Format: bits [63:56] = class (8-bit), bits [55:0] = payload (56-bit). Note: The J source had a bug in the second term; this is the corrected form. """ return (cls << 56) | (payload & ((1 << 56) - 1)) def rw_unpack(word: int) -> tuple[int, int]: """Unpack a ResonanceWord into (class_tag, payload).""" cls = (word >> 56) & 0xFF payload = word & ((1 << 56) - 1) return cls, payload @dataclass class ResonanceWord: word: int cls: int payload: int class_name: str @classmethod def from_pack(cls_tag: int, payload: int) -> "ResonanceWord": w = rw_pack(cls_tag, payload) return ResonanceWord( word=w, cls=cls_tag, payload=payload, class_name=CLASS_NAMES.get(cls_tag, f"0x{cls_tag:02x}"), ) @classmethod def from_word(word: int) -> "ResonanceWord": cls_tag, payload = rw_unpack(word) return ResonanceWord( word=word, cls=cls_tag, payload=payload, class_name=CLASS_NAMES.get(cls_tag, f"0x{cls_tag:02x}"), ) def hex(self) -> str: return f"0x{self.word:016x}" def describe(self) -> dict: return { "word_hex": self.hex(), "class": self.class_name, "class_tag": f"0x{self.cls:02x}", "payload": self.payload, "payload_hex": f"0x{self.payload:014x}", } # ── Tokenizer ───────────────────────────────────────────────────────────────── def abjad_value(ch: str) -> int: """ Map character to Abjad-like value. Latin: ordinal * 7 mod 1000 + 1 (deterministic proxy). """ return 1 + (ord(ch) * 7) % 1000 def tokenize(text: str) -> list[ResonanceWord]: """Convert text to a stream of CLASS_SOVEREIGN ResonanceWords.""" words = [] for ch in text: v = abjad_value(ch) w = rw_pack(CLASS["SOVEREIGN"], v) words.append(ResonanceWord( word=w, cls=CLASS["SOVEREIGN"], payload=v, class_name="SOVEREIGN", )) return words # ── Lattice routing ─────────────────────────────────────────────────────────── @dataclass class LatticeRoute: word: ResonanceWord idx: int # payload mod 12288 p: int # idx // 256 b: int # idx % 256 def lattice_route(rw: ResonanceWord) -> LatticeRoute: """Map a ResonanceWord to a lattice element (p, b, index).""" idx = rw.payload % LATTICE_ORDER return LatticeRoute(word=rw, idx=idx, p=idx // 256, b=idx % 256) # ── UREF 11 involutions — agent dispatch ───────────────────────────────────── def uref_dispatch(entropy_bits: int) -> int: """ Apply 11 UREF involution generators to entropy_bits. Returns agent index in [0, 10]. """ state = entropy_bits for gen in UREF_GEN: if state & gen: state ^= gen return state % 11