#!/usr/bin/env python3
"""Hyperbolic Lexicon: the chain-track trace the browser core must reproduce line for line (Phase 3; L8-L10;
Amendment 7; the common rule "Authority").

    python3 -B "Pages/Hyperbolic Lexicon/reference/traces_chain.py"           write traces/chain_v1.txt
    python3 -B "Pages/Hyperbolic Lexicon/reference/traces_chain.py" --check   rebuild in memory and compare

What it holds (each line type is documented in the file's '#' header):
  - the codec on hand-made byte strings (packets, the kernel's normalisation, residuals, leaves, the float quaternion),
    V8's Math.hypot and Number.prototype.toFixed on doubles from Rng(8060);
  - Amendment 7 item 2's synthetic shards with seeds 8000 + i (i = 0, 1, 2) and the first two verb gloss shards
    (spike S4's cut, from the WordNet archive in the cache), each grown with engrams off and on: every record field,
    every chunk, the controls' growers (BioChain-AI's packet normalisation, leaves hashed over binary bytes), the exact
    weave and its segments, the V0 id stream;
  - the records: LVOCAB/1 (V0, the real one), a trace LADDR/1 over a 12-row table of fake tokens and words from
    Rng(8200 + r) (the real addrTableRoot needs map A over WordNet), LPREP/1 (Tier S and Tier P) of a fixed fake
    corpus 'lexicon-trace-v1', LCORPUS/1, LGROW/1;
  - RFC 6962: the Certificate Transparency vector and RFC 6962's worked proofs, then lists of K in (1, 2, 3, 4, 5, 7,
    8, 13) leaves from Rng(8100 + j): roots, every inclusion and consistency proof and their verdicts, every
    alteration, the shdccp/2 rule's duplicate-last collision and the XOR summary's blindness to swaps;
  - ATTEST/1 under two fixed test keys (A: RFC 6979 A.2.5's published key, active; B: derived from a label, revoked)
    with RFC 6979 deterministic ECDSA, so every signature is the same on every run; P1363 / DER vectors;
  - the hardened verifier's verdicts on clean packages and on hand-made tampers T1-T9 and more.
Seeds 8000 + i are reserved for traces; no claim uses them. Nothing here decides a claim, and no C0 shard is read.
--check exits 0 equal, 1 different, 2 skipped (no WordNet archive in the cache).
"""
import hashlib
import os
import struct
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
from lexicon import attest as A  # noqa: E402
from lexicon import chain as C  # noqa: E402
from lexicon import manifest as M  # noqa: E402
from lexicon import records as R  # noqa: E402
from lexicon import verify as V  # noqa: E402
from lexicon import wordnet as W  # noqa: E402
from lexicon import wordpiece as WP  # noqa: E402
from hpstep.prng import Rng  # noqa: E402
from hpstep.problems import children  # noqa: E402

TRACE = os.path.join(HERE, 'traces', 'chain_v1.txt')
CORPUS = 'lexicon-trace-v1'
CORPUS_OFF = 'lexicon-trace-off-v1'
CORPUS_P = 'lexicon-trace-p-v1'
EPOCH = A.epoch_token('2000-01-01')
RFC_KS = (1, 2, 3, 4, 5, 7, 8, 13)
SEED_SYNTH, SEED_HYPOT, SEED_RFC, SEED_ADDR, SEED_CODEC = 8000, 8060, 8100, 8200, 8050
SCALES = (1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1.0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6)

HEADER = '''# chain_v1: the chain track (lexicon.chain, records, manifest, attest, verify). Python is the authority; every line
# below must be regenerated by lexicon_core.js. Hex is lowercase except packets (16 upper hex digits, as Owl writes
# them). sha256(x) is of x's UTF-8. A list is written comma-joined, '-' when empty. Numbers are JS Number::toString.
# codec <name> <bytes> <packet> <kernelPacket> <has80> <quatHex(crystalQuat)> <leaf_hash(0, bytes)> <residual hex
#   length> <residual, or sha256:<hex> when longer than 128 digits>  (the cases: C0 'a', C1 'Hello, world!', C2
#   [75,0,7], C3 bytes 0..255, C4 720 zero bytes, C5 720 bytes Rng(8050).below(256), C6 720 bytes 97 +
#   Rng(8051).below(4), C7 'é' x 150 as UTF-8)
# decode <name> <decode_chunk(residual, bytes) equals the bytes>
# silent <name> <count of single-bit residual flips that decode the same> <first 8 as i:bit:kind>  (S4's order: hex
#   digit i ascending, then bit 0..3 of that digit; kind padding / escape / other)
# hypot <i> <a0> <a1> <a2> <a3> <Math.hypot(a0..a3)> <quatHex(qn(a))>: doubles as 16 hex digits of their IEEE-754
#   bits; from one Rng(8060), each component is (2 random() - 1) * [1e-6, 1e-5, ..., 1e6][below(13)], then 0 when
#   below(8) == 0, then -0 in place of a 0 when below(16) == 0
# fixed <i> <x as 16 hex digits> <x.toFixed(12)> <x.toFixed(4)> <String(x)>: x from the same Rng after the hypot cases
# synth <seed> <draws> <bytes> <code points> <two-byte code points> <chunks> <chunks with a 0x80 crystal byte>
#   <sha256 of the UTF-8>  (Amendment 7 item 2's generator, Rng(seed))
# shard <name> <bytes> <sha256 of the UTF-8>: S0-S2 the synthetic shards of seeds 8000-8002, V0 and V1 the first two
#   verb gloss shards (spike S4's cut, lexicon.chain.gloss_shards)
# rec <shard> <off|on> <field> <value>: every field of grow(text, engrams) in Owl's key order; stream and
#   engramStream as <length> <sha256>, leafHashes as <count> <sha256(JSON.stringify(leafHashes))>, segCuts as JSON
# recjson <shard> <off|on> <sha256(JSON.stringify(record))>
# chunk <shard> <k> <packet> <kernelPacket> <residual hex length> <sha256(residual)> <leafHash>  (engrams off)
# kernel <shard> <sha256(stream)> <stream differs from Owl's> <chainId equal> <merkleRoot equal>: L8 control 1's
#   grower (BioChain-AI's normalisation, 0x80 -> 0x81)
# binary <shard> <leaf 0> <merkleRoot> <chainId> <every leaf differs>: L8 control 2's grower (leaves over binary)
# weave3 <shard> <K> <bit lengths of |q0|..|q3|> <weave3Sha> <norm = 5^(28K)> <unfold gives the prefixes> <the
#   segment product equals it>
# seg <shard> <s> <lo> <hi> <sha256 of the segment sub-weave's components in decimal, comma-joined>
# weave3full <name> <q0> <q1> <q2> <q3>: the exact weave of codec case C1's one-chunk record
# ids <shard> <number of V0 ids> <idStreamSha256> <first 12 ids>
# lvocab <LVOCAB/1 string>, lvocabHash <hash>
# addrrow <row>: 12 trace rows id|token|word|len|h3PointSha|h2PointSha (word r from Rng(8200 + r): 1 + below(12)
#   letters, the root letter below(6), then children(word)[below(5)]); addrTableRoot <hex>; laddr <LADDR/1 string
#   with V0's hash and the trace table's root>; laddrHash <hash>
# lprep <shard> <off|on> <LPREP/1 Tier S>; lprepHash <shard> <off|on> <hash>; lprepP <shard> <off|on> <salt> <Tier P>
#   (corpusId lexicon-trace-v1, shardIndex 0..4 in the order S0 S1 S2 V0 V1, spdx CC0-1.0 for S, the WordNet ref for
#   V, sourceUrlSha of 'rng:8000+i' or the WordNet URL; the salt of shard j is the test salt of j)
# ct <n> <root>: the Certificate Transparency reference vector's roots, n = 0..8
# rfcpath <m> <names>, rfcproof <m> <names>: RFC 6962 2.1.3's audit paths of d0, d3, d4, d6 and PROOF(3), (4), (6)
#   over its 7 leaves, the hashes named a..l as the RFC names them
# rfc <j> <K> <root>: K leaves of 32 bytes from Rng(8100 + j), eight next_u32 big-endian each
# incl <j> <i> <proof> <verifies> <rejected for leaf i+1 mod K> <rejected at position i+1 mod K> ('-' when K = 1)
# cons <j> <m> <proof> <verifies> <rejected with the old root's first byte flipped>, m = 1..K
# alter <j> <class> <i> <root changed>: replace (first byte of entry i flipped), delete i, insert 32 zero bytes
#   before i, swap i and i+1, duplicate-last, truncate to the first i (i = 1..K-1)
# shdccp2 <j> <K> <root of the hex leaves> <root with the last duplicated> <equal>
# xor <j> <K> <XOR of the 64-bit prefixes, 16 hex digits>; swap <j> <i> <XOR unchanged> <exact weave changed>
# identity <identityId> | epoch <token> | keyVector <hex> | key <A|B> <keyId> <x> <y> <status>
# registry <sha256> <snapshot JSON>
# attest <package> <payload> <signature P1363> <signature DER>  (RFC 6979 deterministic ECDSA, P-256, SHA-256; a
#   port without RFC 6979 reads the signatures from these lines, and from short lines the DER, as input)
# twin <package> <high-S twin P1363>
# short <r|s> <n> <DER> <P1363, r and s left-padded> <P1363 unpadded>: the first n with r (or s) < 2^248 among
#   key A's deterministic signatures of 'hyperbolic-lexicon trace short <n>'
# lcorpus <corpus> <string> | lgrow <corpus> <string> | inclproof <corpus> <j> <proof> | consproof <corpus> <proof>
#   corpora: lexicon-trace-v1 (engrams on, Tier S; packages PS0..PV1), lexicon-trace-off-v1 (engrams off; OS0..OV1),
#   lexicon-trace-p-v1 (engrams on, Tier P with the test salts; QS0..QV1); every LPREP attested by key A
# pkg <name> <sha256(JSON.stringify(package))>: keys record, lprep, salt (Tier P), attestation, lcorpus, inclusion,
#   lgrow, consistency; proofs as lists of hex strings
# tampers of PS0 and PV0 (<package>.<case>): T1 leafHashes and merkleRoot deleted; T2 packet 2's crystal32 bit 0
#   (word bit 24) flipped, parity re-fixed; T3 the LPREP's lvocabHash with its last hex digit changed (S4's rule: '0'
#   becomes '1', anything else '0'); T4 the LPREP attested by key B (revoked); T5 the next package's attestation; T6
#   the Tier-P package Q of the same shard with the next shard's salt; T7 frames 0 and 1 swapped; T7last the last two
#   frames swapped; T8 chainId's last hex digit changed; T9 frame 0's residual with the first silent flip (line flip);
#   twin the high-S twin signature; unknown attested by key C (SHA-256('hyperbolic-lexicon trace key C') mod (n - 1)
#   + 1, not in the registry); keyVector the block's keyVector with its last digit changed; sig the signature's last
#   hex digit changed; engramCount + 1; value + 0.0001 (toFixed(4)); shardIndex + 1 in the LPREP; inclusion the first
#   proof hash's last digit changed; consistency the last proof hash's; lcorpusRoot the LCORPUS root's
# flip <package> <frame> <i:bit:kind>: the T9 flip used
# verdict <name> <selfConsistent> <authentic> <failed>
# checks <name> <one letter per check, in order: P pass, F fail, - not run>
#   checks: parity canonical crystal leaves weaves derived commitment attestation inclusion consistency
'''


def dhex(x):
    return struct.pack('>d', x).hex()


def lst(xs):
    xs = [str(x) for x in xs]
    return ','.join(xs) if xs else '-'


def tf(b):
    return 'true' if b else 'false'


def codec_cases():
    r5 = Rng(SEED_CODEC)
    r6 = Rng(SEED_CODEC + 1)
    return [('C0', b'a'), ('C1', b'Hello, world!'), ('C2', bytes([75, 0, 7])), ('C3', bytes(range(256))),
            ('C4', bytes(720)), ('C5', bytes(r5.below(256) for _ in range(720))),
            ('C6', bytes(97 + r6.below(4) for _ in range(720))), ('C7', ('é' * 150).encode('utf-8'))]


def trace_words():
    out = []
    for r in range(12):
        g = Rng(SEED_ADDR + r)
        n = 1 + g.below(12)
        w = [g.below(6)]
        while len(w) < n:
            w.append(children(w)[g.below(5)])
        out.append(tuple(w))
    return out


TRACE_TOKENS = ['[PAD]', '[UNK]', 'the', '##ing', 'cat', '##s', '|', 'é', '中', 'run', '42', '...']


def build(wn):
    out = [HEADER.rstrip('\n')]
    say = out.append
    v0 = WP.load_v0()

    # ---- codec
    for name, b in codec_cases():
        res = C.encode_chunk(b)
        rtxt = res if len(res) <= 128 else 'sha256:' + hashlib.sha256(res.encode()).hexdigest()
        say(f'codec {name} {len(b)} {C.crystallize(b)} {C.kernel_crystallize(b)} {tf(C.crystal_has_80(b))} '
            f'{C.quat_hex(C.crystal_quat(b))} {C.leaf_hash(0, b)} {len(res)} {rtxt}')
        say(f'decode {name} {tf(C.decode_chunk(res, len(b)) == list(b))}')
    for name, b in codec_cases():
        if name in ('C1', 'C6'):
            fl = C.silent_flips(C.encode_chunk(b), b)
            say(f'silent {name} {len(fl)} ' + lst(f"{x['i']}:{x['bit']}:{x['kind']}" for x in fl[:8]))

    # ---- V8's Math.hypot and toFixed
    g = Rng(SEED_HYPOT)

    def draw():
        x = (2 * g.random() - 1) * SCALES[g.below(13)]
        if g.below(8) == 0:
            x = 0.0
        if g.below(16) == 0:
            x = -0.0 if x == 0 else x
        return x
    for i in range(40):
        a = [draw() for _ in range(4)]
        say(f'hypot {i} ' + ' '.join(dhex(x) for x in a) + f' {dhex(C.v8_hypot(a))} {C.quat_hex(C.qn(a))}')
    for i in range(40):
        x = draw()
        say(f'fixed {i} {dhex(x)} {C.js_to_fixed(x, 12)} {C.js_to_fixed(x, 4)} {C.js_number(x)}')

    # ---- shards
    shards = []
    for i in range(3):
        text, draws = C.synth_shard(SEED_SYNTH + i)
        data = text.encode('utf-8')
        two, cps = C.two_byte_share(text)
        chunks = [data[k:k + C.CHUNK] for k in range(0, len(data), C.CHUNK)]
        say(f'synth {SEED_SYNTH + i} {draws} {len(data)} {cps} {two} {len(chunks)} '
            f'{sum(C.crystal_has_80(c) for c in chunks)} {hashlib.sha256(data).hexdigest()}')
        shards.append((f'S{i}', text, R.SPDX_CC0, R.source_url_sha_synth(SEED_SYNTH, i)))
    for k, text in enumerate(C.gloss_shards(wn, 'verb')[:2]):
        shards.append((f'V{k}', text, R.SPDX_WORDNET, R.source_url_sha_c0()))
    for name, text, _, _ in shards:
        say(f'shard {name} {len(text.encode())} {hashlib.sha256(text.encode()).hexdigest()}')

    recs = {}
    for name, text, _, _ in shards:
        for mode in ('off', 'on'):
            rec = C.grow(text, engrams=mode == 'on')
            recs[name, mode] = rec
            for k, v in rec.items():
                if k in ('stream', 'engramStream'):
                    val = f'{len(v)} {hashlib.sha256(v.encode()).hexdigest()}'
                elif k == 'leafHashes':
                    val = f'{len(v)} {hashlib.sha256(C.js_json(v).encode()).hexdigest()}'
                elif k == 'segCuts':
                    val = C.js_json(v)
                elif isinstance(v, str):
                    val = v
                else:
                    val = C.js_number(v)
                say(f'rec {name} {mode} {k} {val}')
            say(f'recjson {name} {mode} {hashlib.sha256(C.js_json(rec).encode()).hexdigest()}')
    for name, text, _, _ in shards:
        rec = recs[name, 'off']
        data = text.encode()
        for k, (pk, res, _) in enumerate(C.frames(rec['stream'])):
            ch = data[k * C.CHUNK:(k + 1) * C.CHUNK]
            say(f'chunk {name} {k} {pk} {C.kernel_crystallize(ch)} {len(res)} '
                f'{hashlib.sha256(res.encode()).hexdigest()} {rec["leafHashes"][k]}')
        kr = C.grow(text, engrams=False, packets='kernel')
        say(f'kernel {name} {hashlib.sha256(kr["stream"].encode()).hexdigest()} {tf(kr["stream"] != rec["stream"])} '
            f'{tf(kr["chainId"] == rec["chainId"])} {tf(kr["merkleRoot"] == rec["merkleRoot"])}')
        br = C.grow(text, engrams=False, leaves='binary')
        say(f'binary {name} {br["leafHashes"][0]} {br["merkleRoot"]} {br["chainId"]} '
            f'{tf(all(a != b for a, b in zip(br["leafHashes"], rec["leafHashes"])))}')
    for name, text, _, _ in shards:
        rec = recs[name, 'off']
        q = C.exact_weave(rec['leafHashes'])
        n_ok, u_ok, s_ok = C.exact_weave_checks(rec['leafHashes'], rec['segCuts'], q)
        say(f'weave3 {name} {len(rec["leafHashes"])} ' + ' '.join(str(abs(c).bit_length()) for c in q) +
            f' {C.weave3_sha(q)} {tf(n_ok)} {tf(u_ok)} {tf(s_ok)}')
        cuts = rec['segCuts']
        for s, sw in enumerate(C.segment_weaves(rec['leafHashes'], cuts)):
            say(f'seg {name} {s} {cuts[s]} {cuts[s + 1]} {C.weave3_sha(sw)}')
    one = C.grow(b'Hello, world!', engrams=False)
    say('weave3full C1 ' + ' '.join(str(c) for c in C.exact_weave(one['leafHashes'])))
    for name, text, _, _ in shards:
        ids = WP.encode(text.encode(), v0)[0]
        say(f'ids {name} {len(ids)} {R.id_stream_sha(text.encode(), v0)} {lst(ids[:12])}')

    # ---- records
    lv = R.lvocab(v0)
    say(f'lvocab {lv}')
    say(f'lvocabHash {R.record_hash(lv)}')
    rows = R.addr_rows(TRACE_TOKENS, trace_words())
    for r in rows:
        say(f'addrrow {r}')
    root = R.addr_table_root(rows)
    la = R.laddr(v0.sha256, root)
    say(f'addrTableRoot {root}')
    say(f'laddr {la}')
    say(f'laddrHash {R.record_hash(la)}')
    lvh, lah = R.record_hash(lv), R.record_hash(la)
    lpreps = {}
    for j, (name, text, spdx, src) in enumerate(shards):
        for mode in ('off', 'on'):
            rec = recs[name, mode]
            f = R.lprep_fields(rec, text.encode(), v0, CORPUS, j, spdx, src, lvh, lah)
            s = R.lprep(f)
            lpreps[name, mode] = s
            say(f'lprep {name} {mode} {s}')
            say(f'lprepHash {name} {mode} {R.record_hash(s)}')
            salt = R.test_salt(j)
            say(f'lprepP {name} {mode} {salt} {R.lprep(f, R.TIER_P, salt)}')

    # ---- RFC 6962
    ct = [bytes.fromhex(h) for h in ('', '00', '10', '2021', '3031', '40414243', '5051525354555657',
                                     '606162636465666768696a6b6c6d6e6f')]
    log = M.Log(ct)
    for n in range(9):
        say(f'ct {n} {log.root(n).hex()}')
    L7 = M.Log(ct[:7])
    nm = {'a': L7.mth(0, 1), 'b': L7.mth(1, 2), 'c': L7.mth(2, 3), 'd': L7.mth(3, 4), 'e': L7.mth(4, 5),
          'f': L7.mth(5, 6), 'j': L7.mth(6, 7), 'g': L7.mth(0, 2), 'h': L7.mth(2, 4), 'i': L7.mth(4, 6),
          'k': L7.mth(0, 4), 'l': L7.mth(4, 7)}
    name_of = {v: k for k, v in nm.items()}
    for m in (0, 3, 4, 6):
        say(f'rfcpath {m} ' + ''.join(name_of.get(h, '?') for h in L7.path(m)))
    for m in (3, 4, 6):
        say(f'rfcproof {m} ' + ''.join(name_of.get(h, '?') for h in L7.consistency(m)))
    for j, K in enumerate(RFC_KS):
        D = M.synthetic_leaves(SEED_RFC + j, K)
        lg = M.Log(D)
        rt = lg.root()
        say(f'rfc {j} {K} {rt.hex()}')
        for i in range(K):
            p = lg.path(i)
            ok = M.verify_inclusion(i, K, M.leaf_hash(D[i]), p, rt)
            if K >= 2:
                o = (i + 1) % K
                wl = not M.verify_inclusion(i, K, M.leaf_hash(D[o]), p, rt)
                wp = not M.verify_inclusion(o, K, M.leaf_hash(D[i]), p, rt)
                say(f'incl {j} {i} {lst(h.hex() for h in p)} {tf(ok)} {tf(wl)} {tf(wp)}')
            else:
                say(f'incl {j} {i} {lst(h.hex() for h in p)} {tf(ok)} - -')
        for m in range(1, K + 1):
            p = lg.consistency(m)
            ok = M.verify_consistency(m, K, lg.root(m), rt, p)
            bad = bytearray(lg.root(m))
            bad[0] ^= 1
            say(f'cons {j} {m} {lst(h.hex() for h in p)} {tf(ok)} {tf(not M.verify_consistency(m, K, bytes(bad), rt, p))}')
        for i in range(K):
            rep = bytearray(D[i])
            rep[0] ^= 1
            say(f'alter {j} replace {i} {tf(M.root(M.tampered(D, "replace", i, bytes(rep))) != rt)}')
            say(f'alter {j} delete {i} {tf(M.root(M.tampered(D, "delete", i)) != rt)}')
            say(f'alter {j} insert {i} {tf(M.root(M.tampered(D, "insert", i, bytes(32))) != rt)}')
            if i + 1 < K:
                say(f'alter {j} swap {i} {tf(M.root(M.tampered(D, "swap", i)) != rt)}')
            if i >= 1:
                say(f'alter {j} truncate {i} {tf(M.root(M.tampered(D, "truncate", i)) != rt)}')
        say(f'alter {j} duplicate-last {K - 1} {tf(M.root(M.tampered(D, "duplicate-last")) != rt)}')
        hx = [d.hex() for d in D]
        a, b = M.merkle_shdccp2(hx), M.merkle_shdccp2(hx + [hx[-1]])
        say(f'shdccp2 {j} {K} {a} {b} {tf(a == b)}')
        vals = [int.from_bytes(d[:8], 'big') for d in D]
        say(f'xor {j} {K} {M.xor_summary(vals):016x}')
        q0 = C.chain_quaternion(vals)
        for i in range(K - 1):
            sw = list(vals)
            sw[i], sw[i + 1] = sw[i + 1], sw[i]
            say(f'swap {j} {i} {tf(M.xor_summary(sw) == M.xor_summary(vals))} {tf(C.chain_quaternion(sw) != q0)}')

    # ---- attestation
    ident = A.identity_id()
    kv = A.key_vector(ident, EPOCH)
    privs = {k: A.private_key(d) for k, d in A.TRACE_KEYS.items()}
    privs['C'] = A.private_key(int.from_bytes(hashlib.sha256(b'hyperbolic-lexicon trace key C').digest(), 'big')
                               % (A.P256_N - 1) + 1)
    say(f'identity {ident}')
    say(f'epoch {EPOCH}')
    say(f'keyVector {kv}')
    status = {'A': 'active', 'B': 'revoked'}
    entries = []
    for k in ('A', 'B'):
        jwk = A.jwk_of(privs[k].public_key())
        say(f'key {k} {A.key_id(jwk)} {jwk["x"]} {jwk["y"]} {status[k]}')
        entries.append({'publicJwk': jwk, 'keyVector': kv, 'status': status[k]})
    snap = A.snapshot_text(entries)
    say(f'registry {A.sha256hex(snap)} {snap}')
    session = V.Session(v0, lv, la, snap, A.sha256hex(snap))
    for which in ('r', 's'):
        n = 0
        while True:
            der = privs['A'].sign(f'hyperbolic-lexicon trace short {n}'.encode(), A.ECDSA_SHA256_DET)
            r_, s_ = A.decode_dss_signature(der)
            if (r_ if which == 'r' else s_) < 1 << 248:
                break
            n += 1
        say(f'short {which} {n} {der.hex()} {A.der_to_p1363(der).hex()} {A.der_to_p1363_unpadded(der).hex()}')

    def show(pkg_name, pkg):
        say(f'pkg {pkg_name} {hashlib.sha256(C.js_json(pkg).encode()).hexdigest()}')
        v = V.verify(pkg, session)
        say(f'verdict {pkg_name} {V.verdict_text(v)}')
        say(f'checks {pkg_name} ' + ''.join('P' if ok else '-' if ok is None else 'F' for _, ok, _ in v['checks']))

    def corpus(cid, mode, tier='S'):
        sh = [(recs[name, mode], text.encode()) for name, text, _, _ in shards]
        lps = []
        for j, (name, text, spdx, src) in enumerate(shards):
            f = R.lprep_fields(recs[name, mode], text.encode(), v0, cid, j, spdx, src, lvh, lah)
            lps.append(R.lprep(f, tier, R.test_salt(j) if tier == 'P' else None))
        lg_ = R.corpus_log(lps)
        lc = R.lcorpus(cid, lps, [s[2] for s in shards], lg_)
        lgs, cproof = R.grow_record(lps, cid, log=lg_)
        say(f'lcorpus {cid} {lc}')
        say(f'lgrow {cid} {lgs}')
        say(f'consproof {cid} {lst(h.hex() for h in cproof)}')
        pkgs = []
        for j in range(len(sh)):
            say(f'inclproof {cid} {j} {lst(h.hex() for h in lg_.path(j))}')
            block = A.attest(privs['A'], kv, lps[j], EPOCH)
            pkgs.append(V.make_package(sh[j][0], lps[j], block, lc, lg_.path(j), lgs, cproof,
                                       R.test_salt(j) if tier == 'P' else None))
        return pkgs

    def payload_of(block):
        return A.attestation_payload(block['keyId'], block['keyVector'], block['contentHash'], block['epochToken'])

    names = [s[0] for s in shards]
    pk = corpus(CORPUS, 'on')
    for j, p in enumerate(pk):
        b = p['attestation']
        der = A.p1363_to_der(bytes.fromhex(b['sig']))
        say(f'attest P{names[j]} {payload_of(b)} {b["sig"]} {der.hex()}')
        say(f'twin P{names[j]} {A.high_s_twin(bytes.fromhex(b["sig"])).hex()}')
        show(f'P{names[j]}', p)
    pkoff = corpus(CORPUS_OFF, 'off')
    for j, p in enumerate(pkoff):
        show(f'O{names[j]}', p)
    pkp = corpus(CORPUS_P, 'on', 'P')
    for j, p in enumerate(pkp):
        show(f'Q{names[j]}', p)

    # tampers on PS0 (synthetic) and PV0 (verb 00); T5 takes the next package's attestation, T6 the next salt
    for j in (0, 3):
        base, nm_ = pk[j], f'P{names[j]}'
        K = len(base['record']['leafHashes'])
        fl = V.silent_flips_of(base, 0, limit=1)
        t4 = V.t4_revoked(base, privs['B'], kv, EPOCH)
        say(f'attest {nm_}.T4 {payload_of(t4["attestation"])} {t4["attestation"]["sig"]} '
            f'{A.p1363_to_der(bytes.fromhex(t4["attestation"]["sig"])).hex()}')
        unknown = V.t4_revoked(base, privs['C'], kv, EPOCH)
        say(f'attest {nm_}.unknown {payload_of(unknown["attestation"])} {unknown["attestation"]["sig"]} '
            f'{A.p1363_to_der(bytes.fromhex(unknown["attestation"]["sig"])).hex()}')
        twin = V._copy(base)
        twin['attestation']['sig'] = A.high_s_twin(bytes.fromhex(base['attestation']['sig'])).hex()
        kvm = V._copy(base)
        kvm['attestation']['keyVector'] = V._last_digit(kvm['attestation']['keyVector'])
        sig_t = V._copy(base)
        sig_t['attestation']['sig'] = V._last_digit(sig_t['attestation']['sig'])
        eng = V._copy(base)
        eng['record']['engramCount'] += 1
        val = V._copy(base)
        val['record']['value'] = float(C.js_to_fixed(val['record']['value'] + 0.0001, 4))
        idx = V._copy(base)
        parts = idx['lprep'].split('|')
        parts[2] = str(int(parts[2]) + 1)
        idx['lprep'] = '|'.join(parts)
        inc = V._copy(base)
        inc['inclusion'][0] = V._last_digit(inc['inclusion'][0])
        con = V._copy(base)
        con['consistency'][-1] = V._last_digit(con['consistency'][-1])
        lcr = V._copy(base)
        lcr['lcorpus'] = '|'.join(p if i != 3 else V._last_digit(p) for i, p in enumerate(lcr['lcorpus'].split('|')))
        cases = [('T1', V.t1_strip(base)), ('T2', V.t2_crystal(base, 2, 0)), ('T3', V.t3_lprep(base)),
                 ('T4', t4), ('T5', V.t5_other(base, pk[j + 1])), ('T6', V.t6_salt(pkp[j], R.test_salt(j + 1))),
                 ('T7', V.t7_swap(base, 0)), ('T7last', V.t7_swap(base, K - 2)), ('T8', V.t8_chain_id(base)),
                 ('T9', V.t9_flip(base, 0, fl[0])), ('twin', twin), ('unknown', unknown), ('keyVector', kvm),
                 ('sig', sig_t), ('engramCount', eng), ('value', val), ('shardIndex', idx), ('inclusion', inc),
                 ('consistency', con), ('lcorpusRoot', lcr)]
        say(f'flip {nm_} 0 {fl[0]["i"]}:{fl[0]["bit"]}:{fl[0]["kind"]}')
        for cname, pkg in cases:
            show(f'{nm_}.{cname}', pkg)
    return '\n'.join(out) + '\n'


def main():
    check = '--check' in sys.argv[1:]
    if not W.available():
        print('skipped: no WordNet archive in the cache (run golden/make_wordnet_v1.cjs)', file=sys.stderr)
        return 2
    text = build(W.load())
    if check:
        same = os.path.exists(TRACE) and open(TRACE, 'rb').read() == text.encode('utf-8')
        print(('trace matches' if same else 'MISMATCH') + ': chain_v1.txt (%d lines, sha256 %s)' % (
            text.count('\n'), hashlib.sha256(text.encode()).hexdigest()[:16]), file=sys.stderr if not same else sys.stdout)
        return 0 if same else 1
    os.makedirs(os.path.dirname(TRACE), exist_ok=True)
    with open(TRACE, 'wb') as f:
        f.write(text.encode('utf-8'))
    print('wrote %s: %d lines, sha256 %s' % (os.path.relpath(TRACE, HERE), text.count('\n'),
                                              hashlib.sha256(text.encode()).hexdigest()))
    return 0


if __name__ == '__main__':
    sys.exit(main())
