SSM-Clock Stamp – Reference Pseudocode (11)

Purpose. Compact, language-agnostic pseudocode so any team can implement the core. Everything is plain ASCII; the canonical line is SSMCLOCK1|iso_utc|rasi_idx|theta_deg|sha256(file)|chain[|kv:...].


Utilities (pure functions)

  • wrap360(x) = x - 360*floor(x/360)
  • theta_deg = wrap360( (unix_seconds / 86400) * 360 )
  • rasi_idx = floor(theta_deg / 30)
  • chain_k = H_chain( ascii(chain_{k-1} + "|" + stamp_core) ) with chain_0 = "0"*64
  • rollup_D = sha256( ascii(Stamp_1 "|" ... "|" Stamp_n) ) (anchor fixed to sha256)

Helper signatures (ASCII):

  • ascii(s) -> bytes_ascii (reject any ord(c) > 127)
  • hex64_lower_ok(h) -> bool (len==64 and [0-9a-f])
  • parse_iso_utc_Z(s) -> int_seconds (require YYYY-MM-DDTHH:MM:SSZ, forbid :60)
  • bankers_round_string(x, frac_digits) -> string (round-half-to-even)
  • select_hash(name) -> func (one of sha256_hex, sha3_256_hex, blake2b_256_hex)
  • parse_kv_tail("kv:k1=v1;...") -> map (unknown keys allowed)
  • kv_with_defaults(m) -> map (defaults: algo=sha256; chain_algo=sha256; theta_prec=5; float=ieee75464; time_mode=derived_utc)

Emit one stamp line (single file)

Function (essential steps):
make_stamp_line(file_bytes, iso_utc, prev_chain, kv_map) -> stamp_line

  1. Policy defaults: cfg = kv_with_defaults(kv_map or {})
  2. UTC → epoch: unix = parse_iso_utc_Z(iso_utc)
  3. Clock: theta = wrap360( (unix / 86400.0) * 360.0 ), rasi = floor(theta / 30.0)
    Print: theta_str = bankers_round_string(theta, int(cfg["theta_prec"]))
  4. File digest: H = select_hash(cfg["algo"]), h_file = H(file_bytes)
  5. Core: stamp_core = "SSMCLOCK1|" + iso_utc + "|" + str(rasi) + "|" + theta_str + "|" + h_file
  6. Chain: prev = prev_chain or "0"*64; Hc = select_hash(cfg["chain_algo"]);
    chain = Hc( ascii(prev + "|" + stamp_core) )
  7. Final line:
    • no kv: stamp_core + "|" + chain
    • with kv: stamp_core + "|" + chain + "|kv:" + "k1=v1;...;" (no spaces, | and ; not allowed inside values)

Verify single (sidecar + file)

Function: verify_file_against_stamp(file_bytes, stamp_line) -> {HASH_OK, CLOCK_OK, CHAIN_OK, VERDICT}

  • Split by |; require parts[0] == "SSMCLOCK1" and arity 6 or 7.
  • Parse: iso_utc, rasi_s, theta_s, h_rec, chain_rec (and optional kv).
  • Enforce shapes: UTC ...Z (no :60), rasi ∈ {0..11}, h_rec and chain_rec are lowercase 64-hex.
  • Hash: H = select_hash(cfg["algo"]); h' = H(file_bytes); HASH_OK = (h' == h_rec).
  • Clock: unix = parse_iso_utc_Z(iso_utc); theta' = wrap360((unix/86400)*360);
    rasi' = floor(theta'/30); CLOCK_OK requires rasi' == rasi.
    Recommended string check: theta_prec = int(cfg["theta_prec"]);
    theta_str' = bankers_round_string(theta', theta_prec);
    if theta_str' != theta_s, optionally accept numeric tolerance abs(theta' - float(theta_s)) <= 0.5*10^(-theta_prec).
  • CHAIN_OK = "na" here (done in ledger walk).
  • VERDICT = "PASS" if HASH_OK and CLOCK_OK (and any provided chain/anchor checks elsewhere).

Ledger rewalk (append-only continuity)

Function: rewalk_ledger(ledger_rows) -> {LEDGER_OK, tip, row|reason}

For rows with fields {iso_utc, rasi_idx, theta_deg, digest, chain, kv_tail?}:

  • prev = "0"*64
  • For each row k:
    • core_k = "SSMCLOCK1|" + iso + "|" + rasi + "|" + theta + "|" + digest
    • Hc = select_hash(row.kv.chain_algo or "sha256")
    • calc = Hc( ascii(prev + "|" + core_k) )
    • Require calc == row.chain; else return first failing k.
    • prev = calc
  • Return {LEDGER_OK=true, tip=prev}

Roll-up and anchor verify (UTC day)

Canonical order key: (iso_utc, stamp_core, chain) (all ascending, string compare).
Digest: rollup_D = sha256( ascii(Stamp_1 "|" ... "|" Stamp_n) ).

Functions:

  • canonical_sort(stamp_lines) -> lines_sorted (derive stamp_core from each line; sort by (iso_utc, stamp_core, chain))
  • rollup_digest(stamp_lines) -> {count, rollup_D} (empty set ⇒ sha256( ascii("") ))
  • verify_anchor_for_day(stamps_from_ledger, stamps_from_sidecars, note) -> {ANCHOR_LEDGER_OK, ANCHOR_SIDECARS_OK, VERDICT} where note has date, count, rollup_sha256, source.

Verify-all harness (files, ledger, anchors)

Function: verify_all(files, sidecars_map, ledger_rows, anchor_note) -> summary

  • For each (path, line) in sidecars: verify single; count PASS/FAIL; track orphans.
  • L = rewalk_ledger(ledger_rows)
  • Build day sets from ledger/sidecars; A = verify_anchor_for_day(...)
  • Summarize: files_verified, PASS, FAIL, ORPHANS, LEDGER_OK, ANCHOR_VERDICT.

Determinism & equality hints (checklist)

  • iso_utc must be UTC ...Z only; reject offsets and :60.
  • Print theta_deg with exactly theta_prec digits (default 5), round-half-to-even.
  • All digests are lowercase 64-hex; enforce hex64_lower_ok.
  • All hashing inputs are ascii(...) of the literal strings.
  • Roll-up join is exactly "Stamp_1|...|Stamp_n" (literal |, no leading/trailing).
  • Per-stamp algorithms come from the optional kv: tail; anchors always use sha256 over the ASCII-joined stamp lines.

Navigation
Back: SSM-Clock Stamp – Glossary & Notation (10)
Next: SSM-Clock Stamp – Q&A (12A)