omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
GitHub aien-dev/omega-mv#749 (code only)
Fetch it
curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/0afa0b35957288e7.bundle -o 0afa0b35957288e7.bundle git fetch 0afa0b35957288e7.bundle refs/pull/749/head
Provenance
github-import 1
aien-dev/omega-mv
749
9b9d190c2840a26586624e56dcd670df3174dd01
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":749,"url":"https://github.com/aien-dev/omega-mv/pull/749","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:38:27Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 2041ab198..ab7f7ab68 100644 --- a/.crumb +++ b/.crumb @@ -41,14 +41,14 @@ "generated": { "children": [ { - "digest": "9f19f11674ca5dfc3c9c3ae35fe6106557aee9583210da13d257e1b9d88e052c", - "last_commit": "d863780eb", + "digest": "33894518bb8a0840e7a6609b7f47fe59c6848023ea3c731b80faf0276bca82cb", + "last_commit": "d6edaad17", "name": "calibration", "purpose": "Directory calibration. Purpose not yet described by a human." }, { - "digest": "f5de159859ca96b6aea4337601d0d6f2fe39bc2f8154946e28f1a21c74c7a186", - "last_commit": "876d00ff5", + "digest": "6912df0902eb796f6806c9fc364a615951bc5dac66c1022812bf014173491921", + "last_commit": "7f1f472c5", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -83,8 +83,8 @@ "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "2b63b8ab9af7638881a81c7617109ab9987c2419fb4e0e25079c42309280b866", - "last_commit": "d94a3ab89", + "digest": "0dc33fd1271b6c3fdb83a8718b58e58b119057f89ee5147b1cb3ce8412821ea8", + "last_commit": "874288c64", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -95,15 +95,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:47b4b1b551e499f3ea8c7250dd4d0b972280643720269322aba84be82167e02a", - "compiled_at": "2026-10-25T13:57:08Z", + "children_root": "sha256:80780b1928545850acf417b3a545e5ad1e7ba0ce42e35a0255aaec415ee2d19c", + "compiled_at": "2026-10-25T16:33:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:57fab59c8f46d3af2deabaa08f911574503a3685b65c27f01160c47b6092b2a6", - "evidence_files": 11666, - "evidence_root": "sha256:91249b110c57333badc93f4052f27ad9faba72fbbac89de4d1512a9ec8808721", - "files": 15005, - "generated_at_commit": "1f7c0ab131ab427f334ea4151abf0911c19beaf5", - "source_tree": "sha256:ea63a4f1d3f31bcf1a318eba722a45a25d94ae1625f9d8db3956b524bcc47594" + "digest": "sha256:e151b419f6b8c3cb378ee464ba7c02be14229dfbef051b67369ebd3d8dccc70b", + "evidence_files": 11679, + "evidence_root": "sha256:da6d4b12649d8d96e4ed7f15aa95e21d51988c6a440be9667243c3ccefcdf3c8", + "files": 15056, + "generated_at_commit": "7f1f472c5d689baea5f339bb6ff60cb34645e0a4", + "source_tree": "sha256:06c3d98222663681ca553f84e894932a75366fd77edafd04f3ee54c54f299d65" }, "owner_lane": "", "provenance": { diff --git a/calibration/.crumb b/calibration/.crumb index 781e3f77c..67e68d01d 100644 --- a/calibration/.crumb +++ b/calibration/.crumb @@ -34,44 +34,44 @@ "children": [ { "digest": "e2e9d7d77533c89d8cc919746832ad00529cb86d65c996217bc5348e521dabc7", - "last_commit": "ae1dd00a", + "last_commit": "ae1dd00a2", "name": "docs", "purpose": "Directory calibration/docs. Purpose not yet described by a human." }, { "digest": "6b36741c5f34a363b7e8cd3c29f8b3f0d43acbb3c2cddd3f5b9ebbade3f85d25", - "last_commit": "1cff5260", + "last_commit": "1cff52602", "name": "preregistration", "purpose": "Directory calibration/preregistration. Purpose not yet described by a human." }, { - "digest": "8af50fb73adf0c6bc10d02dfdcac403b5c3039d35fd6b46061fe324f38f411d8", - "last_commit": "92750b58", + "digest": "e93dafae991b7c94376799fd99bfe19a710977679309c9b4c636d05745a19350", + "last_commit": "6697ed705", "name": "profiles", "purpose": "Directory calibration/profiles. Purpose not yet described by a human." }, { "digest": "d3e110eae40e5c48b4242c0a3e3d595178cd822cc0510bdb09ce2bfc29146ba3", - "last_commit": "c803862f", + "last_commit": "c803862f1", "name": "schemas", "purpose": "Directory calibration/schemas. Purpose not yet described by a human." }, { "digest": "af056765c0d3db17ce1819cb8aae90f04ae2a9b21b399ec347daf81efe9eff80", - "last_commit": "dc7c1304", + "last_commit": "d863780eb", "name": "scripts", "purpose": "Directory calibration/scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:d303c33c7df5e0b63fa0438a79bb26c0cb619369ae6f644ce332e10ddc05241b", - "compiled_at": "2026-10-24T04:15:18Z", + "children_root": "sha256:6bdd05ac0cd25d0d78aa5927bafcc335c13ffefdefc94410f70181ab864e5d7c", + "compiled_at": "2026-10-25T15:00:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:9f19f11674ca5dfc3c9c3ae35fe6106557aee9583210da13d257e1b9d88e052c", + "digest": "sha256:33894518bb8a0840e7a6609b7f47fe59c6848023ea3c731b80faf0276bca82cb", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 110, - "generated_at_commit": "dc7c1304c038f49db3b121d5a5e27d7bcab5bf6c", - "source_tree": "sha256:2df583871de57822c268162a2ba6c6e6d9ecfa3f443af02dc8b47b39ea4b1b9b" + "files": 112, + "generated_at_commit": "6697ed705f1340cbe41c71198b56b028f1ca00bd", + "source_tree": "sha256:c08caa852910eb0a5d74d9c6761cff4f0f61982f4574b7d90c4aa3e5dc927b59" }, "owner_lane": "", "provenance": { diff --git a/calibration/profiles/.crumb b/calibration/profiles/.crumb index 6c809ef43..0d64361b1 100644 --- a/calibration/profiles/.crumb +++ b/calibration/profiles/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T13:47:05Z", + "compiled_at": "2026-10-25T15:00:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:8af50fb73adf0c6bc10d02dfdcac403b5c3039d35fd6b46061fe324f38f411d8", + "digest": "sha256:e93dafae991b7c94376799fd99bfe19a710977679309c9b4c636d05745a19350", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 4, - "generated_at_commit": "834ee93e4314a42c1127a2fcda8b29cf3b511245", - "source_tree": "sha256:6b5ffafe8479a91f6b31c08e9cb44f76dbbee278aaf061e5c02a9bc9d5985f0e" + "files": 6, + "generated_at_commit": "6697ed705f1340cbe41c71198b56b028f1ca00bd", + "source_tree": "sha256:e714a4ae1e281675885e90455a797f4c4318c2226e5cec61488162080aa80e7a" }, "owner_lane": "", "provenance": { diff --git a/calibration/profiles/Turing-profile-REUSE-v1.0.sha256 b/calibration/profiles/Turing-profile-REUSE-v1.0.sha256 new file mode 100644 index 000000000..b2152a456 --- /dev/null +++ b/calibration/profiles/Turing-profile-REUSE-v1.0.sha256 @@ -0,0 +1 @@ +8558b4070128a86e1df6538f996b522c0db40ca8fc284f165a88c9d6372264c4 Turing-profile-REUSE-v1.0.toml diff --git a/calibration/profiles/Turing-profile-REUSE-v1.0.toml b/calibration/profiles/Turing-profile-REUSE-v1.0.toml new file mode 100644 index 000000000..c8f636adb --- /dev/null +++ b/calibration/profiles/Turing-profile-REUSE-v1.0.toml @@ -0,0 +1,33 @@ +# Turing-profile-REUSE-v1.0: the measurement profile for the Turing accounting of REUSE-1 +# (omega-mv #742/#743/#744, spark-rsi-mv #73), owned by the Turing instrument issue omega-mv #700. +# +# Definitions 1-3 of "Computing Machinery and Understanding" v1.14 (aien-dev/aienos.com +# c6c6d8bafcfc0b7398dc9337e2f3dd7715e0c814 public/research/computing-machinery-and-understanding/paper.tex, +# sha256 8376faa31bea089b5adb53098e4aa6332e0a685f0d1fa0450f7be828142951f3) are binding. +# +# Format: flat TOML, one `key = value` per line, as calibration/profiles/Turing-profile-v1.1.toml. +# Commitment: Turing-profile-REUSE-v1.0.sha256 = SHA-256 over the exact bytes of this file. +# After the freeze commit this file never changes; any change is a new profile id. + +profile_id = "Turing-profile-REUSE-v1.0" +profile_version = "1.0.0" +profile_digest = "SIDECAR" +profile_status = "FROZEN at the commit that adds this file, its sidecar and docs/reuse/REUSE-1.md, before any implementation or data" +formula = "T = [L(B|G) + L(D|B,G)] - [L(M|G) + L(D|M,G)] (Definition 3)" +unit = "bits" +headline = "T(arm) for arms method, delta, idelta, delta', select, cache; D is predicted exactly by every model, so T(arm) = -(model charge of the arm beyond B). A positive T is impossible in this profile by construction; equivalent-execution savings are reported separately and are not a Turing gain." +evaluator_freeze = "spark-rsi-mv om/reuse-eval at the evaluator-freeze commit named on #73 (REUSE-1 section 7.3)" +d_definition = "The low 4 bits of r0 of every FINAL QUERY response of the evaluator's reference, in FINAL request order (all groups, repetition 1). Symbol alphabet K = 16." +observation = "One symbol per FINAL QUERY. Inputs (PROG, state, changes) are context given to every model and are not coded." +probability_representation = "Every model predicts the exact symbol: q = 65521 on the predicted symbol and q = 1 on each of the 15 others (sum 65536; CODER_SPEC section 2 requires every q >= 1)." +tps1_mapping = "TPS1 v1, K 16, qbits 16, one record per D symbol, context_key 0, crumb ordinal = observation index; profile_digest = SHA-256 of this file; model_digest = SHA-256('turing.reuse.model.v1' 0x00 || manifest bytes of the arm's library, or 32 zero bytes for B); dataset_digest = SHA-256 of the FINAL request files (the holdout-set digest of REUSE-1 7.5). TSY1 carries the reference symbols." +reference_coder = "turing-coder of omega-mv a647b6a9 (src/turing), rANS primary and range second, as pinned for ORIGSEL; built as an external referee outside the delivered .om closure." +codelength_method = "L(D|X,G) = coded bits of X's stream (T_real) and ideal bits sum(-log2 q/65536) (T_ideal). B and every M produce identical q rows, so the data terms are equal; the coder run shows it (H1: |coded - ideal| within the CODER_SPEC band)." +baseline = "B = the baseline router (drive.om base: every unit of REUSE-1 section 12 with an empty library)." +candidates = "M(arm) = B plus the arm's library (method, delta, select: the TRAIN library; delta': the TRAIN' library; idelta, cache, full: none) plus the calibration values the arm reads (select only)." +shared_background = "G = omc 734f0a39, profile R1, om/sha256/sha256.om, every handwritten unit of REUSE-1 section 12 including the arm code paths (they are in B), and codegen.om's fixed snippet text." +model_code = "P1 (primary, declared parameters, the existing convention): 64 bits per manifest template word (8 words per template) plus 64 bits per calibration value the arm reads. P2 (sensitivity): 8 bits per byte of generated method source plus the manifest's 2048 bytes." +causal_chain_disclosure = "Not decoder inputs, declared per paper s.5.7: learn.om (the template admission rule of REUSE-1 5.3), the TRAIN experience, the CALIB measurements, the authoring agents (omp sessions on anthropic/claude-opus-5-5 and openai-codex models)." +delta_t = "Delta T between two library generations = -(change of the P1 charge). A positive Delta T can only come from deleting templates and is never reported as a gain." +verdict_rule = "Reported: T_ideal(arm), T_real(arm) under P1 and P2, n = number of D symbols, and the H1 check. No Turing verdict word is issued; REUSE-1 section 10 decides GO / INVESTIGATE / STOP on cost and correctness, separately." +limits = "T_real needs the external coder build; if it cannot be built the value is reported NOT_RUN and only T_ideal is given." diff --git a/docs/.crumb b/docs/.crumb index 01649e8af..6bbd61f19 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -88,7 +88,7 @@ }, { "digest": "69239ac409b797d24c5975ac9b8510a229185b8477225efefc485e43205050d6", - "last_commit": "d863780eb", + "last_commit": "876d00ff5", "name": "osc", "purpose": "Directory docs/osc. Purpose not yet described by a human." }, @@ -124,14 +124,14 @@ } ], "children_root": "sha256:2d1071e88cc921d774f3363f69d352b080c718f9ab04d32d60b8bcd9b65cbc87", - "compiled_at": "2026-10-25T03:11:55Z", + "compiled_at": "2026-10-25T16:33:17Z", "compiler": "crumb-compile/1", - "digest": "sha256:f5de159859ca96b6aea4337601d0d6f2fe39bc2f8154946e28f1a21c74c7a186", + "digest": "sha256:6912df0902eb796f6806c9fc364a615951bc5dac66c1022812bf014173491921", "evidence_files": 34, "evidence_root": "sha256:589d8b3706e6d7a202ab20b278dac5fc5bafc973f1c2a867b2668aa7210b65cc", - "files": 132, - "generated_at_commit": "a0eafa39ac9a92786dac68f7aa6d8564a28fa981", - "source_tree": "sha256:0189588a1cafb896f5d9c0f45907e90045130e065812ebb732007a2d886bf050" + "files": 134, + "generated_at_commit": "7f1f472c5d689baea5f339bb6ff60cb34645e0a4", + "source_tree": "sha256:46dd80b36ef4eb2d7257cc032b1ecdc16a6cd41daafb1878ecfabb72abdcaee4" }, "owner_lane": "", "provenance": { diff --git a/docs/reuse/REUSE-1-CLARIFICATIONS.md b/docs/reuse/REUSE-1-CLARIFICATIONS.md new file mode 100644 index 000000000..e71131daa --- /dev/null +++ b/docs/reuse/REUSE-1-CLARIFICATIONS.md @@ -0,0 +1,112 @@ +# REUSE-1 v1.0 clarifications + +Rulings that resolve ambiguities in `REUSE-1.md` v1.0 without changing any rule there. All of +them were made before any experience collection or measurement; each names who raised it. + +K1 (CandLib, EvalJudge; 2026-10-25). Calibration location. `drive build` copies CALIB into the +library as `gen-G.cal` (written before `gen-G.man`, same ADR 0015 order, counted in LIBDIR bytes); +manifest words 52..55 hold its SHA-256 (0, and no file, when CALIB is `-`); `lbload` verifies it +before exec; a loaded router reads its calibration from `LIBDIR/gen-G.cal`. The evaluator passes no +extra argument. An arm started with LIBDIR `-` has no calibration (the fixed rule of 5.2). + +K2 (CandCore; relayed to the evaluator). Framing order and statuses of section 2: +BATCH item DEL with b != 0 is 3; BATCH with p = 1 + 3m and m outside 1..1024 is 3, p != 1 + 3m is +4. BEGIN: p < 65 or p != 65 + n is 4; n > 4096 is 5; invalid PROG 2; a value outside the domain 3; +a refused BEGIN keeps the previous session. Ops 2..8 with no session are 6 (after the framing check +4, before 2 and 3). STATS and END without a session are 0. EOF on stdin without END: the arm exits +3 without a response. The echo arm validates and applies every request like full; only its QUERY +answer is 0, 0, 0. PROGRAM: invalid PROG is 2 (checked first), then out of domain 3. + +K3 (CandLib). Template admission and manifest order (5.3, 3.4): the FSM template is decided first; +AGG shapes are admitted in descending TRAIN QUERY count (ties lower shape id) while count >= 8 and +K < 16; library order is AGG in admission order, then FSM. Lineage digests are sorted ascending. +A duplicate experience file or more than 8 is a usage error (exit 2). The self-check is FNV-1a 64 +(offset basis 14695981039346656037, prime 1099511628211) over the 2040 little-endian bytes of +words 0..254. + +K4 (CandSelect). Bind rule cells (5.2): bind, full and method are all read at change class 0; any +of the three missing gives the fixed rule. A calibration file whose words 3..7 are nonzero, or whose +size is not exactly 1600 bytes, is malformed and refused whole. + +K5 (EvalJudge). The evaluator vendors a byte-identical copy of omega-mv main `acb3f15c` +`om/sha256/sha256.om` as spark-rsi-mv `om/reuse-eval/sha256.om` with a pin file. + +KAT (integrator). `tests/reuse/kat.txt` (626 records, sha256 +`31c153e1881e3a842cb838448ac53b5331c8e99300023752ac0ae3522b16b7cb`) was produced outside Omega: +inputs and answers by a Python script, then every answer recomputed independently in Wolfram +Language (Wolfram MCP kernel, 2026-10-25): `{626, 0, {}}` = 626 records, 0 disagreements. Record +format: REUSE-1 integrator decision 4 (kinds 1 lane step, 2 fold, 3 AGG program, 4 FSM program). +The generating script is integrator tooling, not part of the delivered closure; the file is data. + +K6 (EvalStats). FINAL seed use: bytes 0..7 (LE) seed the bootstrap (section 10 as written); +bytes 8..15 seed the FINAL generator; bytes 16..23 seed the arm-order permutation. + +K7 (EvalStats, EvalJudge). Control outcomes are recorded one per control and sub-case as detected, +undetected, reported (C12 ablations only) or not run, with a flag when a tampered or revoked +artifact answered a QUERY as a method. Undetected or flagged: STOP. Not run: GO is impossible +(INVESTIGATE at best); a control is never silently passed. + +K8 (EvalGen). Workload details of 7.1 and 7.2: FSM type C sessions are 4 new machines (FSM has one +shape); of the 10 TRAIN machines 5 are general and 5 are permutation machines, FSM type A recall +uses the permutation ones and type A new uses new permutation machines; AGG type A programs of all +three classes have f in {MIN, MAX} (the generator fails loudly if the TRAIN pool has no such +shape); type I starts with DEL and alternates; type P puts a PROGRAM before QUERY 4, 8, 12 and 16 +(AGG: same shape, new constants; FSM: same S and A, new s0, λ and rules); TRAIN reuse: AGG +sessions j with j mod 4 = 3 (50 of 200) repeat a uniformly chosen earlier TRAIN AGG program; +TRAIN session s is FSM iff s mod 6 = 5; CALIB: 2 families x 3 n x 4 change classes x 4 sessions = +96 sessions of BEGIN + 16 x (change, QUERY), change = none, one SET, a BATCH of 2..64 SETs, or a +BATCH of 1024 SETs; AGG type A deletes the first index whose mapped value equals the current fold +result, then inserts a random value at a random index. + +K9..K17 provenance. These entries were written into this file on 2026-10-25 after the FINAL run +(commit after the candidate freeze `6ad09bff`, which carries K1..K8 only). Each records a decision +already implemented in the frozen code before FINAL (notes in judge.om, lib.om, router.om, +select.om, delta.om at evaluator `4345eda9` and candidate `6ad09bff`, and the evaluator-internal +format notes for K6, K7, K17). No rule, threshold or control changed after the data was seen. + +K9 (CandCore, EvalJudge). Experience tap (6.1): the evaluator runs one baseline router B process +over every group of a split, in order, as `BASE tap - - - TAPFILE SPLITARG`. TAPFILE must not exist +(a fresh file per tap); SPLITARG is the record word 9 value: 1 TRAIN and TRAINP, 2 CALIB, 3 DEV. The +operator admits a tap by appending `<sha256> AUTHORIZED <label>` to ALLOW. + +K10 (CandLib, EvalJudge). Control mutants C1, C1b, C1c are codegen-only (`drive build ... +mutant:c1|c1b|c1c`): c1 compares AGG shape ids with the wl byte cleared; c1b ends every wl-16 lane +map with `v = and v 255`; c1c reads c_j from R1P word 5 + 2j. C1 and C1b also run with +`unit:check=NOG3` (G3 removed); C1c runs with G3 only. C1 is detected by G3 (route 5 on every QUERY) +or by wrong answers; C1b and C1c with G3 must give route 5 and no wrong answer; the NOG3 variants of +C1 and C1b must give wrong answers. + +K11 (EvalJudge). C1 detection evidence: the controls log prints both the route-5 count and the +wrong count, so the record shows which mechanism caught the mutant. + +K12 (CandDelta, CandSelect). Churn (5.2): an invalidation is a MIN/MAX extreme rescan or a drop of +live delta state (at PROGRAM, or when a change or restore finds the state unusable), counted at most +once per drop; a cutoff miss is an FSM recompute unit that ran to n without a cutoff. From 64 +invalidations or 64 cutoff misses since the session's BEGIN snapshot, delta is invalid for the rest +of the session. + +K13 (EvalJudge). C3 equality scope: with diagnostic cost words rotated across records, gen-G.om and +gen-G.elf must be byte-identical and manifest word 4 and words 56..183 equal; the lineage words +differ by construction. + +K14 (EvalJudge). STATS capture: after the last request the judge sends STATS and stores the status, +q and the q counters zero-padded to 32 words in `stats-A-R-gNN.ctr`. Diagnostic only and excluded +from every latency sum. + +K15 (CandLib, EvalJudge). C8 uses `crashafter:K` for K = 1..9, one per publish step, over a copy of +the honest generation; drive exits 97, and the library must stay self-consistent and loadable with +its own MANSHA. + +K16 (CandLib, from the independent candidate review). Calibration handoff: lbload reads gen-G.cal +once, checks the SHA-256 of exactly those bytes against manifest words 52..55, and passes the same +bytes to the generation on a memfd at fd 7; the router reads `/proc/self/fd/7`, never LIBDIR. This +closes a check-then-read race (P0). + +K17 (EvalJudge, from the independent evaluator review). END handling: proc.log word 15 is the END +round trip (part of the process cost); word 10 is the exit status plus 1024 when END failed +(status not 0, q not 0, end of file or timeout). Any END failure or nonzero exit is one wrong refusal +(STOP). + +Independence note. The lanes were written by separate agents under one operator account +(UNISOLATED). One slip is recorded and not mitigated: helper names of `r1.om` were broadcast to the +evaluator agents during integration. The evaluator's reference `ref.om` was written from the contract. diff --git a/docs/reuse/REUSE-1.md b/docs/reuse/REUSE-1.md new file mode 100644 index 000000000..9f6641aab --- /dev/null +++ b/docs/reuse/REUSE-1.md @@ -0,0 +1,518 @@ +# REUSE-1: experience-selected guarded specialization, profile R1 (contract v1.0) + +**Status:** FROZEN by the commit that adds this file, before any implementation, collection or +measurement. A change after that commit is a new version with its reason recorded; data gathered +under one version is never re-scored under another. +**NOT A MASTER PLAN.** Component-local experiment contract for omega-mv #742 (methods), #743 +(incremental execution), #744 (lifecycle-cost selection) and spark-rsi-mv #73 (independent +qualification). Parents: omega-mv #584, #652; aien-architecture-mv #307. It changes no release +sequencing, no milestone status (M11, M25, M26, M38 stay PLANNED) and no subsystem ownership. + +Host only: Linux arm64, DGX Spark CPU. GB10: NOT_RUN. Energy: NOT_MEASURED (a host power method +exists for the #657 gate but is not integrated or calibrated for this experiment; missing joules +are unavailable, never zero). The 27B and 35B services are not touched. + +Reviewed before freezing by two independent read-only consultants (2026-10-25); their required +changes are applied. Recorded limits are in section 11. + +## 0. Owners, successor slices, non-goals + +| Need | Existing owner | REUSE-1 slice (R1 only, follows the owner's semantics) | +|---|---|---| +| Compiler | seeded omc: omega-mv `seed/omc/omc.om` sha256 `2e35db7d6c2066fb…`, binary sha256 `734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61` | none; every first-party program here is handwritten `.om` compiled by that binary | +| Synthesis, trace learning | #656, #658 (`src/omega_synthesis.c`) | `om/reuse/learn.om`: selects R1 templates from admitted experience | +| Guarded compilation | #665 | `om/reuse/codegen.om`, `om/reuse/check.om` | +| Incremental semantics | #657, `spec/omega-incremental-semantics.md`, `rx_semantic` | `om/reuse/delta.om` | +| Generations | ADR 0015, `rx_generation.h` | `om/reuse/lib.om` (commit order of ADR 0015 for the library files) | +| Cost selection | #522, #583, #584, #600, #698, #704, `rx_costmodel.c` | `om/reuse/select.om` | +| Experience | #699 (C, PR #707) | `om/reuse/exp.om`: R1 query records, see 11 | +| Durable memory | Cortex `rx_cortex` (ADR 0022) | none: BLOCKED, see 11 | +| Independent judge | spark-rsi-mv #67 (Rust BlindJudge, receipt v2), #68 | `spark-rsi-mv om/reuse-eval/`: the R1 evaluation slice of the #67 judge's `.om` successor | +| Turing scorer | omega-mv #700, existing turing-coder | `calibration/profiles/Turing-profile-REUSE-v1.0.toml` | + +No second compiler, runtime, memory store, scheduler, trainer, judge, signer or promotion +authority. No method enters any production path. The library directory is a frozen, +reproducible build output (section 3.5), not memory. Nothing learns at run time (section 5). + +## 1. Profile R1: supported semantics + +Words are unsigned 64-bit little endian. Arithmetic is exact as written. + +### 1.1 Program PROG (64 words, 512 bytes) + +AGG (family 1): + +| Word | Meaning | +|---|---| +| 0 | magic 3544411312749556050 ("R1PROG01") | +| 1 | 1 | +| 2 | lane width wl: 8 or 16 | +| 3 | fold f: 1 ADD, 2 XOR, 3 MIN, 4 MAX, 5 OR, 6 AND | +| 4 | steps k: 1..6 | +| 3+2j, 4+2j for j = 1..k (words 5..16) | op_j, c_j. op: 1 ADD, 2 SUB, 3 MUL, 4 AND, 5 OR, 6 XOR, 7 SHL, 8 SHR. c_j < 2^wl; for SHL and SHR c_j < wl | +| pair words with j > k, and 17..63 | 0 | + +FSM (family 2): + +| Word | Meaning | +|---|---| +| 0 | magic | +| 1 | 2 | +| 2 | states S: 2..16 | +| 3 | symbols A: 2..16 | +| 4 | start s0 < S | +| 5 | rule count R: 1..24 | +| 6+s, s < 16 | λ[s] < 256; 0 for s >= S | +| 22+r, r < 24 | rule r = s + 256 lo + 65536 hi + 16777216 t with s < S, t < S, lo <= hi < A; 0 for r >= R | +| 46..63 | 0 | + +Any other value makes PROG invalid (status 2). Program digest = SHA-256 of the 512 bytes +(`om/sha256/sha256.om` of omega-mv main, concatenated unchanged). A digest is held as 4 words, +word j = bytes 8j..8j+7 read big endian. + +Shape id (u64): AGG = wl + 256 f + 4096 k + 65536 * sum over j of op_j * 16^(j-1); FSM = 1. + +### 1.2 State and result + +AGG: X = (x_0..x_{n-1}), 0 <= n <= 4096, x_i < 2^wl. Lane map e(x): v = x; for j = 1..k: +ADD v = (v + c) mod 2^wl; SUB (v - c) mod 2^wl; MUL (v c) mod 2^wl; AND, OR, XOR bitwise; +SHL (v 2^c) mod 2^wl; SHR floor(v / 2^c). Fold: acc = id_f, then acc = acc f e(x_i) for +i = 0..n-1; ADD is mod 2^64 (it cannot wrap: 4096 (2^16 - 1) < 2^28), the others are the plain +u64 operations. id: ADD 0, XOR 0, MIN 2^64 - 1, MAX 0, OR 0, AND 2^64 - 1. Result r0 = acc, +r1 = n. All six folds are commutative monoids, so order never matters. + +FSM: w = (w_1..w_n), 0 <= n <= 4096, w_t < A, stored at index t - 1. q_0 = s0, +q_t = δ(q_{t-1}, w_t); δ(s, a) = t of the lowest-numbered rule with rule.s = s and +lo <= a <= hi, else s. Result r0 = q_n, r1 = sum over t = 1..n of λ[q_t]. + +No trap is part of R1. Every operation is total on valid input; a trap on valid input FAILS. +Work is bounded by the profile (AGG n k lane steps, FSM n R rule tests); no fuel word is needed. +A session owns one state array; R1 has no references and no aliasing. + +### 1.3 Changes + +Valid only inside the current program's domain (AGG v < 2^wl, FSM v < A), with indices in range +and n <= 4096 after the change. Invalid: status 3, nothing changes. + +| op | payload words | meaning (0-based index) | +|---|---|---| +| 3 SET | i, v | x_i = v | +| 4 INS | i, v | insert v before index i, 0 <= i <= n | +| 5 DEL | i | remove index i | +| 6 SWAP | i, j | exchange indices i and j (i = j allowed) | +| 7 BATCH | m, then m triples (op, a, b), m 1..1024, op 3..6 (b unused by DEL, 0) | applied in order, all or nothing; validity of each item is judged on the state left by the items before it | +| 8 PROGRAM | 64 PROG words | new program, same state; refused (3) if any value is outside the new domain, refused (2) if PROG is invalid | + +Incremental obligation (#743): result(P, apply(s, d)) is what a delta path must return after any +valid change d. R1 is exact integers; no floating-point mode exists. + +## 2. Arm protocol (evaluator <-> arm process) + +The evaluator starts an arm with `execprog`, stdin and stdout pipes. Request: op, p, then p +words. Response: status, q, then q words. + +| op | p | q | +|---|---|---| +| 1 BEGIN | 65 + n: PROG, n, n values | 0; replaces any session | +| 2 QUERY | 0 | 3: r0, r1, route | +| 3, 4, 6 | 2 | 0 | +| 5 | 1 | 0 | +| 7 BATCH | 1 + 3m | 0 | +| 8 PROGRAM | 64 | 0 | +| 9 STATS | 0 | up to 32 counters (diagnostic) | +| 10 END | 0 | 0, then exit 0 | + +Status 0 ok, 2 invalid program, 3 invalid change, 4 unsupported request, 5 capacity, 6 no session. +An unknown op or p above 65 + 4096 + 1 + 3072 is answered 4 and the arm exits 4. A p that does not +match its op is 4 (the payload is read and discarded). route (never trusted): 1 FULL, 2 CACHE hit, +3 METHOD, 4 DELTA, 5 FALLBACK (a method or generation was refused; baseline used). + +argv: `ARM LIBDIR MANSHA ALLOW TAPFILE SPLIT` (unused = `-`). MANSHA is the hex SHA-256 of the +manifest the evaluator authorizes (section 3.5). ARM: + +| ARM | behaviour | +|---|---| +| echo | QUERY answers r0 = r1 = 0, route 0; pipe-overhead control, never scored for correctness | +| full | the qualified baseline: interprets PROG over the whole state at every QUERY | +| cache | exact answer cache: key = SHA-256(program digest, n, state digest); the state digest is recomputed only at the first QUERY after a change; at most 4096 entries, FIFO eviction; miss = full | +| method | methods recompute the whole state at every QUERY; no template or refused bind = full | +| delta | method plus delta maintenance (section 4); with no bound method the delta path uses the baseline lane map and δ | +| select | delta plus the #744 selector (section 5) | +| tap | full, plus one experience record per QUERY appended to TAPFILE with split SPLIT | + +Every arm binary is a router built by `om/reuse/drive.om`. The **baseline router B** is the router +built with an empty library. B serves `full`, `cache`, `tap`, `echo` and loads generations. + +## 3. Methods and the library (#742) + +### 3.1 Experience record (128 words, 1024 bytes) + +| Word | Meaning | +|---|---| +| 0 | magic 3544386028377026898 ("R1EXP001") | +| 1 | version 1 | +| 2 | sequence number, strictly increasing from 1 in a file | +| 3 | family | +| 4 | n | +| 5, 6 | r0, r1 of the baseline | +| 7 | ns of the baseline evaluation (diagnostic; never used for admission or selection) | +| 8 | steps (AGG n k, FSM rule tests) | +| 9 | split: 1 TRAIN, 2 CALIB, 3 DEV | +| 10, 11, 12 | session ordinal, query ordinal in the session, changes since the previous QUERY | +| 13 | provenance 1 = operator-owned local synthetic workload, consent yes, Apache-2.0, local only | +| 14 | sample count m, 0..16 | +| 16..47 | AGG: m pairs (x, e(x)) for the first m distinct lane values in index order. FSM: m words q + 256 a + 65536 q' for the first m distinct transitions taken. Rest 0 | +| 48..51 | state digest: SHA-256 of the n values as LE words | +| 52..55 | program digest | +| 15, 56..63 | 0 | +| 64..127 | PROG | + +Admission (`exp.om`): SHA-256 of the whole file appears in ALLOW as `<64 hex> AUTHORIZED <label>` +or `<64 hex> SYNTHETIC <label>` and no `<64 hex> REVOKED <label>` line exists; file size a +positive multiple of 1024, at most 32 MiB; every record: magic, version, sequence, family, valid +PROG, program digest equal to the recomputed one, split 1..3, provenance 1, m <= 16, zero words +zero, and **every sample verified** against the baseline (AGG e(x) recomputed; FSM δ recomputed). +One bad record refuses the whole file. r0, r1 and word 7 cannot be re-derived (the state is not +stored) and are never used. Admitted experience grants no authority. + +### 3.2 Templates and methods + +A **template** is an AGG shape (wl, f, k, op_1..op_k) with c_1..c_k as parameters, or the one FSM +template. A **method** is a template's generated function in a router. It applies to a program iff +the program is valid (guard G1) and its shape id equals the template's (G2), and it is used only +after bind validation (G3, section 3.3). A method computes the result for any state; it is not a +cached answer. + +AGG method code (generated): `mtl<t>` evaluates the template's ops in order with the constants +read from PROG words 4+2j; the run loop is the fixed text: acc = `r1ident f`; for i < n: +acc = `r1fold f acc (mtl<t> x_i)`. The fold and identity are the baseline's own functions. +FSM method: at bind, the table δ[s][a] for every s < S, a < A is filled by the baseline `r1delta` +(at most 256 cells); the run loop reads only the table and the baseline λ. + +Learning (honest name: **experience-selected guarded specialization**): `learn.om` counts QUERY +records per shape in admitted TRAIN experience and admits templates by the rule of section 5.3. +Outcomes are not used to form a method; experience decides which shapes are compiled. FSM methods +are tabulation of the rule program (guarded compilation), not learned transition semantics. No +rewrite (op-sequence simplification) exists in v1.0. + +### 3.3 Checking + +G3 bind validation, inside the deployed router, before any result of that method is returned: +AGG: for every x < 2^wl, `mtl<t> x` = baseline `r1lane x`, all 64 bits compared (at most 65,536 +pairs per program). Passed program digests are kept in a FIFO of 256 (process memory only; a +restart validates again). Failure = route 5 for that session. FSM: the table is built by the +baseline; nothing else is checked at bind. + +What this establishes, and its trusted base: for a bound AGG program the method's lane map equals +the omc-compiled baseline lane map on the whole lane domain, so (the fold, identity and loop being +the fixed baseline-calling text) the method's result equals the omc-compiled baseline's result for +every valid state. For FSM, the table equals the baseline δ on every cell; the run loop and λ +accumulation are fixed text checked by unit tests and by the evaluator's per-QUERY comparison, +not proven. Trusted base: omc 734f0a39, the Linux arm64 host, and the fixed loop text. B, the +methods and the evaluator's reference are all compiled by the same omc, so an omc miscompile can +affect them in common; known-answer vectors computed outside Omega (Wolfram Language, recorded in +`tests/reuse/kat.txt`) cover every op, every fold and both widths at their boundaries. +Finite probes, observational signatures and the six-input synthesis signature are never used as +proof. + +### 3.4 Library files and generations + +LIBDIR (0700): `gen-G.om` (router source), `gen-G.elf` (router), `gen-G.man` (manifest), +`CURRENT` (40 bytes: G, then the SHA-256 of gen-G.man). + +Manifest (256 words): 0 magic 3544386019401740626 ("R1MAN001"); 1 version 1; 2 G; 3 parent G; +4 template count K <= 16; 5 router source bytes; 6..9 router source SHA-256; 10..13 router ELF +SHA-256; 14..17 compiler SHA-256; 18 profile version 1; 19 lineage count L <= 8; 20..51 lineage: +L SHA-256 digests of admitted experience files (4 words each); 52..55 calibration file SHA-256 +(0 if none); 56..183 per template 8 words (shape id, family, wl, f, k, TRAIN QUERY count, source +bytes of its functions, 0); 184..254 0; 255 FNV-1a 64 of words 0..254 (corruption check only). + +Publish (ADR 0015 order): gen-G.om, gen-G.elf, gen-G.man are each written as `*.tmp`, fsync'd and +renamed with `renames` flag 1 (never replacing an existing generation); fsync LIBDIR; `CURRENT.tmp` +written, fsync'd, renamed onto CURRENT; fsync LIBDIR. A crash at any step leaves CURRENT naming +the previous complete generation. + +Load (B started with ARM method, delta or select): read CURRENT; read gen-G.man; require +SHA-256(gen-G.man) = the CURRENT digest = MANSHA from the evaluator; check magic, version, +self-check, compiler SHA-256 = the pinned omc, profile version 1, every lineage digest AUTHORIZED or +SYNTHETIC and not REVOKED in ALLOW; read gen-G.elf into a `memfd`, require its SHA-256 = manifest +words 10..13, then `execprog /proc/self/fd/N` with the same argv plus `loaded`. Any failure: +B serves the request stream itself as `full` with route 5 on every QUERY. A generation never +checks itself; B, which the evaluator pins, does the checking before exec, and the evaluator pins +MANSHA from its own reproducible rebuild (section 7.4). + +### 3.5 Identity and bounds + +Restart identity: two loads of one generation give the same manifest, ELF SHA-256 and responses. +`drive.om build` is deterministic: the same inputs give byte-identical gen-G.om, gen-G.elf and +gen-G.man. Bounds: K <= 16, router source <= 512 KiB, LIBDIR <= 8 MiB including retained +generations, delta state <= 1 MiB per session, cache <= 4096 entries. + +## 4. Incremental execution (#743) + +Terms of `spec/omega-incremental-semantics.md`: sources are the lanes or symbols; derived values +are the mapped lanes and the fold (AGG) and the states q_t and the λ sum (FSM). derivation_id = +SHA-256("r1.method" 0x00, out_type 2, library generation G, SHA-256 of the method's source text), +or SHA-256("r1.baseline" 0x00, 2, 0, SHA-256 of B's source) for the baseline lane map. A session +binds one derivation_id at BEGIN or PROGRAM; a response never mixes two. + +AGG delta state. ADD: acc' = acc - e(old) + e(new) mod 2^64; XOR: acc' = acc xor e(old) xor e(new). +OR and AND: count[b] = number of mapped lanes with bit b set, b < 16; result for n > 0: OR = +bits with count > 0, AND = bits with count = n (bits 16..63 are 0); n = 0 gives the identity. +MIN and MAX: a count per mapped value (2^wl counters) and the current extreme; when the last copy +of the extreme goes, scan to the next nonzero count (at most 2^wl steps, the adversarial case); +n = 0 gives the identity. SET = remove old + add new; INS = add; DEL = remove; SWAP = no work. + +FSM delta state: q_0..q_n (stored trajectory) and the sum. A SET at index i changes w_{i+1}; +recompute q_t for t = i+1, i+2, ... and stop at the first t > i with new q_t = stored q_t: from +there on every state is unchanged (early cutoff). INS at index i: the new symbol is w_{i+1}, old +w_t for t > i become w_{t+1}; recompute from t = i+1, compare new q_t with the stored state of the +same symbol suffix (old index t - 1), stop at the first equality, then shift the stored suffix. +DEL at index i: old w_{t+1} for t > i become w_t; recompute from t = i+1 comparing with old q_{t+1}, +then shift. SWAP = two SETs. BATCH: items are applied to the stored trajectory in order, cutoff +judged per item. The sum changes by the λ difference over the recomputed range only. No cutoff +means recomputation to n. + +Transactions: a change or BATCH is validated completely (section 1.3) before anything is +written; then the state and the delta state are updated; a refused request changes nothing. A +PROGRAM change or a revocation seen at QUERY drops the delta state and the binding and rebinds. +Revocation snapshot: ALLOW is read and hashed at BEGIN; at each QUERY its stat (inode, size, +mtime ns, ctime ns) is compared and on any difference ALLOW is re-read and the lineage re-checked; +a revoked lineage gives route 5 from that QUERY on. R1 graphs are a chain (FSM) or a one-level +DAG (AGG); no recursion occurs. Missing tracking or an unsupported case falls back explicitly +(route 3 or 1). + +## 5. Selection by lifecycle cost (#744) + +### 5.1 Order + +Validity and authority first: G1, G2, G3, generation identity, lineage not revoked. Only valid +options are ranked by cost. Cost never waives a guard. An unsupported op, family or path id is +refused before any cost lookup and never counted in a supported cost bucket (the #704 rule). + +### 5.2 The select arm + +Per QUERY, among the valid options {delta, method, full}; per BEGIN or PROGRAM, whether to bind a +method at all (bind validation costs up to 65,536 lane pairs). Inputs: the frozen calibration file +and deterministic features only (family, wl, f, k, n size class, changes since the last QUERY, +binding state). The select arm learns nothing at run time and measures nothing about itself. + +Calibration file (`R1CAL001`, magic 3544386010811150674), produced by the **evaluator** from its +own round-trip measurements on CALIB workloads (section 7): per family, per path (full, method, +delta, bind), per n class {64, 512, 4096}, per change class {0, 1, 2..64, >64}: p50 and p90 ns. +Missing file or a missing cell: the fixed rule of the delta arm (cold-start behaviour). +Rule: bind iff p50(bind) < E_q (p50(full) - p50(method)), E_q = mean QUERYs per session in CALIB; +per QUERY choose the lowest p50 among valid paths. Adversarial churn: after 64 invalidations or +64 cutoff misses in one session the delta path is disabled for that session. + +Run-time discovery is **NOT_RUN** in v1.0: it would make LIBDIR learn from FINAL and hide its cost +in children. Bounded discovery is the offline build (5.3). + +### 5.3 Offline template admission (the bounded discovery) + +From admitted TRAIN experience: count QUERY records per AGG shape. Admit shapes in descending +count (ties: lower shape id) while count >= 8 and K < 16. Stated assumption: the FINAL shape +distribution is the TRAIN distribution plus unseen shapes; no probabilistic guarantee is claimed. +The FSM template is admitted iff TRAIN has at least 8 FSM QUERY records. Budget: the build stops +with no publish if omc takes more than 120 s; every failed compile is recorded. + +### 5.4 Cost terms charged + +Per request (evaluator round trip): routing, guards, bind validation, hashing, delta work, method +or baseline execution, invalidation and rebinding, and arm start-to-ready including generation +load and identity checks (charged to the first request of each process). Offline: acquisition = +learn + codegen + omc + publish, measured by the evaluator, charged once per repetition. +Residency: LIBDIR bytes and peak RSS. Elapsed ns, CPU time and bytes are reported separately. + +Required tested behaviour: cold start (no calibration), high novelty (all-cold sessions), rule +drift, incorrect confidence (a calibration file 10x wrong: correctness unchanged, cost reported), +adversarial invalidation churn, exhausted budgets (K = 16 reached; omc over 120 s). + +## 6. Measurements + +By the evaluator: round-trip ns per request (monons), cold (first QUERY after BEGIN, PROGRAM or +process start) and warm, p50 / p95 / p99; each QUERY's r0, r1 against the evaluator's own reference +implementation; refusal statuses against the reference's; arm CPU time from `/proc/PID/stat` +(utime, stime, cutime, cstime) and memory from `/proc/PID/status` (VmHWM, VmRSS), read before +END; LIBDIR bytes; acquisition elapsed and CPU time (the build run as a timed child; omc child CPU +via cutime/cstime). By the arm, STATS (diagnostic only): route counts, binds, bind failures, +invalidations, cutoff lengths, delta disables. + +Payback: QUERYs until cumulative (full - arm) round-trip ns exceeds acquisition ns. +#744 normalized cost: Cbar = r + (1 - q) e + q + K/N with r, q, e, K, N re-estimated from these +measurements; the Wolfram numbers in #744 are illustrations, not targets. + +Placement: the evaluator runs on CPU 18 and every arm on CPU 19 (both X925 class, 3.9 GHz), set +by `taskset` at exec (an OS tool; `.om` has no affinity word); the CPU list and load average are +recorded per run. Every run is `nice -n 10`. + +## 7. Workloads, splits and holdout (#73) + +### 7.1 Splits + +The generator is part of the evaluator. TRAIN (experience), TRAIN' (the shape-permuted control +experience: same sizes, shape pool disjoint from TRAIN's), CALIB (calibration), DEV (adaptive +development, any number of runs), FINAL (sealed, one run). TRAIN, TRAIN', CALIB and DEV seeds are +public constants of the evaluator. AGG shape pool: 24 seen shapes drawn from the TRAIN seed; TRAIN +sessions use them with a 1/(rank + 1) weight; TRAIN' uses 24 other shapes with the same weights. + +### 7.2 FINAL composition (frozen counts) + +Families AGG and FSM; n in {64, 512, 4096}; session types, 4 sessions per family x type x n: + +| type | requests after BEGIN | +|---|---| +| Z zero change | 32 x QUERY | +| S sparse | 64 x (SET, QUERY) | +| D dense | 8 x (BATCH of 1024 SET, QUERY) | +| R reorder | 64 x (SWAP, QUERY) | +| I insert/delete | 64 x (INS or DEL alternating, QUERY), n kept within 1..4096 | +| A adversarial | AGG: f in {MIN, MAX}, 64 x (DEL of a current extreme, INS of a random value, QUERY); FSM: machines with no reconvergence, 64 x (SET at index 0, QUERY) | +| P rule drift | 16 x QUERY with a PROGRAM (same shape, new constants or new rules) before every 4th | +| C all-cold | as S, programs of unseen shapes only | + +That is 2 x 8 x 3 x 4 = 192 sessions. Novelty classes, AGG types Z S D R I A P: per 4 sessions +1 recall (a TRAIN program), 2 transfer (a seen shape, new constants), 1 novel (an unseen shape); +type C: 4 novel. FSM: 2 recall (TRAIN machines), 2 new machines. Sessions are dealt into 24 groups +of 8 by a seeded permutation; one group is one arm process lifetime (a restart between groups). + +Arms in FINAL: echo, full, cache, method, delta, idelta (delta with the empty library: B's delta +path), delta' (delta with the TRAIN' library), select. Each group runs on every arm, arm order per +group and repetition a seeded permutation; 3 repetitions; LIBDIR is copied fresh from the +evaluator's rebuild for every repetition. + +### 7.3 Holdout + +Before the evaluator freeze: a 32-byte secret from `random` is stored in the operator's TPM vault +(`atlas-vault add REUSE73_FINAL_SECRET`), and SHA-256(secret) is committed and pushed in +spark-rsi-mv `evidence/REUSE-73/final-commitment.txt`. FINAL seed = SHA-256(secret || +candidate-freeze commit id || evaluator-freeze commit id) (ASCII hex ids). Order: evaluator freeze +(spark-rsi-mv commit pushed), candidate freeze (omega-mv commit pushed, with the TRAIN and CALIB +library and its manifest SHA-256), then the reveal and one FINAL run. A void run burns the seed; +a new run needs a new secret and a recorded reason. Independence status: **UNISOLATED**. One +operator account holds both sides; non-access is procedural (separate authoring agents that read +only this contract, vault read only by the evaluator run) and auditable by commit order, not +enforced by the machine. + +### 7.4 Evaluator rebuild + +The evaluator rebuilds the library from the admitted TRAIN experience with the frozen `drive.om` +and requires its manifest SHA-256 to equal the candidate's frozen value; that value is MANSHA. +A mismatch is BLOCKED (not reproducible). + +### 7.5 Receipt for the #67 judge + +The evaluator writes a receipt with the RECEIPT-V2 identity fields of spark-rsi-mv +`docs/RECEIPT-V2.md` (subject = candidate-freeze commit and MANSHA, holdout-set digest = SHA-256 +of the generated FINAL request files, policy digest = SHA-256 of this contract, evaluator binary +digest, counts) and an evaluation-budget record (one FINAL run). Signing is BLOCKED: the #67 P-256 +signer is Rust and no `.om` signer exists; the receipt is unsigned and says so. + +## 8. Turing accounting (Definitions 1-3, omega-mv #700) + +Profile `calibration/profiles/Turing-profile-REUSE-v1.0.toml` (frozen with this file). D = the low +4 bits of r0 of every FINAL QUERY, in request order, given the inputs. B = the baseline router; +M(arm) = B plus that arm's library, calibration parameters and arm code. Every model predicts the +exact value; the coded distribution puts 65,521 / 65,536 on it and 1 / 65,536 on each other symbol +(CODER_SPEC: K 16, every q >= 1). B and M give identical streams, so their coded data lengths are +equal and cancel; the existing turing-coder (a647b6a9, rANS and range) is run on both to show it. +Model charge P1 (primary, declared parameters): 64 bits per template word of the manifest entries +plus 64 bits per calibration value an arm reads. P2 (sensitivity): 8 bits per byte of generated +method source and of arm-specific unit source. T(arm) = -(L(M|G) - L(B|G)) < 0 for every arm with a +library; ΔT between generations is minus the change of the charge, and a positive ΔT can only come +from deleting templates and is never reported as a gain. Equivalent-execution savings are +reported separately (section 6). A positive T is impossible in R1 by construction; this is stated +before any data. + +## 9. Controls (each must be detected; an undetected control is a STOP) + +| id | control | expected | +|---|---|---| +| C1 | router mutant: G2 ignores wl | wrong answers on the control workload | +| C1b | router mutant: a method that drops a final `AND 255` at wl 16 | G3 refuses at x = 256, route 5, no wrong answer; with G3 also disabled: wrong answers | +| C1c | router mutant: `mtl` reads c_j from the wrong word | G3 refuses | +| C2 | one byte changed in an admitted experience file; a record with a wrong program digest | admission refuses the file | +| C3 | sample outputs permuted across records; then costs permuted | first: admission refuses (sample check); second: rebuilt library byte-identical (costs are not used) | +| C4 | delta mutant keeping delta state across PROGRAM | wrong answers | +| C5 | lanes 2^wl - 1 with MUL and SHL; values >= 2^wl | known-answer values; status 3 | +| C6 | one byte of gen-G.elf; one byte of gen-G.man; a coherent replacement (new ELF, manifest and CURRENT consistent with each other) | load refuses (route 5): the first by ELF digest, the second by MANSHA and self-check, the third by MANSHA | +| C7 | lineage REVOKED before start; REVOKED during a session | load refuses; route 5 from the next QUERY | +| C8 | publisher killed after each publish step | CURRENT names a complete generation that loads | +| C9 | manifest compiler SHA-256 or profile version changed | refused | +| C10 | truncated manifest or CURRENT | refused | +| C11 | arm killed with SIGKILL mid-session | a fresh arm loads the same identity and answers equal | +| C12 | shape-permuted library (delta') and empty library (idelta) | reported as ablations, scored like delta | + +## 10. Decision (#73) and per-issue conclusions + +rho(arm) = (sum over FINAL of the arm's round-trip ns, every request including start-to-ready, +plus acquisition ns) / (the same sum for full). Uncertainty: percentile bootstrap over the 24 +session groups (clusters; each group's 3 repetitions averaged first; paired across arms), 10,000 +resamples, splitmix64 seeded with the first 8 bytes of the FINAL seed; 95 % intervals. + +- **STOP**: any wrong answer or wrong refusal from full, cache, method, delta, idelta, delta' or + select on FINAL; an undetected control; a tampered or revoked artifact served as a method; or + lower bound of rho(delta) >= 1. +- **GO**: no STOP condition; upper bound of rho(delta) < 1; upper bound of rho(delta)/rho(cache) + < 1; upper bound of rho(delta)/rho(idelta) < 1 (the learned library contributes); and at least + half of the transfer-class QUERYs of delta served by a method (route 3 or 4). +- **INVESTIGATE**: otherwise. + +Reported, never folded into the decision: rho and p50/p95/p99 per arm, session type, novelty +class, family and n; coverage and fallback; memory; payback; Cbar terms; T and ΔT; echo-subtracted +ratios as a secondary figure; the ns per interpreter step of full against the evaluator's +reference (so the denominator is not a strawman). #742 conclusion: transfer coverage, bind +failures, controls C1-C3, C6-C11, restart identity. #743: rho(delta)/rho(method) and +rho(idelta)/rho(full) per change regime; C4, C8. #744: rho(select)/rho(delta) (selector enabled +versus disabled at matched correctness): upper bound < 1 "selector helps", lower bound >= 1 "does +not help", otherwise inconclusive. Verdicts are given separately for source (review), host (Spark +CPU runs), simulator (NOT_RUN) and physical (NOT_RUN). + +## 11. Limits and blockers, stated before implementation + +- **Cortex (#742 persistence item): BLOCKED.** `rx_cortex` is C; the omega-bridge ABI runs + C -> .om only; there is no Cortex CLI; an `.om` Cortex writer would be a second writer (ADR 0022). + Admission claims go to the evidence directory. Route: #651 and the ADR 0022 owner. +- **#699 experience (#73 item): NOT MET.** R1 records come from the `.om` baseline tap on generated + workloads, not from World/J-Space transitions committed through compose, AEGIS and Cortex (PR + #707, C). R1 records carry #699's fields (prior state digest, action = the change counts and + query, outcome, cost) but not its receipt chain. +- **Judge signature: BLOCKED** (7.5). +- **Independence: UNISOLATED** (7.3). +- **FSM run loop**: not proven (3.3). +- Run-time discovery and rewrites: NOT_RUN in v1.0. +- GB10 NOT_RUN; energy NOT_MEASURED; simulator NOT_RUN. + +## 12. Unit interfaces (frozen for parallel authors) + +Units are concatenated by `drive.om` in this order into one program: `om/sha256/sha256.om` (main, +unchanged), `r1.om`, `exp.om`, `check.om`, `delta.om`, `select.om`, `lib.om`, generated +`methods.om`, `router.om` (holds `fn main`). Name prefixes: r1 (R1*), ex (EX*), ck (CK*), dl (DL*), +sl (SL*), lb (LB*), mt (MT*), rt (RT*), lg and cg (learn.om, codegen.om, in `drive.om` only). +Globals are pools; scalar state is a 1-element pool. + +r1.om: `global R1P ulong 64`, `R1N ulong 1`, `R1X ulong 4096`, `R1R ulong 2`, `R1T ulong 256` +(FSM table). `fn r1valid to int` (0 or 2, R1P), `fn r1indom v ulong to bool`, +`fn r1lane x ulong to ulong`, `fn r1ident f ulong to ulong`, `fn r1fold f ulong a ulong b ulong to +ulong`, `fn r1delta s ulong a ulong to ulong`, `fn r1full to void` (R1P, R1N, R1X -> R1R), +`fn r1shape to ulong`, `fn r1pdig to void` and `fn r1sdig to void` (digests into `R1PD ulong 4`, +`R1SD ulong 4`). Globals cannot be passed, so hashing goes through one fixed global buffer: +`global R1HB uchar 65536`, and `fn r1hash n ulong to void` hashes R1HB[0..n) into `R1HD ulong 4` +(through local 4096-byte chunks handed to `omsha`). +methods.om (generated; empty library = no templates): `fn mtfind to long` (template index for R1P +or -1), `fn mtlane t ulong x ulong to ulong`, `fn mtrun t ulong to void` (-> R1R), `fn mtcount to +ulong`, `fn mtshape t ulong to ulong`, `fn mtsrc t ulong D edit ulong 4 to void` (source SHA-256). +check.om: `fn ckbind t ulong to int` (0 pass, 1 fail; AGG all-x check, FSM table build into R1T). +delta.om: `fn dlbind t long to int` (t = -1: baseline lane map / δ), `fn dlchg op ulong i ulong v +ulong old ulong to int` called after the router has validated and applied one change to R1X +(`old` = previous value at i for SET and DEL, the value at j for SWAP; for SWAP v = j), +`fn dlres to int` (0 = R1R filled, 1 = needs recomputation), `fn dldrop to void`. +select.om: `fn slload to int` (calibration from `SLPATH uchar 4096`, 0 ok, 1 absent), +`fn slbind t long to bool`, `fn slpick nchg ulong to int` (1 full, 3 method, 4 delta). +lib.om: `fn lbload to int` (0 = exec'd, never returns; nonzero = refused with a code), `fn lbpub` +(drive.om only), manifest read and check functions. +router.om: argv, the request loop, change validation and application, the arms. +drive.om: `drive base OMC OUTDIR`, `drive build OMC ALLOW CALIB OUTLIB EXP...`, `drive selftest`; +it forks omc with stdin and stdout redirected by `dupfd`. +Build input and output files are named in `om/reuse/README.md`. An interface change needs a +contract version bump. diff --git a/evidence/REUSE-742/final/RESULT.md b/evidence/REUSE-742/final/RESULT.md new file mode 100644 index 000000000..19092d18f --- /dev/null +++ b/evidence/REUSE-742/final/RESULT.md @@ -0,0 +1,76 @@ +# REUSE-1 FINAL result for the candidate (#742, #743, #744) + +The frozen candidate is omega-mv `6ad09bff0c544bf73ceb3c63b349ba0cd6998e52`, with source `2b25e1bf`. +Commits after it change only `docs/` and `evidence/`. + +Clarifications K1..K8 are in the freeze. K9..K17 record decisions already present in the frozen +code; they were written into `docs/reuse/REUSE-1-CLARIFICATIONS.md` after FINAL, and no rule changed. + +The evaluation ran once on the FINAL split. The independent evaluator is spark-rsi-mv `4345eda9`. +Its raw evidence, reproduction commands and receipt are in spark-rsi-mv `84900162`, under +`evidence/REUSE-73/final/`. + +**Decision: STOP.** Contract section 10 stops when the lower bound of rho(delta) is at least 1. +Here rho(delta) = 1.164, with a 95 % interval of 1.040 to 1.307. + +Correctness passed: there were 0 wrong answers and 0 wrong refusals in 189,504 scored requests. +All 36 negative controls were detected. + +## Lifecycle cost + +Sums cover all 24 groups and 3 repetitions. Units are ms. + +| Arm | Round trips | Start to ready | Acquisition | Total | rho (95 % interval) | +|---|---|---|---|---|---| +| full | 854 | 65 | 0 | 867 | 1 | +| method | 824 | 134 | 222 | 1057 | 1.219 (1.091 to 1.363) | +| delta | 777 | 136 | 222 | 1010 | 1.164 (1.040 to 1.307) | +| idelta (empty library) | 638 | 62 | 0 | 651 | 0.751 (0.650 to 0.867) | +| select | 634 | 131 | 222 | 868 | 1.001 (0.887 to 1.127) | + +How the delta cost splits: +- Acquisition (three library builds) costs 222 ms. +- Loading a generation costs about 71 ms more start-to-ready than B. That covers manifest and lineage verification, the memfd ELF and the exec. +- Both arms use the same delta algebra, and every request in both was routed to it. Even so, the generation's round trips sum to 139 ms more than idelta's. +- That excess is concentrated at small n. Request-only rho at n = 64 is 1.585 for delta and 1.094 for idelta. +- [INFERENCE] The likely cause is the larger generation binary (111 kB of `.om` against 103 kB for B) plus per-request method-state maintenance. This was not profiled. + +## Per issue + +**#742, guarded methods from admitted experience.** +- Transfer coverage was 100 %: methods served all 5,616 transfer QUERYs. +- No QUERY fell back to route 5. +- Every integrity and lineage control was detected: C1, C1b, C1c with G3, C2, C3, C6, C7, C8 (K = 1..9), C9, C10 and C11 restart identity. +- The method arm costs more than full over the lifecycle: rho(method) = 1.219 (1.091 to 1.363). Its round trips alone are 0.965 of full's. +- No payback within the horizon. +- Cortex persistence: BLOCKED. There is no `.om` to C path; it is routed to #651 and the ADR 0022 owner. +- The #699 experience item: NOT MET. +- Run-time discovery and rewrites: NOT_RUN. + +**#743, incremental execution with exact invalidation and atomic generations.** +- Incremental execution without a library reduces lifecycle cost: rho(idelta) = 0.751 (0.650 to 0.867). +- Payback for idelta is immediate, at 0 queries. +- With the learned library it does not reduce cost: + - delta/method = 0.955 (0.854 to 1.079): inconclusive; + - delta/idelta = 1.551 (1.406 to 1.737): the library makes delta worse. +- Dense regime (type D) is a loss: delta/method 2.43 and idelta/full 2.41. Every other regime is between 0.37 and 1.04. +- Controls C4 (wrong delta unit) and C8 (crash after each of the 9 publish steps) were detected. +- Peak memory high-water mark is 832 kB for delta, against 232 kB for full. + +**#744, lifecycle-cost selection.** +- The selector helps relative to delta: select/delta = 0.859 (0.759 to 0.957). +- It is not better than full: rho(select) = 1.001 (0.887 to 1.127). +- It used 23,055 delta routes, 3,939 full routes and 78 method routes. +- Calibration came from CALIB only, and was handed over on fd 7 (K16). + +## Verdicts + +| Class | Verdict | +|---|---| +| Source | PASS after independent review (two candidate findings, P0 calibration TOCTOU and P1 churn counting, both fixed and verified) | +| Host (Spark CPU) | Correctness and controls PASS; lifecycle cost STOP | +| Simulator | NOT_RUN | +| Physical (GB10) | NOT_RUN | +| Energy | NOT_MEASURED | + +Independence is UNISOLATED: both sides ran under one operator account. The receipt is UNSIGNED, because the #67 signer is Rust. diff --git a/evidence/REUSE-742/freeze/FREEZE.txt b/evidence/REUSE-742/freeze/FREEZE.txt new file mode 100644 index 000000000..91207ca13 --- /dev/null +++ b/evidence/REUSE-742/freeze/FREEZE.txt @@ -0,0 +1,19 @@ +REUSE-1 candidate freeze (omega-mv #742 #743 #744; contract docs/reuse/REUSE-1.md v1.0, d6edaad1) + +candidate_source_commit=2b25e1bf4499cfccbd556ab66b797ca377424a2e (feat/742-reuse-r1; tests/reuse/run.om 19 passed, 0 failed) +evaluator_freeze_commit=4345eda9e58fe625a4d9b8e9175305160369f760 (spark-rsi-mv feat/73-reuse-eval, pushed 2026-10-25T16:20:47Z) +omc_sha256=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 +drive_elf_sha256=ade1c0497cf35a2733cc05510a8c438b0330a6296ef239bd3ec0e2469e1e9825 +base_elf_sha256=bf7dd0184a2f6e831d90a5617d760d1d50f50542541dbfaf64d20933d9382568 +evaluator_elf_sha256=b550ad7e5fed2a91381c7a11599979860eff5f10859de44020e5d65d42ec46cf +gen_elf_sha256=50930b4c2b4ab0ab1122e7ff4163c0dbe86782297d559b28bf22641e591387a9 +exp_TRAIN_sha256=cc6298e61e36a4ccd43d053b5ae88b92d186f90e1ea5c27bc2d8870582d38fd8 bytes=1966080 records=1920 +exp_TRAINP_sha256=31af132b2fb8dec33a91de37f493bcefd927dd88a659b7c4b6529d1788c7cc48 bytes=1966080 records=1920 +calib_sha256=c7971e77c59e588abf5f20399e18d35e5cf4fa3b88a64dc27b73282326a76ab0 (written by the frozen evaluator from CALIB runs of arms full, method, delta, 3 repetitions, with the TRAIN library built without calibration, manifest aef37d546f763dc0cbe141a82810fa1e9f3624c692a261220d2bdee8666ddf56) +library_TRAIN_mansha=05f163f8301ecaa40f43a30a6bebcdc253b65688aa384e209c83eba9dfaa7cf6 templates=16 generation=1 elf_sha256=b83e7040d8c75e9cf55586031dc00736612447224d794c115a23df0590a87ac9 om_bytes=111472 +library_TRAINP_mansha=b15f71dcfc2e095a93d470cd1c54ea2ed43a35726452f220f93563f506d94c61 generation=1 elf_sha256=cb6c802a1cc1aa4c7a6fd524311f54be3912469c69c2773b34d6c47dd3f007f9 om_bytes=111139 +rebuild_check=3 builds of each library by the frozen evaluator (judge build, repetitions 0..2): identical manifests, ELF and source (contract 7.4: reproducible) +acquisition_ns_TRAIN=81051123 68514396 72818116 +acquisition_ns_TRAINP=66233973 75181259 66066436 +library ELFs are not committed: drive.om rebuilds them byte for byte from this source, the experience files and calib.r1cal. +FINAL: not generated before this commit is pushed; seed = SHA-256(secret || this commit id || evaluator_freeze_commit), commitment spark-rsi-mv evidence/REUSE-73/final-commitment.txt. diff --git a/evidence/REUSE-742/freeze/allow.txt b/evidence/REUSE-742/freeze/allow.txt new file mode 100644 index 000000000..a6f638c36 --- /dev/null +++ b/evidence/REUSE-742/freeze/allow.txt @@ -0,0 +1,2 @@ +cc6298e61e36a4ccd43d053b5ae88b92d186f90e1ea5c27bc2d8870582d38fd8 AUTHORIZED reuse1-train-freeze +31af132b2fb8dec33a91de37f493bcefd927dd88a659b7c4b6529d1788c7cc48 AUTHORIZED reuse1-trainp-freeze diff --git a/evidence/REUSE-742/freeze/calib.r1cal b/evidence/REUSE-742/freeze/calib.r1cal new file mode 100644 index 000000000..fae477009 Binary files /dev/null and b/evidence/REUSE-742/freeze/calib.r1cal differ diff --git a/evidence/REUSE-742/freeze/exp-TRAIN.bin b/evidence/REUSE-742/freeze/exp-TRAIN.bin new file mode 100644 index 000000000..a69f39be9 Binary files /dev/null and b/evidence/REUSE-742/freeze/exp-TRAIN.bin differ diff --git a/evidence/REUSE-742/freeze/exp-TRAINP.bin b/evidence/REUSE-742/freeze/exp-TRAINP.bin new file mode 100644 index 000000000..6db57678e Binary files /dev/null and b/evidence/REUSE-742/freeze/exp-TRAINP.bin differ diff --git a/evidence/REUSE-742/freeze/lib-P/CURRENT b/evidence/REUSE-742/freeze/lib-P/CURRENT new file mode 100644 index 000000000..dd0830fd0 Binary files /dev/null and b/evidence/REUSE-742/freeze/lib-P/CURRENT differ diff --git a/evidence/REUSE-742/freeze/lib-P/gen-1.man b/evidence/REUSE-742/freeze/lib-P/gen-1.man new file mode 100644 index 000000000..02d83655c Binary files /dev/null and b/evidence/REUSE-742/freeze/lib-P/gen-1.man differ diff --git a/evidence/REUSE-742/freeze/lib-P/gen-1.om b/evidence/REUSE-742/freeze/lib-P/gen-1.om new file mode 100644 index 000000000..3161745a2 --- /dev/null +++ b/evidence/REUSE-742/freeze/lib-P/gen-1.om @@ -0,0 +1,5979 @@ +note SPDX-License-Identifier: Apache-2.0 +note sha256.om: FIPS 180-4 SHA-256 as one function-mode unit, bound through the omega-bridge native +note call ABI (docs/NATIVE-ABI.md) and exported as omega_omsha. It replaces the algorithm of +note src/sha256.c: the C file keeps only the sha256_* entry points that cut caller data into +note pieces of at most 4096 bytes. +note +note The whole hash state lives in the caller's sha256_ctx (104 bytes, src/sha256.h): +note bytes 0..31 state[8], uint32 little endian +note bytes 32..39 count, uint64 little endian (bytes absorbed so far) +note bytes 40..103 buffer, the pending partial block +note omsha c b n op w b8: +note op 0 init: count 0, FIPS initial hash value +note op 1 absorb b[0..n), n <= 4096, with src/sha256.c's buffering rules +note op 2 final: pad, absorb the bit length, the 32-byte digest packed into w[0..4) +note op 3 one shot: the digest of the n < 4096 bytes of b8 packed into w[0..4); c is not used +note w is the caller's scratch for the message schedule (64 words, contents ignored and overwritten): +note Omega writes every word before reading it, so the caller need not clear it. +note n > 4096 traps 3; any other op traps 9. + +note little-endian word of nb bytes at byte offset p of the context +fn getle c edit uchar 104 p ulong nb ulong to ulong +v = ulong 0 +i = nb +while gt i 0 bound 8 +i = sub i 1 +v = or shl v 8 ulong get c add p i +end +ret v +end + +fn putle c edit uchar 104 p ulong nb ulong v ulong to void +x = v +for i ulong nb +put c add p i uchar and x 255 +x = shr x 8 +end +end + +note the hash words of the context into h, and back +fn loadh c edit uchar 104 h edit uint 8 to void +for i ulong 8 +put h i uint getle c mul i 4 4 +end +end + +fn storeh c edit uchar 104 h edit uint 8 to void +for i ulong 8 +putle c mul i 4 4 ulong get h i +end +end + +note big-endian words of the block at byte o of b into w[0..16), then the schedule +fn schedb b view uchar 4096 o ulong w edit ulong 64 to void +for i ulong 16 +q = add o mul i 4 +v = ulong get b q +v = or shl v 8 ulong get b add q 1 +v = or shl v 8 ulong get b add q 2 +v = or shl v 8 ulong get b add q 3 +put w i or v shl v 32 +end +expand w +end + +note big-endian words of the block at byte 40 of b into w[0..16), then the schedule +fn schedc b edit uchar 104 w edit ulong 64 to void +for i ulong 16 +q = add 40 mul i 4 +v = ulong get b q +v = or shl v 8 ulong get b add q 1 +v = or shl v 8 ulong get b add q 2 +v = or shl v 8 ulong get b add q 3 +put w i or v shl v 32 +end +expand w +end + +note the 64 rounds, unrolled with the working words in locals (names rotate instead of moving). +note Same lazy low-32-bit cells and rotate trick as expand; K is an immediate; the eight words are +note reduced to 32 bits only when they are added back into h. +fn rounds h edit uint 8 w view ulong 64 to void +a = ulong get h 0 +b = ulong get h 1 +c = ulong get h 2 +d = ulong get h 3 +e = ulong get h 4 +f = ulong get h 5 +g = ulong get h 6 +x = ulong get h 7 +t = ulong 0 +u = ulong 0 +t = add x add get w 0 1116352408 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 1 1899447441 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 2 3049323471 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 3 3921009573 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 4 961987163 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 5 1508970993 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 6 2453635748 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 7 2870763221 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 8 3624381080 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 9 310598401 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 10 607225278 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 11 1426881987 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 12 1925078388 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 13 2162078206 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 14 2614888103 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 15 3248222580 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 16 3835390401 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 17 4022224774 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 18 264347078 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 19 604807628 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 20 770255983 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 21 1249150122 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 22 1555081692 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 23 1996064986 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 24 2554220882 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 25 2821834349 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 26 2952996808 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 27 3210313671 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 28 3336571891 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 29 3584528711 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 30 113926993 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 31 338241895 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 32 666307205 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 33 773529912 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 34 1294757372 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 35 1396182291 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 36 1695183700 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 37 1986661051 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 38 2177026350 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 39 2456956037 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 40 2730485921 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 41 2820302411 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 42 3259730800 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 43 3345764771 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 44 3516065817 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 45 3600352804 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 46 4094571909 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 47 275423344 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 48 430227734 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 49 506948616 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 50 659060556 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 51 883997877 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 52 958139571 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 53 1322822218 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 54 1537002063 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 55 1747873779 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 56 1955562222 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 57 2024104815 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 58 2227730452 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 59 2361852424 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 60 2428436474 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 61 2756734187 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 62 3204031479 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 63 3329325298 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add ulong get h 0 a +put h 0 uint and t 4294967295 +t = add ulong get h 1 b +put h 1 uint and t 4294967295 +t = add ulong get h 2 c +put h 2 uint and t 4294967295 +t = add ulong get h 3 d +put h 3 uint and t 4294967295 +t = add ulong get h 4 e +put h 4 uint and t 4294967295 +t = add ulong get h 5 f +put h 5 uint and t 4294967295 +t = add ulong get h 6 g +put h 6 uint and t 4294967295 +t = add ulong get h 7 x +put h 7 uint and t 4294967295 +end + +fn tobuf c edit uchar 104 bi ulong b view uchar 4096 bo ulong n ulong to void +for i ulong n +put c add 40 add bi i get b add bo i +end +end + +note absorb b[0..n), n <= 4096, exactly as src/sha256.c sha256_update does for that piece +fn absorb c edit uchar 104 b view uchar 4096 n ulong w edit ulong 64 to void +if gt n 4096 +trap 3 +end +h = new uint 8 0 +cnt = getle c 32 8 +bi = and cnt 63 +putle c 32 8 add cnt n +off = ulong 0 +if gt bi 0 +lf = sub 64 bi +if lt n lf +tobuf c bi b 0 n +ret +end +tobuf c bi b 0 lf +loadh c h +schedc c w +rounds h w +storeh c h +off = lf +end +if le add off 64 n +loadh c h +while le add off 64 n bound 65 +schedb b off w +rounds h w +off = add off 64 +end +storeh c h +end +tobuf c 0 b off sub n off +end + +fn init c edit uchar 104 to void +putle c 32 8 0 +putle c 0 4 1779033703 +putle c 4 4 3144134277 +putle c 8 4 1013904242 +putle c 12 4 2773480762 +putle c 16 4 1359893119 +putle c 20 4 2600822924 +putle c 24 4 528734635 +putle c 28 4 1541459225 +end + +note compress the context's own block buffer (bytes 40..103) into its hash words +fn compbuf c edit uchar 104 w edit ulong 64 to void +h = new uint 8 0 +loadh c h +schedc c w +rounds h w +storeh c h +end + +note zero buffer bytes s..e-1 (indices into the 64-byte block) +fn zbuf c edit uchar 104 s ulong e ulong to void +i = s +while lt i e bound 64 +put c add 40 i 0 +i = add i 1 +end +end + +note sha256_final of src/sha256.c, padding in place: 0x80, zeros to 56 mod 64 (one more block +note when the pending part is 56 bytes or more), the 64-bit big-endian bit count, then the digest. +note The count field is left as it was (src/sha256.c adds the padding to it; nothing reads it after). +fn final c edit uchar 104 w edit ulong 64 to void +cnt = getle c 32 8 +bi = and cnt 63 +put c add 40 bi 128 +zbuf c add bi 1 64 +if ge bi 56 +compbuf c w +zbuf c 0 56 +end +bits = mul cnt 8 +for i ulong 8 +sh = mul sub 7 i 8 +put c add 96 i uchar and shr bits sh 255 +end +compbuf c w +h = new uint 8 0 +loadh c h +digest8 h w +end + +note IV into h +fn initiv h edit uint 8 to void +put h 0 uint 1779033703 +put h 1 uint 3144134277 +put h 2 uint 1013904242 +put h 3 uint 2773480762 +put h 4 uint 1359893119 +put h 5 uint 2600822924 +put h 6 uint 528734635 +put h 7 uint 1541459225 +end + +note expand w[0..16) into the full 64-word schedule, unrolled (constant indices need no bounds checks). +note Every schedule word is stored duplicated, the 32-bit value in both halves (d = (x << 32) | x), so +note a rotate right by r is the low half of d >> r and a plain shift right by n is d >> (32 + n); sums +note are formed in the low 32 bits (add, and, xor, or need no masks) and duplicated once on store. +fn expand w edit ulong 64 to void +u = ulong 0 +v = ulong 0 +x = ulong 0 +x = get w 14 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 1 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 9 +u = add u get w 0 +u = shl u 32 +put w 16 or u shr u 32 +x = get w 15 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 2 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 10 +u = add u get w 1 +u = shl u 32 +put w 17 or u shr u 32 +x = get w 16 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 3 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 11 +u = add u get w 2 +u = shl u 32 +put w 18 or u shr u 32 +x = get w 17 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 4 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 12 +u = add u get w 3 +u = shl u 32 +put w 19 or u shr u 32 +x = get w 18 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 5 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 13 +u = add u get w 4 +u = shl u 32 +put w 20 or u shr u 32 +x = get w 19 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 6 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 14 +u = add u get w 5 +u = shl u 32 +put w 21 or u shr u 32 +x = get w 20 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 7 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 15 +u = add u get w 6 +u = shl u 32 +put w 22 or u shr u 32 +x = get w 21 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 8 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 16 +u = add u get w 7 +u = shl u 32 +put w 23 or u shr u 32 +x = get w 22 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 9 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 17 +u = add u get w 8 +u = shl u 32 +put w 24 or u shr u 32 +x = get w 23 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 10 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 18 +u = add u get w 9 +u = shl u 32 +put w 25 or u shr u 32 +x = get w 24 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 11 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 19 +u = add u get w 10 +u = shl u 32 +put w 26 or u shr u 32 +x = get w 25 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 12 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 20 +u = add u get w 11 +u = shl u 32 +put w 27 or u shr u 32 +x = get w 26 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 13 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 21 +u = add u get w 12 +u = shl u 32 +put w 28 or u shr u 32 +x = get w 27 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 14 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 22 +u = add u get w 13 +u = shl u 32 +put w 29 or u shr u 32 +x = get w 28 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 15 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 23 +u = add u get w 14 +u = shl u 32 +put w 30 or u shr u 32 +x = get w 29 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 16 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 24 +u = add u get w 15 +u = shl u 32 +put w 31 or u shr u 32 +x = get w 30 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 17 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 25 +u = add u get w 16 +u = shl u 32 +put w 32 or u shr u 32 +x = get w 31 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 18 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 26 +u = add u get w 17 +u = shl u 32 +put w 33 or u shr u 32 +x = get w 32 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 19 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 27 +u = add u get w 18 +u = shl u 32 +put w 34 or u shr u 32 +x = get w 33 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 20 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 28 +u = add u get w 19 +u = shl u 32 +put w 35 or u shr u 32 +x = get w 34 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 21 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 29 +u = add u get w 20 +u = shl u 32 +put w 36 or u shr u 32 +x = get w 35 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 22 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 30 +u = add u get w 21 +u = shl u 32 +put w 37 or u shr u 32 +x = get w 36 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 23 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 31 +u = add u get w 22 +u = shl u 32 +put w 38 or u shr u 32 +x = get w 37 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 24 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 32 +u = add u get w 23 +u = shl u 32 +put w 39 or u shr u 32 +x = get w 38 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 25 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 33 +u = add u get w 24 +u = shl u 32 +put w 40 or u shr u 32 +x = get w 39 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 26 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 34 +u = add u get w 25 +u = shl u 32 +put w 41 or u shr u 32 +x = get w 40 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 27 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 35 +u = add u get w 26 +u = shl u 32 +put w 42 or u shr u 32 +x = get w 41 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 28 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 36 +u = add u get w 27 +u = shl u 32 +put w 43 or u shr u 32 +x = get w 42 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 29 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 37 +u = add u get w 28 +u = shl u 32 +put w 44 or u shr u 32 +x = get w 43 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 30 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 38 +u = add u get w 29 +u = shl u 32 +put w 45 or u shr u 32 +x = get w 44 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 31 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 39 +u = add u get w 30 +u = shl u 32 +put w 46 or u shr u 32 +x = get w 45 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 32 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 40 +u = add u get w 31 +u = shl u 32 +put w 47 or u shr u 32 +x = get w 46 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 33 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 41 +u = add u get w 32 +u = shl u 32 +put w 48 or u shr u 32 +x = get w 47 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 34 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 42 +u = add u get w 33 +u = shl u 32 +put w 49 or u shr u 32 +x = get w 48 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 35 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 43 +u = add u get w 34 +u = shl u 32 +put w 50 or u shr u 32 +x = get w 49 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 36 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 44 +u = add u get w 35 +u = shl u 32 +put w 51 or u shr u 32 +x = get w 50 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 37 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 45 +u = add u get w 36 +u = shl u 32 +put w 52 or u shr u 32 +x = get w 51 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 38 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 46 +u = add u get w 37 +u = shl u 32 +put w 53 or u shr u 32 +x = get w 52 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 39 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 47 +u = add u get w 38 +u = shl u 32 +put w 54 or u shr u 32 +x = get w 53 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 40 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 48 +u = add u get w 39 +u = shl u 32 +put w 55 or u shr u 32 +x = get w 54 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 41 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 49 +u = add u get w 40 +u = shl u 32 +put w 56 or u shr u 32 +x = get w 55 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 42 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 50 +u = add u get w 41 +u = shl u 32 +put w 57 or u shr u 32 +x = get w 56 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 43 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 51 +u = add u get w 42 +u = shl u 32 +put w 58 or u shr u 32 +x = get w 57 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 44 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 52 +u = add u get w 43 +u = shl u 32 +put w 59 or u shr u 32 +x = get w 58 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 45 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 53 +u = add u get w 44 +u = shl u 32 +put w 60 or u shr u 32 +x = get w 59 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 46 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 54 +u = add u get w 45 +u = shl u 32 +put w 61 or u shr u 32 +x = get w 60 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 47 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 55 +u = add u get w 46 +u = shl u 32 +put w 62 or u shr u 32 +x = get w 61 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 48 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 56 +u = add u get w 47 +u = shl u 32 +put w 63 or u shr u 32 +end + +note Word access for the aligned paths. b8 is the caller's 8-byte-aligned copy of the input lent a +note second time as ulong cells (b views the same bytes), so one load brings 8 bytes. Swapping the +note bytes inside each 32-bit half (two mask-and-shift steps; the constants stay in registers) turns +note little-endian bytes b0..b7 into the big-endian words b0..b3 (low half) and b4..b7 (high half). +note Each word is stored duplicated, as expand requires. +fn blk8 b8 view ulong 512 q ulong w edit ulong 64 to void +for i ulong 8 +x = get b8 add q i +y = or shl and x 71777214294589695 8 and shr x 8 71777214294589695 +y = or shl and y 281470681808895 16 and shr y 16 281470681808895 +j = mul i 2 +t = shl y 32 +put w j or t shr t 32 +put w add j 1 or and y 18446744069414584320 shr y 32 +end +end + +note the padded tail b[off..n) (less than 64 bytes), read as words through b8 (the bytes past n +note are inside the caller's 4096-byte copy and are masked off here): the word holding byte n keeps +note its k valid bytes and gets 0x80 after them, later words are zero, the 64-bit bit count goes in +note words 14 and 15; a second block when the tail is 56 bytes or more +fn tail8 h edit uint 8 b8 view ulong 512 off ulong n ulong w edit ulong 64 to void +blk8 b8 shr off 3 w +r = sub n off +fw = shr r 2 +k = and r 3 +sh = mul k 8 +v = get w fw +v = and v shl 4294967295 sub 32 sh +v = and v 4294967295 +v = or v shl 128 sub 24 sh +put w fw or v shl v 32 +i = add fw 1 +while lt i 16 bound 16 +put w i 0 +i = add i 1 +end +if ge r 56 +expand w +rounds h w +for j ulong 14 +put w j 0 +end +end +bits = mul n 8 +v = shr bits 32 +put w 14 or v shl v 32 +v = and bits 4294967295 +put w 15 or v shl v 32 +expand w +rounds h w +end + +note the digest of h as 32 bytes packed little endian into w[0..4): word i holds the big-endian +note bytes of h[2i] then h[2i+1], so the caller copies the digest out of w with memcpy +fn digest8 h edit uint 8 w edit ulong 64 to void +for i ulong 4 +j = mul i 2 +x = or ulong get h j shl ulong get h add j 1 32 +y = or shl and x 71777214294589695 8 and shr x 8 71777214294589695 +y = or shl and y 281470681808895 16 and shr y 16 281470681808895 +put w i y +end +end + +note SHA-256 of the n < 4096 bytes in b8, state in locals only (sha256_hash's short path) +fn oneshot b8 view ulong 512 n ulong w edit ulong 64 to void +if ge n 4096 +trap 3 +end +h = new uint 8 0 +initiv h +off = ulong 0 +while le add off 64 n bound 64 +blk8 b8 shr off 3 w +expand w +rounds h w +off = add off 64 +end +tail8 h b8 off n w +digest8 h w +end + +fn omsha c edit uchar 104 b view uchar 4096 n ulong op ulong w edit ulong 64 b8 view ulong 512 to void +if eq op 0 +init c +ret +end +if eq op 1 +absorb c b n w +ret +end +if eq op 2 +final c w +ret +end +if eq op 3 +oneshot b8 n w +ret +end +trap 9 +end +note SPDX-License-Identifier: Apache-2.0 +note r1.om: the REUSE-1 profile R1 baseline (docs/reuse/REUSE-1.md sections 1.1-1.3 and 12). +note Concatenated after om/sha256/sha256.om. All state is in the pools below (globals cannot be passed). +note R1P the current program, 64 words R1N n (1 word) R1X the state, 4096 words +note R1R r0, r1 of the last r1full R1T FSM table, delta(s, a) at index s*16 + a +note R1PD program digest, R1SD state digest, R1HD r1hash output: SHA-256 as 4 words, word j = +note digest bytes 8j..8j+7 read big endian +note R1HB the hash input buffer (r1hash hashes R1HB[0..n), n <= 65536) +note Exported beyond contract 12 (r1 prefix): +note r1step op c v m one lane op (1 ADD .. 8 SHR) on v with mask m = 2^wl - 1 +note r1chg op a b n validity of one change (op 3..6, payload a, b; DEL needs b = 0) against +note the current program's domain and a state of n values: the new n, or -1 +note r1apply op a b apply one change already validated by r1chg to R1X and R1N; returns +note the old value at a (SET, DEL), the old value at b (SWAP), 0 (INS) +note r1alldom every x_i (i < n) inside the current program's domain +note r1bswap x the byte reversal of x +note r1putw off w w as 8 little-endian bytes into R1HB[off..off+8) +note A call outside a valid program (unknown op or fold) traps 10; R1 never reaches it on valid input. +global R1P ulong 64 +global R1N ulong 1 +global R1X ulong 4096 +global R1R ulong 2 +global R1T ulong 256 +global R1PD ulong 4 +global R1SD ulong 4 +global R1HB uchar 65536 +global R1HD ulong 4 + +note AGG words 2..63 (word 0 and 1 are checked by r1valid) +fn r1vagg to int +wl = get R1P 2 +if and ne wl 8 ne wl 16 +ret 2 +end +f = get R1P 3 +if or lt f 1 gt f 6 +ret 2 +end +k = get R1P 4 +if or lt k 1 gt k 6 +ret 2 +end +lim = shl ulong 1 wl +for j ulong 6 +op = get R1P add 5 mul j 2 +c = get R1P add 6 mul j 2 +if lt j k +if or lt op 1 gt op 8 +ret 2 +end +if ge c lim +ret 2 +end +if and ge op 7 ge c wl +ret 2 +end +else +if or ne op 0 ne c 0 +ret 2 +end +end +end +for i ulong 47 +if ne get R1P add i 17 0 +ret 2 +end +end +ret 0 +end + +note one FSM rule word for S = ns states and A = na symbols: 0 valid, 2 invalid +fn r1vrule w ulong ns ulong na ulong to int +s = and w 255 +lo = and shr w 8 255 +hi = and shr w 16 255 +t = shr w 24 +if or ge s ns ge t ns +ret 2 +end +if or gt lo hi ge hi na +ret 2 +end +ret 0 +end + +note FSM words 2..63 +fn r1vfsm to int +ns = get R1P 2 +if or lt ns 2 gt ns 16 +ret 2 +end +na = get R1P 3 +if or lt na 2 gt na 16 +ret 2 +end +if ge get R1P 4 ns +ret 2 +end +nr = get R1P 5 +if or lt nr 1 gt nr 24 +ret 2 +end +for i ulong 16 +v = get R1P add 6 i +if lt i ns +if ge v 256 +ret 2 +end +else +if ne v 0 +ret 2 +end +end +end +for i ulong 24 +w = get R1P add 22 i +if lt i nr +if ne r1vrule w ns na 0 +ret 2 +end +else +if ne w 0 +ret 2 +end +end +end +for i ulong 18 +if ne get R1P add i 46 0 +ret 2 +end +end +ret 0 +end + +note 0 when R1P is a valid R1 program (1.1), else 2 +fn r1valid to int +if ne get R1P 0 3544411312749556050 +ret 2 +end +fam = get R1P 1 +if eq fam 1 +ret r1vagg +end +if eq fam 2 +ret r1vfsm +end +ret 2 +end + +note v inside the current (valid) program's domain: AGG v < 2^wl, FSM v < A +fn r1indom v ulong to bool +if eq get R1P 1 1 +ret lt v shl ulong 1 get R1P 2 +end +ret lt v get R1P 3 +end + +fn r1step op ulong c ulong v ulong m ulong to ulong +if eq op 1 +ret and add v c m +end +if eq op 2 +ret and sub v c m +end +if eq op 3 +ret and mul v c m +end +if eq op 4 +ret and v c +end +if eq op 5 +ret or v c +end +if eq op 6 +ret xor v c +end +if eq op 7 +ret and shl v c m +end +if eq op 8 +ret shr v c +end +trap 10 +end + +note the lane map e(x) of the current AGG program (1.2) +fn r1lane x ulong to ulong +m = sub shl ulong 1 get R1P 2 1 +k = get R1P 4 +v = x +for j ulong k +q = add 5 mul j 2 +v = r1step get R1P q get R1P add q 1 v m +end +ret v +end + +fn r1ident f ulong to ulong +if or eq f 1 eq f 2 +ret 0 +end +if eq f 3 +ret 18446744073709551615 +end +if or eq f 4 eq f 5 +ret 0 +end +if eq f 6 +ret 18446744073709551615 +end +trap 10 +end + +fn r1fold f ulong a ulong b ulong to ulong +if eq f 1 +ret add a b +end +if eq f 2 +ret xor a b +end +if eq f 3 +if lt b a +ret b +end +ret a +end +if eq f 4 +if gt b a +ret b +end +ret a +end +if eq f 5 +ret or a b +end +if eq f 6 +ret and a b +end +trap 10 +end + +note delta(s, a) of the current FSM program: the target of the lowest-numbered matching rule, else s +fn r1delta s ulong a ulong to ulong +nr = get R1P 5 +for i ulong nr +w = get R1P add 22 i +if eq and w 255 s +lo = and shr w 8 255 +hi = and shr w 16 255 +if and le lo a le a hi +ret shr w 24 +end +end +end +ret s +end + +fn r1fagg to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc r1lane get R1X i +end +put R1R 0 acc +put R1R 1 n +end + +fn r1ffsm to void +n = get R1N 0 +q = get R1P 4 +sum = ulong 0 +for i ulong n +q = r1delta q get R1X i +sum = add sum get R1P add 6 q +end +put R1R 0 q +put R1R 1 sum +end + +note the baseline: R1P, R1N, R1X -> R1R (1.2) +fn r1full to void +if eq get R1P 1 1 +r1fagg +ret +end +r1ffsm +end + +note shape id (1.1): AGG wl + 256 f + 4096 k + 65536 sum op_j 16^(j-1); FSM 1 +fn r1shape to ulong +if eq get R1P 1 2 +ret 1 +end +k = get R1P 4 +s = add get R1P 2 add mul get R1P 3 256 mul k 4096 +m = ulong 65536 +for j ulong k +s = add s mul m get R1P add 5 mul j 2 +m = mul m 16 +end +ret s +end + +fn r1bswap x ulong to ulong +y = ulong 0 +v = x +for i ulong 8 +y = or shl y 8 and v 255 +v = shr v 8 +end +ret y +end + +fn r1putw off ulong w ulong to void +v = w +for i ulong 8 +put R1HB add off i uchar and v 255 +v = shr v 8 +end +end + +note SHA-256 of R1HB[0..n) into R1HD, through omsha in local 4096-byte chunks +fn r1hash n ulong to void +if gt n 65536 +trap 3 +end +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +off = ulong 0 +while lt off n bound 17 +m = sub n off +if gt m 4096 +m = 4096 +end +for i ulong m +put b i get R1HB add off i +end +omsha c b m 1 w b8 +off = add off m +end +omsha c b 0 2 w b8 +for j ulong 4 +put R1HD j r1bswap get w j +end +end + +note program digest: SHA-256 of the 512 bytes of R1P (64 LE words) into R1PD +fn r1pdig to void +for i ulong 64 +r1putw mul i 8 get R1P i +end +r1hash 512 +for j ulong 4 +put R1PD j get R1HD j +end +end + +note state digest: SHA-256 of the n values as LE words into R1SD +fn r1sdig to void +n = get R1N 0 +for i ulong n +r1putw mul i 8 get R1X i +end +r1hash mul n 8 +for j ulong 4 +put R1SD j get R1HD j +end +end + +fn r1alldom to bool +n = get R1N 0 +for i ulong n +if not r1indom get R1X i +ret false +end +end +ret true +end + +note one change (1.3) against a state of n values: the new n, or -1 when invalid +fn r1chg op ulong a ulong b ulong n ulong to long +if eq op 3 +if and lt a n r1indom b +ret long n +end +ret -1 +end +if eq op 4 +if and and le a n lt n 4096 r1indom b +ret long add n 1 +end +ret -1 +end +if eq op 5 +if and lt a n eq b 0 +ret long sub n 1 +end +ret -1 +end +if eq op 6 +if and lt a n lt b n +ret long n +end +ret -1 +end +ret -1 +end + +fn r1apply op ulong a ulong b ulong to ulong +n = get R1N 0 +if eq op 3 +old = get R1X a +put R1X a b +ret old +end +if eq op 4 +t = n +while gt t a bound 4097 +put R1X t get R1X sub t 1 +t = sub t 1 +end +put R1X a b +put R1N 0 add n 1 +ret 0 +end +if eq op 5 +old = get R1X a +t = a +while lt add t 1 n bound 4097 +put R1X t get R1X add t 1 +t = add t 1 +end +put R1N 0 sub n 1 +ret old +end +if eq op 6 +old = get R1X b +put R1X b get R1X a +put R1X a old +ret old +end +trap 10 +end +note SPDX-License-Identifier: Apache-2.0 +note exp.om: R1 experience records (docs/reuse/REUSE-1.md section 3.1): the tap writer and admission. +note Concatenated after r1.om. Exported: +note EXAPATH uchar 4096, EXALEN ulong 1 the ALLOW path (caller fills; no 0 byte needed) +note exaload to int read ALLOW (at most 1 MiB) into the allow table and its SHA-256 into EXAD: +note 0 ok; 1 unreadable, 2 too large, 3 malformed line, 4 more than 8192 lines. +note Lines: `<64 hex> AUTHORIZED|SYNTHETIC|REVOKED <label>`, label nonempty, one +note space between fields, lowercase or uppercase hex; empty lines are skipped. +note exallow d view ulong 4 to int digest (word j = bytes 8j..8j+7 big endian) against the loaded +note table: 1 allowed (an AUTHORIZED or SYNTHETIC line and no REVOKED line), +note 0 absent, -1 revoked +note exastat to bool stat of ALLOW (inode, size, mtime ns, ctime ns) into EXAS; true when it +note differs from the snapshot taken by the previous call (or on the first call) +note EXFPATH uchar 4096, EXFLEN ulong 1 the experience file path +note exadmit to int admission of that file against the loaded ALLOW: 0 admitted; on 0 the file +note is in EXFW (LE words, record r = EXFW[128r .. 128r+128)), EXFN = records, +note EXFD = the file's SHA-256. Codes: 1 unreadable, 2 size, 3 not in ALLOW, +note 4 revoked, 5 ALLOW not loaded, then the first failing record check: 6 magic, +note 7 version, 8 sequence, 9 family, 10 PROG invalid, 11 program digest, +note 12 split, 13 provenance, 14 m, 15 a zero word, 16 AGG sample, 17 FSM sample. +note EXFBAD = the 0-based index of the failing record. Clobbers R1P, R1PD, R1HB, +note R1HD. +note extopen to int the tap: argv 5 TAPFILE is created (O_EXCL; an existing file is refused), +note argv 6 SPLIT must be 1, 2 or 3: 0 ok, 1 split, 2 file exists or not creatable +note extap ses ulong qn ulong nchg ulong ns ulong to int one record for the current R1P, R1N, R1X +note and R1R (the baseline result, ns its evaluation time): sets R1PD and R1SD; +note 0 ok, 1 write failed. Sequence numbers start at 1. +global EXAPATH uchar 4096 +global EXALEN ulong 1 +global EXAB uchar 1048576 +global EXAT ulong 40960 +global EXAN ulong 1 +global EXAOK ulong 1 +global EXAD ulong 4 +global EXAS ulong 5 +global EXSB uchar 128 +global EXFPATH uchar 4096 +global EXFLEN ulong 1 +global EXFW ulong 4194304 +global EXFN ulong 1 +global EXFD ulong 4 +global EXFBAD ulong 1 +global EXTFD ulong 1 +global EXTSEQ ulong 1 +global EXTSPL ulong 1 +global EXTR ulong 128 +global EXTB uchar 1024 +global EXTP uchar 4096 + +note a hex digit's value, or 16 when it is not one +fn exhex c ulong to ulong +if and ge c 48 le c 57 +ret sub c 48 +end +if and ge c 97 le c 102 +ret sub c 87 +end +if and ge c 65 le c 70 +ret sub c 55 +end +ret 16 +end + +note keyword at EXAB[p..): 1 AUTHORIZED, 2 SYNTHETIC, 3 REVOKED, 0 none; the word must be followed +note by a space (checked by the caller through its length) +fn exkw p ulong e ulong to ulong +kw = new uchar 10 0 +r = ulong 1 +while le r 3 bound 4 +if eq r 1 +put kw 0 65 +put kw 1 85 +put kw 2 84 +put kw 3 72 +put kw 4 79 +put kw 5 82 +put kw 6 73 +put kw 7 90 +put kw 8 69 +put kw 9 68 +end +if eq r 2 +put kw 0 83 +put kw 1 89 +put kw 2 78 +put kw 3 84 +put kw 4 72 +put kw 5 69 +put kw 6 84 +put kw 7 73 +put kw 8 67 +end +if eq r 3 +put kw 0 82 +put kw 1 69 +put kw 2 86 +put kw 3 79 +put kw 4 75 +put kw 5 69 +put kw 6 68 +end +n = exkwlen r +ok = lt add p n e +for i ulong n +if ok +if ne get EXAB add p i get kw i +ok = false +end +end +end +if ok +if eq get EXAB add p n 32 +ret r +end +end +r = add r 1 +end +ret 0 +end + +fn exkwlen r ulong to ulong +if eq r 1 +ret 10 +end +if eq r 2 +ret 9 +end +ret 7 +end + +note one ALLOW line EXAB[s..e): 0 ok (entry appended), 3 malformed, 4 table full +fn exaline s ulong e ulong to int +if lt sub e s 68 +ret 3 +end +n = get EXAN 0 +if ge n 8192 +ret 4 +end +b = mul n 5 +for j ulong 4 +v = ulong 0 +for i ulong 16 +h = exhex ulong get EXAB add s add mul j 16 i +if gt h 15 +ret 3 +end +v = add mul v 16 h +end +put EXAT add b j v +end +if ne get EXAB add s 64 32 +ret 3 +end +k = exkw add s 65 e +if eq k 0 +ret 3 +end +if le e add add s 66 exkwlen k +ret 3 +end +put EXAT add b 4 k +put EXAN 0 add n 1 +ret 0 +end + +note SHA-256 of EXAB[0..n) into EXAD +fn exahash n ulong to void +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +off = ulong 0 +while lt off n bound 257 +m = sub n off +if gt m 4096 +m = 4096 +end +for i ulong m +put b i get EXAB add off i +end +omsha c b m 1 w b8 +off = add off m +end +omsha c b 0 2 w b8 +for j ulong 4 +put EXAD j r1bswap get w j +end +end + +fn exaload to int +put EXAOK 0 0 +put EXAN 0 0 +fd = fopen EXAPATH 0 get EXALEN 0 0 0 +if lt fd 0 +ret 1 +end +got = ulong 0 +while lt got 1048576 bound 100000 +k = read int fd EXAB got sub 1048576 got +if le k 0 +break +end +got = add got ulong k +end +one = new uchar 1 0 +k = read int fd one 0 1 +r = fclose int fd +if or ne k 0 ne r 0 +ret 2 +end +exahash got +s = ulong 0 +for i ulong got +if eq get EXAB i 10 +if gt i s +x = exaline s i +if ne x 0 +ret x +end +end +s = add i 1 +end +end +if lt s got +x = exaline s got +if ne x 0 +ret x +end +end +put EXAOK 0 1 +ret 0 +end + +fn exallow d view ulong 4 to int +n = get EXAN 0 +seen = int 0 +for e ulong n +b = mul e 5 +hit = true +for j ulong 4 +if ne get EXAT add b j get d j +hit = false +end +end +if hit +if eq get EXAT add b 4 3 +ret -1 +end +seen = 1 +end +end +ret seen +end + +note the 8-byte LE word at byte p of EXSB +fn exsw p ulong to ulong +v = ulong 0 +for i ulong 8 +v = or v shl ulong get EXSB add p i mul i 8 +end +ret v +end + +fn exastat to bool +for i ulong 128 +put EXSB i 0 +end +r = stat EXAPATH 0 get EXALEN 0 EXSB 0 128 +s = new ulong 5 0 +put s 0 exsw 8 +put s 1 exsw 48 +put s 2 add mul exsw 88 1000000000 exsw 96 +put s 3 add mul exsw 104 1000000000 exsw 112 +if eq r 0 +put s 4 1 +else +put s 4 2 +end +ch = false +for j ulong 5 +if ne get s j get EXAS j +ch = true +end +put EXAS j get s j +end +ret ch +end + +note the experience file into EXFW and its SHA-256 into EXFD: the byte count, or -1 unreadable, +note -2 larger than 32 MiB +fn exfread to long +fd = fopen EXFPATH 0 get EXFLEN 0 0 0 +if lt fd 0 +ret -1 +end +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +tot = ulong 0 +eof = false +while not eof bound 8194 +fill = ulong 0 +while lt fill 4096 bound 4097 +k = read int fd b fill sub 4096 fill +if le k 0 +eof = true +break +end +fill = add fill ulong k +end +if gt add tot fill 33554432 +r = fclose int fd +ret -2 +end +omsha c b fill 1 w b8 +exfwords b tot fill +tot = add tot fill +end +r = fclose int fd +omsha c b 0 2 w b8 +for j ulong 4 +put EXFD j r1bswap get w j +end +ret long tot +end + +note bytes b[0..fill) at file offset tot into EXFW (whole words only) +fn exfwords b view uchar 4096 tot ulong fill ulong to void +nw = shr fill 3 +base = shr tot 3 +for i ulong nw +v = ulong 0 +p = add mul i 8 7 +for k ulong 8 +v = or shl v 8 ulong get b sub p k +end +put EXFW add base i v +end +end + +note AGG samples of record base: m pairs (x, e) with x < 2^wl and e = r1lane x; R1P holds its PROG +fn exsagg base ulong m ulong to bool +lim = shl ulong 1 get R1P 2 +for i ulong m +x = get EXFW add base add 16 mul i 2 +e = get EXFW add base add 17 mul i 2 +if ge x lim +ret false +end +if ne r1lane x e +ret false +end +end +ret true +end + +note FSM samples: m words q + 256 a + 65536 q' with q < S, a < A, q' = delta(q, a) +fn exsfsm base ulong m ulong to bool +for i ulong m +w = get EXFW add base add 16 i +q = and w 255 +a = and shr w 8 255 +if or ge q get R1P 2 ge a get R1P 3 +ret false +end +if ne shr w 16 r1delta q a +ret false +end +end +ret true +end + +note words that must be 0: 15, 56..63, and the sample words past the m samples +fn exzero base ulong fam ulong m ulong to bool +if ne get EXFW add base 15 0 +ret false +end +for i ulong 8 +if ne get EXFW add base add 56 i 0 +ret false +end +end +used = m +if eq fam 1 +used = mul m 2 +end +for i ulong 32 +if ge i used +if ne get EXFW add base add 16 i 0 +ret false +end +end +end +ret true +end + +note record r (0-based), prev = the previous sequence number: 0 or a code 6..17 +fn exrec r ulong prev ulong to int +base = mul r 128 +if ne get EXFW base 3544386028377026898 +ret 6 +end +if ne get EXFW add base 1 1 +ret 7 +end +sq = get EXFW add base 2 +if or le sq prev and eq r 0 ne sq 1 +ret 8 +end +fam = get EXFW add base 3 +if or ne fam get EXFW add base 65 and ne fam 1 ne fam 2 +ret 9 +end +for i ulong 64 +put R1P i get EXFW add base add 64 i +end +if ne r1valid 0 +ret 10 +end +r1pdig +for j ulong 4 +if ne get R1PD j get EXFW add base add 52 j +ret 11 +end +end +sp = get EXFW add base 9 +if or lt sp 1 gt sp 3 +ret 12 +end +if ne get EXFW add base 13 1 +ret 13 +end +m = get EXFW add base 14 +if gt m 16 +ret 14 +end +if not exzero base fam m +ret 15 +end +if eq fam 1 +if not exsagg base m +ret 16 +end +else +if not exsfsm base m +ret 17 +end +end +ret 0 +end + +fn exadmit to int +put EXFN 0 0 +put EXFBAD 0 0 +if ne get EXAOK 0 1 +ret 5 +end +tot = exfread +if eq tot -1 +ret 1 +end +if or lt tot 0 or eq tot 0 ne rem tot 1024 0 +ret 2 +end +a = new ulong 4 0 +for j ulong 4 +put a j get EXFD j +end +al = exallow a +if eq al -1 +ret 4 +end +if ne al 1 +ret 3 +end +nr = shr ulong tot 10 +prev = ulong 0 +for r ulong nr +put EXFBAD 0 r +x = exrec r prev +if ne x 0 +ret x +end +prev = get EXFW add mul r 128 2 +end +put EXFN 0 nr +ret 0 +end + +note argv i into EXTP: the length, or -1 +fn exarg i ulong to long +n = argread i EXTP 0 4096 +if or lt n 1 gt n 4095 +ret -1 +end +ret n +end + +fn extopen to int +put EXTSEQ 0 0 +n = exarg 6 +if ne n 1 +ret 1 +end +sp = sub ulong get EXTP 0 48 +if or lt sp 1 gt sp 3 +ret 1 +end +put EXTSPL 0 sp +n = exarg 5 +if lt n 0 +ret 2 +end +fd = fopen EXTP 0 ulong n 193 384 +if lt fd 0 +ret 2 +end +put EXTFD 0 ulong fd +ret 0 +end + +note rules tested by delta(q, a): the index of the first matching rule plus 1, else R +fn exscan q ulong a ulong to ulong +nr = get R1P 5 +for i ulong nr +w = get R1P add 22 i +if eq and w 255 q +lo = and shr w 8 255 +hi = and shr w 16 255 +if and le lo a le a hi +ret add i 1 +end +end +end +ret nr +end + +note FSM rule tests of the whole run (word 8) +fn exsteps to ulong +n = get R1N 0 +q = get R1P 4 +st = ulong 0 +for t ulong n +a = get R1X t +st = add st exscan q a +q = r1delta q a +end +ret st +end + +note the first m (<= 16) distinct lane values in index order, (x, e(x)) into EXTR[16..48) +fn exsmagg to ulong +n = get R1N 0 +m = ulong 0 +for i ulong n +if lt m 16 +x = get R1X i +seen = false +for j ulong m +if eq get EXTR add 16 mul j 2 x +seen = true +end +end +if not seen +put EXTR add 16 mul m 2 x +put EXTR add 17 mul m 2 r1lane x +m = add m 1 +end +end +end +ret m +end + +note the first m distinct transitions q + 256 a + 65536 q' taken +fn exsmfsm to ulong +n = get R1N 0 +m = ulong 0 +q = get R1P 4 +for i ulong n +a = get R1X i +nq = r1delta q a +if lt m 16 +w = add add q mul a 256 mul nq 65536 +seen = false +for j ulong m +if eq get EXTR add 16 j w +seen = true +end +end +if not seen +put EXTR add 16 m w +m = add m 1 +end +end +q = nq +end +ret m +end + +fn extap ses ulong qn ulong nchg ulong ns ulong to int +for i ulong 128 +put EXTR i 0 +end +sq = add get EXTSEQ 0 1 +put EXTSEQ 0 sq +put EXTR 0 3544386028377026898 +put EXTR 1 1 +put EXTR 2 sq +fam = get R1P 1 +put EXTR 3 fam +put EXTR 4 get R1N 0 +put EXTR 5 get R1R 0 +put EXTR 6 get R1R 1 +put EXTR 7 ns +if eq fam 1 +put EXTR 8 mul get R1N 0 get R1P 4 +put EXTR 14 exsmagg +else +put EXTR 8 exsteps +put EXTR 14 exsmfsm +end +put EXTR 9 get EXTSPL 0 +put EXTR 10 ses +put EXTR 11 qn +put EXTR 12 nchg +put EXTR 13 1 +r1sdig +r1pdig +for j ulong 4 +put EXTR add 48 j get R1SD j +put EXTR add 52 j get R1PD j +end +for i ulong 64 +put EXTR add 64 i get R1P i +end +ret exflush +end + +fn exflush to int +for i ulong 128 +v = get EXTR i +for k ulong 8 +put EXTB add mul i 8 k uchar and v 255 +v = shr v 8 +end +end +fd = int get EXTFD 0 +off = ulong 0 +while lt off 1024 bound 1025 +k = write fd EXTB off sub 1024 off +if le k 0 +ret 1 +end +off = add off ulong k +end +ret 0 +end +note SPDX-License-Identifier: Apache-2.0 +note check.om: G3 bind validation (docs/reuse/REUSE-1.md section 3.3). Concatenated after exp.om; +note calls mtlane of the generated methods.om (or a test's fixture). +note ckbind t 0 pass, 1 fail. AGG: every x < 2^wl of R1P has mtlane t x = r1lane x, all 64 bits. +note FSM: fills R1T[s*16 + a] = r1delta s a for s < S, a < A; nothing else is checked. +note Sets R1PD (r1pdig). A passed AGG (program digest, t) enters a FIFO of 256 entries +note (process memory only); a later bind of the same pair skips the lane comparison. +note CKST counters: 0 binds, 1 failures, 2 FIFO hits, 3 full lane comparisons, 4 lane pairs +note compared +global CKFIFO ulong 1280 +global CKFN ulong 1 +global CKFH ulong 1 +global CKST ulong 5 + +fn ckinc k ulong n ulong to void +put CKST k add get CKST k n +end + +note entry e of the FIFO holds R1PD and t +fn ckeq e ulong t ulong to bool +b = mul e 5 +for j ulong 4 +if ne get CKFIFO add b j get R1PD j +ret false +end +end +ret eq get CKFIFO add b 4 t +end + +fn ckhas t ulong to bool +n = get CKFN 0 +for e ulong n +if ckeq e t +ret true +end +end +ret false +end + +fn ckadd t ulong to void +e = get CKFH 0 +b = mul e 5 +for j ulong 4 +put CKFIFO add b j get R1PD j +end +put CKFIFO add b 4 t +put CKFH 0 rem add e 1 256 +if lt get CKFN 0 256 +put CKFN 0 add get CKFN 0 1 +end +end + +note the lane comparison over x < 2^wl: 0 equal, 1 the first difference +fn cklanes t ulong to int +lim = shl ulong 1 get R1P 2 +ckinc 3 1 +for x ulong lim +if ne mtlane t x r1lane x +ckinc 4 add x 1 +ret 1 +end +end +ckinc 4 lim +ret 0 +end + +fn ckfsm to void +ns = get R1P 2 +na = get R1P 3 +for s ulong ns +for a ulong na +put R1T add mul s 16 a r1delta s a +end +end +end + +fn ckbind t ulong to int +ckinc 0 1 +r1pdig +if eq get R1P 1 2 +ckfsm +ret 0 +end +if ckhas t +ckinc 2 1 +ret 0 +end +if ne cklanes t 0 +ckinc 1 1 +ret 1 +end +ckadd t +ret 0 +end +note SPDX-License-Identifier: Apache-2.0 +note delta.om: REUSE-1 incremental execution (#743), contract docs/reuse/REUSE-1.md v1.0 sections 4, 12. +note Unit order: sha256 r1 exp check delta select lib methods router. Names: dl / DL only. +note Reads R1P R1N R1X R1T, writes R1R (dlres only). Calls r1valid r1lane r1ident r1delta and, +note for a bound AGG method, mtlane t x; a bound FSM method reads R1T[s*16 + a] (filled by ckbind). +note +note Router protocol (agreed with CandCore): +note BEGIN after R1P R1N R1X are set: dlsreset, then dlbind t (t = template index, -1 = baseline) +note PROGRAM after R1P is replaced: dldrop, then dlbind t +note change the router validates the whole request (s1.3), then for each item in order applies +note exactly that item to R1X / R1N and calls dlchg op i v old before the next item +note SET 3 i v old = previous x_i INS 4 i v old unused (0) +note DEL 5 i 0 old = the removed value SWAP 6 i v = j, old = x_j before the swap (unused) +note AGG uses v and old (SWAP no work); FSM reads the symbols from R1X and compares new q_t with +note the stored trajectory (s4), so it needs neither. FSM SWAP = SET at min(i, j), then SET at max. +note (The other order is also correct: the stored trajectory is a valid chain under the current +note symbols everywhere except at changed positions, so every cutoff is sound; the reversed order +note passed both delta test suites when tried as a mutant.) +note QUERY dlres: 0 = R1R holds r0 r1 from the delta state, 1 = no usable state (serve full) +note dlbind returns 0 bound, 1 refused (PROG invalid or family unknown). dlbind on a session that is +note already bound with the same t is a no-op, so dldrop before a rebind is mandatory (C4 relies on it). +note dlchg returns 0 tracked, 1 delta state dropped (unbound until the next dlbind). +note +note Delta state (s4), 557,488 bytes of DL globals per process, one session (<= 1 MiB, s3.5): +note DLC 65536 words MIN/MAX count per mapped value (2^wl used) +note DLQ 4097 words FSM trajectory q_0..q_n +note DLBC 16 words OR/AND count per bit +note DLL 16 words FSM lambda copy; scalars below +note Worst cases per change: AGG ADD/XOR/OR/AND O(1) (16 bit counts); MIN/MAX one extreme scan of +note at most 2^wl - 1 counts when the last copy of the extreme goes; FSM SET at i recomputes at most +note n - i steps, INS at most n + 1 - i plus a suffix shift of at most n + 1 words, DEL at most n - i - 1 +note plus a shift of at most n words; SWAP two SETs. dlbind: one pass +note over the state plus, for MIN/MAX, clearing 2^wl counts. +note +note Counters (diagnostic, process-cumulative, never reset): dlstat k +note 0 changes tracked 1 invalidations 2 scan steps 3 FSM recompute units 4 cutoffs found +note 5 cutoff misses 6 recomputed steps 7 longest recompute 8 binds 9 drops 10 dlres served +note 11 dlres refused +note Select churn (s5.2): dlinvn = invalidations (MIN/MAX extreme scans, and every drop of live +note delta state: dldrop at PROGRAM, dlchg or dlres finding the state unusable; never twice for one), +note dlmissn = FSM recompute units that ran to n without a cutoff (at least one step recomputed). + +global DLB ulong 1 +global DLTB long 1 +global DLF ulong 1 +global DLFO ulong 1 +global DLW ulong 1 +global DLN ulong 1 +global DLA ulong 1 +global DLU ulong 1 +global DLS ulong 1 +global DLBC ulong 16 +global DLL ulong 16 +global DLC ulong 65536 +global DLQ ulong 4097 +global DLST ulong 12 + +fn dlbump k ulong v ulong to void +put DLST k add get DLST k v +end + +fn dlstat k ulong to ulong +if ge k 12 +ret 0 +end +ret get DLST k +end + +fn dlinvn to ulong +ret get DLST 1 +end + +fn dlmissn to ulong +ret get DLST 5 +end + +note mapped lane: the bound method's lane map, or the baseline's +fn dlmap x ulong to ulong +t = get DLTB 0 +if ge t 0 +ret mtlane ulong t x +end +ret r1lane x +end + +note one FSM step: the bound method's table, or the baseline delta +fn dlstep s ulong a ulong to ulong +if eq get DLU 0 1 +ret get R1T add mul s 16 a +end +ret r1delta s a +end + +fn dllam s ulong to ulong +ret get DLL s +end + +note MIN/MAX: the last copy of the extreme went; scan to the next nonzero count +fn dlscan to void +dlbump 1 1 +if eq get DLN 0 0 +put DLA 0 0 +ret +end +a = get DLA 0 +if eq get DLFO 0 3 +top = shl ulong 1 get DLW 0 +st = add a 1 +for j ulong sub top st +v = add st j +if ne get DLC v 0 +put DLA 0 v +dlbump 2 add j 1 +ret +end +end +dlbump 2 sub top st +ret +end +for j ulong a +v = sub sub a 1 j +if ne get DLC v 0 +put DLA 0 v +dlbump 2 add j 1 +ret +end +end +dlbump 2 a +end + +fn dlbitadd y ulong d ulong to void +for b ulong 16 +if ne and shr y b 1 0 +put DLBC b add get DLBC b d +end +end +end + +fn dlbitsub y ulong to void +for b ulong 16 +if ne and shr y b 1 0 +put DLBC b sub get DLBC b 1 +end +end +end + +note add one mapped lane y to the AGG delta state +fn dladd y ulong to void +f = get DLFO 0 +n = get DLN 0 +a = get DLA 0 +if eq f 1 +put DLA 0 add a y +end +if eq f 2 +put DLA 0 xor a y +end +if eq f 3 +put DLC y add get DLC y 1 +if or eq n 0 lt y a +put DLA 0 y +end +end +if eq f 4 +put DLC y add get DLC y 1 +if or eq n 0 gt y a +put DLA 0 y +end +end +if ge f 5 +dlbitadd y 1 +end +put DLN 0 add n 1 +end + +note remove one mapped lane y from the AGG delta state +fn dlrem y ulong to void +f = get DLFO 0 +a = get DLA 0 +put DLN 0 sub get DLN 0 1 +if eq f 1 +put DLA 0 sub a y +end +if eq f 2 +put DLA 0 xor a y +end +if ge f 5 +dlbitsub y +end +if or eq f 3 eq f 4 +c = sub get DLC y 1 +put DLC y c +if and eq c 0 eq y a +dlscan +end +end +end + +fn dlbagg to void +f = get R1P 3 +put DLFO 0 f +put DLW 0 get R1P 2 +put DLN 0 0 +put DLA 0 0 +for b ulong 16 +put DLBC b 0 +end +if or eq f 3 eq f 4 +top = shl ulong 1 get DLW 0 +for v ulong top +put DLC v 0 +end +end +n = get R1N 0 +for i ulong n +dladd dlmap get R1X i +end +end + +fn dlbfsm to void +for s ulong 16 +put DLL s get R1P add 6 s +end +q = get R1P 4 +put DLQ 0 q +sum = ulong 0 +n = get R1N 0 +for i ulong n +q = dlstep q get R1X i +put DLQ add i 1 q +sum = add sum dllam q +end +put DLS 0 sum +put DLN 0 n +end + +fn dlbind t long to int +if and eq get DLB 0 1 eq get DLTB 0 t +ret 0 +end +put DLB 0 0 +if ne r1valid 0 +ret 1 +end +fam = get R1P 1 +put DLF 0 fam +put DLTB 0 t +put DLU 0 0 +if eq fam 1 +dlbagg +end +if eq fam 2 +if ge t 0 +put DLU 0 1 +end +dlbfsm +end +if or eq fam 1 eq fam 2 +put DLB 0 1 +dlbump 8 1 +ret 0 +end +ret 1 +end + +note a drop of LIVE delta state is one invalidation (dlinvn); dropping an unbound state counts none +fn dldrop to void +if eq get DLB 0 1 +dlbump 1 1 +end +put DLB 0 0 +dlbump 9 1 +end + +fn dlsreset to void +put DLB 0 0 +put DLTB 0 -1 +end + +note record one FSM recompute unit of sl steps; cut 1 = a cutoff was found +fn dlfrec sl ulong cut ulong to void +dlbump 3 1 +dlbump 6 sl +if gt sl get DLST 7 +put DLST 7 sl +end +if eq cut 1 +dlbump 4 1 +end +if and eq cut 0 gt sl 0 +dlbump 5 1 +end +end + +note FSM: w_(i+1) changed (x_i); recompute from t = i+1, stop at the first equal stored q_t +fn dlfset i ulong to void +n = get DLN 0 +q = get DLQ i +sum = get DLS 0 +t = add i 1 +cut = ulong 0 +sl = ulong 0 +while le t n bound 4097 +nq = dlstep q get R1X sub t 1 +sl = add sl 1 +old = get DLQ t +if eq nq old +cut = 1 +break +end +sum = add sub sum dllam old dllam nq +put DLQ t nq +q = nq +t = add t 1 +end +put DLS 0 sum +dlfrec sl cut +end + +note FSM INS at i (R1X already holds the insertion): new q_t is compared with the stored state of the +note same symbol suffix, old q_(t-1); at the first equality the stored suffix shifts up by one +fn dlfins i ulong to void +n = get DLN 0 +q = get DLQ i +po = q +sum = get DLS 0 +t = add i 1 +cut = ulong 0 +sl = ulong 0 +while le t add n 1 bound 4098 +nq = dlstep q get R1X sub t 1 +sl = add sl 1 +if eq nq po +cut = 1 +break +end +sum = add sum dllam nq +if le t n +nx = get DLQ t +sum = sub sum dllam nx +po = nx +end +put DLQ t nq +q = nq +t = add t 1 +end +if eq cut 1 +sum = add sum dllam po +s = n +while ge s t bound 4097 +put DLQ add s 1 get DLQ s +if eq s 0 +break +end +s = sub s 1 +end +put DLQ t po +end +put DLS 0 sum +put DLN 0 add n 1 +dlfrec sl cut +end + +note FSM DEL at i (R1X already shifted down): new q_t is compared with old q_(t+1); at cutoff t (or +note t = n with no cutoff) old q_t leaves the sum and the stored suffix from t+1 shifts down by one +fn dlfdel i ulong to void +n = get DLN 0 +q = get DLQ i +sum = get DLS 0 +t = add i 1 +cut = ulong 0 +sl = ulong 0 +while lt t n bound 4097 +nq = dlstep q get R1X sub t 1 +sl = add sl 1 +if eq nq get DLQ add t 1 +cut = 1 +break +end +sum = add sub sum dllam get DLQ t dllam nq +put DLQ t nq +q = nq +t = add t 1 +end +sum = sub sum dllam get DLQ t +s = t +while lt s n bound 4097 +put DLQ s get DLQ add s 1 +s = add s 1 +end +put DLS 0 sum +put DLN 0 sub n 1 +dlfrec sl cut +end + +fn dlfchg op ulong i ulong v ulong to int +n = get DLN 0 +if eq op 3 +if ge i n +ret 1 +end +dlfset i +ret 0 +end +if eq op 4 +if gt i n +ret 1 +end +dlfins i +ret 0 +end +if eq op 5 +if ge i n +ret 1 +end +dlfdel i +ret 0 +end +if eq op 6 +if or ge i n ge v n +ret 1 +end +if lt i v +dlfset i +dlfset v +end +if gt i v +dlfset v +dlfset i +end +ret 0 +end +ret 1 +end + +fn dlachg op ulong i ulong v ulong old ulong to int +n = get DLN 0 +if eq op 3 +if ge i n +ret 1 +end +dladd dlmap v +dlrem dlmap old +ret 0 +end +if eq op 4 +if gt i n +ret 1 +end +dladd dlmap v +ret 0 +end +if eq op 5 +if ge i n +ret 1 +end +dlrem dlmap old +ret 0 +end +if eq op 6 +if or ge i n ge v n +ret 1 +end +ret 0 +end +ret 1 +end + +fn dlchg op ulong i ulong v ulong old ulong to int +if eq get DLB 0 0 +ret 1 +end +r = int 1 +if eq get DLF 0 1 +r = dlachg op i v old +end +if eq get DLF 0 2 +r = dlfchg op i v +end +if eq r 0 +if eq get DLN 0 get R1N 0 +dlbump 0 1 +ret 0 +end +end +put DLB 0 0 +dlbump 1 1 +ret 1 +end + +fn dlragg to void +f = get DLFO 0 +n = get DLN 0 +r = get DLA 0 +if or eq f 3 eq f 4 +if eq n 0 +r = r1ident f +end +end +if ge f 5 +r = r1ident f +if gt n 0 +r = 0 +for b ulong 16 +c = get DLBC b +if and eq f 5 gt c 0 +r = or r shl ulong 1 b +end +if and eq f 6 eq c n +r = or r shl ulong 1 b +end +end +end +end +put R1R 0 r +end + +fn dlres to int +if eq get DLB 0 0 +dlbump 11 1 +ret 1 +end +if ne get DLN 0 get R1N 0 +put DLB 0 0 +dlbump 1 1 +dlbump 11 1 +ret 1 +end +if eq get DLF 0 1 +dlragg +put R1R 1 get DLN 0 +end +if eq get DLF 0 2 +put R1R 0 get DLQ get DLN 0 +put R1R 1 get DLS 0 +end +dlbump 10 1 +ret 0 +end +note SPDX-License-Identifier: Apache-2.0 +note select.om: the REUSE-1 #744 frozen cost selector (contract v1.0 sections 5.1, 5.2, 12; +note integrator decision 3 fixes the calibration layout). Unit order: sha256 r1 exp check delta +note select lib methods router. Reads R1P word 1 (family) and R1N (n) of r1.om, and the delta +note churn counters through dlinvn and dlmissn of delta.om. Nothing here learns, times itself or +note writes a file: every choice is a pure function of the frozen calibration and the features. +note +note Calibration file R1CAL001: exactly 200 words (1600 bytes), little endian: +note 0 magic 3544386010811150674, 1 version 1, 2 E_q x 1000, 3..7 zero; +note cell c = ((family - 1) * 4 + path) * 12 + nclass * 4 + cclass; +note word 8 + 2c = p50 ns, word 9 + 2c = p90 ns, 0 = missing. +note path 0 full, 1 method, 2 delta, 3 bind (bind only in cclass 0). +note nclass by n: 0 (n <= 64), 1 (<= 512), 2 (<= 4096). cclass by changes since the previous +note QUERY: 0 (0), 1 (1), 2 (2..64), 3 (> 64). +note Any other size, magic, version or a nonzero word 3..7 refuses the whole file: nothing of it is +note kept, and the selector runs the fixed rules (cold start). p90 is never read. +note +note Router protocol: +note SLPATH calibration path bytes up to the first 0 byte; empty or "-" = no calibration. +note slload once at start: 0 loaded, 1 absent or refused. +note slsess at every accepted BEGIN: clears the session's validity marks and churn state. +note slbind t at BEGIN or PROGRAM, t = template index or -1: bind a method at all? +note no calibration, or a missing bind, full or method cell (cclass 0): bind iff t >= 0; +note else bind iff p50(bind) < E_q (p50(full) - p50(method)), compared exactly. +note slset meth dval before every QUERY: which paths are valid now (full always is). +note slpick nchg: 1 full, 3 method, 4 delta, only ever a valid path. Lowest p50 of the valid +note paths for (family, nclass, cclass), ties to delta, then method, then full; no calibration +note or a missing cell of a valid path: the fixed rule (delta if valid, else method if bound, +note else full). Churn: from 64 invalidations or 64 cutoff misses in the session (counted from +note the slsess snapshot) delta is invalid for the rest of the session. +note sloff: true iff churn has disabled delta for this session, as judged by the last slpick; +note stays true until the next slsess. The router may then drop the delta state and stop +note its maintenance for the session. +note An unsupported family (R1P word 1 not 1 or 2), n > 4096, template index outside -1..15, or +note a path or class id outside its range is refused before any lookup: slpick returns full, +note slbind returns false, and SLREF counts it (the #704 rule). No cell is indexed with such an id. +note STATS: SLREF refusals, SLDIS churn disables (both process totals). + +global SLPATH uchar 4096 +global SLCAL ulong 200 +global SLOK ulong 1 +global SLBUF uchar 2048 +global SLMB ulong 1 +global SLDV ulong 1 +global SLOFF ulong 1 +global SLINV0 ulong 1 +global SLMIS0 ulong 1 +global SLREF ulong 1 +global SLDIS ulong 1 + +fn slref to void +put SLREF 0 add get SLREF 0 1 +end + +note bytes of SLPATH before the first 0; 4096 when there is none (refused by slload) +fn slpathn to ulong +for i ulong 4096 +if eq get SLPATH i 0 +ret i +end +end +ret 4096 +end + +note read the open file into SLBUF until end of file; 2048 = too long or a read error +fn slreadall fd int to ulong +tot = ulong 0 +while lt tot 2048 bound 4096 +k = read fd SLBUF tot sub 2048 tot +if lt k 0 +ret 2048 +end +if eq k 0 +ret tot +end +tot = add tot ulong k +end +ret tot +end + +note little-endian word i of SLBUF +fn slword i ulong to ulong +v = ulong 0 +p = mul i 8 +j = ulong 8 +while gt j 0 bound 8 +j = sub j 1 +v = or shl v 8 ulong get SLBUF add p j +end +ret v +end + +note check the 1600 bytes in SLBUF; only a fully valid file is copied into SLCAL +fn slparse to int +if ne slword 0 3544386010811150674 +ret 1 +end +if ne slword 1 1 +ret 1 +end +for i ulong 5 +if ne slword add i 3 0 +ret 1 +end +end +for i ulong 200 +put SLCAL i slword i +end +put SLOK 0 1 +ret 0 +end + +fn slload to int +put SLOK 0 0 +for i ulong 200 +put SLCAL i 0 +end +pn = slpathn +if or eq pn 0 ge pn 4096 +ret 1 +end +if and eq pn 1 eq get SLPATH 0 45 +ret 1 +end +fd = fopen SLPATH 0 pn 524288 0 +if lt fd 0 +ret 1 +end +tot = slreadall int fd +r = fclose int fd +if or ne tot 1600 lt r 0 +ret 1 +end +ret slparse +end + +note p50 ns of one cell, 0 = missing; an id out of range is refused and counted, never indexed +fn slcell fam ulong path ulong ncl ulong ccl ulong to ulong +if or or lt fam 1 gt fam 2 or gt path 3 or gt ncl 2 gt ccl 3 +slref +ret 0 +end +if eq get SLOK 0 0 +ret 0 +end +c = add mul add mul sub fam 1 4 path 12 add mul ncl 4 ccl +ret get SLCAL add 8 mul 2 c +end + +note n size class of R1N; 3 = unsupported (n > 4096) +fn slncl to ulong +n = get R1N 0 +if le n 64 +ret 0 +end +if le n 512 +ret 1 +end +if le n 4096 +ret 2 +end +ret 3 +end + +note change class of the changes since the previous QUERY +fn slccl nchg ulong to ulong +if eq nchg 0 +ret 0 +end +if eq nchg 1 +ret 1 +end +if le nchg 64 +ret 2 +end +ret 3 +end + +note high 64 bits of the 128-bit product a b +fn slmulhi a ulong b ulong to ulong +m = ulong 4294967295 +a0 = and a m +a1 = shr a 32 +b0 = and b m +b1 = shr b 32 +p01 = mul a0 b1 +p10 = mul a1 b0 +mid = add shr mul a0 b0 32 add and p01 m and p10 m +h = add mul a1 b1 add shr p01 32 shr p10 32 +ret add h shr mid 32 +end + +note exact a b < c d over 128 bits +fn slltp a ulong b ulong c ulong d ulong to bool +h1 = slmulhi a b +h2 = slmulhi c d +if ne h1 h2 +ret lt h1 h2 +end +ret lt mul a b mul c d +end + +fn slsess to void +put SLOFF 0 0 +put SLMB 0 0 +put SLDV 0 0 +put SLINV0 0 dlinvn +put SLMIS0 0 dlmissn +end + +fn slset meth bool dval bool to void +put SLMB 0 0 +put SLDV 0 0 +if meth +put SLMB 0 1 +end +if dval +put SLDV 0 1 +end +end + +fn slbind t long to bool +fam = get R1P 1 +ncl = slncl +if or or lt fam 1 gt fam 2 gt ncl 2 +slref +ret false +end +if or lt t -1 gt t 15 +slref +ret false +end +if eq t -1 +ret false +end +if eq get SLOK 0 0 +ret true +end +cb = slcell fam 3 ncl 0 +cf = slcell fam 0 ncl 0 +cm = slcell fam 1 ncl 0 +if or or eq cb 0 eq cf 0 eq cm 0 +ret true +end +if le cf cm +ret false +end +ret slltp cb 1000 get SLCAL 2 sub cf cm +end + +note churn: 64 invalidations or 64 cutoff misses since slsess disable delta for the session +fn slchurn to void +if eq get SLOFF 0 1 +ret +end +inv = sub dlinvn get SLINV0 0 +mis = sub dlmissn get SLMIS0 0 +if or ge inv 64 ge mis 64 +put SLOFF 0 1 +put SLDIS 0 add get SLDIS 0 1 +end +end + +fn sloff to bool +ret eq get SLOFF 0 1 +end + +fn slfixed md ulong dv ulong to int +if eq dv 1 +ret 4 +end +if eq md 1 +ret 3 +end +ret 1 +end + +note lowest p50 among the valid paths; a missing cell of a valid path gives the fixed rule +fn slrank fam ulong ncl ulong ccl ulong md ulong dv ulong to int +best = int 1 +cm = ulong 0 +cd = ulong 0 +bc = slcell fam 0 ncl ccl +if eq bc 0 +ret slfixed md dv +end +if eq md 1 +cm = slcell fam 1 ncl ccl +if eq cm 0 +ret slfixed md dv +end +if le cm bc +best = 3 +bc = cm +end +end +if eq dv 1 +cd = slcell fam 2 ncl ccl +if eq cd 0 +ret slfixed md dv +end +if le cd bc +best = 4 +end +end +ret best +end + +fn slpick nchg ulong to int +fam = get R1P 1 +ncl = slncl +if or or lt fam 1 gt fam 2 gt ncl 2 +slref +ret 1 +end +slchurn +md = get SLMB 0 +dv = get SLDV 0 +if eq get SLOFF 0 1 +dv = 0 +end +ccl = slccl nchg +if eq get SLOK 0 0 +ret slfixed md dv +end +ret slrank fam ncl ccl md dv +end +note SPDX-License-Identifier: Apache-2.0 +note lib.om: REUSE-1 library generations (contract v1.0 section 3.4, clarifications K1 K3 K16). +note Part of every router (lbload, lblineage) and of drive.om (lbpub and helpers). lbload hands the +note verified calibration bytes to the generation as a memfd on fd 7 (K16). +note Needs sha256 (omsha), r1 and exp (exaload, exallow, EXAPATH, EXALEN) before it. +note Manifest: 256 LE words in LBMAN, file bytes in LBMB; word 255 = FNV-1a 64 over the 2040 LE +note bytes of words 0..254 (K3). CURRENT: 40 bytes, G LE then the 32 SHA-256 bytes of gen-G.man. +note Digests are 4 words, word j = digest bytes 8j..8j+7 read big endian (contract 1.1). +note lbload refusal codes (each check distinct, nothing is trusted before all pass): +note 1 argv (count, LIBDIR length) 2 MANSHA not 64 hex digits 3 CURRENT unreadable +note 4 CURRENT not exactly 40 bytes 5 gen-G.man unreadable 6 gen-G.man not exactly 2048 bytes +note 7 SHA-256(gen-G.man) differs from the CURRENT digest 8 CURRENT digest differs from MANSHA +note 9 magic 10 version 11 self-check 12 G differs from CURRENT or parent G >= G +note 13 compiler SHA-256 is not the pinned omc 14 profile version 15 K > 16 or L > 8 +note 16 a reserved word is nonzero 17 ALLOW unreadable 18 a lineage digest not allowed +note 19 gen-G.cal unreadable or 4096 bytes or more 20 gen-G.cal digest 21 gen-G.elf unreadable +note 22 memfd (or dup onto fd 7) failed 23 copy into a memfd failed 24 ELF digest 25 exec failed + +global LBMAN ulong 256 +global LBMB uchar 2048 +global LBH ulong 4 +global LBCD ulong 4 +global LBMS ulong 4 +global LBDIR uchar 4096 +global LBDN ulong 1 +global LBP uchar 4096 +global LBPN ulong 1 +global LBPK uchar 4096 +global LBIO uchar 4096 +global LBST uchar 128 +global LBG ulong 1 +global LBLD ulong 32 +global LBLN ulong 1 +global LBGEN ulong 1 +global LBCAL ulong 1 +text LBTGEN "gen-" +text LBTOM ".om" +text LBTELF ".elf" +text LBTMAN ".man" +text LBTCAL ".cal" +text LBTTMP ".tmp" +text LBTCUR "CURRENT" +text LBTFD "/proc/self/fd/" +text LBTLD "loaded" +text LBTMFD "r1gen" + +note byte swap of a 64-bit word +fn lbbswap x ulong to ulong +y = or shl and x 71777214294589695 8 and shr x 8 71777214294589695 +y = or shl and y 281470681808895 16 and shr y 16 281470681808895 +ret or shl y 32 shr y 32 +end + +note omsha's packed digest (w[i] holds digest bytes 8i..8i+7 little endian) into LBH +fn lbdig w view ulong 64 to void +for i ulong 4 +put LBH i lbbswap get w i +end +end + +note SHA-256 of a whole open file read with pread from position 0 into LBH; bytes or -errno +fn lbhfd fd int to long +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +tot = long 0 +while true bound 999999 +r = pread fd b 0 4096 tot +if lt r 0 +ret r +end +if eq r 0 +break +end +omsha c b ulong r 1 w b8 +tot = add tot r +end +omsha c b 0 2 w b8 +lbdig w +ret tot +end + +note SHA-256 of the 2048 manifest bytes LBMB into LBH +fn lbhman to void +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +for i ulong 2048 +put b i get LBMB i +end +omsha c b 0 0 w b8 +omsha c b 2048 1 w b8 +omsha c b 0 2 w b8 +lbdig w +end + +note manifest words to bytes and back +fn lbm2b to void +for i ulong 256 +v = get LBMAN i +for k ulong 8 +put LBMB add mul i 8 k uchar and shr v mul k 8 255 +end +end +end + +fn lbb2m to void +for i ulong 256 +v = ulong 0 +for k ulong 8 +v = or v shl ulong get LBMB add mul i 8 k mul k 8 +end +put LBMAN i v +end +end + +note FNV-1a 64 over LBMB[0..2040) (fill LBMB with lbm2b first) +fn lbfnv to ulong +h = ulong 14695981039346656037 +for i ulong 2040 +h = xor h ulong get LBMB i +h = mul h 1099511628211 +end +ret h +end + +note self-check word 255 of LBMAN (drive.om, tests) +fn lbseal to void +lbm2b +put LBMAN 255 lbfnv +lbm2b +end + +note little/big endian word of LBIO at byte o +fn lbgetle o ulong to ulong +v = ulong 0 +for k ulong 8 +v = or v shl ulong get LBIO add o k mul k 8 +end +ret v +end + +fn lbgetbe o ulong to ulong +v = ulong 0 +for k ulong 8 +v = or shl v 8 ulong get LBIO add o k +end +ret v +end + +fn lbputbe o ulong v ulong to void +for k ulong 8 +put LBIO add o k uchar and shr v mul sub 7 k 8 255 +end +end + +note path builder: LBP[0..LBPN) +fn lbpz to void +put LBPN 0 0 +end + +fn lbpc c ulong to void +n = get LBPN 0 +if ge n 4095 +trap 3 +end +put LBP n uchar c +put LBPN 0 add n 1 +end + +fn lbpnum v ulong to void +d = new uchar 24 0 +n = ulong 0 +x = v +while true bound 24 +put d n uchar add rem x 10 48 +n = add n 1 +x = div x 10 +if eq x 0 +break +end +end +while gt n 0 bound 24 +n = sub n 1 +lbpc ulong get d n +end +end + +note fixed name parts: 1 gen- 2 .om 3 .elf 4 .man 5 .cal 6 .tmp 7 CURRENT 8 /proc/self/fd/ 9 loaded +fn lbptx k ulong to void +if eq k 1 +for i ulong len LBTGEN +lbpc ulong get LBTGEN i +end +end +if eq k 2 +for i ulong len LBTOM +lbpc ulong get LBTOM i +end +end +if eq k 3 +for i ulong len LBTELF +lbpc ulong get LBTELF i +end +end +if eq k 4 +for i ulong len LBTMAN +lbpc ulong get LBTMAN i +end +end +if eq k 5 +for i ulong len LBTCAL +lbpc ulong get LBTCAL i +end +end +if eq k 6 +for i ulong len LBTTMP +lbpc ulong get LBTTMP i +end +end +if eq k 7 +for i ulong len LBTCUR +lbpc ulong get LBTCUR i +end +end +if eq k 8 +for i ulong len LBTFD +lbpc ulong get LBTFD i +end +end +if eq k 9 +for i ulong len LBTLD +lbpc ulong get LBTLD i +end +end +end + +fn lbpdir to void +for i ulong get LBDN 0 +lbpc ulong get LBDIR i +end +lbpc 47 +end + +note LIBDIR/gen-G.EXT[.tmp], ext 1 om 2 elf 3 man 4 cal +fn lbpgen g ulong ext ulong tmp bool to void +lbpz +lbpdir +lbptx 1 +lbpnum g +lbptx add ext 1 +if tmp +lbptx 6 +end +end + +fn lbpcurr tmp bool to void +lbpz +lbpdir +lbptx 7 +if tmp +lbptx 6 +end +end + +note open LBP; descriptor or -1 +fn lbopen flags int mode int to int +r = fopen LBP 0 get LBPN 0 flags mode +if lt r 0 +ret -1 +end +ret int r +end + +note read up to cap (<= 4096) bytes of fd into LBIO; count or -errno +fn lbrdio fd int cap ulong to long +tot = ulong 0 +while lt tot cap bound 4097 +r = read fd LBIO tot sub cap tot +if lt r 0 +ret r +end +if eq r 0 +break +end +tot = add tot ulong r +end +ret long tot +end + +note write LBIO[0..n) completely; 0 or 1 +fn lbwrio fd int n ulong to int +tot = ulong 0 +while lt tot n bound 4097 +r = write fd LBIO tot sub n tot +if le r 0 +ret 1 +end +tot = add tot ulong r +end +ret 0 +end + +note size of the file named by LBP, or -1 +fn lbfsize to long +r = stat LBP 0 get LBPN 0 LBST 0 128 +if ne r 0 +ret -1 +end +v = ulong 0 +for k ulong 8 +v = or v shl ulong get LBST add 48 k mul k 8 +end +ret long v +end + +note SHA-256 of the file named by LBP into LBH; bytes or negative +fn lbhpath to long +fd = lbopen 524288 0 +if lt fd 0 +ret -1 +end +n = lbhfd fd +r = fclose fd +ret n +end + +fn lbhexd c ulong to ulong +if and ge c 48 le c 57 +ret sub c 48 +end +if and ge c 97 le c 102 +ret sub c 87 +end +if and ge c 65 le c 70 +ret sub c 55 +end +ret 16 +end + +note 64 hex digits in LBIO[0..64) into 4 digest words at LBMS; 0 or 1 +fn lbhex2ms to int +for j ulong 4 +v = ulong 0 +for k ulong 16 +d = lbhexd ulong get LBIO add mul j 16 k +if gt d 15 +ret 1 +end +v = or shl v 4 d +end +put LBMS j v +end +ret 0 +end + +note LIBDIR (argv 2) into LBDIR, MANSHA (argv 3) into LBMS +fn lbargs to int +if lt argcnt 7 +ret 1 +end +n = argread 2 LBDIR 0 3000 +if or lt n 1 gt n 2999 +ret 1 +end +put LBDN 0 ulong n +m = argread 3 LBIO 0 4096 +if ne m 64 +ret 2 +end +if ne lbhex2ms 0 +ret 2 +end +ret 0 +end + +note CURRENT of LBDIR: G into LBG, digest words into LBCD; 0, 3 unreadable, 4 not 40 bytes +fn lbcurrd to int +lbpcurr false +fd = lbopen 524288 0 +if lt fd 0 +ret 3 +end +n = lbrdio fd 41 +r = fclose fd +if ne n 40 +ret 4 +end +put LBG 0 lbgetle 0 +for j ulong 4 +put LBCD j lbgetbe add 8 mul j 8 +end +ret 0 +end + +note gen-G.man of LBDIR into LBMB and LBMAN; 0, 5 unreadable, 6 not 2048 bytes +fn lbmanrd to int +lbpgen get LBG 0 3 false +fd = lbopen 524288 0 +if lt fd 0 +ret 5 +end +n = lbrdio fd 2049 +r = fclose fd +if ne n 2048 +ret 6 +end +for i ulong 2048 +put LBMB i get LBIO i +end +lbb2m +ret 0 +end + +note LBMAN words a..b-1 all zero +fn lbzero a ulong b ulong to bool +i = a +while lt i b bound 257 +if ne get LBMAN i 0 +ret false +end +i = add i 1 +end +ret true +end + +note manifest field checks (LBMAN, LBMB); 0 or 9..16 +fn lbmchk to int +if ne get LBMAN 0 3544386019401740626 +ret 9 +end +if ne get LBMAN 1 1 +ret 10 +end +lbm2b +if ne lbfnv get LBMAN 255 +ret 11 +end +g = get LBMAN 2 +if or ne g get LBG 0 ge get LBMAN 3 g +ret 12 +end +if or ne get LBMAN 14 8308871078283820969 ne get LBMAN 15 3511899704727027462 +ret 13 +end +if or ne get LBMAN 16 15311527216533678964 ne get LBMAN 17 17706733685053746017 +ret 13 +end +if ne get LBMAN 18 1 +ret 14 +end +k = get LBMAN 4 +l = get LBMAN 19 +if or gt k 16 gt l 8 +ret 15 +end +if not lbzero add 20 mul l 4 52 +ret 16 +end +if not lbzero add 56 mul k 8 255 +ret 16 +end +for t ulong k +if ne get LBMAN add 63 mul t 8 0 +ret 16 +end +end +ret 0 +end + +note digest LBH equals 4 words of LBMAN at w +fn lbhmeq w ulong to bool +for j ulong 4 +if ne get LBH j get LBMAN add w j +ret false +end +end +ret true +end + +note shared load prefix: argv, CURRENT, manifest, MANSHA, fields; 0 or 1..16 +fn lbman to int +r = lbargs +if ne r 0 +ret r +end +r = lbcurrd +if ne r 0 +ret r +end +r = lbmanrd +if ne r 0 +ret r +end +lbhman +for j ulong 4 +if ne get LBH j get LBCD j +ret 7 +end +end +for j ulong 4 +if ne get LBCD j get LBMS j +ret 8 +end +end +ret lbmchk +end + +note every lineage digest allowed by ALLOW (argv 4); 0, 17, 18 +fn lballow to int +d = new ulong 4 0 +l = get LBMAN 19 +if eq l 0 +ret 0 +end +n = argread 4 EXAPATH 0 4095 +if lt n 1 +ret 17 +end +put EXAPATH ulong n 0 +put EXALEN 0 ulong n +if ne exaload 0 +ret 17 +end +for i ulong l +for j ulong 4 +put d j get LBMAN add add 20 mul i 4 j +end +if ne exallow d 1 +ret 18 +end +end +ret 0 +end + +note SHA-256 of LBIO[0..n) (n <= 4096) into LBH +fn lbhio n ulong to void +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +for i ulong n +put b i get LBIO i +end +omsha c b 0 0 w b8 +omsha c b n 1 w b8 +omsha c b 0 2 w b8 +lbdig w +end + +note calibration handoff (K1, K16): gen-G.cal read once into LBIO, SHA-256 of exactly those bytes +note against manifest 52..55, the same bytes written into a memfd left on fd 7 without CLOEXEC for +note the generation (which reads /proc/self/fd/7, never LIBDIR). 0, 19 unreadable or not under +note 4096 bytes, 20 digest, 22 memfd or dup failed, 23 write failed. No calibration: 0, fd 7 untouched. +fn lbcalchk to int +if lbzero 52 56 +ret 0 +end +lbpgen get LBG 0 4 false +fd = lbopen 524288 0 +if lt fd 0 +ret 19 +end +n = lbrdio fd 4096 +r = fclose fd +if or lt n 1 ge n 4096 +ret 19 +end +lbhio ulong n +if not lbhmeq 52 +ret 20 +end +m = memfd LBTMFD 0 len LBTMFD 0 +if lt m 0 +ret 22 +end +mf = int m +if ne lbwrio mf ulong n 0 +ret 23 +end +if ne mf 7 +d = dupfd mf 7 0 +r = fclose mf +if ne d 7 +ret 22 +end +end +ret 0 +end + +note gen-G.elf copied into a memfd whose SHA-256 must equal manifest 10..13; fd or -21..-24 +fn lbelfmem to int +lbpgen get LBG 0 2 false +fd = lbopen 524288 0 +if lt fd 0 +ret -21 +end +m = memfd LBTMFD 0 len LBTMFD 1 +if lt m 0 +r = fclose fd +ret -22 +end +mf = int m +while true bound 999999 +r = read fd LBIO 0 4096 +if lt r 0 +r = fclose fd +ret -23 +end +if eq r 0 +break +end +if ne lbwrio mf ulong r 0 +r = fclose fd +ret -23 +end +end +r = fclose fd +n = lbhfd mf +if lt n 0 +ret -23 +end +if not lbhmeq 10 +ret -24 +end +ret mf +end + +note append LBP to the packed list LBPK as one record at o; new offset +fn lbpkadd o ulong to ulong +n = get LBPN 0 +put LBPK o uchar and n 255 +put LBPK add o 1 uchar shr n 8 +for i ulong n +put LBPK add add o 2 i get LBP i +end +ret add add o 2 n +end + +note exec /proc/self/fd/MF with argv 1..6 and loaded; returns only on failure +fn lbexec mf int to int +put LBPK 0 8 +put LBPK 1 0 +lbpz +lbptx 8 +lbpnum ulong mf +o = lbpkadd 2 +for a ulong 6 +n = argread add a 1 LBPK add o 2 sub 4000 o +if lt n 0 +ret 25 +end +put LBPK o uchar and ulong n 255 +put LBPK add o 1 uchar shr ulong n 8 +o = add add o 2 ulong n +if gt o 4000 +ret 25 +end +end +lbpz +lbptx 9 +o = lbpkadd o +r = execprog LBPK 0 o +ret 25 +end + +note B's load (contract 3.4): 0 never (exec'd), nonzero = refused with the code listed above +fn lbload to int +if ne argcnt 7 +ret 1 +end +r = lbman +if ne r 0 +ret r +end +r = lballow +if ne r 0 +ret r +end +r = lbcalchk +if ne r 0 +ret r +end +mf = lbelfmem +if lt mf 0 +ret neg mf +end +ret lbexec mf +end + +note manifest lineage for revocation checks in a loaded generation: LBLD, LBLN, LBGEN, LBCAL +fn lblineage to int +r = lbman +if ne r 0 +ret r +end +put LBLN 0 get LBMAN 19 +for i ulong 32 +put LBLD i get LBMAN add 20 i +end +put LBGEN 0 get LBG 0 +put LBCAL 0 0 +if not lbzero 52 56 +put LBCAL 0 1 +end +ret 0 +end + +note drive.om side. Remove a stale unpublished generation g (crash leftovers) and CURRENT.tmp. +fn lbclean g ulong to void +for e ulong 4 +lbpgen g add e 1 false +r = unlink LBP 0 get LBPN 0 +lbpgen g add e 1 true +r = unlink LBP 0 get LBPN 0 +end +lbpcurr true +r = unlink LBP 0 get LBPN 0 +end + +note fsync the file named by LBP; 0 or 1 +fn lbsyncp to int +fd = lbopen 524288 0 +if lt fd 0 +ret 1 +end +r = fsync fd +s = fclose fd +if ne r 0 +ret 1 +end +ret 0 +end + +fn lbsyncdir to int +r = fopen LBDIR 0 get LBDN 0 524288 0 +if lt r 0 +ret 1 +end +fd = int r +s = fsync fd +r = fclose fd +if ne s 0 +ret 1 +end +ret 0 +end + +note gen-G.EXT.tmp: fsync, then renames flag 1 onto gen-G.EXT; 0 or 1 +fn lbrengen g ulong ext ulong to int +lbpgen g ext true +if ne lbsyncp 0 +ret 1 +end +o = lbpkadd 2 +lbpgen g ext false +o = lbpkadd o +put LBPK 0 2 +put LBPK 1 0 +r = renames LBPK 0 o 1 +if ne r 0 +ret 1 +end +ret 0 +end + +note publish generation LBG with manifest LBMAN (sealed) in ADR 0015 order. Expects gen-G.om.tmp, +note gen-G.elf.tmp and, when manifest 52..55 is nonzero, gen-G.cal.tmp already written. Steps: +note 1 om, 2 elf, 3 cal (no-op without calibration), 4 man.tmp written and fsync'd, 5 man renamed, +note 6 LIBDIR fsync, 7 CURRENT.tmp written and fsync'd, 8 CURRENT renamed, 9 LIBDIR fsync. +note crash = K stops after step K with 97 (control C8); 0 ok; 100 + step on failure. +fn lbpub crash ulong to int +g = get LBG 0 +if ne lbrengen g 1 0 +ret 101 +end +if eq crash 1 +ret 97 +end +if ne lbrengen g 2 0 +ret 102 +end +if eq crash 2 +ret 97 +end +if not lbzero 52 56 +if ne lbrengen g 4 0 +ret 103 +end +end +if eq crash 3 +ret 97 +end +lbm2b +lbpgen g 3 true +fd = lbopen 524865 384 +if lt fd 0 +ret 104 +end +w = write fd LBMB 0 2048 +s = fsync fd +r = fclose fd +if or ne w 2048 ne s 0 +ret 104 +end +if eq crash 4 +ret 97 +end +lbpgen g 3 true +o = lbpkadd 2 +lbpgen g 3 false +o = lbpkadd o +put LBPK 0 2 +put LBPK 1 0 +r = renames LBPK 0 o 1 +if ne r 0 +ret 105 +end +if eq crash 5 +ret 97 +end +if ne lbsyncdir 0 +ret 106 +end +if eq crash 6 +ret 97 +end +lbhman +for k ulong 8 +put LBIO k uchar and shr g mul k 8 255 +end +for j ulong 4 +lbputbe add 8 mul j 8 get LBH j +end +lbpcurr true +fd = lbopen 524865 384 +if lt fd 0 +ret 107 +end +w2 = lbwrio fd 40 +s = fsync fd +r = fclose fd +if or ne w2 0 ne s 0 +ret 107 +end +if eq crash 7 +ret 97 +end +lbpcurr true +o = lbpkadd 2 +lbpcurr false +o = lbpkadd o +put LBPK 0 2 +put LBPK 1 0 +r = renames LBPK 0 o 0 +if ne r 0 +ret 108 +end +if eq crash 8 +ret 97 +end +if ne lbsyncdir 0 +ret 109 +end +if eq crash 9 +ret 97 +end +ret 0 +end +note SPDX-License-Identifier: Apache-2.0 +note methods.om: generated by om/reuse/codegen.om (REUSE-1 contract 3.2 and 12). Do not edit. +fn mtl0 x ulong to ulong +v = x +v = shr v get R1P 6 +v = and v get R1P 8 +v = and add v get R1P 10 255 +v = and add v get R1P 12 255 +v = and shl v get R1P 14 255 +v = shr v get R1P 16 +ret v +end +fn mtr0 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl0 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl1 x ulong to ulong +v = x +v = and add v get R1P 6 65535 +ret v +end +fn mtr1 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl1 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl2 x ulong to ulong +v = x +v = and mul v get R1P 6 65535 +v = shr v get R1P 8 +v = xor v get R1P 10 +v = and sub v get R1P 12 65535 +ret v +end +fn mtr2 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl2 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl3 x ulong to ulong +v = x +v = and add v get R1P 6 255 +v = xor v get R1P 8 +ret v +end +fn mtr3 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl3 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl4 x ulong to ulong +v = x +v = and shl v get R1P 6 255 +v = shr v get R1P 8 +v = and sub v get R1P 10 255 +ret v +end +fn mtr4 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl4 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl5 x ulong to ulong +v = x +v = and sub v get R1P 6 65535 +ret v +end +fn mtr5 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl5 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl6 x ulong to ulong +v = x +v = and mul v get R1P 6 255 +v = or v get R1P 8 +v = and sub v get R1P 10 255 +v = and mul v get R1P 12 255 +ret v +end +fn mtr6 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl6 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl7 x ulong to ulong +v = x +v = and sub v get R1P 6 65535 +v = shr v get R1P 8 +v = and shl v get R1P 10 65535 +v = and shl v get R1P 12 65535 +ret v +end +fn mtr7 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl7 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl8 x ulong to ulong +v = x +v = shr v get R1P 6 +ret v +end +fn mtr8 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl8 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl9 x ulong to ulong +v = x +v = and add v get R1P 6 65535 +v = and mul v get R1P 8 65535 +ret v +end +fn mtr9 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl9 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl10 x ulong to ulong +v = x +v = or v get R1P 6 +v = shr v get R1P 8 +v = and add v get R1P 10 65535 +v = and mul v get R1P 12 65535 +ret v +end +fn mtr10 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl10 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl11 x ulong to ulong +v = x +v = xor v get R1P 6 +v = and shl v get R1P 8 65535 +v = and add v get R1P 10 65535 +v = and v get R1P 12 +v = xor v get R1P 14 +v = or v get R1P 16 +ret v +end +fn mtr11 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl11 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl12 x ulong to ulong +v = x +v = xor v get R1P 6 +ret v +end +fn mtr12 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl12 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl13 x ulong to ulong +v = x +v = and shl v get R1P 6 255 +v = or v get R1P 8 +ret v +end +fn mtr13 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl13 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl14 x ulong to ulong +v = x +v = xor v get R1P 6 +ret v +end +fn mtr14 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl14 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtr15 to void +q = get R1P 4 +n = get R1N 0 +sum = ulong 0 +for i ulong n +q = get R1T add mul q 16 get R1X i +sum = add sum get R1P add 6 q +end +put R1R 0 q +put R1R 1 sum +end +fn mtcount to ulong +ret 16 +end +fn mtshape t ulong to ulong +if eq t 0 +ret 580110541832 +end +if eq t 1 +ret 70672 +end +if eq t 2 +ret 646137104 +end +if eq t 3 +ret 6366216 +end +if eq t 4 +ret 42414856 +end +if eq t 5 +ret 135440 +end +if eq t 6 +ret 844318216 +end +if eq t 7 +ret 2005026320 +end +if eq t 8 +ret 529936 +end +if eq t 9 +ret 3219728 +end +if eq t 10 +ret 830817296 +end +if eq t 11 +ret 370465465360 +end +if eq t 12 +ret 398352 +end +if eq t 13 +ret 5710088 +end +if eq t 14 +ret 398344 +end +if eq t 15 +ret 1 +end +ret 0 +end +fn mtfind to long +if ne r1valid 0 +ret -1 +end +s = r1shape +if eq s 580110541832 +ret 0 +end +if eq s 70672 +ret 1 +end +if eq s 646137104 +ret 2 +end +if eq s 6366216 +ret 3 +end +if eq s 42414856 +ret 4 +end +if eq s 135440 +ret 5 +end +if eq s 844318216 +ret 6 +end +if eq s 2005026320 +ret 7 +end +if eq s 529936 +ret 8 +end +if eq s 3219728 +ret 9 +end +if eq s 830817296 +ret 10 +end +if eq s 370465465360 +ret 11 +end +if eq s 398352 +ret 12 +end +if eq s 5710088 +ret 13 +end +if eq s 398344 +ret 14 +end +if eq s 1 +ret 15 +end +ret -1 +end +fn mtlane t ulong x ulong to ulong +if eq t 0 +ret mtl0 x +end +if eq t 1 +ret mtl1 x +end +if eq t 2 +ret mtl2 x +end +if eq t 3 +ret mtl3 x +end +if eq t 4 +ret mtl4 x +end +if eq t 5 +ret mtl5 x +end +if eq t 6 +ret mtl6 x +end +if eq t 7 +ret mtl7 x +end +if eq t 8 +ret mtl8 x +end +if eq t 9 +ret mtl9 x +end +if eq t 10 +ret mtl10 x +end +if eq t 11 +ret mtl11 x +end +if eq t 12 +ret mtl12 x +end +if eq t 13 +ret mtl13 x +end +if eq t 14 +ret mtl14 x +end +ret 0 +end +fn mtrun t ulong to void +if eq t 0 +mtr0 +ret +end +if eq t 1 +mtr1 +ret +end +if eq t 2 +mtr2 +ret +end +if eq t 3 +mtr3 +ret +end +if eq t 4 +mtr4 +ret +end +if eq t 5 +mtr5 +ret +end +if eq t 6 +mtr6 +ret +end +if eq t 7 +mtr7 +ret +end +if eq t 8 +mtr8 +ret +end +if eq t 9 +mtr9 +ret +end +if eq t 10 +mtr10 +ret +end +if eq t 11 +mtr11 +ret +end +if eq t 12 +mtr12 +ret +end +if eq t 13 +mtr13 +ret +end +if eq t 14 +mtr14 +ret +end +if eq t 15 +mtr15 +ret +end +r1full +end +fn mtsrc t ulong D edit ulong 4 to void +if eq t 0 +put D 0 13956669196267666850 +put D 1 16257126963529257265 +put D 2 1405145371321743116 +put D 3 17760178884020577383 +ret +end +if eq t 1 +put D 0 16326708910157820744 +put D 1 3637440196527792780 +put D 2 7614385845250149790 +put D 3 2771496369924541204 +ret +end +if eq t 2 +put D 0 16877686864802232791 +put D 1 15111118989403976913 +put D 2 14528391367598743313 +put D 3 3401917217711249322 +ret +end +if eq t 3 +put D 0 13927010687405460251 +put D 1 11336324721807575576 +put D 2 14452245014462828173 +put D 3 17446810644995259200 +ret +end +if eq t 4 +put D 0 15534822331246716171 +put D 1 9506174321960071820 +put D 2 10887431937852286478 +put D 3 4072147763287097614 +ret +end +if eq t 5 +put D 0 16787454951761859370 +put D 1 17846581186593258686 +put D 2 6679271396805651942 +put D 3 63794893902553172 +ret +end +if eq t 6 +put D 0 16154932368086511111 +put D 1 4410123057694529404 +put D 2 2738225962833191960 +put D 3 3395758471500380270 +ret +end +if eq t 7 +put D 0 17213268167526360733 +put D 1 2734722253015343528 +put D 2 10361122349800966421 +put D 3 9644199633733047973 +ret +end +if eq t 8 +put D 0 10473156190873895805 +put D 1 12648395775458477319 +put D 2 9092058131804576619 +put D 3 7141339892388031631 +ret +end +if eq t 9 +put D 0 12154204761550884436 +put D 1 3845224749664794519 +put D 2 2863352576014568853 +put D 3 4735062814132508435 +ret +end +if eq t 10 +put D 0 12823728127347081233 +put D 1 2474108056008288794 +put D 2 11822154959845612860 +put D 3 6280287911338619165 +ret +end +if eq t 11 +put D 0 17312590123913157856 +put D 1 16754795256309437279 +put D 2 11323165121633928988 +put D 3 13689713254852536391 +ret +end +if eq t 12 +put D 0 816950227113715371 +put D 1 8364610021896710454 +put D 2 11166026112771622937 +put D 3 18409906715905725576 +ret +end +if eq t 13 +put D 0 7747653742871137153 +put D 1 1735523690990067922 +put D 2 16728278509358837195 +put D 3 6978874779406273489 +ret +end +if eq t 14 +put D 0 1944621461038878652 +put D 1 18439625667258543098 +put D 2 14812921912109527545 +put D 3 4804251481274239608 +ret +end +if eq t 15 +put D 0 5697826474538046150 +put D 1 17605239106479653115 +put D 2 2589925873017448613 +put D 3 11652210143643333060 +ret +end +put D 0 0 +put D 1 0 +put D 2 0 +put D 3 0 +end +note SPDX-License-Identifier: Apache-2.0 +note router.om: the REUSE-1 arm process (docs/reuse/REUSE-1.md section 2, 3.4 load, 4 revocation, and +note clarifications K1, K2, K9). Last unit; holds fn main. Every arm binary is this router. +note argv: ARM LIBDIR MANSHA ALLOW TAPFILE SPLIT [loaded]; ARM echo full cache method delta select tap. +note Startup exit codes (no response is written): 2 usage (argc, ARM), 2 tap refused (extopen). +note Protocol: little-endian u64 words on stdin/stdout. Request op, p, p words; response status, q, +note q words. Framing (K2): op outside 1..10 or p > 7234 -> 4 and exit 4 (the payload is not read); +note otherwise the payload is read whole; a p that does not match its op -> 4; ops 2..8 without a +note session -> 6; then the op's own statuses. EOF on stdin without END -> exit 3, no response. +note Load (3.4): ARM method/delta/select, LIBDIR not "-", no trailing "loaded": lbload (lib.om) execs +note the generation and never returns on success; on its nonzero return this process serves every +note QUERY as full with route 5. A generation (argv ends in "loaded") never loads again; it reads its +note lineage (lblineage) and checks it against ALLOW at every BEGIN and, whenever the stat snapshot +note of ALLOW (inode, size, mtime ns, ctime ns) changed, at QUERY: a revoked or unverifiable lineage +note gives route 5 from that QUERY on for the rest of the process. select in a generation reads its +note calibration from /proc/self/fd/7, the memfd of verified bytes lbload left open before exec (K16), +note iff the manifest names one (LBCAL); never LIBDIR/gen-G.cal. With LIBDIR "-" it has no calibration. +note Arms at QUERY (route): echo 0 0 (0); full r1full (1); tap r1full and one experience record (1); +note cache key SHA-256(program digest, n, state digest), the state digest recomputed only at the first +note QUERY after a change, 4096 entries FIFO: hit (2), miss r1full (1); method mtrun (3) when a +note template is bound and passed G3, else r1full (1), G3 failure r1full (5) for the session; delta: +note the delta state (4) of the bound method or of the baseline (t = -1), rebuilt by dlbind at a QUERY +note after a drop, else method (3) or full (1); select: slpick among full (1), method (3), delta (4); +note once sloff reports churn (5.2) the delta state is dropped and not maintained for the session. +note STATS answers 32 counters RTST: 0..5 QUERYs by route 0..5, 6 sessions, 7 change items applied, +note 8 PROGRAMs applied, 9 refused requests, 10 ckbind calls, 11 bind failures, 12 G3 FIFO hits, +note 13 G3 lane pairs, 14..25 dlstat 0..11, 26 select delta disables (SLDIS), 27 select refusals +note (SLREF), 28 cache entries, 29 template count (mtcount), 30 load refusal code, 31 flags +note (1 generation, 2 refused, 4 revoked, 8 calibration path set, 16 calibration loaded by slload). +text RTTE "echo" +text RTTF "full" +text RTTC "cache" +text RTTM "method" +text RTTD "delta" +text RTTS "select" +text RTTT "tap" +text RTTL "loaded" +text RTTCAL "/proc/self/fd/7" +global RTARM ulong 1 +global RTFLG ulong 1 +global RTLC ulong 1 +global RTLIN ulong 1 +global RTSES ulong 1 +global RTSN ulong 1 +global RTQN ulong 1 +global RTNC ulong 1 +global RTT long 1 +global RTBF ulong 1 +global RTDB ulong 1 +global RTSD ulong 1 +global RTPS ulong 64 +global RTQ ulong 7234 +global RTIB uchar 65536 +global RTIP ulong 2 +global RTOW ulong 34 +global RTOB uchar 272 +global RTST ulong 32 +global RTCK ulong 24576 +global RTCN ulong 1 +global RTCH ulong 1 +global RTKEY ulong 4 +global RTA uchar 4096 + +note ---- input and output ---- +fn rtfill to bool +k = read 0 RTIB 0 65536 +if le k 0 +ret false +end +put RTIP 0 0 +put RTIP 1 ulong k +ret true +end + +fn rtbyte to ulong +p = get RTIP 0 +if ge p get RTIP 1 +if not rtfill +exit 3 +end +p = 0 +end +put RTIP 0 add p 1 +ret ulong get RTIB p +end + +fn rtword to ulong +p = get RTIP 0 +if le add p 8 get RTIP 1 +v = ulong 0 +for i ulong 8 +v = or shl v 8 ulong get RTIB sub add p 7 i +end +put RTIP 0 add p 8 +ret v +end +v = ulong 0 +for i ulong 8 +v = or v shl rtbyte mul i 8 +end +ret v +end + +note status s, q words RTOW[0..q) +fn rtresp s ulong q ulong to void +rtputw 0 s +rtputw 8 q +for i ulong q +rtputw mul add i 2 8 get RTOW i +end +n = mul add q 2 8 +off = ulong 0 +while lt off n bound 1000 +k = write 1 RTOB off sub n off +if le k 0 +exit 5 +end +off = add off ulong k +end +end + +fn rtputw o ulong w ulong to void +v = w +for i ulong 8 +put RTOB add o i uchar and v 255 +v = shr v 8 +end +end + +fn rtstat s ulong to void +if ne s 0 +put RTST 9 add get RTST 9 1 +end +rtresp s 0 +end + +note ---- argv ---- +note argv i into RTA: its length, or -1 +fn rtarg i ulong to long +n = argread i RTA 0 4096 +if or lt n 0 gt n 4095 +ret -1 +end +ret n +end + +fn rtdash i ulong to bool +n = rtarg i +ret and eq n 1 eq get RTA 0 45 +end + +note RTA[0..n) equals text id (1 echo .. 7 tap, 8 loaded) +fn rteq id ulong n ulong to bool +if eq id 1 +ret rteqe n +end +if eq id 2 +ret rteqf n +end +if eq id 3 +ret rteqc n +end +if eq id 4 +ret rteqm n +end +if eq id 5 +ret rteqd n +end +if eq id 6 +ret rteqs n +end +if eq id 7 +ret rteqt n +end +ret rteql n +end + +fn rteqe n ulong to bool +if ne n len RTTE +ret false +end +for i ulong n +if ne get RTA i get RTTE i +ret false +end +end +ret true +end + +fn rteqf n ulong to bool +if ne n len RTTF +ret false +end +for i ulong n +if ne get RTA i get RTTF i +ret false +end +end +ret true +end + +fn rteqc n ulong to bool +if ne n len RTTC +ret false +end +for i ulong n +if ne get RTA i get RTTC i +ret false +end +end +ret true +end + +fn rteqm n ulong to bool +if ne n len RTTM +ret false +end +for i ulong n +if ne get RTA i get RTTM i +ret false +end +end +ret true +end + +fn rteqd n ulong to bool +if ne n len RTTD +ret false +end +for i ulong n +if ne get RTA i get RTTD i +ret false +end +end +ret true +end + +fn rteqs n ulong to bool +if ne n len RTTS +ret false +end +for i ulong n +if ne get RTA i get RTTS i +ret false +end +end +ret true +end + +fn rteqt n ulong to bool +if ne n len RTTT +ret false +end +for i ulong n +if ne get RTA i get RTTT i +ret false +end +end +ret true +end + +fn rteql n ulong to bool +if ne n len RTTL +ret false +end +for i ulong n +if ne get RTA i get RTTL i +ret false +end +end +ret true +end + +note ARM: 1 echo 2 full 3 cache 4 method 5 delta 6 select 7 tap, 0 unknown +fn rtparm to ulong +n = rtarg 1 +if lt n 0 +ret 0 +end +for id ulong 7 +if rteq add id 1 ulong n +ret add id 1 +end +end +ret 0 +end + +note the arm has a library path: method, delta, select +fn rtlibarm to bool +a = get RTARM 0 +ret and ge a 4 le a 6 +end + +fn rtdelarm to bool +a = get RTARM 0 +ret or eq a 5 eq a 6 +end + +note route 5 for every QUERY: refused generation or revoked lineage +fn rtdead to bool +ret ne and get RTFLG 0 6 0 +end + +note ---- startup ---- +fn rtsetallow to void +if rtdash 4 +put EXALEN 0 0 +ret +end +n = ulong rtarg 4 +for i ulong n +put EXAPATH i get RTA i +end +put EXALEN 0 n +end + +fn rtcal to void +if ne get LBCAL 0 1 +ret +end +for i ulong len RTTCAL +put SLPATH i get RTTCAL i +end +put SLPATH len RTTCAL 0 +put RTFLG 0 or get RTFLG 0 8 +end + +note a refused generation load: code x (STATS 30; 255 when it is not 1..254), route 5 from now on +fn rtrefuse x int to void +c = ulong 255 +if and gt x 0 lt x 255 +c = ulong x +end +put RTLC 0 c +put RTFLG 0 or get RTFLG 0 2 +end + +fn rtinit to void +c = argcnt +if and ne c 7 ne c 8 +exit 2 +end +loaded = false +if eq c 8 +n = rtarg 7 +if or lt n 0 not rteq 8 ulong n +exit 2 +end +loaded = true +end +a = rtparm +if eq a 0 +exit 2 +end +put RTARM 0 a +put RTT 0 -1 +rtsetallow +if eq a 7 +if ne extopen 0 +exit 2 +end +end +if and rtlibarm not rtdash 2 +if loaded +rtgen +else +x = lbload +rtrefuse x +end +end +if eq a 6 +if eq slload 0 +put RTFLG 0 or get RTFLG 0 16 +end +end +end + +note a generation: lineage from the manifest pinned by MANSHA, calibration path +fn rtgen to void +put RTFLG 0 or get RTFLG 0 1 +x = lblineage +if ne x 0 +rtrefuse x +ret +end +if gt get LBLN 0 0 +put RTLIN 0 1 +end +if eq get RTARM 0 6 +rtcal +end +end + +note ---- revocation ---- +note every lineage digest allowed by the loaded ALLOW +fn rtlinok to bool +d = new ulong 4 0 +n = get LBLN 0 +for e ulong n +for j ulong 4 +put d j get LBLD add mul e 4 j +end +if ne exallow d 1 +ret false +end +end +ret true +end + +fn rtrevoke to void +put RTFLG 0 or get RTFLG 0 4 +if rtdelarm +dldrop +end +put RTDB 0 0 +put RTT 0 -1 +end + +note read ALLOW and check the lineage; snapshot its stat +fn rtrecheck to void +ch = exastat +ok = false +if gt get EXALEN 0 0 +if eq exaload 0 +ok = rtlinok +end +end +if not ok +rtrevoke +end +end + +fn rtrevq to void +if or eq get RTLIN 0 0 rtdead +ret +end +if exastat +x = exaload +if or ne x 0 not rtlinok +rtrevoke +end +end +end + +note ---- binding ---- +note bind for the current R1P (BEGIN or PROGRAM); the delta state was reset or dropped by the caller +fn rtbind to void +put RTT 0 -1 +put RTBF 0 0 +put RTDB 0 0 +a = get RTARM 0 +if or lt a 4 gt a 6 +ret +end +if rtdead +ret +end +t = mtfind +want = ge t 0 +if eq a 6 +want = and want slbind t +end +if want +if eq ckbind ulong t 0 +put RTT 0 t +else +put RTBF 0 1 +ret +end +end +if rtdelarm +if rtseloff +ret +end +if eq dlbind get RTT 0 0 +put RTDB 0 1 +end +end +end + +note select: churn disabled the delta path for this session (sloff, after the last slpick) +fn rtseloff to bool +if eq get RTARM 0 6 +ret sloff +end +ret false +end + +note ---- requests ---- +fn rtsavep to void +for i ulong 64 +put RTPS i get R1P i +put R1P i get RTQ i +end +end + +fn rtrestp to void +for i ulong 64 +put R1P i get RTPS i +end +end + +note BEGIN: payload PROG, n, n values (p checked by the caller) +fn rtbegin to ulong +n = get RTQ 64 +if gt n 4096 +ret 5 +end +rtsavep +if ne r1valid 0 +rtrestp +ret 2 +end +for i ulong n +if not r1indom get RTQ add 65 i +rtrestp +ret 3 +end +end +put R1N 0 n +for i ulong n +put R1X i get RTQ add 65 i +end +put RTSES 0 1 +put RTSN 0 add get RTSN 0 1 +put RTQN 0 0 +put RTNC 0 0 +put RTSD 0 1 +put RTST 6 add get RTST 6 1 +if gt get RTLIN 0 0 +if not rtdead +rtrecheck +end +end +if eq get RTARM 0 6 +slsess +end +if rtdelarm +dlsreset +end +if eq get RTARM 0 3 +r1pdig +end +rtbind +ret 0 +end + +fn rtprog to ulong +rtsavep +if ne r1valid 0 +rtrestp +ret 2 +end +if not r1alldom +rtrestp +ret 3 +end +put RTNC 0 add get RTNC 0 1 +put RTST 8 add get RTST 8 1 +if rtdelarm +dldrop +end +if eq get RTARM 0 3 +r1pdig +end +rtbind +ret 0 +end + +note apply one validated change and tell the delta state +fn rtapply op ulong a ulong b ulong to void +old = r1apply op a b +put RTNC 0 add get RTNC 0 1 +put RTSD 0 1 +put RTST 7 add get RTST 7 1 +if eq get RTDB 0 1 +v = b +if eq op 5 +v = 0 +end +if ne dlchg op a v old 0 +put RTDB 0 0 +end +end +end + +fn rtchange op ulong to ulong +a = get RTQ 0 +b = ulong 0 +if ne op 5 +b = get RTQ 1 +end +if lt r1chg op a b get R1N 0 0 +ret 3 +end +rtapply op a b +ret 0 +end + +note BATCH item i: validity against a state of n values (the new n or -1), and application +fn rtitemchk i ulong n ulong to long +q = add 1 mul i 3 +op = get RTQ q +a = get RTQ add q 1 +ret r1chg op a get RTQ add q 2 n +end + +fn rtitem i ulong to void +q = add 1 mul i 3 +op = get RTQ q +a = get RTQ add q 1 +rtapply op a get RTQ add q 2 +end + +fn rtbatch to ulong +m = get RTQ 0 +if or lt m 1 gt m 1024 +ret 3 +end +n = long get R1N 0 +for i ulong m +n = rtitemchk i ulong n +if lt n 0 +ret 3 +end +end +for i ulong m +rtitem i +end +ret 0 +end + +note ---- QUERY ---- +fn rtfull route ulong to ulong +r1full +ret route +end + +fn rtcache to ulong +if eq get RTSD 0 1 +r1sdig +put RTSD 0 0 +end +for j ulong 4 +r1putw mul j 8 r1bswap get R1PD j +r1putw add 40 mul j 8 r1bswap get R1SD j +end +r1putw 32 get R1N 0 +r1hash 72 +for j ulong 4 +put RTKEY j get R1HD j +end +n = get RTCN 0 +for e ulong n +if rtckeq mul e 6 +put R1R 0 get RTCK add mul e 6 4 +put R1R 1 get RTCK add mul e 6 5 +ret 2 +end +end +r1full +h = get RTCH 0 +b = mul h 6 +for j ulong 4 +put RTCK add b j get RTKEY j +end +put RTCK add b 4 get R1R 0 +put RTCK add b 5 get R1R 1 +put RTCH 0 rem add h 1 4096 +if lt n 4096 +put RTCN 0 add n 1 +end +ret 1 +end + +fn rtckeq b ulong to bool +if ne get RTCK b get RTKEY 0 +ret false +end +for j ulong 3 +if ne get RTCK add b add j 1 get RTKEY add j 1 +ret false +end +end +ret true +end + +fn rttap to ulong +t0 = monons +r1full +ns = ulong sub monons t0 +if ne extap get RTSN 0 get RTQN 0 get RTNC 0 ns 0 +exit 12 +end +ret 1 +end + +fn rtmethod to ulong +t = get RTT 0 +if ge t 0 +mtrun ulong t +ret 3 +end +ret rtfull 1 +end + +note the delta path: rebuild after a drop, then dlres; 1 when it could not answer +fn rtdelta to bool +if eq get RTDB 0 0 +if eq dlbind get RTT 0 0 +put RTDB 0 1 +end +end +if eq get RTDB 0 1 +if eq dlres 0 +ret true +end +end +ret false +end + +fn rtselect to ulong +meth = ge get RTT 0 0 +slset meth true +p = slpick get RTNC 0 +if rtseloff +if eq get RTDB 0 1 +dldrop +put RTDB 0 0 +end +end +if eq p 4 +if rtdelta +ret 4 +end +end +if and eq p 3 meth +ret rtmethod +end +if meth +if ne p 1 +ret rtmethod +end +end +ret rtfull 1 +end + +fn rtroute to ulong +a = get RTARM 0 +if eq a 1 +put R1R 0 0 +put R1R 1 0 +ret 0 +end +if eq a 2 +ret rtfull 1 +end +if eq a 3 +ret rtcache +end +if eq a 7 +ret rttap +end +rtrevq +if or rtdead eq get RTBF 0 1 +ret rtfull 5 +end +if eq a 4 +ret rtmethod +end +if eq a 5 +if rtdelta +ret 4 +end +ret rtmethod +end +ret rtselect +end + +fn rtquery to void +put RTQN 0 add get RTQN 0 1 +r = rtroute +put RTNC 0 0 +put RTST r add get RTST r 1 +put RTOW 0 get R1R 0 +put RTOW 1 get R1R 1 +put RTOW 2 r +rtresp 0 3 +end + +fn rtstats to void +for k ulong 14 +put RTOW k get RTST k +end +put RTOW 10 get CKST 0 +put RTOW 11 get CKST 1 +put RTOW 12 get CKST 2 +put RTOW 13 get CKST 4 +for k ulong 12 +put RTOW add 14 k dlstat k +end +put RTOW 26 get SLDIS 0 +put RTOW 27 get SLREF 0 +put RTOW 28 get RTCN 0 +put RTOW 29 mtcount +put RTOW 30 get RTLC 0 +put RTOW 31 get RTFLG 0 +rtresp 0 32 +end + +fn rtend to void +rtresp 0 0 +if eq get RTARM 0 7 +fd = int get EXTFD 0 +r = fsync fd +r = fclose fd +end +exit 0 +end + +note p matches op (K2) +fn rtpok op ulong p ulong to bool +if eq op 1 +if lt p 65 +ret false +end +n = get RTQ 64 +ret and le n 7169 eq p add 65 n +end +if or eq op 3 or eq op 4 eq op 6 +ret eq p 2 +end +if eq op 5 +ret eq p 1 +end +if eq op 7 +if lt p 1 +ret false +end +m = get RTQ 0 +ret and le m 2411 eq p add 1 mul m 3 +end +if eq op 8 +ret eq p 64 +end +ret eq p 0 +end + +fn rtop op ulong p ulong to void +if not rtpok op p +rtstat 4 +ret +end +if eq op 10 +rtend +end +if eq op 9 +rtstats +ret +end +if eq op 1 +rtstat rtbegin +ret +end +if eq get RTSES 0 0 +rtstat 6 +ret +end +if eq op 2 +rtquery +ret +end +if eq op 7 +rtstat rtbatch +ret +end +if eq op 8 +rtstat rtprog +ret +end +rtstat rtchange op +end + +fn rtreq to void +op = rtword +p = rtword +if or or lt op 1 gt op 10 gt p 7234 +rtstat 4 +exit 4 +end +for i ulong p +put RTQ i rtword +end +rtop op p +end + +fn main to int +rtinit +for o ulong 1000 +for i ulong 999999 +rtreq +end +end +ret 4 +end diff --git a/evidence/REUSE-742/freeze/lib-T/CURRENT b/evidence/REUSE-742/freeze/lib-T/CURRENT new file mode 100644 index 000000000..669a15fcf Binary files /dev/null and b/evidence/REUSE-742/freeze/lib-T/CURRENT differ diff --git a/evidence/REUSE-742/freeze/lib-T/gen-1.cal b/evidence/REUSE-742/freeze/lib-T/gen-1.cal new file mode 100644 index 000000000..fae477009 Binary files /dev/null and b/evidence/REUSE-742/freeze/lib-T/gen-1.cal differ diff --git a/evidence/REUSE-742/freeze/lib-T/gen-1.man b/evidence/REUSE-742/freeze/lib-T/gen-1.man new file mode 100644 index 000000000..5ad2ab08e Binary files /dev/null and b/evidence/REUSE-742/freeze/lib-T/gen-1.man differ diff --git a/evidence/REUSE-742/freeze/lib-T/gen-1.om b/evidence/REUSE-742/freeze/lib-T/gen-1.om new file mode 100644 index 000000000..d7daf89d7 --- /dev/null +++ b/evidence/REUSE-742/freeze/lib-T/gen-1.om @@ -0,0 +1,5992 @@ +note SPDX-License-Identifier: Apache-2.0 +note sha256.om: FIPS 180-4 SHA-256 as one function-mode unit, bound through the omega-bridge native +note call ABI (docs/NATIVE-ABI.md) and exported as omega_omsha. It replaces the algorithm of +note src/sha256.c: the C file keeps only the sha256_* entry points that cut caller data into +note pieces of at most 4096 bytes. +note +note The whole hash state lives in the caller's sha256_ctx (104 bytes, src/sha256.h): +note bytes 0..31 state[8], uint32 little endian +note bytes 32..39 count, uint64 little endian (bytes absorbed so far) +note bytes 40..103 buffer, the pending partial block +note omsha c b n op w b8: +note op 0 init: count 0, FIPS initial hash value +note op 1 absorb b[0..n), n <= 4096, with src/sha256.c's buffering rules +note op 2 final: pad, absorb the bit length, the 32-byte digest packed into w[0..4) +note op 3 one shot: the digest of the n < 4096 bytes of b8 packed into w[0..4); c is not used +note w is the caller's scratch for the message schedule (64 words, contents ignored and overwritten): +note Omega writes every word before reading it, so the caller need not clear it. +note n > 4096 traps 3; any other op traps 9. + +note little-endian word of nb bytes at byte offset p of the context +fn getle c edit uchar 104 p ulong nb ulong to ulong +v = ulong 0 +i = nb +while gt i 0 bound 8 +i = sub i 1 +v = or shl v 8 ulong get c add p i +end +ret v +end + +fn putle c edit uchar 104 p ulong nb ulong v ulong to void +x = v +for i ulong nb +put c add p i uchar and x 255 +x = shr x 8 +end +end + +note the hash words of the context into h, and back +fn loadh c edit uchar 104 h edit uint 8 to void +for i ulong 8 +put h i uint getle c mul i 4 4 +end +end + +fn storeh c edit uchar 104 h edit uint 8 to void +for i ulong 8 +putle c mul i 4 4 ulong get h i +end +end + +note big-endian words of the block at byte o of b into w[0..16), then the schedule +fn schedb b view uchar 4096 o ulong w edit ulong 64 to void +for i ulong 16 +q = add o mul i 4 +v = ulong get b q +v = or shl v 8 ulong get b add q 1 +v = or shl v 8 ulong get b add q 2 +v = or shl v 8 ulong get b add q 3 +put w i or v shl v 32 +end +expand w +end + +note big-endian words of the block at byte 40 of b into w[0..16), then the schedule +fn schedc b edit uchar 104 w edit ulong 64 to void +for i ulong 16 +q = add 40 mul i 4 +v = ulong get b q +v = or shl v 8 ulong get b add q 1 +v = or shl v 8 ulong get b add q 2 +v = or shl v 8 ulong get b add q 3 +put w i or v shl v 32 +end +expand w +end + +note the 64 rounds, unrolled with the working words in locals (names rotate instead of moving). +note Same lazy low-32-bit cells and rotate trick as expand; K is an immediate; the eight words are +note reduced to 32 bits only when they are added back into h. +fn rounds h edit uint 8 w view ulong 64 to void +a = ulong get h 0 +b = ulong get h 1 +c = ulong get h 2 +d = ulong get h 3 +e = ulong get h 4 +f = ulong get h 5 +g = ulong get h 6 +x = ulong get h 7 +t = ulong 0 +u = ulong 0 +t = add x add get w 0 1116352408 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 1 1899447441 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 2 3049323471 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 3 3921009573 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 4 961987163 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 5 1508970993 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 6 2453635748 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 7 2870763221 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 8 3624381080 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 9 310598401 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 10 607225278 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 11 1426881987 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 12 1925078388 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 13 2162078206 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 14 2614888103 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 15 3248222580 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 16 3835390401 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 17 4022224774 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 18 264347078 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 19 604807628 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 20 770255983 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 21 1249150122 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 22 1555081692 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 23 1996064986 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 24 2554220882 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 25 2821834349 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 26 2952996808 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 27 3210313671 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 28 3336571891 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 29 3584528711 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 30 113926993 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 31 338241895 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 32 666307205 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 33 773529912 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 34 1294757372 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 35 1396182291 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 36 1695183700 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 37 1986661051 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 38 2177026350 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 39 2456956037 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 40 2730485921 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 41 2820302411 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 42 3259730800 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 43 3345764771 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 44 3516065817 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 45 3600352804 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 46 4094571909 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 47 275423344 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 48 430227734 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 49 506948616 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 50 659060556 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 51 883997877 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 52 958139571 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 53 1322822218 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 54 1537002063 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 55 1747873779 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add x add get w 56 1955562222 +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor g and e xor f g +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor b and xor a b xor b c +d = add d t +x = add t u +t = add g add get w 57 2024104815 +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor f and d xor e f +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor a and xor x a xor a b +c = add c t +g = add t u +t = add f add get w 58 2227730452 +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor e and c xor d e +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor x and xor g x xor x a +b = add b t +f = add t u +t = add e add get w 59 2361852424 +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor d and b xor c d +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor g and xor f g xor g x +a = add a t +e = add t u +t = add d add get w 60 2428436474 +u = shl a 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor c and a xor b c +u = shl e 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor f and xor e f xor f g +x = add x t +d = add t u +t = add c add get w 61 2756734187 +u = shl x 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor b and x xor a b +u = shl d 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor e and xor d e xor e f +g = add g t +c = add t u +t = add b add get w 62 3204031479 +u = shl g 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor a and g xor x a +u = shl c 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor d and xor c d xor d e +f = add f t +b = add t u +t = add a add get w 63 3329325298 +u = shl f 32 +u = or u shr u 32 +u = xor xor shr u 6 shr u 11 shr u 25 +t = add t u +t = add t xor x and f xor g x +u = shl b 32 +u = or u shr u 32 +u = xor xor shr u 2 shr u 13 shr u 22 +u = add u xor c and xor b c xor c d +e = add e t +a = add t u +t = add ulong get h 0 a +put h 0 uint and t 4294967295 +t = add ulong get h 1 b +put h 1 uint and t 4294967295 +t = add ulong get h 2 c +put h 2 uint and t 4294967295 +t = add ulong get h 3 d +put h 3 uint and t 4294967295 +t = add ulong get h 4 e +put h 4 uint and t 4294967295 +t = add ulong get h 5 f +put h 5 uint and t 4294967295 +t = add ulong get h 6 g +put h 6 uint and t 4294967295 +t = add ulong get h 7 x +put h 7 uint and t 4294967295 +end + +fn tobuf c edit uchar 104 bi ulong b view uchar 4096 bo ulong n ulong to void +for i ulong n +put c add 40 add bi i get b add bo i +end +end + +note absorb b[0..n), n <= 4096, exactly as src/sha256.c sha256_update does for that piece +fn absorb c edit uchar 104 b view uchar 4096 n ulong w edit ulong 64 to void +if gt n 4096 +trap 3 +end +h = new uint 8 0 +cnt = getle c 32 8 +bi = and cnt 63 +putle c 32 8 add cnt n +off = ulong 0 +if gt bi 0 +lf = sub 64 bi +if lt n lf +tobuf c bi b 0 n +ret +end +tobuf c bi b 0 lf +loadh c h +schedc c w +rounds h w +storeh c h +off = lf +end +if le add off 64 n +loadh c h +while le add off 64 n bound 65 +schedb b off w +rounds h w +off = add off 64 +end +storeh c h +end +tobuf c 0 b off sub n off +end + +fn init c edit uchar 104 to void +putle c 32 8 0 +putle c 0 4 1779033703 +putle c 4 4 3144134277 +putle c 8 4 1013904242 +putle c 12 4 2773480762 +putle c 16 4 1359893119 +putle c 20 4 2600822924 +putle c 24 4 528734635 +putle c 28 4 1541459225 +end + +note compress the context's own block buffer (bytes 40..103) into its hash words +fn compbuf c edit uchar 104 w edit ulong 64 to void +h = new uint 8 0 +loadh c h +schedc c w +rounds h w +storeh c h +end + +note zero buffer bytes s..e-1 (indices into the 64-byte block) +fn zbuf c edit uchar 104 s ulong e ulong to void +i = s +while lt i e bound 64 +put c add 40 i 0 +i = add i 1 +end +end + +note sha256_final of src/sha256.c, padding in place: 0x80, zeros to 56 mod 64 (one more block +note when the pending part is 56 bytes or more), the 64-bit big-endian bit count, then the digest. +note The count field is left as it was (src/sha256.c adds the padding to it; nothing reads it after). +fn final c edit uchar 104 w edit ulong 64 to void +cnt = getle c 32 8 +bi = and cnt 63 +put c add 40 bi 128 +zbuf c add bi 1 64 +if ge bi 56 +compbuf c w +zbuf c 0 56 +end +bits = mul cnt 8 +for i ulong 8 +sh = mul sub 7 i 8 +put c add 96 i uchar and shr bits sh 255 +end +compbuf c w +h = new uint 8 0 +loadh c h +digest8 h w +end + +note IV into h +fn initiv h edit uint 8 to void +put h 0 uint 1779033703 +put h 1 uint 3144134277 +put h 2 uint 1013904242 +put h 3 uint 2773480762 +put h 4 uint 1359893119 +put h 5 uint 2600822924 +put h 6 uint 528734635 +put h 7 uint 1541459225 +end + +note expand w[0..16) into the full 64-word schedule, unrolled (constant indices need no bounds checks). +note Every schedule word is stored duplicated, the 32-bit value in both halves (d = (x << 32) | x), so +note a rotate right by r is the low half of d >> r and a plain shift right by n is d >> (32 + n); sums +note are formed in the low 32 bits (add, and, xor, or need no masks) and duplicated once on store. +fn expand w edit ulong 64 to void +u = ulong 0 +v = ulong 0 +x = ulong 0 +x = get w 14 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 1 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 9 +u = add u get w 0 +u = shl u 32 +put w 16 or u shr u 32 +x = get w 15 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 2 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 10 +u = add u get w 1 +u = shl u 32 +put w 17 or u shr u 32 +x = get w 16 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 3 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 11 +u = add u get w 2 +u = shl u 32 +put w 18 or u shr u 32 +x = get w 17 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 4 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 12 +u = add u get w 3 +u = shl u 32 +put w 19 or u shr u 32 +x = get w 18 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 5 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 13 +u = add u get w 4 +u = shl u 32 +put w 20 or u shr u 32 +x = get w 19 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 6 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 14 +u = add u get w 5 +u = shl u 32 +put w 21 or u shr u 32 +x = get w 20 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 7 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 15 +u = add u get w 6 +u = shl u 32 +put w 22 or u shr u 32 +x = get w 21 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 8 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 16 +u = add u get w 7 +u = shl u 32 +put w 23 or u shr u 32 +x = get w 22 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 9 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 17 +u = add u get w 8 +u = shl u 32 +put w 24 or u shr u 32 +x = get w 23 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 10 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 18 +u = add u get w 9 +u = shl u 32 +put w 25 or u shr u 32 +x = get w 24 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 11 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 19 +u = add u get w 10 +u = shl u 32 +put w 26 or u shr u 32 +x = get w 25 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 12 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 20 +u = add u get w 11 +u = shl u 32 +put w 27 or u shr u 32 +x = get w 26 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 13 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 21 +u = add u get w 12 +u = shl u 32 +put w 28 or u shr u 32 +x = get w 27 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 14 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 22 +u = add u get w 13 +u = shl u 32 +put w 29 or u shr u 32 +x = get w 28 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 15 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 23 +u = add u get w 14 +u = shl u 32 +put w 30 or u shr u 32 +x = get w 29 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 16 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 24 +u = add u get w 15 +u = shl u 32 +put w 31 or u shr u 32 +x = get w 30 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 17 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 25 +u = add u get w 16 +u = shl u 32 +put w 32 or u shr u 32 +x = get w 31 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 18 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 26 +u = add u get w 17 +u = shl u 32 +put w 33 or u shr u 32 +x = get w 32 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 19 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 27 +u = add u get w 18 +u = shl u 32 +put w 34 or u shr u 32 +x = get w 33 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 20 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 28 +u = add u get w 19 +u = shl u 32 +put w 35 or u shr u 32 +x = get w 34 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 21 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 29 +u = add u get w 20 +u = shl u 32 +put w 36 or u shr u 32 +x = get w 35 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 22 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 30 +u = add u get w 21 +u = shl u 32 +put w 37 or u shr u 32 +x = get w 36 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 23 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 31 +u = add u get w 22 +u = shl u 32 +put w 38 or u shr u 32 +x = get w 37 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 24 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 32 +u = add u get w 23 +u = shl u 32 +put w 39 or u shr u 32 +x = get w 38 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 25 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 33 +u = add u get w 24 +u = shl u 32 +put w 40 or u shr u 32 +x = get w 39 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 26 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 34 +u = add u get w 25 +u = shl u 32 +put w 41 or u shr u 32 +x = get w 40 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 27 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 35 +u = add u get w 26 +u = shl u 32 +put w 42 or u shr u 32 +x = get w 41 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 28 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 36 +u = add u get w 27 +u = shl u 32 +put w 43 or u shr u 32 +x = get w 42 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 29 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 37 +u = add u get w 28 +u = shl u 32 +put w 44 or u shr u 32 +x = get w 43 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 30 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 38 +u = add u get w 29 +u = shl u 32 +put w 45 or u shr u 32 +x = get w 44 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 31 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 39 +u = add u get w 30 +u = shl u 32 +put w 46 or u shr u 32 +x = get w 45 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 32 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 40 +u = add u get w 31 +u = shl u 32 +put w 47 or u shr u 32 +x = get w 46 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 33 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 41 +u = add u get w 32 +u = shl u 32 +put w 48 or u shr u 32 +x = get w 47 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 34 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 42 +u = add u get w 33 +u = shl u 32 +put w 49 or u shr u 32 +x = get w 48 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 35 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 43 +u = add u get w 34 +u = shl u 32 +put w 50 or u shr u 32 +x = get w 49 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 36 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 44 +u = add u get w 35 +u = shl u 32 +put w 51 or u shr u 32 +x = get w 50 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 37 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 45 +u = add u get w 36 +u = shl u 32 +put w 52 or u shr u 32 +x = get w 51 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 38 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 46 +u = add u get w 37 +u = shl u 32 +put w 53 or u shr u 32 +x = get w 52 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 39 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 47 +u = add u get w 38 +u = shl u 32 +put w 54 or u shr u 32 +x = get w 53 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 40 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 48 +u = add u get w 39 +u = shl u 32 +put w 55 or u shr u 32 +x = get w 54 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 41 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 49 +u = add u get w 40 +u = shl u 32 +put w 56 or u shr u 32 +x = get w 55 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 42 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 50 +u = add u get w 41 +u = shl u 32 +put w 57 or u shr u 32 +x = get w 56 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 43 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 51 +u = add u get w 42 +u = shl u 32 +put w 58 or u shr u 32 +x = get w 57 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 44 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 52 +u = add u get w 43 +u = shl u 32 +put w 59 or u shr u 32 +x = get w 58 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 45 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 53 +u = add u get w 44 +u = shl u 32 +put w 60 or u shr u 32 +x = get w 59 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 46 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 54 +u = add u get w 45 +u = shl u 32 +put w 61 or u shr u 32 +x = get w 60 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 47 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 55 +u = add u get w 46 +u = shl u 32 +put w 62 or u shr u 32 +x = get w 61 +u = xor xor shr x 17 shr x 19 shr x 42 +x = get w 48 +v = xor xor shr x 7 shr x 18 shr x 35 +u = add u v +u = add u get w 56 +u = add u get w 47 +u = shl u 32 +put w 63 or u shr u 32 +end + +note Word access for the aligned paths. b8 is the caller's 8-byte-aligned copy of the input lent a +note second time as ulong cells (b views the same bytes), so one load brings 8 bytes. Swapping the +note bytes inside each 32-bit half (two mask-and-shift steps; the constants stay in registers) turns +note little-endian bytes b0..b7 into the big-endian words b0..b3 (low half) and b4..b7 (high half). +note Each word is stored duplicated, as expand requires. +fn blk8 b8 view ulong 512 q ulong w edit ulong 64 to void +for i ulong 8 +x = get b8 add q i +y = or shl and x 71777214294589695 8 and shr x 8 71777214294589695 +y = or shl and y 281470681808895 16 and shr y 16 281470681808895 +j = mul i 2 +t = shl y 32 +put w j or t shr t 32 +put w add j 1 or and y 18446744069414584320 shr y 32 +end +end + +note the padded tail b[off..n) (less than 64 bytes), read as words through b8 (the bytes past n +note are inside the caller's 4096-byte copy and are masked off here): the word holding byte n keeps +note its k valid bytes and gets 0x80 after them, later words are zero, the 64-bit bit count goes in +note words 14 and 15; a second block when the tail is 56 bytes or more +fn tail8 h edit uint 8 b8 view ulong 512 off ulong n ulong w edit ulong 64 to void +blk8 b8 shr off 3 w +r = sub n off +fw = shr r 2 +k = and r 3 +sh = mul k 8 +v = get w fw +v = and v shl 4294967295 sub 32 sh +v = and v 4294967295 +v = or v shl 128 sub 24 sh +put w fw or v shl v 32 +i = add fw 1 +while lt i 16 bound 16 +put w i 0 +i = add i 1 +end +if ge r 56 +expand w +rounds h w +for j ulong 14 +put w j 0 +end +end +bits = mul n 8 +v = shr bits 32 +put w 14 or v shl v 32 +v = and bits 4294967295 +put w 15 or v shl v 32 +expand w +rounds h w +end + +note the digest of h as 32 bytes packed little endian into w[0..4): word i holds the big-endian +note bytes of h[2i] then h[2i+1], so the caller copies the digest out of w with memcpy +fn digest8 h edit uint 8 w edit ulong 64 to void +for i ulong 4 +j = mul i 2 +x = or ulong get h j shl ulong get h add j 1 32 +y = or shl and x 71777214294589695 8 and shr x 8 71777214294589695 +y = or shl and y 281470681808895 16 and shr y 16 281470681808895 +put w i y +end +end + +note SHA-256 of the n < 4096 bytes in b8, state in locals only (sha256_hash's short path) +fn oneshot b8 view ulong 512 n ulong w edit ulong 64 to void +if ge n 4096 +trap 3 +end +h = new uint 8 0 +initiv h +off = ulong 0 +while le add off 64 n bound 64 +blk8 b8 shr off 3 w +expand w +rounds h w +off = add off 64 +end +tail8 h b8 off n w +digest8 h w +end + +fn omsha c edit uchar 104 b view uchar 4096 n ulong op ulong w edit ulong 64 b8 view ulong 512 to void +if eq op 0 +init c +ret +end +if eq op 1 +absorb c b n w +ret +end +if eq op 2 +final c w +ret +end +if eq op 3 +oneshot b8 n w +ret +end +trap 9 +end +note SPDX-License-Identifier: Apache-2.0 +note r1.om: the REUSE-1 profile R1 baseline (docs/reuse/REUSE-1.md sections 1.1-1.3 and 12). +note Concatenated after om/sha256/sha256.om. All state is in the pools below (globals cannot be passed). +note R1P the current program, 64 words R1N n (1 word) R1X the state, 4096 words +note R1R r0, r1 of the last r1full R1T FSM table, delta(s, a) at index s*16 + a +note R1PD program digest, R1SD state digest, R1HD r1hash output: SHA-256 as 4 words, word j = +note digest bytes 8j..8j+7 read big endian +note R1HB the hash input buffer (r1hash hashes R1HB[0..n), n <= 65536) +note Exported beyond contract 12 (r1 prefix): +note r1step op c v m one lane op (1 ADD .. 8 SHR) on v with mask m = 2^wl - 1 +note r1chg op a b n validity of one change (op 3..6, payload a, b; DEL needs b = 0) against +note the current program's domain and a state of n values: the new n, or -1 +note r1apply op a b apply one change already validated by r1chg to R1X and R1N; returns +note the old value at a (SET, DEL), the old value at b (SWAP), 0 (INS) +note r1alldom every x_i (i < n) inside the current program's domain +note r1bswap x the byte reversal of x +note r1putw off w w as 8 little-endian bytes into R1HB[off..off+8) +note A call outside a valid program (unknown op or fold) traps 10; R1 never reaches it on valid input. +global R1P ulong 64 +global R1N ulong 1 +global R1X ulong 4096 +global R1R ulong 2 +global R1T ulong 256 +global R1PD ulong 4 +global R1SD ulong 4 +global R1HB uchar 65536 +global R1HD ulong 4 + +note AGG words 2..63 (word 0 and 1 are checked by r1valid) +fn r1vagg to int +wl = get R1P 2 +if and ne wl 8 ne wl 16 +ret 2 +end +f = get R1P 3 +if or lt f 1 gt f 6 +ret 2 +end +k = get R1P 4 +if or lt k 1 gt k 6 +ret 2 +end +lim = shl ulong 1 wl +for j ulong 6 +op = get R1P add 5 mul j 2 +c = get R1P add 6 mul j 2 +if lt j k +if or lt op 1 gt op 8 +ret 2 +end +if ge c lim +ret 2 +end +if and ge op 7 ge c wl +ret 2 +end +else +if or ne op 0 ne c 0 +ret 2 +end +end +end +for i ulong 47 +if ne get R1P add i 17 0 +ret 2 +end +end +ret 0 +end + +note one FSM rule word for S = ns states and A = na symbols: 0 valid, 2 invalid +fn r1vrule w ulong ns ulong na ulong to int +s = and w 255 +lo = and shr w 8 255 +hi = and shr w 16 255 +t = shr w 24 +if or ge s ns ge t ns +ret 2 +end +if or gt lo hi ge hi na +ret 2 +end +ret 0 +end + +note FSM words 2..63 +fn r1vfsm to int +ns = get R1P 2 +if or lt ns 2 gt ns 16 +ret 2 +end +na = get R1P 3 +if or lt na 2 gt na 16 +ret 2 +end +if ge get R1P 4 ns +ret 2 +end +nr = get R1P 5 +if or lt nr 1 gt nr 24 +ret 2 +end +for i ulong 16 +v = get R1P add 6 i +if lt i ns +if ge v 256 +ret 2 +end +else +if ne v 0 +ret 2 +end +end +end +for i ulong 24 +w = get R1P add 22 i +if lt i nr +if ne r1vrule w ns na 0 +ret 2 +end +else +if ne w 0 +ret 2 +end +end +end +for i ulong 18 +if ne get R1P add i 46 0 +ret 2 +end +end +ret 0 +end + +note 0 when R1P is a valid R1 program (1.1), else 2 +fn r1valid to int +if ne get R1P 0 3544411312749556050 +ret 2 +end +fam = get R1P 1 +if eq fam 1 +ret r1vagg +end +if eq fam 2 +ret r1vfsm +end +ret 2 +end + +note v inside the current (valid) program's domain: AGG v < 2^wl, FSM v < A +fn r1indom v ulong to bool +if eq get R1P 1 1 +ret lt v shl ulong 1 get R1P 2 +end +ret lt v get R1P 3 +end + +fn r1step op ulong c ulong v ulong m ulong to ulong +if eq op 1 +ret and add v c m +end +if eq op 2 +ret and sub v c m +end +if eq op 3 +ret and mul v c m +end +if eq op 4 +ret and v c +end +if eq op 5 +ret or v c +end +if eq op 6 +ret xor v c +end +if eq op 7 +ret and shl v c m +end +if eq op 8 +ret shr v c +end +trap 10 +end + +note the lane map e(x) of the current AGG program (1.2) +fn r1lane x ulong to ulong +m = sub shl ulong 1 get R1P 2 1 +k = get R1P 4 +v = x +for j ulong k +q = add 5 mul j 2 +v = r1step get R1P q get R1P add q 1 v m +end +ret v +end + +fn r1ident f ulong to ulong +if or eq f 1 eq f 2 +ret 0 +end +if eq f 3 +ret 18446744073709551615 +end +if or eq f 4 eq f 5 +ret 0 +end +if eq f 6 +ret 18446744073709551615 +end +trap 10 +end + +fn r1fold f ulong a ulong b ulong to ulong +if eq f 1 +ret add a b +end +if eq f 2 +ret xor a b +end +if eq f 3 +if lt b a +ret b +end +ret a +end +if eq f 4 +if gt b a +ret b +end +ret a +end +if eq f 5 +ret or a b +end +if eq f 6 +ret and a b +end +trap 10 +end + +note delta(s, a) of the current FSM program: the target of the lowest-numbered matching rule, else s +fn r1delta s ulong a ulong to ulong +nr = get R1P 5 +for i ulong nr +w = get R1P add 22 i +if eq and w 255 s +lo = and shr w 8 255 +hi = and shr w 16 255 +if and le lo a le a hi +ret shr w 24 +end +end +end +ret s +end + +fn r1fagg to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc r1lane get R1X i +end +put R1R 0 acc +put R1R 1 n +end + +fn r1ffsm to void +n = get R1N 0 +q = get R1P 4 +sum = ulong 0 +for i ulong n +q = r1delta q get R1X i +sum = add sum get R1P add 6 q +end +put R1R 0 q +put R1R 1 sum +end + +note the baseline: R1P, R1N, R1X -> R1R (1.2) +fn r1full to void +if eq get R1P 1 1 +r1fagg +ret +end +r1ffsm +end + +note shape id (1.1): AGG wl + 256 f + 4096 k + 65536 sum op_j 16^(j-1); FSM 1 +fn r1shape to ulong +if eq get R1P 1 2 +ret 1 +end +k = get R1P 4 +s = add get R1P 2 add mul get R1P 3 256 mul k 4096 +m = ulong 65536 +for j ulong k +s = add s mul m get R1P add 5 mul j 2 +m = mul m 16 +end +ret s +end + +fn r1bswap x ulong to ulong +y = ulong 0 +v = x +for i ulong 8 +y = or shl y 8 and v 255 +v = shr v 8 +end +ret y +end + +fn r1putw off ulong w ulong to void +v = w +for i ulong 8 +put R1HB add off i uchar and v 255 +v = shr v 8 +end +end + +note SHA-256 of R1HB[0..n) into R1HD, through omsha in local 4096-byte chunks +fn r1hash n ulong to void +if gt n 65536 +trap 3 +end +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +off = ulong 0 +while lt off n bound 17 +m = sub n off +if gt m 4096 +m = 4096 +end +for i ulong m +put b i get R1HB add off i +end +omsha c b m 1 w b8 +off = add off m +end +omsha c b 0 2 w b8 +for j ulong 4 +put R1HD j r1bswap get w j +end +end + +note program digest: SHA-256 of the 512 bytes of R1P (64 LE words) into R1PD +fn r1pdig to void +for i ulong 64 +r1putw mul i 8 get R1P i +end +r1hash 512 +for j ulong 4 +put R1PD j get R1HD j +end +end + +note state digest: SHA-256 of the n values as LE words into R1SD +fn r1sdig to void +n = get R1N 0 +for i ulong n +r1putw mul i 8 get R1X i +end +r1hash mul n 8 +for j ulong 4 +put R1SD j get R1HD j +end +end + +fn r1alldom to bool +n = get R1N 0 +for i ulong n +if not r1indom get R1X i +ret false +end +end +ret true +end + +note one change (1.3) against a state of n values: the new n, or -1 when invalid +fn r1chg op ulong a ulong b ulong n ulong to long +if eq op 3 +if and lt a n r1indom b +ret long n +end +ret -1 +end +if eq op 4 +if and and le a n lt n 4096 r1indom b +ret long add n 1 +end +ret -1 +end +if eq op 5 +if and lt a n eq b 0 +ret long sub n 1 +end +ret -1 +end +if eq op 6 +if and lt a n lt b n +ret long n +end +ret -1 +end +ret -1 +end + +fn r1apply op ulong a ulong b ulong to ulong +n = get R1N 0 +if eq op 3 +old = get R1X a +put R1X a b +ret old +end +if eq op 4 +t = n +while gt t a bound 4097 +put R1X t get R1X sub t 1 +t = sub t 1 +end +put R1X a b +put R1N 0 add n 1 +ret 0 +end +if eq op 5 +old = get R1X a +t = a +while lt add t 1 n bound 4097 +put R1X t get R1X add t 1 +t = add t 1 +end +put R1N 0 sub n 1 +ret old +end +if eq op 6 +old = get R1X b +put R1X b get R1X a +put R1X a old +ret old +end +trap 10 +end +note SPDX-License-Identifier: Apache-2.0 +note exp.om: R1 experience records (docs/reuse/REUSE-1.md section 3.1): the tap writer and admission. +note Concatenated after r1.om. Exported: +note EXAPATH uchar 4096, EXALEN ulong 1 the ALLOW path (caller fills; no 0 byte needed) +note exaload to int read ALLOW (at most 1 MiB) into the allow table and its SHA-256 into EXAD: +note 0 ok; 1 unreadable, 2 too large, 3 malformed line, 4 more than 8192 lines. +note Lines: `<64 hex> AUTHORIZED|SYNTHETIC|REVOKED <label>`, label nonempty, one +note space between fields, lowercase or uppercase hex; empty lines are skipped. +note exallow d view ulong 4 to int digest (word j = bytes 8j..8j+7 big endian) against the loaded +note table: 1 allowed (an AUTHORIZED or SYNTHETIC line and no REVOKED line), +note 0 absent, -1 revoked +note exastat to bool stat of ALLOW (inode, size, mtime ns, ctime ns) into EXAS; true when it +note differs from the snapshot taken by the previous call (or on the first call) +note EXFPATH uchar 4096, EXFLEN ulong 1 the experience file path +note exadmit to int admission of that file against the loaded ALLOW: 0 admitted; on 0 the file +note is in EXFW (LE words, record r = EXFW[128r .. 128r+128)), EXFN = records, +note EXFD = the file's SHA-256. Codes: 1 unreadable, 2 size, 3 not in ALLOW, +note 4 revoked, 5 ALLOW not loaded, then the first failing record check: 6 magic, +note 7 version, 8 sequence, 9 family, 10 PROG invalid, 11 program digest, +note 12 split, 13 provenance, 14 m, 15 a zero word, 16 AGG sample, 17 FSM sample. +note EXFBAD = the 0-based index of the failing record. Clobbers R1P, R1PD, R1HB, +note R1HD. +note extopen to int the tap: argv 5 TAPFILE is created (O_EXCL; an existing file is refused), +note argv 6 SPLIT must be 1, 2 or 3: 0 ok, 1 split, 2 file exists or not creatable +note extap ses ulong qn ulong nchg ulong ns ulong to int one record for the current R1P, R1N, R1X +note and R1R (the baseline result, ns its evaluation time): sets R1PD and R1SD; +note 0 ok, 1 write failed. Sequence numbers start at 1. +global EXAPATH uchar 4096 +global EXALEN ulong 1 +global EXAB uchar 1048576 +global EXAT ulong 40960 +global EXAN ulong 1 +global EXAOK ulong 1 +global EXAD ulong 4 +global EXAS ulong 5 +global EXSB uchar 128 +global EXFPATH uchar 4096 +global EXFLEN ulong 1 +global EXFW ulong 4194304 +global EXFN ulong 1 +global EXFD ulong 4 +global EXFBAD ulong 1 +global EXTFD ulong 1 +global EXTSEQ ulong 1 +global EXTSPL ulong 1 +global EXTR ulong 128 +global EXTB uchar 1024 +global EXTP uchar 4096 + +note a hex digit's value, or 16 when it is not one +fn exhex c ulong to ulong +if and ge c 48 le c 57 +ret sub c 48 +end +if and ge c 97 le c 102 +ret sub c 87 +end +if and ge c 65 le c 70 +ret sub c 55 +end +ret 16 +end + +note keyword at EXAB[p..): 1 AUTHORIZED, 2 SYNTHETIC, 3 REVOKED, 0 none; the word must be followed +note by a space (checked by the caller through its length) +fn exkw p ulong e ulong to ulong +kw = new uchar 10 0 +r = ulong 1 +while le r 3 bound 4 +if eq r 1 +put kw 0 65 +put kw 1 85 +put kw 2 84 +put kw 3 72 +put kw 4 79 +put kw 5 82 +put kw 6 73 +put kw 7 90 +put kw 8 69 +put kw 9 68 +end +if eq r 2 +put kw 0 83 +put kw 1 89 +put kw 2 78 +put kw 3 84 +put kw 4 72 +put kw 5 69 +put kw 6 84 +put kw 7 73 +put kw 8 67 +end +if eq r 3 +put kw 0 82 +put kw 1 69 +put kw 2 86 +put kw 3 79 +put kw 4 75 +put kw 5 69 +put kw 6 68 +end +n = exkwlen r +ok = lt add p n e +for i ulong n +if ok +if ne get EXAB add p i get kw i +ok = false +end +end +end +if ok +if eq get EXAB add p n 32 +ret r +end +end +r = add r 1 +end +ret 0 +end + +fn exkwlen r ulong to ulong +if eq r 1 +ret 10 +end +if eq r 2 +ret 9 +end +ret 7 +end + +note one ALLOW line EXAB[s..e): 0 ok (entry appended), 3 malformed, 4 table full +fn exaline s ulong e ulong to int +if lt sub e s 68 +ret 3 +end +n = get EXAN 0 +if ge n 8192 +ret 4 +end +b = mul n 5 +for j ulong 4 +v = ulong 0 +for i ulong 16 +h = exhex ulong get EXAB add s add mul j 16 i +if gt h 15 +ret 3 +end +v = add mul v 16 h +end +put EXAT add b j v +end +if ne get EXAB add s 64 32 +ret 3 +end +k = exkw add s 65 e +if eq k 0 +ret 3 +end +if le e add add s 66 exkwlen k +ret 3 +end +put EXAT add b 4 k +put EXAN 0 add n 1 +ret 0 +end + +note SHA-256 of EXAB[0..n) into EXAD +fn exahash n ulong to void +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +off = ulong 0 +while lt off n bound 257 +m = sub n off +if gt m 4096 +m = 4096 +end +for i ulong m +put b i get EXAB add off i +end +omsha c b m 1 w b8 +off = add off m +end +omsha c b 0 2 w b8 +for j ulong 4 +put EXAD j r1bswap get w j +end +end + +fn exaload to int +put EXAOK 0 0 +put EXAN 0 0 +fd = fopen EXAPATH 0 get EXALEN 0 0 0 +if lt fd 0 +ret 1 +end +got = ulong 0 +while lt got 1048576 bound 100000 +k = read int fd EXAB got sub 1048576 got +if le k 0 +break +end +got = add got ulong k +end +one = new uchar 1 0 +k = read int fd one 0 1 +r = fclose int fd +if or ne k 0 ne r 0 +ret 2 +end +exahash got +s = ulong 0 +for i ulong got +if eq get EXAB i 10 +if gt i s +x = exaline s i +if ne x 0 +ret x +end +end +s = add i 1 +end +end +if lt s got +x = exaline s got +if ne x 0 +ret x +end +end +put EXAOK 0 1 +ret 0 +end + +fn exallow d view ulong 4 to int +n = get EXAN 0 +seen = int 0 +for e ulong n +b = mul e 5 +hit = true +for j ulong 4 +if ne get EXAT add b j get d j +hit = false +end +end +if hit +if eq get EXAT add b 4 3 +ret -1 +end +seen = 1 +end +end +ret seen +end + +note the 8-byte LE word at byte p of EXSB +fn exsw p ulong to ulong +v = ulong 0 +for i ulong 8 +v = or v shl ulong get EXSB add p i mul i 8 +end +ret v +end + +fn exastat to bool +for i ulong 128 +put EXSB i 0 +end +r = stat EXAPATH 0 get EXALEN 0 EXSB 0 128 +s = new ulong 5 0 +put s 0 exsw 8 +put s 1 exsw 48 +put s 2 add mul exsw 88 1000000000 exsw 96 +put s 3 add mul exsw 104 1000000000 exsw 112 +if eq r 0 +put s 4 1 +else +put s 4 2 +end +ch = false +for j ulong 5 +if ne get s j get EXAS j +ch = true +end +put EXAS j get s j +end +ret ch +end + +note the experience file into EXFW and its SHA-256 into EXFD: the byte count, or -1 unreadable, +note -2 larger than 32 MiB +fn exfread to long +fd = fopen EXFPATH 0 get EXFLEN 0 0 0 +if lt fd 0 +ret -1 +end +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +tot = ulong 0 +eof = false +while not eof bound 8194 +fill = ulong 0 +while lt fill 4096 bound 4097 +k = read int fd b fill sub 4096 fill +if le k 0 +eof = true +break +end +fill = add fill ulong k +end +if gt add tot fill 33554432 +r = fclose int fd +ret -2 +end +omsha c b fill 1 w b8 +exfwords b tot fill +tot = add tot fill +end +r = fclose int fd +omsha c b 0 2 w b8 +for j ulong 4 +put EXFD j r1bswap get w j +end +ret long tot +end + +note bytes b[0..fill) at file offset tot into EXFW (whole words only) +fn exfwords b view uchar 4096 tot ulong fill ulong to void +nw = shr fill 3 +base = shr tot 3 +for i ulong nw +v = ulong 0 +p = add mul i 8 7 +for k ulong 8 +v = or shl v 8 ulong get b sub p k +end +put EXFW add base i v +end +end + +note AGG samples of record base: m pairs (x, e) with x < 2^wl and e = r1lane x; R1P holds its PROG +fn exsagg base ulong m ulong to bool +lim = shl ulong 1 get R1P 2 +for i ulong m +x = get EXFW add base add 16 mul i 2 +e = get EXFW add base add 17 mul i 2 +if ge x lim +ret false +end +if ne r1lane x e +ret false +end +end +ret true +end + +note FSM samples: m words q + 256 a + 65536 q' with q < S, a < A, q' = delta(q, a) +fn exsfsm base ulong m ulong to bool +for i ulong m +w = get EXFW add base add 16 i +q = and w 255 +a = and shr w 8 255 +if or ge q get R1P 2 ge a get R1P 3 +ret false +end +if ne shr w 16 r1delta q a +ret false +end +end +ret true +end + +note words that must be 0: 15, 56..63, and the sample words past the m samples +fn exzero base ulong fam ulong m ulong to bool +if ne get EXFW add base 15 0 +ret false +end +for i ulong 8 +if ne get EXFW add base add 56 i 0 +ret false +end +end +used = m +if eq fam 1 +used = mul m 2 +end +for i ulong 32 +if ge i used +if ne get EXFW add base add 16 i 0 +ret false +end +end +end +ret true +end + +note record r (0-based), prev = the previous sequence number: 0 or a code 6..17 +fn exrec r ulong prev ulong to int +base = mul r 128 +if ne get EXFW base 3544386028377026898 +ret 6 +end +if ne get EXFW add base 1 1 +ret 7 +end +sq = get EXFW add base 2 +if or le sq prev and eq r 0 ne sq 1 +ret 8 +end +fam = get EXFW add base 3 +if or ne fam get EXFW add base 65 and ne fam 1 ne fam 2 +ret 9 +end +for i ulong 64 +put R1P i get EXFW add base add 64 i +end +if ne r1valid 0 +ret 10 +end +r1pdig +for j ulong 4 +if ne get R1PD j get EXFW add base add 52 j +ret 11 +end +end +sp = get EXFW add base 9 +if or lt sp 1 gt sp 3 +ret 12 +end +if ne get EXFW add base 13 1 +ret 13 +end +m = get EXFW add base 14 +if gt m 16 +ret 14 +end +if not exzero base fam m +ret 15 +end +if eq fam 1 +if not exsagg base m +ret 16 +end +else +if not exsfsm base m +ret 17 +end +end +ret 0 +end + +fn exadmit to int +put EXFN 0 0 +put EXFBAD 0 0 +if ne get EXAOK 0 1 +ret 5 +end +tot = exfread +if eq tot -1 +ret 1 +end +if or lt tot 0 or eq tot 0 ne rem tot 1024 0 +ret 2 +end +a = new ulong 4 0 +for j ulong 4 +put a j get EXFD j +end +al = exallow a +if eq al -1 +ret 4 +end +if ne al 1 +ret 3 +end +nr = shr ulong tot 10 +prev = ulong 0 +for r ulong nr +put EXFBAD 0 r +x = exrec r prev +if ne x 0 +ret x +end +prev = get EXFW add mul r 128 2 +end +put EXFN 0 nr +ret 0 +end + +note argv i into EXTP: the length, or -1 +fn exarg i ulong to long +n = argread i EXTP 0 4096 +if or lt n 1 gt n 4095 +ret -1 +end +ret n +end + +fn extopen to int +put EXTSEQ 0 0 +n = exarg 6 +if ne n 1 +ret 1 +end +sp = sub ulong get EXTP 0 48 +if or lt sp 1 gt sp 3 +ret 1 +end +put EXTSPL 0 sp +n = exarg 5 +if lt n 0 +ret 2 +end +fd = fopen EXTP 0 ulong n 193 384 +if lt fd 0 +ret 2 +end +put EXTFD 0 ulong fd +ret 0 +end + +note rules tested by delta(q, a): the index of the first matching rule plus 1, else R +fn exscan q ulong a ulong to ulong +nr = get R1P 5 +for i ulong nr +w = get R1P add 22 i +if eq and w 255 q +lo = and shr w 8 255 +hi = and shr w 16 255 +if and le lo a le a hi +ret add i 1 +end +end +end +ret nr +end + +note FSM rule tests of the whole run (word 8) +fn exsteps to ulong +n = get R1N 0 +q = get R1P 4 +st = ulong 0 +for t ulong n +a = get R1X t +st = add st exscan q a +q = r1delta q a +end +ret st +end + +note the first m (<= 16) distinct lane values in index order, (x, e(x)) into EXTR[16..48) +fn exsmagg to ulong +n = get R1N 0 +m = ulong 0 +for i ulong n +if lt m 16 +x = get R1X i +seen = false +for j ulong m +if eq get EXTR add 16 mul j 2 x +seen = true +end +end +if not seen +put EXTR add 16 mul m 2 x +put EXTR add 17 mul m 2 r1lane x +m = add m 1 +end +end +end +ret m +end + +note the first m distinct transitions q + 256 a + 65536 q' taken +fn exsmfsm to ulong +n = get R1N 0 +m = ulong 0 +q = get R1P 4 +for i ulong n +a = get R1X i +nq = r1delta q a +if lt m 16 +w = add add q mul a 256 mul nq 65536 +seen = false +for j ulong m +if eq get EXTR add 16 j w +seen = true +end +end +if not seen +put EXTR add 16 m w +m = add m 1 +end +end +q = nq +end +ret m +end + +fn extap ses ulong qn ulong nchg ulong ns ulong to int +for i ulong 128 +put EXTR i 0 +end +sq = add get EXTSEQ 0 1 +put EXTSEQ 0 sq +put EXTR 0 3544386028377026898 +put EXTR 1 1 +put EXTR 2 sq +fam = get R1P 1 +put EXTR 3 fam +put EXTR 4 get R1N 0 +put EXTR 5 get R1R 0 +put EXTR 6 get R1R 1 +put EXTR 7 ns +if eq fam 1 +put EXTR 8 mul get R1N 0 get R1P 4 +put EXTR 14 exsmagg +else +put EXTR 8 exsteps +put EXTR 14 exsmfsm +end +put EXTR 9 get EXTSPL 0 +put EXTR 10 ses +put EXTR 11 qn +put EXTR 12 nchg +put EXTR 13 1 +r1sdig +r1pdig +for j ulong 4 +put EXTR add 48 j get R1SD j +put EXTR add 52 j get R1PD j +end +for i ulong 64 +put EXTR add 64 i get R1P i +end +ret exflush +end + +fn exflush to int +for i ulong 128 +v = get EXTR i +for k ulong 8 +put EXTB add mul i 8 k uchar and v 255 +v = shr v 8 +end +end +fd = int get EXTFD 0 +off = ulong 0 +while lt off 1024 bound 1025 +k = write fd EXTB off sub 1024 off +if le k 0 +ret 1 +end +off = add off ulong k +end +ret 0 +end +note SPDX-License-Identifier: Apache-2.0 +note check.om: G3 bind validation (docs/reuse/REUSE-1.md section 3.3). Concatenated after exp.om; +note calls mtlane of the generated methods.om (or a test's fixture). +note ckbind t 0 pass, 1 fail. AGG: every x < 2^wl of R1P has mtlane t x = r1lane x, all 64 bits. +note FSM: fills R1T[s*16 + a] = r1delta s a for s < S, a < A; nothing else is checked. +note Sets R1PD (r1pdig). A passed AGG (program digest, t) enters a FIFO of 256 entries +note (process memory only); a later bind of the same pair skips the lane comparison. +note CKST counters: 0 binds, 1 failures, 2 FIFO hits, 3 full lane comparisons, 4 lane pairs +note compared +global CKFIFO ulong 1280 +global CKFN ulong 1 +global CKFH ulong 1 +global CKST ulong 5 + +fn ckinc k ulong n ulong to void +put CKST k add get CKST k n +end + +note entry e of the FIFO holds R1PD and t +fn ckeq e ulong t ulong to bool +b = mul e 5 +for j ulong 4 +if ne get CKFIFO add b j get R1PD j +ret false +end +end +ret eq get CKFIFO add b 4 t +end + +fn ckhas t ulong to bool +n = get CKFN 0 +for e ulong n +if ckeq e t +ret true +end +end +ret false +end + +fn ckadd t ulong to void +e = get CKFH 0 +b = mul e 5 +for j ulong 4 +put CKFIFO add b j get R1PD j +end +put CKFIFO add b 4 t +put CKFH 0 rem add e 1 256 +if lt get CKFN 0 256 +put CKFN 0 add get CKFN 0 1 +end +end + +note the lane comparison over x < 2^wl: 0 equal, 1 the first difference +fn cklanes t ulong to int +lim = shl ulong 1 get R1P 2 +ckinc 3 1 +for x ulong lim +if ne mtlane t x r1lane x +ckinc 4 add x 1 +ret 1 +end +end +ckinc 4 lim +ret 0 +end + +fn ckfsm to void +ns = get R1P 2 +na = get R1P 3 +for s ulong ns +for a ulong na +put R1T add mul s 16 a r1delta s a +end +end +end + +fn ckbind t ulong to int +ckinc 0 1 +r1pdig +if eq get R1P 1 2 +ckfsm +ret 0 +end +if ckhas t +ckinc 2 1 +ret 0 +end +if ne cklanes t 0 +ckinc 1 1 +ret 1 +end +ckadd t +ret 0 +end +note SPDX-License-Identifier: Apache-2.0 +note delta.om: REUSE-1 incremental execution (#743), contract docs/reuse/REUSE-1.md v1.0 sections 4, 12. +note Unit order: sha256 r1 exp check delta select lib methods router. Names: dl / DL only. +note Reads R1P R1N R1X R1T, writes R1R (dlres only). Calls r1valid r1lane r1ident r1delta and, +note for a bound AGG method, mtlane t x; a bound FSM method reads R1T[s*16 + a] (filled by ckbind). +note +note Router protocol (agreed with CandCore): +note BEGIN after R1P R1N R1X are set: dlsreset, then dlbind t (t = template index, -1 = baseline) +note PROGRAM after R1P is replaced: dldrop, then dlbind t +note change the router validates the whole request (s1.3), then for each item in order applies +note exactly that item to R1X / R1N and calls dlchg op i v old before the next item +note SET 3 i v old = previous x_i INS 4 i v old unused (0) +note DEL 5 i 0 old = the removed value SWAP 6 i v = j, old = x_j before the swap (unused) +note AGG uses v and old (SWAP no work); FSM reads the symbols from R1X and compares new q_t with +note the stored trajectory (s4), so it needs neither. FSM SWAP = SET at min(i, j), then SET at max. +note (The other order is also correct: the stored trajectory is a valid chain under the current +note symbols everywhere except at changed positions, so every cutoff is sound; the reversed order +note passed both delta test suites when tried as a mutant.) +note QUERY dlres: 0 = R1R holds r0 r1 from the delta state, 1 = no usable state (serve full) +note dlbind returns 0 bound, 1 refused (PROG invalid or family unknown). dlbind on a session that is +note already bound with the same t is a no-op, so dldrop before a rebind is mandatory (C4 relies on it). +note dlchg returns 0 tracked, 1 delta state dropped (unbound until the next dlbind). +note +note Delta state (s4), 557,488 bytes of DL globals per process, one session (<= 1 MiB, s3.5): +note DLC 65536 words MIN/MAX count per mapped value (2^wl used) +note DLQ 4097 words FSM trajectory q_0..q_n +note DLBC 16 words OR/AND count per bit +note DLL 16 words FSM lambda copy; scalars below +note Worst cases per change: AGG ADD/XOR/OR/AND O(1) (16 bit counts); MIN/MAX one extreme scan of +note at most 2^wl - 1 counts when the last copy of the extreme goes; FSM SET at i recomputes at most +note n - i steps, INS at most n + 1 - i plus a suffix shift of at most n + 1 words, DEL at most n - i - 1 +note plus a shift of at most n words; SWAP two SETs. dlbind: one pass +note over the state plus, for MIN/MAX, clearing 2^wl counts. +note +note Counters (diagnostic, process-cumulative, never reset): dlstat k +note 0 changes tracked 1 invalidations 2 scan steps 3 FSM recompute units 4 cutoffs found +note 5 cutoff misses 6 recomputed steps 7 longest recompute 8 binds 9 drops 10 dlres served +note 11 dlres refused +note Select churn (s5.2): dlinvn = invalidations (MIN/MAX extreme scans, and every drop of live +note delta state: dldrop at PROGRAM, dlchg or dlres finding the state unusable; never twice for one), +note dlmissn = FSM recompute units that ran to n without a cutoff (at least one step recomputed). + +global DLB ulong 1 +global DLTB long 1 +global DLF ulong 1 +global DLFO ulong 1 +global DLW ulong 1 +global DLN ulong 1 +global DLA ulong 1 +global DLU ulong 1 +global DLS ulong 1 +global DLBC ulong 16 +global DLL ulong 16 +global DLC ulong 65536 +global DLQ ulong 4097 +global DLST ulong 12 + +fn dlbump k ulong v ulong to void +put DLST k add get DLST k v +end + +fn dlstat k ulong to ulong +if ge k 12 +ret 0 +end +ret get DLST k +end + +fn dlinvn to ulong +ret get DLST 1 +end + +fn dlmissn to ulong +ret get DLST 5 +end + +note mapped lane: the bound method's lane map, or the baseline's +fn dlmap x ulong to ulong +t = get DLTB 0 +if ge t 0 +ret mtlane ulong t x +end +ret r1lane x +end + +note one FSM step: the bound method's table, or the baseline delta +fn dlstep s ulong a ulong to ulong +if eq get DLU 0 1 +ret get R1T add mul s 16 a +end +ret r1delta s a +end + +fn dllam s ulong to ulong +ret get DLL s +end + +note MIN/MAX: the last copy of the extreme went; scan to the next nonzero count +fn dlscan to void +dlbump 1 1 +if eq get DLN 0 0 +put DLA 0 0 +ret +end +a = get DLA 0 +if eq get DLFO 0 3 +top = shl ulong 1 get DLW 0 +st = add a 1 +for j ulong sub top st +v = add st j +if ne get DLC v 0 +put DLA 0 v +dlbump 2 add j 1 +ret +end +end +dlbump 2 sub top st +ret +end +for j ulong a +v = sub sub a 1 j +if ne get DLC v 0 +put DLA 0 v +dlbump 2 add j 1 +ret +end +end +dlbump 2 a +end + +fn dlbitadd y ulong d ulong to void +for b ulong 16 +if ne and shr y b 1 0 +put DLBC b add get DLBC b d +end +end +end + +fn dlbitsub y ulong to void +for b ulong 16 +if ne and shr y b 1 0 +put DLBC b sub get DLBC b 1 +end +end +end + +note add one mapped lane y to the AGG delta state +fn dladd y ulong to void +f = get DLFO 0 +n = get DLN 0 +a = get DLA 0 +if eq f 1 +put DLA 0 add a y +end +if eq f 2 +put DLA 0 xor a y +end +if eq f 3 +put DLC y add get DLC y 1 +if or eq n 0 lt y a +put DLA 0 y +end +end +if eq f 4 +put DLC y add get DLC y 1 +if or eq n 0 gt y a +put DLA 0 y +end +end +if ge f 5 +dlbitadd y 1 +end +put DLN 0 add n 1 +end + +note remove one mapped lane y from the AGG delta state +fn dlrem y ulong to void +f = get DLFO 0 +a = get DLA 0 +put DLN 0 sub get DLN 0 1 +if eq f 1 +put DLA 0 sub a y +end +if eq f 2 +put DLA 0 xor a y +end +if ge f 5 +dlbitsub y +end +if or eq f 3 eq f 4 +c = sub get DLC y 1 +put DLC y c +if and eq c 0 eq y a +dlscan +end +end +end + +fn dlbagg to void +f = get R1P 3 +put DLFO 0 f +put DLW 0 get R1P 2 +put DLN 0 0 +put DLA 0 0 +for b ulong 16 +put DLBC b 0 +end +if or eq f 3 eq f 4 +top = shl ulong 1 get DLW 0 +for v ulong top +put DLC v 0 +end +end +n = get R1N 0 +for i ulong n +dladd dlmap get R1X i +end +end + +fn dlbfsm to void +for s ulong 16 +put DLL s get R1P add 6 s +end +q = get R1P 4 +put DLQ 0 q +sum = ulong 0 +n = get R1N 0 +for i ulong n +q = dlstep q get R1X i +put DLQ add i 1 q +sum = add sum dllam q +end +put DLS 0 sum +put DLN 0 n +end + +fn dlbind t long to int +if and eq get DLB 0 1 eq get DLTB 0 t +ret 0 +end +put DLB 0 0 +if ne r1valid 0 +ret 1 +end +fam = get R1P 1 +put DLF 0 fam +put DLTB 0 t +put DLU 0 0 +if eq fam 1 +dlbagg +end +if eq fam 2 +if ge t 0 +put DLU 0 1 +end +dlbfsm +end +if or eq fam 1 eq fam 2 +put DLB 0 1 +dlbump 8 1 +ret 0 +end +ret 1 +end + +note a drop of LIVE delta state is one invalidation (dlinvn); dropping an unbound state counts none +fn dldrop to void +if eq get DLB 0 1 +dlbump 1 1 +end +put DLB 0 0 +dlbump 9 1 +end + +fn dlsreset to void +put DLB 0 0 +put DLTB 0 -1 +end + +note record one FSM recompute unit of sl steps; cut 1 = a cutoff was found +fn dlfrec sl ulong cut ulong to void +dlbump 3 1 +dlbump 6 sl +if gt sl get DLST 7 +put DLST 7 sl +end +if eq cut 1 +dlbump 4 1 +end +if and eq cut 0 gt sl 0 +dlbump 5 1 +end +end + +note FSM: w_(i+1) changed (x_i); recompute from t = i+1, stop at the first equal stored q_t +fn dlfset i ulong to void +n = get DLN 0 +q = get DLQ i +sum = get DLS 0 +t = add i 1 +cut = ulong 0 +sl = ulong 0 +while le t n bound 4097 +nq = dlstep q get R1X sub t 1 +sl = add sl 1 +old = get DLQ t +if eq nq old +cut = 1 +break +end +sum = add sub sum dllam old dllam nq +put DLQ t nq +q = nq +t = add t 1 +end +put DLS 0 sum +dlfrec sl cut +end + +note FSM INS at i (R1X already holds the insertion): new q_t is compared with the stored state of the +note same symbol suffix, old q_(t-1); at the first equality the stored suffix shifts up by one +fn dlfins i ulong to void +n = get DLN 0 +q = get DLQ i +po = q +sum = get DLS 0 +t = add i 1 +cut = ulong 0 +sl = ulong 0 +while le t add n 1 bound 4098 +nq = dlstep q get R1X sub t 1 +sl = add sl 1 +if eq nq po +cut = 1 +break +end +sum = add sum dllam nq +if le t n +nx = get DLQ t +sum = sub sum dllam nx +po = nx +end +put DLQ t nq +q = nq +t = add t 1 +end +if eq cut 1 +sum = add sum dllam po +s = n +while ge s t bound 4097 +put DLQ add s 1 get DLQ s +if eq s 0 +break +end +s = sub s 1 +end +put DLQ t po +end +put DLS 0 sum +put DLN 0 add n 1 +dlfrec sl cut +end + +note FSM DEL at i (R1X already shifted down): new q_t is compared with old q_(t+1); at cutoff t (or +note t = n with no cutoff) old q_t leaves the sum and the stored suffix from t+1 shifts down by one +fn dlfdel i ulong to void +n = get DLN 0 +q = get DLQ i +sum = get DLS 0 +t = add i 1 +cut = ulong 0 +sl = ulong 0 +while lt t n bound 4097 +nq = dlstep q get R1X sub t 1 +sl = add sl 1 +if eq nq get DLQ add t 1 +cut = 1 +break +end +sum = add sub sum dllam get DLQ t dllam nq +put DLQ t nq +q = nq +t = add t 1 +end +sum = sub sum dllam get DLQ t +s = t +while lt s n bound 4097 +put DLQ s get DLQ add s 1 +s = add s 1 +end +put DLS 0 sum +put DLN 0 sub n 1 +dlfrec sl cut +end + +fn dlfchg op ulong i ulong v ulong to int +n = get DLN 0 +if eq op 3 +if ge i n +ret 1 +end +dlfset i +ret 0 +end +if eq op 4 +if gt i n +ret 1 +end +dlfins i +ret 0 +end +if eq op 5 +if ge i n +ret 1 +end +dlfdel i +ret 0 +end +if eq op 6 +if or ge i n ge v n +ret 1 +end +if lt i v +dlfset i +dlfset v +end +if gt i v +dlfset v +dlfset i +end +ret 0 +end +ret 1 +end + +fn dlachg op ulong i ulong v ulong old ulong to int +n = get DLN 0 +if eq op 3 +if ge i n +ret 1 +end +dladd dlmap v +dlrem dlmap old +ret 0 +end +if eq op 4 +if gt i n +ret 1 +end +dladd dlmap v +ret 0 +end +if eq op 5 +if ge i n +ret 1 +end +dlrem dlmap old +ret 0 +end +if eq op 6 +if or ge i n ge v n +ret 1 +end +ret 0 +end +ret 1 +end + +fn dlchg op ulong i ulong v ulong old ulong to int +if eq get DLB 0 0 +ret 1 +end +r = int 1 +if eq get DLF 0 1 +r = dlachg op i v old +end +if eq get DLF 0 2 +r = dlfchg op i v +end +if eq r 0 +if eq get DLN 0 get R1N 0 +dlbump 0 1 +ret 0 +end +end +put DLB 0 0 +dlbump 1 1 +ret 1 +end + +fn dlragg to void +f = get DLFO 0 +n = get DLN 0 +r = get DLA 0 +if or eq f 3 eq f 4 +if eq n 0 +r = r1ident f +end +end +if ge f 5 +r = r1ident f +if gt n 0 +r = 0 +for b ulong 16 +c = get DLBC b +if and eq f 5 gt c 0 +r = or r shl ulong 1 b +end +if and eq f 6 eq c n +r = or r shl ulong 1 b +end +end +end +end +put R1R 0 r +end + +fn dlres to int +if eq get DLB 0 0 +dlbump 11 1 +ret 1 +end +if ne get DLN 0 get R1N 0 +put DLB 0 0 +dlbump 1 1 +dlbump 11 1 +ret 1 +end +if eq get DLF 0 1 +dlragg +put R1R 1 get DLN 0 +end +if eq get DLF 0 2 +put R1R 0 get DLQ get DLN 0 +put R1R 1 get DLS 0 +end +dlbump 10 1 +ret 0 +end +note SPDX-License-Identifier: Apache-2.0 +note select.om: the REUSE-1 #744 frozen cost selector (contract v1.0 sections 5.1, 5.2, 12; +note integrator decision 3 fixes the calibration layout). Unit order: sha256 r1 exp check delta +note select lib methods router. Reads R1P word 1 (family) and R1N (n) of r1.om, and the delta +note churn counters through dlinvn and dlmissn of delta.om. Nothing here learns, times itself or +note writes a file: every choice is a pure function of the frozen calibration and the features. +note +note Calibration file R1CAL001: exactly 200 words (1600 bytes), little endian: +note 0 magic 3544386010811150674, 1 version 1, 2 E_q x 1000, 3..7 zero; +note cell c = ((family - 1) * 4 + path) * 12 + nclass * 4 + cclass; +note word 8 + 2c = p50 ns, word 9 + 2c = p90 ns, 0 = missing. +note path 0 full, 1 method, 2 delta, 3 bind (bind only in cclass 0). +note nclass by n: 0 (n <= 64), 1 (<= 512), 2 (<= 4096). cclass by changes since the previous +note QUERY: 0 (0), 1 (1), 2 (2..64), 3 (> 64). +note Any other size, magic, version or a nonzero word 3..7 refuses the whole file: nothing of it is +note kept, and the selector runs the fixed rules (cold start). p90 is never read. +note +note Router protocol: +note SLPATH calibration path bytes up to the first 0 byte; empty or "-" = no calibration. +note slload once at start: 0 loaded, 1 absent or refused. +note slsess at every accepted BEGIN: clears the session's validity marks and churn state. +note slbind t at BEGIN or PROGRAM, t = template index or -1: bind a method at all? +note no calibration, or a missing bind, full or method cell (cclass 0): bind iff t >= 0; +note else bind iff p50(bind) < E_q (p50(full) - p50(method)), compared exactly. +note slset meth dval before every QUERY: which paths are valid now (full always is). +note slpick nchg: 1 full, 3 method, 4 delta, only ever a valid path. Lowest p50 of the valid +note paths for (family, nclass, cclass), ties to delta, then method, then full; no calibration +note or a missing cell of a valid path: the fixed rule (delta if valid, else method if bound, +note else full). Churn: from 64 invalidations or 64 cutoff misses in the session (counted from +note the slsess snapshot) delta is invalid for the rest of the session. +note sloff: true iff churn has disabled delta for this session, as judged by the last slpick; +note stays true until the next slsess. The router may then drop the delta state and stop +note its maintenance for the session. +note An unsupported family (R1P word 1 not 1 or 2), n > 4096, template index outside -1..15, or +note a path or class id outside its range is refused before any lookup: slpick returns full, +note slbind returns false, and SLREF counts it (the #704 rule). No cell is indexed with such an id. +note STATS: SLREF refusals, SLDIS churn disables (both process totals). + +global SLPATH uchar 4096 +global SLCAL ulong 200 +global SLOK ulong 1 +global SLBUF uchar 2048 +global SLMB ulong 1 +global SLDV ulong 1 +global SLOFF ulong 1 +global SLINV0 ulong 1 +global SLMIS0 ulong 1 +global SLREF ulong 1 +global SLDIS ulong 1 + +fn slref to void +put SLREF 0 add get SLREF 0 1 +end + +note bytes of SLPATH before the first 0; 4096 when there is none (refused by slload) +fn slpathn to ulong +for i ulong 4096 +if eq get SLPATH i 0 +ret i +end +end +ret 4096 +end + +note read the open file into SLBUF until end of file; 2048 = too long or a read error +fn slreadall fd int to ulong +tot = ulong 0 +while lt tot 2048 bound 4096 +k = read fd SLBUF tot sub 2048 tot +if lt k 0 +ret 2048 +end +if eq k 0 +ret tot +end +tot = add tot ulong k +end +ret tot +end + +note little-endian word i of SLBUF +fn slword i ulong to ulong +v = ulong 0 +p = mul i 8 +j = ulong 8 +while gt j 0 bound 8 +j = sub j 1 +v = or shl v 8 ulong get SLBUF add p j +end +ret v +end + +note check the 1600 bytes in SLBUF; only a fully valid file is copied into SLCAL +fn slparse to int +if ne slword 0 3544386010811150674 +ret 1 +end +if ne slword 1 1 +ret 1 +end +for i ulong 5 +if ne slword add i 3 0 +ret 1 +end +end +for i ulong 200 +put SLCAL i slword i +end +put SLOK 0 1 +ret 0 +end + +fn slload to int +put SLOK 0 0 +for i ulong 200 +put SLCAL i 0 +end +pn = slpathn +if or eq pn 0 ge pn 4096 +ret 1 +end +if and eq pn 1 eq get SLPATH 0 45 +ret 1 +end +fd = fopen SLPATH 0 pn 524288 0 +if lt fd 0 +ret 1 +end +tot = slreadall int fd +r = fclose int fd +if or ne tot 1600 lt r 0 +ret 1 +end +ret slparse +end + +note p50 ns of one cell, 0 = missing; an id out of range is refused and counted, never indexed +fn slcell fam ulong path ulong ncl ulong ccl ulong to ulong +if or or lt fam 1 gt fam 2 or gt path 3 or gt ncl 2 gt ccl 3 +slref +ret 0 +end +if eq get SLOK 0 0 +ret 0 +end +c = add mul add mul sub fam 1 4 path 12 add mul ncl 4 ccl +ret get SLCAL add 8 mul 2 c +end + +note n size class of R1N; 3 = unsupported (n > 4096) +fn slncl to ulong +n = get R1N 0 +if le n 64 +ret 0 +end +if le n 512 +ret 1 +end +if le n 4096 +ret 2 +end +ret 3 +end + +note change class of the changes since the previous QUERY +fn slccl nchg ulong to ulong +if eq nchg 0 +ret 0 +end +if eq nchg 1 +ret 1 +end +if le nchg 64 +ret 2 +end +ret 3 +end + +note high 64 bits of the 128-bit product a b +fn slmulhi a ulong b ulong to ulong +m = ulong 4294967295 +a0 = and a m +a1 = shr a 32 +b0 = and b m +b1 = shr b 32 +p01 = mul a0 b1 +p10 = mul a1 b0 +mid = add shr mul a0 b0 32 add and p01 m and p10 m +h = add mul a1 b1 add shr p01 32 shr p10 32 +ret add h shr mid 32 +end + +note exact a b < c d over 128 bits +fn slltp a ulong b ulong c ulong d ulong to bool +h1 = slmulhi a b +h2 = slmulhi c d +if ne h1 h2 +ret lt h1 h2 +end +ret lt mul a b mul c d +end + +fn slsess to void +put SLOFF 0 0 +put SLMB 0 0 +put SLDV 0 0 +put SLINV0 0 dlinvn +put SLMIS0 0 dlmissn +end + +fn slset meth bool dval bool to void +put SLMB 0 0 +put SLDV 0 0 +if meth +put SLMB 0 1 +end +if dval +put SLDV 0 1 +end +end + +fn slbind t long to bool +fam = get R1P 1 +ncl = slncl +if or or lt fam 1 gt fam 2 gt ncl 2 +slref +ret false +end +if or lt t -1 gt t 15 +slref +ret false +end +if eq t -1 +ret false +end +if eq get SLOK 0 0 +ret true +end +cb = slcell fam 3 ncl 0 +cf = slcell fam 0 ncl 0 +cm = slcell fam 1 ncl 0 +if or or eq cb 0 eq cf 0 eq cm 0 +ret true +end +if le cf cm +ret false +end +ret slltp cb 1000 get SLCAL 2 sub cf cm +end + +note churn: 64 invalidations or 64 cutoff misses since slsess disable delta for the session +fn slchurn to void +if eq get SLOFF 0 1 +ret +end +inv = sub dlinvn get SLINV0 0 +mis = sub dlmissn get SLMIS0 0 +if or ge inv 64 ge mis 64 +put SLOFF 0 1 +put SLDIS 0 add get SLDIS 0 1 +end +end + +fn sloff to bool +ret eq get SLOFF 0 1 +end + +fn slfixed md ulong dv ulong to int +if eq dv 1 +ret 4 +end +if eq md 1 +ret 3 +end +ret 1 +end + +note lowest p50 among the valid paths; a missing cell of a valid path gives the fixed rule +fn slrank fam ulong ncl ulong ccl ulong md ulong dv ulong to int +best = int 1 +cm = ulong 0 +cd = ulong 0 +bc = slcell fam 0 ncl ccl +if eq bc 0 +ret slfixed md dv +end +if eq md 1 +cm = slcell fam 1 ncl ccl +if eq cm 0 +ret slfixed md dv +end +if le cm bc +best = 3 +bc = cm +end +end +if eq dv 1 +cd = slcell fam 2 ncl ccl +if eq cd 0 +ret slfixed md dv +end +if le cd bc +best = 4 +end +end +ret best +end + +fn slpick nchg ulong to int +fam = get R1P 1 +ncl = slncl +if or or lt fam 1 gt fam 2 gt ncl 2 +slref +ret 1 +end +slchurn +md = get SLMB 0 +dv = get SLDV 0 +if eq get SLOFF 0 1 +dv = 0 +end +ccl = slccl nchg +if eq get SLOK 0 0 +ret slfixed md dv +end +ret slrank fam ncl ccl md dv +end +note SPDX-License-Identifier: Apache-2.0 +note lib.om: REUSE-1 library generations (contract v1.0 section 3.4, clarifications K1 K3 K16). +note Part of every router (lbload, lblineage) and of drive.om (lbpub and helpers). lbload hands the +note verified calibration bytes to the generation as a memfd on fd 7 (K16). +note Needs sha256 (omsha), r1 and exp (exaload, exallow, EXAPATH, EXALEN) before it. +note Manifest: 256 LE words in LBMAN, file bytes in LBMB; word 255 = FNV-1a 64 over the 2040 LE +note bytes of words 0..254 (K3). CURRENT: 40 bytes, G LE then the 32 SHA-256 bytes of gen-G.man. +note Digests are 4 words, word j = digest bytes 8j..8j+7 read big endian (contract 1.1). +note lbload refusal codes (each check distinct, nothing is trusted before all pass): +note 1 argv (count, LIBDIR length) 2 MANSHA not 64 hex digits 3 CURRENT unreadable +note 4 CURRENT not exactly 40 bytes 5 gen-G.man unreadable 6 gen-G.man not exactly 2048 bytes +note 7 SHA-256(gen-G.man) differs from the CURRENT digest 8 CURRENT digest differs from MANSHA +note 9 magic 10 version 11 self-check 12 G differs from CURRENT or parent G >= G +note 13 compiler SHA-256 is not the pinned omc 14 profile version 15 K > 16 or L > 8 +note 16 a reserved word is nonzero 17 ALLOW unreadable 18 a lineage digest not allowed +note 19 gen-G.cal unreadable or 4096 bytes or more 20 gen-G.cal digest 21 gen-G.elf unreadable +note 22 memfd (or dup onto fd 7) failed 23 copy into a memfd failed 24 ELF digest 25 exec failed + +global LBMAN ulong 256 +global LBMB uchar 2048 +global LBH ulong 4 +global LBCD ulong 4 +global LBMS ulong 4 +global LBDIR uchar 4096 +global LBDN ulong 1 +global LBP uchar 4096 +global LBPN ulong 1 +global LBPK uchar 4096 +global LBIO uchar 4096 +global LBST uchar 128 +global LBG ulong 1 +global LBLD ulong 32 +global LBLN ulong 1 +global LBGEN ulong 1 +global LBCAL ulong 1 +text LBTGEN "gen-" +text LBTOM ".om" +text LBTELF ".elf" +text LBTMAN ".man" +text LBTCAL ".cal" +text LBTTMP ".tmp" +text LBTCUR "CURRENT" +text LBTFD "/proc/self/fd/" +text LBTLD "loaded" +text LBTMFD "r1gen" + +note byte swap of a 64-bit word +fn lbbswap x ulong to ulong +y = or shl and x 71777214294589695 8 and shr x 8 71777214294589695 +y = or shl and y 281470681808895 16 and shr y 16 281470681808895 +ret or shl y 32 shr y 32 +end + +note omsha's packed digest (w[i] holds digest bytes 8i..8i+7 little endian) into LBH +fn lbdig w view ulong 64 to void +for i ulong 4 +put LBH i lbbswap get w i +end +end + +note SHA-256 of a whole open file read with pread from position 0 into LBH; bytes or -errno +fn lbhfd fd int to long +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +tot = long 0 +while true bound 999999 +r = pread fd b 0 4096 tot +if lt r 0 +ret r +end +if eq r 0 +break +end +omsha c b ulong r 1 w b8 +tot = add tot r +end +omsha c b 0 2 w b8 +lbdig w +ret tot +end + +note SHA-256 of the 2048 manifest bytes LBMB into LBH +fn lbhman to void +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +for i ulong 2048 +put b i get LBMB i +end +omsha c b 0 0 w b8 +omsha c b 2048 1 w b8 +omsha c b 0 2 w b8 +lbdig w +end + +note manifest words to bytes and back +fn lbm2b to void +for i ulong 256 +v = get LBMAN i +for k ulong 8 +put LBMB add mul i 8 k uchar and shr v mul k 8 255 +end +end +end + +fn lbb2m to void +for i ulong 256 +v = ulong 0 +for k ulong 8 +v = or v shl ulong get LBMB add mul i 8 k mul k 8 +end +put LBMAN i v +end +end + +note FNV-1a 64 over LBMB[0..2040) (fill LBMB with lbm2b first) +fn lbfnv to ulong +h = ulong 14695981039346656037 +for i ulong 2040 +h = xor h ulong get LBMB i +h = mul h 1099511628211 +end +ret h +end + +note self-check word 255 of LBMAN (drive.om, tests) +fn lbseal to void +lbm2b +put LBMAN 255 lbfnv +lbm2b +end + +note little/big endian word of LBIO at byte o +fn lbgetle o ulong to ulong +v = ulong 0 +for k ulong 8 +v = or v shl ulong get LBIO add o k mul k 8 +end +ret v +end + +fn lbgetbe o ulong to ulong +v = ulong 0 +for k ulong 8 +v = or shl v 8 ulong get LBIO add o k +end +ret v +end + +fn lbputbe o ulong v ulong to void +for k ulong 8 +put LBIO add o k uchar and shr v mul sub 7 k 8 255 +end +end + +note path builder: LBP[0..LBPN) +fn lbpz to void +put LBPN 0 0 +end + +fn lbpc c ulong to void +n = get LBPN 0 +if ge n 4095 +trap 3 +end +put LBP n uchar c +put LBPN 0 add n 1 +end + +fn lbpnum v ulong to void +d = new uchar 24 0 +n = ulong 0 +x = v +while true bound 24 +put d n uchar add rem x 10 48 +n = add n 1 +x = div x 10 +if eq x 0 +break +end +end +while gt n 0 bound 24 +n = sub n 1 +lbpc ulong get d n +end +end + +note fixed name parts: 1 gen- 2 .om 3 .elf 4 .man 5 .cal 6 .tmp 7 CURRENT 8 /proc/self/fd/ 9 loaded +fn lbptx k ulong to void +if eq k 1 +for i ulong len LBTGEN +lbpc ulong get LBTGEN i +end +end +if eq k 2 +for i ulong len LBTOM +lbpc ulong get LBTOM i +end +end +if eq k 3 +for i ulong len LBTELF +lbpc ulong get LBTELF i +end +end +if eq k 4 +for i ulong len LBTMAN +lbpc ulong get LBTMAN i +end +end +if eq k 5 +for i ulong len LBTCAL +lbpc ulong get LBTCAL i +end +end +if eq k 6 +for i ulong len LBTTMP +lbpc ulong get LBTTMP i +end +end +if eq k 7 +for i ulong len LBTCUR +lbpc ulong get LBTCUR i +end +end +if eq k 8 +for i ulong len LBTFD +lbpc ulong get LBTFD i +end +end +if eq k 9 +for i ulong len LBTLD +lbpc ulong get LBTLD i +end +end +end + +fn lbpdir to void +for i ulong get LBDN 0 +lbpc ulong get LBDIR i +end +lbpc 47 +end + +note LIBDIR/gen-G.EXT[.tmp], ext 1 om 2 elf 3 man 4 cal +fn lbpgen g ulong ext ulong tmp bool to void +lbpz +lbpdir +lbptx 1 +lbpnum g +lbptx add ext 1 +if tmp +lbptx 6 +end +end + +fn lbpcurr tmp bool to void +lbpz +lbpdir +lbptx 7 +if tmp +lbptx 6 +end +end + +note open LBP; descriptor or -1 +fn lbopen flags int mode int to int +r = fopen LBP 0 get LBPN 0 flags mode +if lt r 0 +ret -1 +end +ret int r +end + +note read up to cap (<= 4096) bytes of fd into LBIO; count or -errno +fn lbrdio fd int cap ulong to long +tot = ulong 0 +while lt tot cap bound 4097 +r = read fd LBIO tot sub cap tot +if lt r 0 +ret r +end +if eq r 0 +break +end +tot = add tot ulong r +end +ret long tot +end + +note write LBIO[0..n) completely; 0 or 1 +fn lbwrio fd int n ulong to int +tot = ulong 0 +while lt tot n bound 4097 +r = write fd LBIO tot sub n tot +if le r 0 +ret 1 +end +tot = add tot ulong r +end +ret 0 +end + +note size of the file named by LBP, or -1 +fn lbfsize to long +r = stat LBP 0 get LBPN 0 LBST 0 128 +if ne r 0 +ret -1 +end +v = ulong 0 +for k ulong 8 +v = or v shl ulong get LBST add 48 k mul k 8 +end +ret long v +end + +note SHA-256 of the file named by LBP into LBH; bytes or negative +fn lbhpath to long +fd = lbopen 524288 0 +if lt fd 0 +ret -1 +end +n = lbhfd fd +r = fclose fd +ret n +end + +fn lbhexd c ulong to ulong +if and ge c 48 le c 57 +ret sub c 48 +end +if and ge c 97 le c 102 +ret sub c 87 +end +if and ge c 65 le c 70 +ret sub c 55 +end +ret 16 +end + +note 64 hex digits in LBIO[0..64) into 4 digest words at LBMS; 0 or 1 +fn lbhex2ms to int +for j ulong 4 +v = ulong 0 +for k ulong 16 +d = lbhexd ulong get LBIO add mul j 16 k +if gt d 15 +ret 1 +end +v = or shl v 4 d +end +put LBMS j v +end +ret 0 +end + +note LIBDIR (argv 2) into LBDIR, MANSHA (argv 3) into LBMS +fn lbargs to int +if lt argcnt 7 +ret 1 +end +n = argread 2 LBDIR 0 3000 +if or lt n 1 gt n 2999 +ret 1 +end +put LBDN 0 ulong n +m = argread 3 LBIO 0 4096 +if ne m 64 +ret 2 +end +if ne lbhex2ms 0 +ret 2 +end +ret 0 +end + +note CURRENT of LBDIR: G into LBG, digest words into LBCD; 0, 3 unreadable, 4 not 40 bytes +fn lbcurrd to int +lbpcurr false +fd = lbopen 524288 0 +if lt fd 0 +ret 3 +end +n = lbrdio fd 41 +r = fclose fd +if ne n 40 +ret 4 +end +put LBG 0 lbgetle 0 +for j ulong 4 +put LBCD j lbgetbe add 8 mul j 8 +end +ret 0 +end + +note gen-G.man of LBDIR into LBMB and LBMAN; 0, 5 unreadable, 6 not 2048 bytes +fn lbmanrd to int +lbpgen get LBG 0 3 false +fd = lbopen 524288 0 +if lt fd 0 +ret 5 +end +n = lbrdio fd 2049 +r = fclose fd +if ne n 2048 +ret 6 +end +for i ulong 2048 +put LBMB i get LBIO i +end +lbb2m +ret 0 +end + +note LBMAN words a..b-1 all zero +fn lbzero a ulong b ulong to bool +i = a +while lt i b bound 257 +if ne get LBMAN i 0 +ret false +end +i = add i 1 +end +ret true +end + +note manifest field checks (LBMAN, LBMB); 0 or 9..16 +fn lbmchk to int +if ne get LBMAN 0 3544386019401740626 +ret 9 +end +if ne get LBMAN 1 1 +ret 10 +end +lbm2b +if ne lbfnv get LBMAN 255 +ret 11 +end +g = get LBMAN 2 +if or ne g get LBG 0 ge get LBMAN 3 g +ret 12 +end +if or ne get LBMAN 14 8308871078283820969 ne get LBMAN 15 3511899704727027462 +ret 13 +end +if or ne get LBMAN 16 15311527216533678964 ne get LBMAN 17 17706733685053746017 +ret 13 +end +if ne get LBMAN 18 1 +ret 14 +end +k = get LBMAN 4 +l = get LBMAN 19 +if or gt k 16 gt l 8 +ret 15 +end +if not lbzero add 20 mul l 4 52 +ret 16 +end +if not lbzero add 56 mul k 8 255 +ret 16 +end +for t ulong k +if ne get LBMAN add 63 mul t 8 0 +ret 16 +end +end +ret 0 +end + +note digest LBH equals 4 words of LBMAN at w +fn lbhmeq w ulong to bool +for j ulong 4 +if ne get LBH j get LBMAN add w j +ret false +end +end +ret true +end + +note shared load prefix: argv, CURRENT, manifest, MANSHA, fields; 0 or 1..16 +fn lbman to int +r = lbargs +if ne r 0 +ret r +end +r = lbcurrd +if ne r 0 +ret r +end +r = lbmanrd +if ne r 0 +ret r +end +lbhman +for j ulong 4 +if ne get LBH j get LBCD j +ret 7 +end +end +for j ulong 4 +if ne get LBCD j get LBMS j +ret 8 +end +end +ret lbmchk +end + +note every lineage digest allowed by ALLOW (argv 4); 0, 17, 18 +fn lballow to int +d = new ulong 4 0 +l = get LBMAN 19 +if eq l 0 +ret 0 +end +n = argread 4 EXAPATH 0 4095 +if lt n 1 +ret 17 +end +put EXAPATH ulong n 0 +put EXALEN 0 ulong n +if ne exaload 0 +ret 17 +end +for i ulong l +for j ulong 4 +put d j get LBMAN add add 20 mul i 4 j +end +if ne exallow d 1 +ret 18 +end +end +ret 0 +end + +note SHA-256 of LBIO[0..n) (n <= 4096) into LBH +fn lbhio n ulong to void +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +for i ulong n +put b i get LBIO i +end +omsha c b 0 0 w b8 +omsha c b n 1 w b8 +omsha c b 0 2 w b8 +lbdig w +end + +note calibration handoff (K1, K16): gen-G.cal read once into LBIO, SHA-256 of exactly those bytes +note against manifest 52..55, the same bytes written into a memfd left on fd 7 without CLOEXEC for +note the generation (which reads /proc/self/fd/7, never LIBDIR). 0, 19 unreadable or not under +note 4096 bytes, 20 digest, 22 memfd or dup failed, 23 write failed. No calibration: 0, fd 7 untouched. +fn lbcalchk to int +if lbzero 52 56 +ret 0 +end +lbpgen get LBG 0 4 false +fd = lbopen 524288 0 +if lt fd 0 +ret 19 +end +n = lbrdio fd 4096 +r = fclose fd +if or lt n 1 ge n 4096 +ret 19 +end +lbhio ulong n +if not lbhmeq 52 +ret 20 +end +m = memfd LBTMFD 0 len LBTMFD 0 +if lt m 0 +ret 22 +end +mf = int m +if ne lbwrio mf ulong n 0 +ret 23 +end +if ne mf 7 +d = dupfd mf 7 0 +r = fclose mf +if ne d 7 +ret 22 +end +end +ret 0 +end + +note gen-G.elf copied into a memfd whose SHA-256 must equal manifest 10..13; fd or -21..-24 +fn lbelfmem to int +lbpgen get LBG 0 2 false +fd = lbopen 524288 0 +if lt fd 0 +ret -21 +end +m = memfd LBTMFD 0 len LBTMFD 1 +if lt m 0 +r = fclose fd +ret -22 +end +mf = int m +while true bound 999999 +r = read fd LBIO 0 4096 +if lt r 0 +r = fclose fd +ret -23 +end +if eq r 0 +break +end +if ne lbwrio mf ulong r 0 +r = fclose fd +ret -23 +end +end +r = fclose fd +n = lbhfd mf +if lt n 0 +ret -23 +end +if not lbhmeq 10 +ret -24 +end +ret mf +end + +note append LBP to the packed list LBPK as one record at o; new offset +fn lbpkadd o ulong to ulong +n = get LBPN 0 +put LBPK o uchar and n 255 +put LBPK add o 1 uchar shr n 8 +for i ulong n +put LBPK add add o 2 i get LBP i +end +ret add add o 2 n +end + +note exec /proc/self/fd/MF with argv 1..6 and loaded; returns only on failure +fn lbexec mf int to int +put LBPK 0 8 +put LBPK 1 0 +lbpz +lbptx 8 +lbpnum ulong mf +o = lbpkadd 2 +for a ulong 6 +n = argread add a 1 LBPK add o 2 sub 4000 o +if lt n 0 +ret 25 +end +put LBPK o uchar and ulong n 255 +put LBPK add o 1 uchar shr ulong n 8 +o = add add o 2 ulong n +if gt o 4000 +ret 25 +end +end +lbpz +lbptx 9 +o = lbpkadd o +r = execprog LBPK 0 o +ret 25 +end + +note B's load (contract 3.4): 0 never (exec'd), nonzero = refused with the code listed above +fn lbload to int +if ne argcnt 7 +ret 1 +end +r = lbman +if ne r 0 +ret r +end +r = lballow +if ne r 0 +ret r +end +r = lbcalchk +if ne r 0 +ret r +end +mf = lbelfmem +if lt mf 0 +ret neg mf +end +ret lbexec mf +end + +note manifest lineage for revocation checks in a loaded generation: LBLD, LBLN, LBGEN, LBCAL +fn lblineage to int +r = lbman +if ne r 0 +ret r +end +put LBLN 0 get LBMAN 19 +for i ulong 32 +put LBLD i get LBMAN add 20 i +end +put LBGEN 0 get LBG 0 +put LBCAL 0 0 +if not lbzero 52 56 +put LBCAL 0 1 +end +ret 0 +end + +note drive.om side. Remove a stale unpublished generation g (crash leftovers) and CURRENT.tmp. +fn lbclean g ulong to void +for e ulong 4 +lbpgen g add e 1 false +r = unlink LBP 0 get LBPN 0 +lbpgen g add e 1 true +r = unlink LBP 0 get LBPN 0 +end +lbpcurr true +r = unlink LBP 0 get LBPN 0 +end + +note fsync the file named by LBP; 0 or 1 +fn lbsyncp to int +fd = lbopen 524288 0 +if lt fd 0 +ret 1 +end +r = fsync fd +s = fclose fd +if ne r 0 +ret 1 +end +ret 0 +end + +fn lbsyncdir to int +r = fopen LBDIR 0 get LBDN 0 524288 0 +if lt r 0 +ret 1 +end +fd = int r +s = fsync fd +r = fclose fd +if ne s 0 +ret 1 +end +ret 0 +end + +note gen-G.EXT.tmp: fsync, then renames flag 1 onto gen-G.EXT; 0 or 1 +fn lbrengen g ulong ext ulong to int +lbpgen g ext true +if ne lbsyncp 0 +ret 1 +end +o = lbpkadd 2 +lbpgen g ext false +o = lbpkadd o +put LBPK 0 2 +put LBPK 1 0 +r = renames LBPK 0 o 1 +if ne r 0 +ret 1 +end +ret 0 +end + +note publish generation LBG with manifest LBMAN (sealed) in ADR 0015 order. Expects gen-G.om.tmp, +note gen-G.elf.tmp and, when manifest 52..55 is nonzero, gen-G.cal.tmp already written. Steps: +note 1 om, 2 elf, 3 cal (no-op without calibration), 4 man.tmp written and fsync'd, 5 man renamed, +note 6 LIBDIR fsync, 7 CURRENT.tmp written and fsync'd, 8 CURRENT renamed, 9 LIBDIR fsync. +note crash = K stops after step K with 97 (control C8); 0 ok; 100 + step on failure. +fn lbpub crash ulong to int +g = get LBG 0 +if ne lbrengen g 1 0 +ret 101 +end +if eq crash 1 +ret 97 +end +if ne lbrengen g 2 0 +ret 102 +end +if eq crash 2 +ret 97 +end +if not lbzero 52 56 +if ne lbrengen g 4 0 +ret 103 +end +end +if eq crash 3 +ret 97 +end +lbm2b +lbpgen g 3 true +fd = lbopen 524865 384 +if lt fd 0 +ret 104 +end +w = write fd LBMB 0 2048 +s = fsync fd +r = fclose fd +if or ne w 2048 ne s 0 +ret 104 +end +if eq crash 4 +ret 97 +end +lbpgen g 3 true +o = lbpkadd 2 +lbpgen g 3 false +o = lbpkadd o +put LBPK 0 2 +put LBPK 1 0 +r = renames LBPK 0 o 1 +if ne r 0 +ret 105 +end +if eq crash 5 +ret 97 +end +if ne lbsyncdir 0 +ret 106 +end +if eq crash 6 +ret 97 +end +lbhman +for k ulong 8 +put LBIO k uchar and shr g mul k 8 255 +end +for j ulong 4 +lbputbe add 8 mul j 8 get LBH j +end +lbpcurr true +fd = lbopen 524865 384 +if lt fd 0 +ret 107 +end +w2 = lbwrio fd 40 +s = fsync fd +r = fclose fd +if or ne w2 0 ne s 0 +ret 107 +end +if eq crash 7 +ret 97 +end +lbpcurr true +o = lbpkadd 2 +lbpcurr false +o = lbpkadd o +put LBPK 0 2 +put LBPK 1 0 +r = renames LBPK 0 o 0 +if ne r 0 +ret 108 +end +if eq crash 8 +ret 97 +end +if ne lbsyncdir 0 +ret 109 +end +if eq crash 9 +ret 97 +end +ret 0 +end +note SPDX-License-Identifier: Apache-2.0 +note methods.om: generated by om/reuse/codegen.om (REUSE-1 contract 3.2 and 12). Do not edit. +fn mtl0 x ulong to ulong +v = x +v = and mul v get R1P 6 65535 +v = and shl v get R1P 8 65535 +v = and shl v get R1P 10 65535 +ret v +end +fn mtr0 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl0 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl1 x ulong to ulong +v = x +v = and shl v get R1P 6 65535 +v = and v get R1P 8 +v = xor v get R1P 10 +v = and shl v get R1P 12 65535 +v = and v get R1P 14 +v = and v get R1P 16 +ret v +end +fn mtr1 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl1 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl2 x ulong to ulong +v = x +v = and sub v get R1P 6 255 +v = xor v get R1P 8 +v = and add v get R1P 10 255 +v = and mul v get R1P 12 255 +v = xor v get R1P 14 +ret v +end +fn mtr2 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl2 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl3 x ulong to ulong +v = x +v = or v get R1P 6 +ret v +end +fn mtr3 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl3 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl4 x ulong to ulong +v = x +v = and shl v get R1P 6 65535 +ret v +end +fn mtr4 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl4 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl5 x ulong to ulong +v = x +v = and shl v get R1P 6 65535 +v = xor v get R1P 8 +v = xor v get R1P 10 +ret v +end +fn mtr5 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl5 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl6 x ulong to ulong +v = x +v = and sub v get R1P 6 65535 +v = and mul v get R1P 8 65535 +v = and mul v get R1P 10 65535 +v = xor v get R1P 12 +ret v +end +fn mtr6 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl6 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl7 x ulong to ulong +v = x +v = and sub v get R1P 6 65535 +v = and add v get R1P 8 65535 +ret v +end +fn mtr7 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl7 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl8 x ulong to ulong +v = x +v = and shl v get R1P 6 65535 +v = or v get R1P 8 +v = shr v get R1P 10 +ret v +end +fn mtr8 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl8 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl9 x ulong to ulong +v = x +v = and mul v get R1P 6 255 +v = and v get R1P 8 +v = or v get R1P 10 +ret v +end +fn mtr9 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl9 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl10 x ulong to ulong +v = x +v = and v get R1P 6 +v = and shl v get R1P 8 255 +v = and add v get R1P 10 255 +v = xor v get R1P 12 +ret v +end +fn mtr10 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl10 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl11 x ulong to ulong +v = x +v = and v get R1P 6 +v = or v get R1P 8 +v = and v get R1P 10 +v = xor v get R1P 12 +v = shr v get R1P 14 +v = and mul v get R1P 16 255 +ret v +end +fn mtr11 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl11 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl12 x ulong to ulong +v = x +v = or v get R1P 6 +v = and v get R1P 8 +v = and v get R1P 10 +v = and mul v get R1P 12 255 +ret v +end +fn mtr12 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl12 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl13 x ulong to ulong +v = x +v = and mul v get R1P 6 255 +v = and v get R1P 8 +v = and v get R1P 10 +v = and sub v get R1P 12 255 +v = and sub v get R1P 14 255 +ret v +end +fn mtr13 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl13 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtl14 x ulong to ulong +v = x +v = or v get R1P 6 +v = xor v get R1P 8 +v = and add v get R1P 10 255 +v = and mul v get R1P 12 255 +v = and sub v get R1P 14 255 +ret v +end +fn mtr14 to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc mtl14 get R1X i +end +put R1R 0 acc +put R1R 1 n +end +fn mtr15 to void +q = get R1P 4 +n = get R1N 0 +sum = ulong 0 +for i ulong n +q = get R1T add mul q 16 get R1X i +sum = add sum get R1P add 6 q +end +put R1R 0 q +put R1R 1 sum +end +fn mtcount to ulong +ret 16 +end +fn mtshape t ulong to ulong +if eq t 0 +ret 124989968 +end +if eq t 1 +ret 294042165520 +end +if eq t 2 +ret 26598331144 +end +if eq t 3 +ret 332808 +end +if eq t 4 +ret 464400 +end +if eq t 5 +ret 107426320 +end +if eq t 6 +ret 1664237840 +end +if eq t 7 +ret 1189392 +end +if eq t 8 +ret 139933200 +end +if eq t 9 +ret 88290056 +end +if eq t 10 +ret 1635009032 +end +if eq t 11 +ret 242201420040 +end +if eq t 12 +ret 876954632 +end +if eq t 13 +ret 9198327304 +end +if eq t 14 +ret 9418659080 +end +if eq t 15 +ret 1 +end +ret 0 +end +fn mtfind to long +if ne r1valid 0 +ret -1 +end +s = r1shape +if eq s 124989968 +ret 0 +end +if eq s 294042165520 +ret 1 +end +if eq s 26598331144 +ret 2 +end +if eq s 332808 +ret 3 +end +if eq s 464400 +ret 4 +end +if eq s 107426320 +ret 5 +end +if eq s 1664237840 +ret 6 +end +if eq s 1189392 +ret 7 +end +if eq s 139933200 +ret 8 +end +if eq s 88290056 +ret 9 +end +if eq s 1635009032 +ret 10 +end +if eq s 242201420040 +ret 11 +end +if eq s 876954632 +ret 12 +end +if eq s 9198327304 +ret 13 +end +if eq s 9418659080 +ret 14 +end +if eq s 1 +ret 15 +end +ret -1 +end +fn mtlane t ulong x ulong to ulong +if eq t 0 +ret mtl0 x +end +if eq t 1 +ret mtl1 x +end +if eq t 2 +ret mtl2 x +end +if eq t 3 +ret mtl3 x +end +if eq t 4 +ret mtl4 x +end +if eq t 5 +ret mtl5 x +end +if eq t 6 +ret mtl6 x +end +if eq t 7 +ret mtl7 x +end +if eq t 8 +ret mtl8 x +end +if eq t 9 +ret mtl9 x +end +if eq t 10 +ret mtl10 x +end +if eq t 11 +ret mtl11 x +end +if eq t 12 +ret mtl12 x +end +if eq t 13 +ret mtl13 x +end +if eq t 14 +ret mtl14 x +end +ret 0 +end +fn mtrun t ulong to void +if eq t 0 +mtr0 +ret +end +if eq t 1 +mtr1 +ret +end +if eq t 2 +mtr2 +ret +end +if eq t 3 +mtr3 +ret +end +if eq t 4 +mtr4 +ret +end +if eq t 5 +mtr5 +ret +end +if eq t 6 +mtr6 +ret +end +if eq t 7 +mtr7 +ret +end +if eq t 8 +mtr8 +ret +end +if eq t 9 +mtr9 +ret +end +if eq t 10 +mtr10 +ret +end +if eq t 11 +mtr11 +ret +end +if eq t 12 +mtr12 +ret +end +if eq t 13 +mtr13 +ret +end +if eq t 14 +mtr14 +ret +end +if eq t 15 +mtr15 +ret +end +r1full +end +fn mtsrc t ulong D edit ulong 4 to void +if eq t 0 +put D 0 8332033867278475247 +put D 1 3814867826751752014 +put D 2 5696338132649250720 +put D 3 12246281744200300717 +ret +end +if eq t 1 +put D 0 1204314104447044368 +put D 1 2619749534462691067 +put D 2 16726604078246749710 +put D 3 12112054145954228024 +ret +end +if eq t 2 +put D 0 6093375822610831865 +put D 1 204619677006831433 +put D 2 16547372421383235266 +put D 3 6176754672049912843 +ret +end +if eq t 3 +put D 0 8086193846879731236 +put D 1 1980098200398492695 +put D 2 8991607438543816592 +put D 3 2018090475375901527 +ret +end +if eq t 4 +put D 0 3321709468371632568 +put D 1 2598679344901682862 +put D 2 15905291452918590093 +put D 3 11659821835030187278 +ret +end +if eq t 5 +put D 0 10230322967957133993 +put D 1 330447080221051260 +put D 2 12599694564542893486 +put D 3 12520711416274344721 +ret +end +if eq t 6 +put D 0 10229901254434136438 +put D 1 14965132072618019483 +put D 2 8250948492065323570 +put D 3 13100052123027361504 +ret +end +if eq t 7 +put D 0 14394278837397428087 +put D 1 12166746162898314203 +put D 2 4694085810653066660 +put D 3 8820157371867784555 +ret +end +if eq t 8 +put D 0 11908235511046039743 +put D 1 13612445328387765168 +put D 2 5949100647833278376 +put D 3 8619622549270042422 +ret +end +if eq t 9 +put D 0 1258506319409952552 +put D 1 1567867490864531029 +put D 2 4290918390896918664 +put D 3 10526507374814677370 +ret +end +if eq t 10 +put D 0 716349024759043987 +put D 1 13493225571313073340 +put D 2 15313474482300391423 +put D 3 13833747351729635651 +ret +end +if eq t 11 +put D 0 14713588281215133871 +put D 1 4178476975283361016 +put D 2 1243664768712724916 +put D 3 17840158557280776493 +ret +end +if eq t 12 +put D 0 349718677468470902 +put D 1 8062408694973673972 +put D 2 16921974689875705042 +put D 3 10545843818805159935 +ret +end +if eq t 13 +put D 0 1174916492710703254 +put D 1 2358811944602487578 +put D 2 17464809163298902450 +put D 3 14811828270479495629 +ret +end +if eq t 14 +put D 0 5028539561783689278 +put D 1 14335984701034522874 +put D 2 2803479335876008836 +put D 3 11453350361114226845 +ret +end +if eq t 15 +put D 0 5697826474538046150 +put D 1 17605239106479653115 +put D 2 2589925873017448613 +put D 3 11652210143643333060 +ret +end +put D 0 0 +put D 1 0 +put D 2 0 +put D 3 0 +end +note SPDX-License-Identifier: Apache-2.0 +note router.om: the REUSE-1 arm process (docs/reuse/REUSE-1.md section 2, 3.4 load, 4 revocation, and +note clarifications K1, K2, K9). Last unit; holds fn main. Every arm binary is this router. +note argv: ARM LIBDIR MANSHA ALLOW TAPFILE SPLIT [loaded]; ARM echo full cache method delta select tap. +note Startup exit codes (no response is written): 2 usage (argc, ARM), 2 tap refused (extopen). +note Protocol: little-endian u64 words on stdin/stdout. Request op, p, p words; response status, q, +note q words. Framing (K2): op outside 1..10 or p > 7234 -> 4 and exit 4 (the payload is not read); +note otherwise the payload is read whole; a p that does not match its op -> 4; ops 2..8 without a +note session -> 6; then the op's own statuses. EOF on stdin without END -> exit 3, no response. +note Load (3.4): ARM method/delta/select, LIBDIR not "-", no trailing "loaded": lbload (lib.om) execs +note the generation and never returns on success; on its nonzero return this process serves every +note QUERY as full with route 5. A generation (argv ends in "loaded") never loads again; it reads its +note lineage (lblineage) and checks it against ALLOW at every BEGIN and, whenever the stat snapshot +note of ALLOW (inode, size, mtime ns, ctime ns) changed, at QUERY: a revoked or unverifiable lineage +note gives route 5 from that QUERY on for the rest of the process. select in a generation reads its +note calibration from /proc/self/fd/7, the memfd of verified bytes lbload left open before exec (K16), +note iff the manifest names one (LBCAL); never LIBDIR/gen-G.cal. With LIBDIR "-" it has no calibration. +note Arms at QUERY (route): echo 0 0 (0); full r1full (1); tap r1full and one experience record (1); +note cache key SHA-256(program digest, n, state digest), the state digest recomputed only at the first +note QUERY after a change, 4096 entries FIFO: hit (2), miss r1full (1); method mtrun (3) when a +note template is bound and passed G3, else r1full (1), G3 failure r1full (5) for the session; delta: +note the delta state (4) of the bound method or of the baseline (t = -1), rebuilt by dlbind at a QUERY +note after a drop, else method (3) or full (1); select: slpick among full (1), method (3), delta (4); +note once sloff reports churn (5.2) the delta state is dropped and not maintained for the session. +note STATS answers 32 counters RTST: 0..5 QUERYs by route 0..5, 6 sessions, 7 change items applied, +note 8 PROGRAMs applied, 9 refused requests, 10 ckbind calls, 11 bind failures, 12 G3 FIFO hits, +note 13 G3 lane pairs, 14..25 dlstat 0..11, 26 select delta disables (SLDIS), 27 select refusals +note (SLREF), 28 cache entries, 29 template count (mtcount), 30 load refusal code, 31 flags +note (1 generation, 2 refused, 4 revoked, 8 calibration path set, 16 calibration loaded by slload). +text RTTE "echo" +text RTTF "full" +text RTTC "cache" +text RTTM "method" +text RTTD "delta" +text RTTS "select" +text RTTT "tap" +text RTTL "loaded" +text RTTCAL "/proc/self/fd/7" +global RTARM ulong 1 +global RTFLG ulong 1 +global RTLC ulong 1 +global RTLIN ulong 1 +global RTSES ulong 1 +global RTSN ulong 1 +global RTQN ulong 1 +global RTNC ulong 1 +global RTT long 1 +global RTBF ulong 1 +global RTDB ulong 1 +global RTSD ulong 1 +global RTPS ulong 64 +global RTQ ulong 7234 +global RTIB uchar 65536 +global RTIP ulong 2 +global RTOW ulong 34 +global RTOB uchar 272 +global RTST ulong 32 +global RTCK ulong 24576 +global RTCN ulong 1 +global RTCH ulong 1 +global RTKEY ulong 4 +global RTA uchar 4096 + +note ---- input and output ---- +fn rtfill to bool +k = read 0 RTIB 0 65536 +if le k 0 +ret false +end +put RTIP 0 0 +put RTIP 1 ulong k +ret true +end + +fn rtbyte to ulong +p = get RTIP 0 +if ge p get RTIP 1 +if not rtfill +exit 3 +end +p = 0 +end +put RTIP 0 add p 1 +ret ulong get RTIB p +end + +fn rtword to ulong +p = get RTIP 0 +if le add p 8 get RTIP 1 +v = ulong 0 +for i ulong 8 +v = or shl v 8 ulong get RTIB sub add p 7 i +end +put RTIP 0 add p 8 +ret v +end +v = ulong 0 +for i ulong 8 +v = or v shl rtbyte mul i 8 +end +ret v +end + +note status s, q words RTOW[0..q) +fn rtresp s ulong q ulong to void +rtputw 0 s +rtputw 8 q +for i ulong q +rtputw mul add i 2 8 get RTOW i +end +n = mul add q 2 8 +off = ulong 0 +while lt off n bound 1000 +k = write 1 RTOB off sub n off +if le k 0 +exit 5 +end +off = add off ulong k +end +end + +fn rtputw o ulong w ulong to void +v = w +for i ulong 8 +put RTOB add o i uchar and v 255 +v = shr v 8 +end +end + +fn rtstat s ulong to void +if ne s 0 +put RTST 9 add get RTST 9 1 +end +rtresp s 0 +end + +note ---- argv ---- +note argv i into RTA: its length, or -1 +fn rtarg i ulong to long +n = argread i RTA 0 4096 +if or lt n 0 gt n 4095 +ret -1 +end +ret n +end + +fn rtdash i ulong to bool +n = rtarg i +ret and eq n 1 eq get RTA 0 45 +end + +note RTA[0..n) equals text id (1 echo .. 7 tap, 8 loaded) +fn rteq id ulong n ulong to bool +if eq id 1 +ret rteqe n +end +if eq id 2 +ret rteqf n +end +if eq id 3 +ret rteqc n +end +if eq id 4 +ret rteqm n +end +if eq id 5 +ret rteqd n +end +if eq id 6 +ret rteqs n +end +if eq id 7 +ret rteqt n +end +ret rteql n +end + +fn rteqe n ulong to bool +if ne n len RTTE +ret false +end +for i ulong n +if ne get RTA i get RTTE i +ret false +end +end +ret true +end + +fn rteqf n ulong to bool +if ne n len RTTF +ret false +end +for i ulong n +if ne get RTA i get RTTF i +ret false +end +end +ret true +end + +fn rteqc n ulong to bool +if ne n len RTTC +ret false +end +for i ulong n +if ne get RTA i get RTTC i +ret false +end +end +ret true +end + +fn rteqm n ulong to bool +if ne n len RTTM +ret false +end +for i ulong n +if ne get RTA i get RTTM i +ret false +end +end +ret true +end + +fn rteqd n ulong to bool +if ne n len RTTD +ret false +end +for i ulong n +if ne get RTA i get RTTD i +ret false +end +end +ret true +end + +fn rteqs n ulong to bool +if ne n len RTTS +ret false +end +for i ulong n +if ne get RTA i get RTTS i +ret false +end +end +ret true +end + +fn rteqt n ulong to bool +if ne n len RTTT +ret false +end +for i ulong n +if ne get RTA i get RTTT i +ret false +end +end +ret true +end + +fn rteql n ulong to bool +if ne n len RTTL +ret false +end +for i ulong n +if ne get RTA i get RTTL i +ret false +end +end +ret true +end + +note ARM: 1 echo 2 full 3 cache 4 method 5 delta 6 select 7 tap, 0 unknown +fn rtparm to ulong +n = rtarg 1 +if lt n 0 +ret 0 +end +for id ulong 7 +if rteq add id 1 ulong n +ret add id 1 +end +end +ret 0 +end + +note the arm has a library path: method, delta, select +fn rtlibarm to bool +a = get RTARM 0 +ret and ge a 4 le a 6 +end + +fn rtdelarm to bool +a = get RTARM 0 +ret or eq a 5 eq a 6 +end + +note route 5 for every QUERY: refused generation or revoked lineage +fn rtdead to bool +ret ne and get RTFLG 0 6 0 +end + +note ---- startup ---- +fn rtsetallow to void +if rtdash 4 +put EXALEN 0 0 +ret +end +n = ulong rtarg 4 +for i ulong n +put EXAPATH i get RTA i +end +put EXALEN 0 n +end + +fn rtcal to void +if ne get LBCAL 0 1 +ret +end +for i ulong len RTTCAL +put SLPATH i get RTTCAL i +end +put SLPATH len RTTCAL 0 +put RTFLG 0 or get RTFLG 0 8 +end + +note a refused generation load: code x (STATS 30; 255 when it is not 1..254), route 5 from now on +fn rtrefuse x int to void +c = ulong 255 +if and gt x 0 lt x 255 +c = ulong x +end +put RTLC 0 c +put RTFLG 0 or get RTFLG 0 2 +end + +fn rtinit to void +c = argcnt +if and ne c 7 ne c 8 +exit 2 +end +loaded = false +if eq c 8 +n = rtarg 7 +if or lt n 0 not rteq 8 ulong n +exit 2 +end +loaded = true +end +a = rtparm +if eq a 0 +exit 2 +end +put RTARM 0 a +put RTT 0 -1 +rtsetallow +if eq a 7 +if ne extopen 0 +exit 2 +end +end +if and rtlibarm not rtdash 2 +if loaded +rtgen +else +x = lbload +rtrefuse x +end +end +if eq a 6 +if eq slload 0 +put RTFLG 0 or get RTFLG 0 16 +end +end +end + +note a generation: lineage from the manifest pinned by MANSHA, calibration path +fn rtgen to void +put RTFLG 0 or get RTFLG 0 1 +x = lblineage +if ne x 0 +rtrefuse x +ret +end +if gt get LBLN 0 0 +put RTLIN 0 1 +end +if eq get RTARM 0 6 +rtcal +end +end + +note ---- revocation ---- +note every lineage digest allowed by the loaded ALLOW +fn rtlinok to bool +d = new ulong 4 0 +n = get LBLN 0 +for e ulong n +for j ulong 4 +put d j get LBLD add mul e 4 j +end +if ne exallow d 1 +ret false +end +end +ret true +end + +fn rtrevoke to void +put RTFLG 0 or get RTFLG 0 4 +if rtdelarm +dldrop +end +put RTDB 0 0 +put RTT 0 -1 +end + +note read ALLOW and check the lineage; snapshot its stat +fn rtrecheck to void +ch = exastat +ok = false +if gt get EXALEN 0 0 +if eq exaload 0 +ok = rtlinok +end +end +if not ok +rtrevoke +end +end + +fn rtrevq to void +if or eq get RTLIN 0 0 rtdead +ret +end +if exastat +x = exaload +if or ne x 0 not rtlinok +rtrevoke +end +end +end + +note ---- binding ---- +note bind for the current R1P (BEGIN or PROGRAM); the delta state was reset or dropped by the caller +fn rtbind to void +put RTT 0 -1 +put RTBF 0 0 +put RTDB 0 0 +a = get RTARM 0 +if or lt a 4 gt a 6 +ret +end +if rtdead +ret +end +t = mtfind +want = ge t 0 +if eq a 6 +want = and want slbind t +end +if want +if eq ckbind ulong t 0 +put RTT 0 t +else +put RTBF 0 1 +ret +end +end +if rtdelarm +if rtseloff +ret +end +if eq dlbind get RTT 0 0 +put RTDB 0 1 +end +end +end + +note select: churn disabled the delta path for this session (sloff, after the last slpick) +fn rtseloff to bool +if eq get RTARM 0 6 +ret sloff +end +ret false +end + +note ---- requests ---- +fn rtsavep to void +for i ulong 64 +put RTPS i get R1P i +put R1P i get RTQ i +end +end + +fn rtrestp to void +for i ulong 64 +put R1P i get RTPS i +end +end + +note BEGIN: payload PROG, n, n values (p checked by the caller) +fn rtbegin to ulong +n = get RTQ 64 +if gt n 4096 +ret 5 +end +rtsavep +if ne r1valid 0 +rtrestp +ret 2 +end +for i ulong n +if not r1indom get RTQ add 65 i +rtrestp +ret 3 +end +end +put R1N 0 n +for i ulong n +put R1X i get RTQ add 65 i +end +put RTSES 0 1 +put RTSN 0 add get RTSN 0 1 +put RTQN 0 0 +put RTNC 0 0 +put RTSD 0 1 +put RTST 6 add get RTST 6 1 +if gt get RTLIN 0 0 +if not rtdead +rtrecheck +end +end +if eq get RTARM 0 6 +slsess +end +if rtdelarm +dlsreset +end +if eq get RTARM 0 3 +r1pdig +end +rtbind +ret 0 +end + +fn rtprog to ulong +rtsavep +if ne r1valid 0 +rtrestp +ret 2 +end +if not r1alldom +rtrestp +ret 3 +end +put RTNC 0 add get RTNC 0 1 +put RTST 8 add get RTST 8 1 +if rtdelarm +dldrop +end +if eq get RTARM 0 3 +r1pdig +end +rtbind +ret 0 +end + +note apply one validated change and tell the delta state +fn rtapply op ulong a ulong b ulong to void +old = r1apply op a b +put RTNC 0 add get RTNC 0 1 +put RTSD 0 1 +put RTST 7 add get RTST 7 1 +if eq get RTDB 0 1 +v = b +if eq op 5 +v = 0 +end +if ne dlchg op a v old 0 +put RTDB 0 0 +end +end +end + +fn rtchange op ulong to ulong +a = get RTQ 0 +b = ulong 0 +if ne op 5 +b = get RTQ 1 +end +if lt r1chg op a b get R1N 0 0 +ret 3 +end +rtapply op a b +ret 0 +end + +note BATCH item i: validity against a state of n values (the new n or -1), and application +fn rtitemchk i ulong n ulong to long +q = add 1 mul i 3 +op = get RTQ q +a = get RTQ add q 1 +ret r1chg op a get RTQ add q 2 n +end + +fn rtitem i ulong to void +q = add 1 mul i 3 +op = get RTQ q +a = get RTQ add q 1 +rtapply op a get RTQ add q 2 +end + +fn rtbatch to ulong +m = get RTQ 0 +if or lt m 1 gt m 1024 +ret 3 +end +n = long get R1N 0 +for i ulong m +n = rtitemchk i ulong n +if lt n 0 +ret 3 +end +end +for i ulong m +rtitem i +end +ret 0 +end + +note ---- QUERY ---- +fn rtfull route ulong to ulong +r1full +ret route +end + +fn rtcache to ulong +if eq get RTSD 0 1 +r1sdig +put RTSD 0 0 +end +for j ulong 4 +r1putw mul j 8 r1bswap get R1PD j +r1putw add 40 mul j 8 r1bswap get R1SD j +end +r1putw 32 get R1N 0 +r1hash 72 +for j ulong 4 +put RTKEY j get R1HD j +end +n = get RTCN 0 +for e ulong n +if rtckeq mul e 6 +put R1R 0 get RTCK add mul e 6 4 +put R1R 1 get RTCK add mul e 6 5 +ret 2 +end +end +r1full +h = get RTCH 0 +b = mul h 6 +for j ulong 4 +put RTCK add b j get RTKEY j +end +put RTCK add b 4 get R1R 0 +put RTCK add b 5 get R1R 1 +put RTCH 0 rem add h 1 4096 +if lt n 4096 +put RTCN 0 add n 1 +end +ret 1 +end + +fn rtckeq b ulong to bool +if ne get RTCK b get RTKEY 0 +ret false +end +for j ulong 3 +if ne get RTCK add b add j 1 get RTKEY add j 1 +ret false +end +end +ret true +end + +fn rttap to ulong +t0 = monons +r1full +ns = ulong sub monons t0 +if ne extap get RTSN 0 get RTQN 0 get RTNC 0 ns 0 +exit 12 +end +ret 1 +end + +fn rtmethod to ulong +t = get RTT 0 +if ge t 0 +mtrun ulong t +ret 3 +end +ret rtfull 1 +end + +note the delta path: rebuild after a drop, then dlres; 1 when it could not answer +fn rtdelta to bool +if eq get RTDB 0 0 +if eq dlbind get RTT 0 0 +put RTDB 0 1 +end +end +if eq get RTDB 0 1 +if eq dlres 0 +ret true +end +end +ret false +end + +fn rtselect to ulong +meth = ge get RTT 0 0 +slset meth true +p = slpick get RTNC 0 +if rtseloff +if eq get RTDB 0 1 +dldrop +put RTDB 0 0 +end +end +if eq p 4 +if rtdelta +ret 4 +end +end +if and eq p 3 meth +ret rtmethod +end +if meth +if ne p 1 +ret rtmethod +end +end +ret rtfull 1 +end + +fn rtroute to ulong +a = get RTARM 0 +if eq a 1 +put R1R 0 0 +put R1R 1 0 +ret 0 +end +if eq a 2 +ret rtfull 1 +end +if eq a 3 +ret rtcache +end +if eq a 7 +ret rttap +end +rtrevq +if or rtdead eq get RTBF 0 1 +ret rtfull 5 +end +if eq a 4 +ret rtmethod +end +if eq a 5 +if rtdelta +ret 4 +end +ret rtmethod +end +ret rtselect +end + +fn rtquery to void +put RTQN 0 add get RTQN 0 1 +r = rtroute +put RTNC 0 0 +put RTST r add get RTST r 1 +put RTOW 0 get R1R 0 +put RTOW 1 get R1R 1 +put RTOW 2 r +rtresp 0 3 +end + +fn rtstats to void +for k ulong 14 +put RTOW k get RTST k +end +put RTOW 10 get CKST 0 +put RTOW 11 get CKST 1 +put RTOW 12 get CKST 2 +put RTOW 13 get CKST 4 +for k ulong 12 +put RTOW add 14 k dlstat k +end +put RTOW 26 get SLDIS 0 +put RTOW 27 get SLREF 0 +put RTOW 28 get RTCN 0 +put RTOW 29 mtcount +put RTOW 30 get RTLC 0 +put RTOW 31 get RTFLG 0 +rtresp 0 32 +end + +fn rtend to void +rtresp 0 0 +if eq get RTARM 0 7 +fd = int get EXTFD 0 +r = fsync fd +r = fclose fd +end +exit 0 +end + +note p matches op (K2) +fn rtpok op ulong p ulong to bool +if eq op 1 +if lt p 65 +ret false +end +n = get RTQ 64 +ret and le n 7169 eq p add 65 n +end +if or eq op 3 or eq op 4 eq op 6 +ret eq p 2 +end +if eq op 5 +ret eq p 1 +end +if eq op 7 +if lt p 1 +ret false +end +m = get RTQ 0 +ret and le m 2411 eq p add 1 mul m 3 +end +if eq op 8 +ret eq p 64 +end +ret eq p 0 +end + +fn rtop op ulong p ulong to void +if not rtpok op p +rtstat 4 +ret +end +if eq op 10 +rtend +end +if eq op 9 +rtstats +ret +end +if eq op 1 +rtstat rtbegin +ret +end +if eq get RTSES 0 0 +rtstat 6 +ret +end +if eq op 2 +rtquery +ret +end +if eq op 7 +rtstat rtbatch +ret +end +if eq op 8 +rtstat rtprog +ret +end +rtstat rtchange op +end + +fn rtreq to void +op = rtword +p = rtword +if or or lt op 1 gt op 10 gt p 7234 +rtstat 4 +exit 4 +end +for i ulong p +put RTQ i rtword +end +rtop op p +end + +fn main to int +rtinit +for o ulong 1000 +for i ulong 999999 +rtreq +end +end +ret 4 +end diff --git a/om/reuse/README.md b/om/reuse/README.md new file mode 100644 index 000000000..47598024d --- /dev/null +++ b/om/reuse/README.md @@ -0,0 +1,155 @@ +# om/reuse: REUSE-1 candidate units (omega-mv #742 #743 #744) + +Contract: `docs/reuse/REUSE-1.md` v1.0 (frozen at d6edaad1) and its clarifications +`docs/reuse/REUSE-1-CLARIFICATIONS.md` (K1..). Every file is handwritten seeded Omegese compiled by +the pinned omc (sha256 `734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61`). + +## Files + +| file | role | prefix | +|---|---|---| +| `om/sha256/sha256.om` | SHA-256 (`omsha`), main, unchanged | | +| `r1.om` | profile R1 baseline: programs, lane map, folds, delta, digests | r1 / R1 | +| `exp.om` | experience records: tap writer, ALLOW, admission | ex / EX | +| `check.om` | G3 bind validation (`ckbind`) | ck / CK | +| `delta.om` | incremental execution | dl / DL | +| `select.om` | lifecycle-cost selector | sl / SL | +| `lib.om` | manifest, CURRENT, publish (drive), `lbload` / `lblineage` (router) | lb / LB | +| methods.om | generated by `codegen.om`, never stored in the tree | mt | +| `router.om` | the arm process (`fn main`) | rt / RT | +| `learn.om` | template admission (drive only) | lg / LG | +| `codegen.om` | methods.om source text (drive only) | cg / CG | +| `drive.om` | the build CLI (`note units: sha256 r1 exp lib learn codegen`) | dr / DR | + +A router is the concatenation, in this order: sha256, r1, exp, check, delta, select, lib, methods, +router (a newline is added after a unit that lacks one). + +## Build and run (cwd = the omega-mv repo root) + +```sh +OMC=$HOME/.local/state/reuse/bin/omc # check: sha256sum = 734f0a39...6b61 +nice -n 19 $OMC < tests/reuse/run.om > /tmp/run.elf && chmod +x /tmp/run.elf +/tmp/run.elf compose $OMC om/reuse/drive.om /tmp/drive.elf # drive by its units line +/tmp/drive.elf base $OMC OUTDIR # OUTDIR/base.om, OUTDIR/base.elf +/tmp/drive.elf build $OMC ALLOW CALIB OUTLIB [unit:NAME=PATH]... [mutant:c1|c1b|c1c]... [crashafter:K] EXP... +/tmp/drive.elf selftest +/tmp/run.elf $OMC # every tests/reuse/*.om +/tmp/run.elf $OMC lib-load # one test +``` + +`drive base` prints `base_elf_sha256=<hex>`. `drive build` prints `manifest_sha256=<hex>`, +`generation=<G>`, `templates=<K>`; `<hex>` is the MANSHA the evaluator passes to the router +(`ARM LIBDIR MANSHA ALLOW TAPFILE SPLIT`). + +### drive build inputs + +- `OMC`: the compiler, forked with stdin = gen-G.om.tmp and stdout = gen-G.elf.tmp (`dupfd`), + stderr inherited; killed after 120 s. Its file SHA-256 goes into manifest words 14..17 (lbload + refuses anything but the pinned omc). +- `ALLOW`: `<64 hex> AUTHORIZED|SYNTHETIC|REVOKED <label>` lines (exp.om); read only when EXP files + are given. +- `CALIB`: `-`, or a 1600-byte R1CAL001 file (magic, version 1, words 3..7 zero, K4); copied to + `gen-G.cal` (K1), its SHA-256 in manifest words 52..55. +- `OUTLIB`: created 0700 when missing. G = (G named by CURRENT) + 1, or 1 without CURRENT. +- `EXP...`: 0..8 experience files, each admitted by `exadmit` (any refusal: exit 10). Zero files + give the empty library (K = 0). +- `unit:NAME=PATH` replaces one handwritten unit (sha256 r1 exp check delta select lib router; + not methods), for control mutants. `mutant:` changes codegen only (K10): `c1` mtfind compares AGG + shape ids with the wl byte cleared; `c1b` every wl-16 mtl ends with `v = and v 255`; `c1c` mtl + reads c_j from R1P word 5 + 2j. `crashafter:K` (K 1..9, control C8) stops after publish step K. + +### Template admission (5.3, K3) + +Only TRAIN records (word 9 = 1) are counted; every record is one QUERY; outcome words (5, 6, 7) +are never read. The FSM template is decided first: admitted iff at least 8 FSM TRAIN records. AGG +shapes (r1shape of the record's PROG) are admitted in descending count, ties to the lower shape id, +while count >= 8 and K < 16 (so at most 15 AGG when FSM is admitted). Library order: AGG in +admission order, then FSM. Lineage digests (file SHA-256s) are sorted ascending; a duplicate file +or more than 8 files is exit 2. + +### Generated methods (3.2, 12) + +Per AGG template t: `mtl<t> x` applies the ops in order with c_j = R1P word 4 + 2j (ADD, SUB, MUL, +SHL masked to 2^wl - 1), and `mtr<t>` is the fixed loop `acc = r1ident f; acc = r1fold f acc +(mtl<t> x_i)`. FSM: `mtr<t>` walks R1T[16 q + a] (filled by ckbind with r1delta) and sums +λ = R1P word 6 + q. Then `mtcount`, `mtshape`, `mtfind` (r1valid, then r1shape equality; -1 +otherwise or for an empty library), `mtlane`, `mtrun` (unknown t: r1full), `mtsrc` (SHA-256 of the +template's functions text, embedded as constants). The text depends only on the template list and +the mutant bits. + +## Library files (3.4, K1, K3, K16) + +LIBDIR (0700): `gen-G.om`, `gen-G.elf`, `gen-G.cal` (only with CALIB), `gen-G.man`, `CURRENT`. + +- Manifest: 256 little-endian words: 0 magic 3544386019401740626, 1 version 1, 2 G, 3 parent G, + 4 K, 5 router source bytes, 6..9 source SHA-256, 10..13 ELF SHA-256, 14..17 compiler SHA-256, + 18 profile version 1, 19 L, 20..51 lineage, 52..55 calibration SHA-256 (0 without), 56..183 + templates (shape id, family, wl, f, k, TRAIN QUERY count, bytes of its generated functions, 0; + FSM: 1, 2, 0, 0, 0, count, bytes, 0), 184..254 zero, 255 FNV-1a 64 (basis + 14695981039346656037, prime 1099511628211) over the 2040 LE bytes of words 0..254. + Digests: word j = digest bytes 8j..8j+7 big endian. +- CURRENT: 40 bytes, G (u64 LE) then the 32 SHA-256 bytes of gen-G.man. +- Publish (lbpub, ADR 0015 order), with the steps `crashafter:K` stops after: + 1 gen-G.om.tmp fsync, renamed (renames flag 1: never replaces) to gen-G.om; + 2 the same for gen-G.elf; 3 the same for gen-G.cal (no-op without CALIB); + 4 gen-G.man.tmp written and fsync'd; 5 renamed to gen-G.man; 6 LIBDIR fsync; + 7 CURRENT.tmp written and fsync'd; 8 renamed onto CURRENT; 9 LIBDIR fsync. + After K <= 7 CURRENT still names the previous generation; after K >= 8 it names G. The next + build removes the unpublished leftovers of generation (CURRENT G) + 1 and CURRENT.tmp first. +- Determinism (3.5): the same inputs and the same OUTLIB state give byte-identical gen-G.om, + gen-G.elf, gen-G.cal and gen-G.man (no time, pid or path in any of them). + +## Load (router side, lib.om) + +`lbload` (B started with ARM method/delta/select, LIBDIR not `-`, argc 7) reads argv itself, and +returns only on refusal; on success it execs `/proc/self/fd/N` (a memfd holding gen-G.elf) with +the same arguments plus `loaded`. When the manifest names a calibration (K16), lbload reads +gen-G.cal once, checks the SHA-256 of exactly those bytes against words 52..55, writes the same +bytes into a memfd and leaves it on fd 7 without CLOEXEC; the loaded router reads its calibration +only from `/proc/self/fd/7` (iff LBCAL = 1), never from LIBDIR. Refusal codes: + +| code | check | code | check | +|---|---|---|---| +| 1 | argv count / LIBDIR length | 14 | profile version | +| 2 | MANSHA not 64 hex digits | 15 | K > 16 or L > 8 | +| 3 | CURRENT unreadable | 16 | a reserved manifest word nonzero | +| 4 | CURRENT not 40 bytes | 17 | ALLOW unreadable (only when L > 0) | +| 5 | gen-G.man unreadable | 18 | a lineage digest not allowed (absent or REVOKED) | +| 6 | gen-G.man not 2048 bytes | 19 | gen-G.cal unreadable, or 4096 bytes or more | +| 7 | SHA-256(gen-G.man) differs from CURRENT | 20 | gen-G.cal digest | +| 8 | CURRENT digest differs from MANSHA | 21 | gen-G.elf unreadable | +| 9 | magic | 22 | memfd failed (ELF or calibration), or dup onto fd 7 failed | +| 10 | version | 23 | copy into a memfd failed | +| 11 | self-check | 24 | ELF digest | +| 12 | G differs from CURRENT, or parent G >= G | 25 | exec failed | +| 13 | compiler SHA-256 not the pinned omc | | | + +`lblineage` (same checks 1..16, no ELF) fills LBLD, LBLN, LBGEN, LBCAL (1 iff the manifest names a +calibration) for the revocation checks and the calibration of a loaded generation. + +## drive exit codes + +0 ok, 2 usage (including a malformed CALIB, an unknown mutant, an unreadable unit, a duplicate or +ninth EXP), 10 experience refused (ALLOW unreadable or an EXP not admitted), 11 compile failed (omc +nonzero), 12 publish failed (including an unreadable existing CURRENT), 13 budget exceeded (omc over +120 s, router source over 512 KiB, LIBDIR over 8 MiB, shape table full), 97 deliberate stop after +publish step K. A refused build removes its tmp files and leaves CURRENT unchanged. + +## Tests (`tests/reuse/`, runner `run.om`) + +The first line of each test is `note units: ...`; `run.om` composes the units and the test, +compiles with OMC, runs it with argv 1 = a fresh temp dir, argv 2 = OMC, argv 3 = a drive ELF it +built from `om/reuse/drive.om`, 120 s each, and prints one PASS/FAIL line per test and a summary +(exit 0 iff all passed). Work files: `/tmp/reuse-test-run-PID/` (left for inspection). + +| test | covers | +|---|---| +| lib-load | every lbload refusal 1..21, 24 as a mutant of a good library, C6 coherent replacement, lblineage; K16: a generation that overwrites LIBDIR/gen-1.cal still reads exactly the verified bytes from fd 7 | +| lib-publish | FNV-1a known answer (computed outside Omega), manifest round trip, CURRENT layout, no replacement, lbpub steps 1..9 | +| learn-select | 5.3 / K3 on experience files admitted by exp.om | +| codegen-run | generated methods compiled and compared with r1full / r1lane on random programs; c1, c1b, c1c caught; empty library | +| drive-publish | drive build output, manifest fields, determinism, load, crashafter 1..9, every drive refusal | +| drive-smoke | built routers on the arm protocol: method, delta, select, full, wrong MANSHA, c1c refused by G3, drive base | + +Not tested: the 120 s omc budget (a test would take 120 s), and lbload codes 22, 23, 25 (memfd, +copy and exec failures cannot be provoked without changing the host). diff --git a/om/reuse/check.om b/om/reuse/check.om new file mode 100644 index 000000000..6959ceca6 --- /dev/null +++ b/om/reuse/check.om @@ -0,0 +1,94 @@ +note SPDX-License-Identifier: Apache-2.0 +note check.om: G3 bind validation (docs/reuse/REUSE-1.md section 3.3). Concatenated after exp.om; +note calls mtlane of the generated methods.om (or a test's fixture). +note ckbind t 0 pass, 1 fail. AGG: every x < 2^wl of R1P has mtlane t x = r1lane x, all 64 bits. +note FSM: fills R1T[s*16 + a] = r1delta s a for s < S, a < A; nothing else is checked. +note Sets R1PD (r1pdig). A passed AGG (program digest, t) enters a FIFO of 256 entries +note (process memory only); a later bind of the same pair skips the lane comparison. +note CKST counters: 0 binds, 1 failures, 2 FIFO hits, 3 full lane comparisons, 4 lane pairs +note compared +global CKFIFO ulong 1280 +global CKFN ulong 1 +global CKFH ulong 1 +global CKST ulong 5 + +fn ckinc k ulong n ulong to void +put CKST k add get CKST k n +end + +note entry e of the FIFO holds R1PD and t +fn ckeq e ulong t ulong to bool +b = mul e 5 +for j ulong 4 +if ne get CKFIFO add b j get R1PD j +ret false +end +end +ret eq get CKFIFO add b 4 t +end + +fn ckhas t ulong to bool +n = get CKFN 0 +for e ulong n +if ckeq e t +ret true +end +end +ret false +end + +fn ckadd t ulong to void +e = get CKFH 0 +b = mul e 5 +for j ulong 4 +put CKFIFO add b j get R1PD j +end +put CKFIFO add b 4 t +put CKFH 0 rem add e 1 256 +if lt get CKFN 0 256 +put CKFN 0 add get CKFN 0 1 +end +end + +note the lane comparison over x < 2^wl: 0 equal, 1 the first difference +fn cklanes t ulong to int +lim = shl ulong 1 get R1P 2 +ckinc 3 1 +for x ulong lim +if ne mtlane t x r1lane x +ckinc 4 add x 1 +ret 1 +end +end +ckinc 4 lim +ret 0 +end + +fn ckfsm to void +ns = get R1P 2 +na = get R1P 3 +for s ulong ns +for a ulong na +put R1T add mul s 16 a r1delta s a +end +end +end + +fn ckbind t ulong to int +ckinc 0 1 +r1pdig +if eq get R1P 1 2 +ckfsm +ret 0 +end +if ckhas t +ckinc 2 1 +ret 0 +end +if ne cklanes t 0 +ckinc 1 1 +ret 1 +end +ckadd t +ret 0 +end diff --git a/om/reuse/codegen.om b/om/reuse/codegen.om new file mode 100644 index 000000000..23ad4bf7d --- /dev/null +++ b/om/reuse/codegen.om @@ -0,0 +1,532 @@ +note SPDX-License-Identifier: Apache-2.0 +note codegen.om (drive.om only, cg prefix): the methods.om source text of REUSE-1 contract 3.2 and +note 12 for the templates in CGSH[0..CGK) (AGG shape ids in admission order, then FSM = 1, K3). +note Needs sha256 (omsha) before it. Output: CGO[0..CGN); per template t the bytes of its +note functions (mtl<t> and mtr<t> for AGG, mtr<t> for FSM) in CGSB[t] and their SHA-256 in +note CGSD[4t..4t+4) (words big endian, contract 1.1), embedded again by mtsrc. Deterministic: the +note text depends only on CGK, CGSH and CGMUT. Control mutants (CGMUT bits, context decision 1, K10): +note 1 = c1 (mtfind compares AGG shape ids with the wl byte cleared), 2 = c1b (every wl-16 mtl ends +note with v = and v 255), 4 = c1c (mtl reads c_j from R1P word 5 + 2j instead of 4 + 2j). +global CGO uchar 1048576 +global CGN ulong 1 +global CGK ulong 1 +global CGSH ulong 16 +global CGMUT ulong 1 +global CGSB ulong 16 +global CGSD ulong 64 +text CGTHDR "note SPDX-License-Identifier: Apache-2.0\nnote methods.om: generated by om/reuse/codegen.om (REUSE-1 contract 3.2 and 12). Do not edit.\n" +text CGTL1 "fn mtl" +text CGTL2 " x ulong to ulong\nv = x\n" +text CGTO1 "v = and add v get R1P " +text CGTO2 "v = and sub v get R1P " +text CGTO3 "v = and mul v get R1P " +text CGTO4 "v = and v get R1P " +text CGTO5 "v = or v get R1P " +text CGTO6 "v = xor v get R1P " +text CGTO7 "v = and shl v get R1P " +text CGTO8 "v = shr v get R1P " +text CGTB "v = and v 255\n" +text CGTRV "ret v\nend\n" +text CGTR1 "fn mtr" +text CGTA2 " to void\nf = get R1P 3\nn = get R1N 0\nacc = r1ident f\nfor i ulong n\nacc = r1fold f acc mtl" +text CGTA3 " get R1X i\nend\nput R1R 0 acc\nput R1R 1 n\nend\n" +text CGTF2 " to void\nq = get R1P 4\nn = get R1N 0\nsum = ulong 0\nfor i ulong n\nq = get R1T add mul q 16 get R1X i\nsum = add sum get R1P add 6 q\nend\nput R1R 0 q\nput R1R 1 sum\nend\n" +text CGTC "fn mtcount to ulong\nret " +text CGTNE "\nend\n" +text CGTS1 "fn mtshape t ulong to ulong\n" +text CGTIF "if eq t " +text CGTRET "ret " +text CGTE "end\n" +text CGTR0 "ret 0\nend\n" +text CGTF1 "fn mtfind to long\nif ne r1valid 0\nret -1\nend\ns = r1shape\n" +text CGTIFS "if eq s " +text CGTIFM "if eq and s 18446744073709551360 " +text CGTRM1 "ret -1\nend\n" +text CGTLN1 "fn mtlane t ulong x ulong to ulong\n" +text CGTRML "ret mtl" +text CGTLN2 " x\nend\n" +text CGTRN1 "fn mtrun t ulong to void\n" +text CGTMTR "mtr" +text CGTRN2 "\nret\nend\n" +text CGTRN3 "r1full\nend\n" +text CGTD1 "fn mtsrc t ulong D edit ulong 4 to void\n" +text CGTPD "put D " +text CGTRT "ret\nend\n" +text CGTDZ "put D 0 0\nput D 1 0\nput D 2 0\nput D 3 0\nend\n" + +fn cgc c ulong to void +n = get CGN 0 +put CGO n uchar c +put CGN 0 add n 1 +end + +fn cgnum v ulong to void +d = new uchar 24 0 +n = ulong 0 +x = v +while true bound 24 +put d n uchar add rem x 10 48 +n = add n 1 +x = div x 10 +if eq x 0 +break +end +end +while gt n 0 bound 24 +n = sub n 1 +cgc ulong get d n +end +end + +note fixed text k (see the table in cgtx2 / cgtx3 for k above 16) +fn cgtx k ulong to void +if gt k 16 +cgtx2 k +ret +end +if eq k 1 +for i ulong len CGTHDR +cgc ulong get CGTHDR i +end +end +if eq k 2 +for i ulong len CGTL1 +cgc ulong get CGTL1 i +end +end +if eq k 3 +for i ulong len CGTL2 +cgc ulong get CGTL2 i +end +end +if eq k 4 +for i ulong len CGTO1 +cgc ulong get CGTO1 i +end +end +if eq k 5 +for i ulong len CGTO2 +cgc ulong get CGTO2 i +end +end +if eq k 6 +for i ulong len CGTO3 +cgc ulong get CGTO3 i +end +end +if eq k 7 +for i ulong len CGTO4 +cgc ulong get CGTO4 i +end +end +if eq k 8 +for i ulong len CGTO5 +cgc ulong get CGTO5 i +end +end +if eq k 9 +for i ulong len CGTO6 +cgc ulong get CGTO6 i +end +end +if eq k 10 +for i ulong len CGTO7 +cgc ulong get CGTO7 i +end +end +if eq k 11 +for i ulong len CGTO8 +cgc ulong get CGTO8 i +end +end +if eq k 12 +for i ulong len CGTB +cgc ulong get CGTB i +end +end +if eq k 13 +for i ulong len CGTRV +cgc ulong get CGTRV i +end +end +if eq k 14 +for i ulong len CGTR1 +cgc ulong get CGTR1 i +end +end +if eq k 15 +for i ulong len CGTA2 +cgc ulong get CGTA2 i +end +end +if eq k 16 +for i ulong len CGTA3 +cgc ulong get CGTA3 i +end +end +end + +fn cgtx2 k ulong to void +if gt k 32 +cgtx3 k +ret +end +if eq k 17 +for i ulong len CGTF2 +cgc ulong get CGTF2 i +end +end +if eq k 18 +for i ulong len CGTC +cgc ulong get CGTC i +end +end +if eq k 19 +for i ulong len CGTNE +cgc ulong get CGTNE i +end +end +if eq k 20 +for i ulong len CGTS1 +cgc ulong get CGTS1 i +end +end +if eq k 21 +for i ulong len CGTIF +cgc ulong get CGTIF i +end +end +if eq k 22 +for i ulong len CGTRET +cgc ulong get CGTRET i +end +end +if eq k 23 +for i ulong len CGTE +cgc ulong get CGTE i +end +end +if eq k 24 +for i ulong len CGTR0 +cgc ulong get CGTR0 i +end +end +if eq k 25 +for i ulong len CGTF1 +cgc ulong get CGTF1 i +end +end +if eq k 26 +for i ulong len CGTIFS +cgc ulong get CGTIFS i +end +end +if eq k 27 +for i ulong len CGTIFM +cgc ulong get CGTIFM i +end +end +if eq k 28 +for i ulong len CGTRM1 +cgc ulong get CGTRM1 i +end +end +if eq k 29 +for i ulong len CGTLN1 +cgc ulong get CGTLN1 i +end +end +if eq k 30 +for i ulong len CGTRML +cgc ulong get CGTRML i +end +end +if eq k 31 +for i ulong len CGTLN2 +cgc ulong get CGTLN2 i +end +end +if eq k 32 +for i ulong len CGTRN1 +cgc ulong get CGTRN1 i +end +end +end + +fn cgtx3 k ulong to void +if eq k 33 +for i ulong len CGTMTR +cgc ulong get CGTMTR i +end +end +if eq k 34 +for i ulong len CGTRN2 +cgc ulong get CGTRN2 i +end +end +if eq k 35 +for i ulong len CGTRN3 +cgc ulong get CGTRN3 i +end +end +if eq k 36 +for i ulong len CGTD1 +cgc ulong get CGTD1 i +end +end +if eq k 37 +for i ulong len CGTPD +cgc ulong get CGTPD i +end +end +if eq k 38 +for i ulong len CGTRT +cgc ulong get CGTRT i +end +end +if eq k 39 +for i ulong len CGTDZ +cgc ulong get CGTDZ i +end +end +end + +fn cgbswap x ulong to ulong +y = or shl and x 71777214294589695 8 and shr x 8 71777214294589695 +y = or shl and y 281470681808895 16 and shr y 16 281470681808895 +ret or shl y 32 shr y 32 +end + +note CGO[p..p+n) into buf +fn cgfill buf edit uchar 4096 p ulong n ulong to void +for i ulong n +put buf i get CGO add p i +end +end + +note SHA-256 of CGO[a..b) into CGSD[4t..4t+4) +fn cghash a ulong b ulong t ulong to void +c = new uchar 104 0 +buf = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c buf 0 0 w b8 +p = a +while lt p b bound 999999 +n = sub b p +if gt n 4096 +n = 4096 +end +cgfill buf p n +omsha c buf n 1 w b8 +p = add p n +end +omsha c buf 0 2 w b8 +for j ulong 4 +put CGSD add mul t 4 j cgbswap get w j +end +end + +note one AGG step: op (1..8), constant word cw, mask m +fn cgop op ulong cw ulong m ulong to void +cgtx add op 3 +cgnum cw +if or le op 3 eq op 7 +cgc 32 +cgnum m +end +cgc 10 +end + +note the functions of template t (shape s) +fn cgtmpl t ulong s ulong to void +if eq s 1 +cgtx 14 +cgnum t +cgtx 17 +ret +end +mut = get CGMUT 0 +wl = and s 255 +m = ulong 255 +if eq wl 16 +m = 65535 +end +cgtx 2 +cgnum t +cgtx 3 +nk = and shr s 12 15 +for j ulong nk +op = and shr s add 16 mul j 4 15 +cw = add 6 mul j 2 +if ne and mut 4 0 +cw = add cw 1 +end +cgop op cw m +end +if and eq wl 16 ne and mut 2 0 +cgtx 12 +end +cgtx 13 +cgtx 14 +cgnum t +cgtx 15 +cgnum t +cgtx 16 +end + +fn cgshapes k ulong to void +cgtx 20 +for t ulong k +cgtx 21 +cgnum t +cgc 10 +cgtx 22 +cgnum get CGSH t +cgc 10 +cgtx 23 +end +cgtx 24 +end + +fn cgfind k ulong to void +mut = get CGMUT 0 +cgtx 25 +for t ulong k +s = get CGSH t +if or eq s 1 eq and mut 1 0 +cgtx 26 +cgnum s +else +cgtx 27 +cgnum and s 18446744073709551360 +end +cgc 10 +cgtx 22 +cgnum t +cgc 10 +cgtx 23 +end +cgtx 28 +end + +fn cglane k ulong to void +cgtx 29 +for t ulong k +if ne get CGSH t 1 +cgtx 21 +cgnum t +cgc 10 +cgtx 30 +cgnum t +cgtx 31 +end +end +cgtx 24 +end + +fn cgrun k ulong to void +cgtx 32 +for t ulong k +cgtx 21 +cgnum t +cgc 10 +cgtx 33 +cgnum t +cgtx 34 +end +cgtx 35 +end + +fn cgsrc k ulong to void +cgtx 36 +for t ulong k +cgtx 21 +cgnum t +cgc 10 +for j ulong 4 +cgtx 37 +cgnum j +cgc 32 +cgnum get CGSD add mul t 4 j +cgc 10 +end +cgtx 38 +end +cgtx 39 +end + +note generate methods.om into CGO; 0 ok, 1 bad input (K > 16 or a shape that is neither FSM nor a +note well-formed AGG shape id) +fn cggen to int +k = get CGK 0 +if gt k 16 +ret 1 +end +for t ulong k +if ne cgcheck get CGSH t 0 +ret 1 +end +end +put CGN 0 0 +cgtx 1 +for t ulong k +st = get CGN 0 +cgtmpl t get CGSH t +put CGSB t sub get CGN 0 st +cghash st get CGN 0 t +end +for t ulong 16 +if ge t k +put CGSB t 0 +for j ulong 4 +put CGSD add mul t 4 j 0 +end +end +end +cgtx 18 +cgnum k +cgtx 19 +cgshapes k +cgfind k +cglane k +cgrun k +cgsrc k +ret 0 +end + +note 0 when s is FSM (1) or an AGG shape id wl + 256 f + 4096 k + 65536 sum op_j 16^(j-1) +note with wl 8 or 16, f 1..6, k 1..6, op_j 1..8 for j <= k and no other bits +fn cgcheck s ulong to int +if eq s 1 +ret 0 +end +wl = and s 255 +f = and shr s 8 15 +nk = and shr s 12 15 +if and ne wl 8 ne wl 16 +ret 1 +end +if or lt f 1 gt f 6 +ret 1 +end +if or lt nk 1 gt nk 6 +ret 1 +end +ops = shr s 16 +for j ulong 6 +op = and shr ops mul j 4 15 +if lt j nk +if or lt op 1 gt op 8 +ret 1 +end +end +if and ge j nk ne op 0 +ret 1 +end +end +if ne shr ops 24 0 +ret 1 +end +ret 0 +end diff --git a/om/reuse/delta.om b/om/reuse/delta.om new file mode 100644 index 000000000..1ff439ee4 --- /dev/null +++ b/om/reuse/delta.om @@ -0,0 +1,539 @@ +note SPDX-License-Identifier: Apache-2.0 +note delta.om: REUSE-1 incremental execution (#743), contract docs/reuse/REUSE-1.md v1.0 sections 4, 12. +note Unit order: sha256 r1 exp check delta select lib methods router. Names: dl / DL only. +note Reads R1P R1N R1X R1T, writes R1R (dlres only). Calls r1valid r1lane r1ident r1delta and, +note for a bound AGG method, mtlane t x; a bound FSM method reads R1T[s*16 + a] (filled by ckbind). +note +note Router protocol (agreed with CandCore): +note BEGIN after R1P R1N R1X are set: dlsreset, then dlbind t (t = template index, -1 = baseline) +note PROGRAM after R1P is replaced: dldrop, then dlbind t +note change the router validates the whole request (s1.3), then for each item in order applies +note exactly that item to R1X / R1N and calls dlchg op i v old before the next item +note SET 3 i v old = previous x_i INS 4 i v old unused (0) +note DEL 5 i 0 old = the removed value SWAP 6 i v = j, old = x_j before the swap (unused) +note AGG uses v and old (SWAP no work); FSM reads the symbols from R1X and compares new q_t with +note the stored trajectory (s4), so it needs neither. FSM SWAP = SET at min(i, j), then SET at max. +note (The other order is also correct: the stored trajectory is a valid chain under the current +note symbols everywhere except at changed positions, so every cutoff is sound; the reversed order +note passed both delta test suites when tried as a mutant.) +note QUERY dlres: 0 = R1R holds r0 r1 from the delta state, 1 = no usable state (serve full) +note dlbind returns 0 bound, 1 refused (PROG invalid or family unknown). dlbind on a session that is +note already bound with the same t is a no-op, so dldrop before a rebind is mandatory (C4 relies on it). +note dlchg returns 0 tracked, 1 delta state dropped (unbound until the next dlbind). +note +note Delta state (s4), 557,488 bytes of DL globals per process, one session (<= 1 MiB, s3.5): +note DLC 65536 words MIN/MAX count per mapped value (2^wl used) +note DLQ 4097 words FSM trajectory q_0..q_n +note DLBC 16 words OR/AND count per bit +note DLL 16 words FSM lambda copy; scalars below +note Worst cases per change: AGG ADD/XOR/OR/AND O(1) (16 bit counts); MIN/MAX one extreme scan of +note at most 2^wl - 1 counts when the last copy of the extreme goes; FSM SET at i recomputes at most +note n - i steps, INS at most n + 1 - i plus a suffix shift of at most n + 1 words, DEL at most n - i - 1 +note plus a shift of at most n words; SWAP two SETs. dlbind: one pass +note over the state plus, for MIN/MAX, clearing 2^wl counts. +note +note Counters (diagnostic, process-cumulative, never reset): dlstat k +note 0 changes tracked 1 invalidations 2 scan steps 3 FSM recompute units 4 cutoffs found +note 5 cutoff misses 6 recomputed steps 7 longest recompute 8 binds 9 drops 10 dlres served +note 11 dlres refused +note Select churn (s5.2): dlinvn = invalidations (MIN/MAX extreme scans, and every drop of live +note delta state: dldrop at PROGRAM, dlchg or dlres finding the state unusable; never twice for one), +note dlmissn = FSM recompute units that ran to n without a cutoff (at least one step recomputed). + +global DLB ulong 1 +global DLTB long 1 +global DLF ulong 1 +global DLFO ulong 1 +global DLW ulong 1 +global DLN ulong 1 +global DLA ulong 1 +global DLU ulong 1 +global DLS ulong 1 +global DLBC ulong 16 +global DLL ulong 16 +global DLC ulong 65536 +global DLQ ulong 4097 +global DLST ulong 12 + +fn dlbump k ulong v ulong to void +put DLST k add get DLST k v +end + +fn dlstat k ulong to ulong +if ge k 12 +ret 0 +end +ret get DLST k +end + +fn dlinvn to ulong +ret get DLST 1 +end + +fn dlmissn to ulong +ret get DLST 5 +end + +note mapped lane: the bound method's lane map, or the baseline's +fn dlmap x ulong to ulong +t = get DLTB 0 +if ge t 0 +ret mtlane ulong t x +end +ret r1lane x +end + +note one FSM step: the bound method's table, or the baseline delta +fn dlstep s ulong a ulong to ulong +if eq get DLU 0 1 +ret get R1T add mul s 16 a +end +ret r1delta s a +end + +fn dllam s ulong to ulong +ret get DLL s +end + +note MIN/MAX: the last copy of the extreme went; scan to the next nonzero count +fn dlscan to void +dlbump 1 1 +if eq get DLN 0 0 +put DLA 0 0 +ret +end +a = get DLA 0 +if eq get DLFO 0 3 +top = shl ulong 1 get DLW 0 +st = add a 1 +for j ulong sub top st +v = add st j +if ne get DLC v 0 +put DLA 0 v +dlbump 2 add j 1 +ret +end +end +dlbump 2 sub top st +ret +end +for j ulong a +v = sub sub a 1 j +if ne get DLC v 0 +put DLA 0 v +dlbump 2 add j 1 +ret +end +end +dlbump 2 a +end + +fn dlbitadd y ulong d ulong to void +for b ulong 16 +if ne and shr y b 1 0 +put DLBC b add get DLBC b d +end +end +end + +fn dlbitsub y ulong to void +for b ulong 16 +if ne and shr y b 1 0 +put DLBC b sub get DLBC b 1 +end +end +end + +note add one mapped lane y to the AGG delta state +fn dladd y ulong to void +f = get DLFO 0 +n = get DLN 0 +a = get DLA 0 +if eq f 1 +put DLA 0 add a y +end +if eq f 2 +put DLA 0 xor a y +end +if eq f 3 +put DLC y add get DLC y 1 +if or eq n 0 lt y a +put DLA 0 y +end +end +if eq f 4 +put DLC y add get DLC y 1 +if or eq n 0 gt y a +put DLA 0 y +end +end +if ge f 5 +dlbitadd y 1 +end +put DLN 0 add n 1 +end + +note remove one mapped lane y from the AGG delta state +fn dlrem y ulong to void +f = get DLFO 0 +a = get DLA 0 +put DLN 0 sub get DLN 0 1 +if eq f 1 +put DLA 0 sub a y +end +if eq f 2 +put DLA 0 xor a y +end +if ge f 5 +dlbitsub y +end +if or eq f 3 eq f 4 +c = sub get DLC y 1 +put DLC y c +if and eq c 0 eq y a +dlscan +end +end +end + +fn dlbagg to void +f = get R1P 3 +put DLFO 0 f +put DLW 0 get R1P 2 +put DLN 0 0 +put DLA 0 0 +for b ulong 16 +put DLBC b 0 +end +if or eq f 3 eq f 4 +top = shl ulong 1 get DLW 0 +for v ulong top +put DLC v 0 +end +end +n = get R1N 0 +for i ulong n +dladd dlmap get R1X i +end +end + +fn dlbfsm to void +for s ulong 16 +put DLL s get R1P add 6 s +end +q = get R1P 4 +put DLQ 0 q +sum = ulong 0 +n = get R1N 0 +for i ulong n +q = dlstep q get R1X i +put DLQ add i 1 q +sum = add sum dllam q +end +put DLS 0 sum +put DLN 0 n +end + +fn dlbind t long to int +if and eq get DLB 0 1 eq get DLTB 0 t +ret 0 +end +put DLB 0 0 +if ne r1valid 0 +ret 1 +end +fam = get R1P 1 +put DLF 0 fam +put DLTB 0 t +put DLU 0 0 +if eq fam 1 +dlbagg +end +if eq fam 2 +if ge t 0 +put DLU 0 1 +end +dlbfsm +end +if or eq fam 1 eq fam 2 +put DLB 0 1 +dlbump 8 1 +ret 0 +end +ret 1 +end + +note a drop of LIVE delta state is one invalidation (dlinvn); dropping an unbound state counts none +fn dldrop to void +if eq get DLB 0 1 +dlbump 1 1 +end +put DLB 0 0 +dlbump 9 1 +end + +fn dlsreset to void +put DLB 0 0 +put DLTB 0 -1 +end + +note record one FSM recompute unit of sl steps; cut 1 = a cutoff was found +fn dlfrec sl ulong cut ulong to void +dlbump 3 1 +dlbump 6 sl +if gt sl get DLST 7 +put DLST 7 sl +end +if eq cut 1 +dlbump 4 1 +end +if and eq cut 0 gt sl 0 +dlbump 5 1 +end +end + +note FSM: w_(i+1) changed (x_i); recompute from t = i+1, stop at the first equal stored q_t +fn dlfset i ulong to void +n = get DLN 0 +q = get DLQ i +sum = get DLS 0 +t = add i 1 +cut = ulong 0 +sl = ulong 0 +while le t n bound 4097 +nq = dlstep q get R1X sub t 1 +sl = add sl 1 +old = get DLQ t +if eq nq old +cut = 1 +break +end +sum = add sub sum dllam old dllam nq +put DLQ t nq +q = nq +t = add t 1 +end +put DLS 0 sum +dlfrec sl cut +end + +note FSM INS at i (R1X already holds the insertion): new q_t is compared with the stored state of the +note same symbol suffix, old q_(t-1); at the first equality the stored suffix shifts up by one +fn dlfins i ulong to void +n = get DLN 0 +q = get DLQ i +po = q +sum = get DLS 0 +t = add i 1 +cut = ulong 0 +sl = ulong 0 +while le t add n 1 bound 4098 +nq = dlstep q get R1X sub t 1 +sl = add sl 1 +if eq nq po +cut = 1 +break +end +sum = add sum dllam nq +if le t n +nx = get DLQ t +sum = sub sum dllam nx +po = nx +end +put DLQ t nq +q = nq +t = add t 1 +end +if eq cut 1 +sum = add sum dllam po +s = n +while ge s t bound 4097 +put DLQ add s 1 get DLQ s +if eq s 0 +break +end +s = sub s 1 +end +put DLQ t po +end +put DLS 0 sum +put DLN 0 add n 1 +dlfrec sl cut +end + +note FSM DEL at i (R1X already shifted down): new q_t is compared with old q_(t+1); at cutoff t (or +note t = n with no cutoff) old q_t leaves the sum and the stored suffix from t+1 shifts down by one +fn dlfdel i ulong to void +n = get DLN 0 +q = get DLQ i +sum = get DLS 0 +t = add i 1 +cut = ulong 0 +sl = ulong 0 +while lt t n bound 4097 +nq = dlstep q get R1X sub t 1 +sl = add sl 1 +if eq nq get DLQ add t 1 +cut = 1 +break +end +sum = add sub sum dllam get DLQ t dllam nq +put DLQ t nq +q = nq +t = add t 1 +end +sum = sub sum dllam get DLQ t +s = t +while lt s n bound 4097 +put DLQ s get DLQ add s 1 +s = add s 1 +end +put DLS 0 sum +put DLN 0 sub n 1 +dlfrec sl cut +end + +fn dlfchg op ulong i ulong v ulong to int +n = get DLN 0 +if eq op 3 +if ge i n +ret 1 +end +dlfset i +ret 0 +end +if eq op 4 +if gt i n +ret 1 +end +dlfins i +ret 0 +end +if eq op 5 +if ge i n +ret 1 +end +dlfdel i +ret 0 +end +if eq op 6 +if or ge i n ge v n +ret 1 +end +if lt i v +dlfset i +dlfset v +end +if gt i v +dlfset v +dlfset i +end +ret 0 +end +ret 1 +end + +fn dlachg op ulong i ulong v ulong old ulong to int +n = get DLN 0 +if eq op 3 +if ge i n +ret 1 +end +dladd dlmap v +dlrem dlmap old +ret 0 +end +if eq op 4 +if gt i n +ret 1 +end +dladd dlmap v +ret 0 +end +if eq op 5 +if ge i n +ret 1 +end +dlrem dlmap old +ret 0 +end +if eq op 6 +if or ge i n ge v n +ret 1 +end +ret 0 +end +ret 1 +end + +fn dlchg op ulong i ulong v ulong old ulong to int +if eq get DLB 0 0 +ret 1 +end +r = int 1 +if eq get DLF 0 1 +r = dlachg op i v old +end +if eq get DLF 0 2 +r = dlfchg op i v +end +if eq r 0 +if eq get DLN 0 get R1N 0 +dlbump 0 1 +ret 0 +end +end +put DLB 0 0 +dlbump 1 1 +ret 1 +end + +fn dlragg to void +f = get DLFO 0 +n = get DLN 0 +r = get DLA 0 +if or eq f 3 eq f 4 +if eq n 0 +r = r1ident f +end +end +if ge f 5 +r = r1ident f +if gt n 0 +r = 0 +for b ulong 16 +c = get DLBC b +if and eq f 5 gt c 0 +r = or r shl ulong 1 b +end +if and eq f 6 eq c n +r = or r shl ulong 1 b +end +end +end +end +put R1R 0 r +end + +fn dlres to int +if eq get DLB 0 0 +dlbump 11 1 +ret 1 +end +if ne get DLN 0 get R1N 0 +put DLB 0 0 +dlbump 1 1 +dlbump 11 1 +ret 1 +end +if eq get DLF 0 1 +dlragg +put R1R 1 get DLN 0 +end +if eq get DLF 0 2 +put R1R 0 get DLQ get DLN 0 +put R1R 1 get DLS 0 +end +dlbump 10 1 +ret 0 +end diff --git a/om/reuse/drive.om b/om/reuse/drive.om new file mode 100644 index 000000000..8390aa5fc --- /dev/null +++ b/om/reuse/drive.om @@ -0,0 +1,1276 @@ +note units: sha256 r1 exp lib learn codegen +note SPDX-License-Identifier: Apache-2.0 +note drive.om: builds REUSE-1 routers (contract 2, 3.4, 3.5, 5.3, 12; clarifications K1 K3 K10). +note cwd = the omega-mv repo root; unit sources om/sha256/sha256.om and om/reuse/NAME.om. +note drive base OMC OUTDIR +note OUTDIR/base.om, OUTDIR/base.elf (router with an empty library); prints base_elf_sha256=HEX +note drive build OMC ALLOW CALIB OUTLIB [unit:NAME=PATH]... [mutant:c1|c1b|c1c]... [crashafter:K] EXP... +note admits EXP (exp.om), selects templates (learn.om), generates methods.om (codegen.om), +note composes gen-G.om, compiles it with OMC (stdin/stdout by dupfd, 120 s budget), copies CALIB +note ('-' = none) to gen-G.cal and publishes generation G in OUTLIB (lib.om lbpub, ADR 0015 order); +note prints manifest_sha256=HEX, generation=G, templates=K +note drive selftest +note Exit: 0 ok, 2 usage, 10 experience refused, 11 compile failed, 12 publish failed, +note 13 budget exceeded (omc over 120 s, router source over 512 KiB, LIBDIR over 8 MiB, shape table +note full), 97 deliberate stop after publish step K (crashafter, control C8 only). +note Unit order: sha256 r1 exp check delta select lib methods router (methods = generated). +global DRA uchar 4096 +global DRAN ulong 1 +global DRO uchar 8192 +global DRON ulong 1 +global DRUP uchar 36864 +global DRUL ulong 9 +global DREI ulong 16 +global DREN ulong 1 +global DRCRASH ulong 1 +global DROMC uchar 4096 +global DROMN ulong 1 +global DRCP uchar 4096 +global DRCN ulong 1 +global DRPI uchar 4096 +global DRPIN ulong 1 +global DRPO uchar 4096 +global DRPON ulong 1 +global DRPK uchar 4096 +global DRST uchar 4 +global DRIO uchar 65536 +global DRLB ulong 1 +global DRCAL ulong 200 +text DRT1 "base_elf_sha256=" +text DRT2 "manifest_sha256=" +text DRT3 "generation=" +text DRT4 "templates=" +text DRT5 "usage: drive base OMC OUTDIR | drive build OMC ALLOW CALIB OUTLIB [unit:NAME=PATH]... [mutant:c1|c1b|c1c]... [crashafter:K] EXP... | drive selftest\n" +text DRT6 "drive: experience refused: " +text DRT7 " code=" +text DRT8 "drive: compile failed rc=" +text DRT9 "drive: omc over the 120 s budget, killed\n" +text DRT10 "drive: publish failed code=" +text DRT11 "selftest PASS\n" +text DRT12 "selftest FAIL " +text DRT13 "drive: budget exceeded: " +text DRT14 "sha256 r1 exp check delta select lib methods router" +text DRT15 "om/sha256/sha256.om" +text DRT16 "om/reuse/" +text DRT17 "unit:" +text DRT18 "mutant:" +text DRT19 "crashafter:" +text DRT20 "c1" +text DRT21 "c1b" +text DRT22 "c1c" +text DRT23 "build" +text DRT24 "base" +text DRT25 "selftest" +text DRT26 "-" +text DRT27 "base.om" +text DRT28 "base.elf" +text DRT29 "drive: usage error " +text DRT30 "router source\n" +text DRT31 "LIBDIR bytes\n" +text DRT32 "shape table\n" +text DRT33 "abc" + +note byte i and length of fixed text k +fn drtl k ulong to ulong +if eq k 1 +ret len DRT1 +end +if eq k 2 +ret len DRT2 +end +if eq k 3 +ret len DRT3 +end +if eq k 4 +ret len DRT4 +end +if eq k 5 +ret len DRT5 +end +if eq k 6 +ret len DRT6 +end +if eq k 7 +ret len DRT7 +end +if eq k 8 +ret len DRT8 +end +if eq k 9 +ret len DRT9 +end +if eq k 10 +ret len DRT10 +end +if eq k 11 +ret len DRT11 +end +if eq k 12 +ret len DRT12 +end +if eq k 13 +ret len DRT13 +end +if eq k 14 +ret len DRT14 +end +if eq k 15 +ret len DRT15 +end +if eq k 16 +ret len DRT16 +end +ret drtl2 k +end + +fn drtl2 k ulong to ulong +if eq k 17 +ret len DRT17 +end +if eq k 18 +ret len DRT18 +end +if eq k 19 +ret len DRT19 +end +if eq k 20 +ret len DRT20 +end +if eq k 21 +ret len DRT21 +end +if eq k 22 +ret len DRT22 +end +if eq k 23 +ret len DRT23 +end +if eq k 24 +ret len DRT24 +end +if eq k 25 +ret len DRT25 +end +if eq k 26 +ret len DRT26 +end +if eq k 27 +ret len DRT27 +end +if eq k 28 +ret len DRT28 +end +if eq k 29 +ret len DRT29 +end +if eq k 30 +ret len DRT30 +end +if eq k 31 +ret len DRT31 +end +if eq k 32 +ret len DRT32 +end +if eq k 33 +ret len DRT33 +end +ret 0 +end + +fn drtc k ulong i ulong to ulong +if eq k 1 +ret ulong get DRT1 i +end +if eq k 2 +ret ulong get DRT2 i +end +if eq k 3 +ret ulong get DRT3 i +end +if eq k 4 +ret ulong get DRT4 i +end +if eq k 5 +ret ulong get DRT5 i +end +if eq k 6 +ret ulong get DRT6 i +end +if eq k 7 +ret ulong get DRT7 i +end +if eq k 8 +ret ulong get DRT8 i +end +if eq k 9 +ret ulong get DRT9 i +end +if eq k 10 +ret ulong get DRT10 i +end +if eq k 11 +ret ulong get DRT11 i +end +if eq k 12 +ret ulong get DRT12 i +end +if eq k 13 +ret ulong get DRT13 i +end +if eq k 14 +ret ulong get DRT14 i +end +if eq k 15 +ret ulong get DRT15 i +end +if eq k 16 +ret ulong get DRT16 i +end +ret drtc2 k i +end + +fn drtc2 k ulong i ulong to ulong +if eq k 17 +ret ulong get DRT17 i +end +if eq k 18 +ret ulong get DRT18 i +end +if eq k 19 +ret ulong get DRT19 i +end +if eq k 20 +ret ulong get DRT20 i +end +if eq k 21 +ret ulong get DRT21 i +end +if eq k 22 +ret ulong get DRT22 i +end +if eq k 23 +ret ulong get DRT23 i +end +if eq k 24 +ret ulong get DRT24 i +end +if eq k 25 +ret ulong get DRT25 i +end +if eq k 26 +ret ulong get DRT26 i +end +if eq k 27 +ret ulong get DRT27 i +end +if eq k 28 +ret ulong get DRT28 i +end +if eq k 29 +ret ulong get DRT29 i +end +if eq k 30 +ret ulong get DRT30 i +end +if eq k 31 +ret ulong get DRT31 i +end +if eq k 32 +ret ulong get DRT32 i +end +if eq k 33 +ret ulong get DRT33 i +end +ret 0 +end + +note output line buffer DRO +fn droc c ulong to void +n = get DRON 0 +if lt n 8192 +put DRO n uchar c +put DRON 0 add n 1 +end +end + +fn drotx k ulong to void +for i ulong drtl k +droc drtc k i +end +end + +fn dronum v ulong to void +d = new uchar 24 0 +n = ulong 0 +x = v +while true bound 24 +put d n uchar add rem x 10 48 +n = add n 1 +x = div x 10 +if eq x 0 +break +end +end +while gt n 0 bound 24 +n = sub n 1 +droc ulong get d n +end +end + +fn dronumi v int to void +if lt v 0 +droc 45 +dronum ulong neg long v +ret +end +dronum ulong v +end + +note the digest LBH as 64 lowercase hex digits +fn drohex to void +for j ulong 4 +v = get LBH j +for k ulong 16 +d = and shr v mul sub 15 k 4 15 +if lt d 10 +droc add d 48 +else +droc add d 87 +end +end +end +end + +fn droflush fd int to void +r = write fd DRO 0 get DRON 0 +put DRON 0 0 +end + +note argument a into DRA; length (0 when missing or too long) +fn drarg a ulong to ulong +if ge a ulong argcnt +put DRAN 0 0 +ret 0 +end +n = argread a DRA 0 4096 +if lt n 0 +put DRAN 0 0 +ret 0 +end +put DRAN 0 ulong n +ret ulong n +end + +note DRA starts with text k (whole match when exact) +fn drpre k ulong exact bool to bool +l = drtl k +n = get DRAN 0 +if lt n l +ret false +end +if and exact ne n l +ret false +end +for i ulong l +if ne ulong get DRA i drtc k i +ret false +end +end +ret true +end + +note unit name at DRA[a..b) as index 0..8 in DRT14, or 9 +fn drunit a ulong b ulong to ulong +u = ulong 0 +p = ulong 0 +l = drtl 14 +while le p l bound 128 +q = p +while lt q l bound 128 +if eq drtc 14 q 32 +break +end +q = add q 1 +end +if eq sub q p sub b a +ok = true +for i ulong sub q p +if ne ulong get DRA add a i drtc 14 add p i +ok = false +end +end +if ok +ret u +end +end +u = add u 1 +p = add q 1 +end +ret 9 +end + +note option in DRA: 0 not an option, 1 accepted, 2 malformed +fn dropt to int +n = get DRAN 0 +if drpre 17 false +e = ulong 5 +while lt e n bound 4096 +if eq get DRA e 61 +break +end +e = add e 1 +end +if ge e n +ret 2 +end +u = drunit 5 e +if or ge u 9 eq u 7 +ret 2 +end +pl = sub sub n e 1 +if or eq pl 0 ge pl 4096 +ret 2 +end +for i ulong pl +put DRUP add mul u 4096 i get DRA add add e 1 i +end +put DRUL u pl +ret 1 +end +if drpre 18 false +ret dropt2 +end +if drpre 19 false +k = ulong 0 +if or eq n 11 gt n 12 +ret 2 +end +for i ulong sub n 11 +c = ulong get DRA add 11 i +if or lt c 48 gt c 57 +ret 2 +end +k = add mul k 10 sub c 48 +end +if or lt k 1 gt k 9 +ret 2 +end +put DRCRASH 0 k +ret 1 +end +ret 0 +end + +note mutant:c1|c1b|c1c +fn dropt2 to int +n = get DRAN 0 +for i ulong sub n 7 +put DRA i get DRA add 7 i +end +put DRAN 0 sub n 7 +m = get CGMUT 0 +if drpre 20 true +put CGMUT 0 or m 1 +ret 1 +end +if drpre 21 true +put CGMUT 0 or m 2 +ret 1 +end +if drpre 22 true +put CGMUT 0 or m 4 +ret 1 +end +ret 2 +end + +note LBP <- path of unit u (override or default) +fn drupath u ulong to void +lbpz +l = get DRUL u +if gt l 0 +for i ulong l +lbpc ulong get DRUP add mul u 4096 i +end +ret +end +if eq u 0 +for i ulong drtl 15 +lbpc drtc 15 i +end +ret +end +for i ulong drtl 16 +lbpc drtc 16 i +end +p = ulong 0 +c = ulong 0 +l2 = drtl 14 +while lt p l2 bound 128 +ch = drtc 14 p +if eq ch 32 +c = add c 1 +else +if eq c u +lbpc ch +end +end +p = add p 1 +end +lbptx 2 +end + +note write n bytes of DRIO to fd; 0 ok +fn drwrio fd int n ulong to int +t = ulong 0 +while lt t n bound 65537 +r = write fd DRIO t sub n t +if le r 0 +ret 1 +end +t = add t ulong r +end +ret 0 +end + +note copy the file named by LBP to fd; bytes copied, or -1 +fn drcat fd int to long +in = lbopen 524288 0 +if lt in 0 +ret -1 +end +tot = long 0 +while true bound 999999 +r = read in DRIO 0 65536 +if lt r 0 +s = fclose in +ret -1 +end +if eq r 0 +break +end +if ne drwrio fd ulong r 0 +s = fclose in +ret -1 +end +put DRLB 0 ulong get DRIO sub ulong r 1 +tot = add tot r +end +s = fclose in +ret tot +end + +note write CGO to fd; bytes, or -1 +fn drcgo fd int to long +n = get CGN 0 +t = ulong 0 +while lt t n bound 999999 +r = write fd CGO t sub n t +if le r 0 +ret -1 +end +t = add t ulong r +end +if gt n 0 +put DRLB 0 ulong get CGO sub n 1 +end +ret long n +end + +note compose the router source into fd (contract 12 order); bytes, -1 unit unreadable, -2 write +fn drcompose fd int to long +tot = long 0 +r = long 0 +for u ulong 9 +put DRLB 0 10 +if eq u 7 +r = drcgo fd +if lt r 0 +ret -2 +end +else +drupath u +r = drcat fd +if lt r 0 +ret -1 +end +end +tot = add tot r +if ne get DRLB 0 10 +put DRIO 0 10 +if ne drwrio fd 1 0 +ret -2 +end +tot = add tot 1 +end +end +ret tot +end + +note save LBP as the compile input (1) or output (2) path +fn drsavep which ulong to void +n = get LBPN 0 +for i ulong n +if eq which 1 +put DRPI i get LBP i +else +put DRPO i get LBP i +end +end +if eq which 1 +put DRPIN 0 n +else +put DRPON 0 n +end +end + +note forked child: stdin from DRPI, stdout to DRPO (mode 0700), exec OMC; never returns +fn drchild to void +r = fopen DRPI 0 get DRPIN 0 0 0 +if lt r 0 +exit 127 +end +s = dupfd int r 0 0 +r2 = fopen DRPO 0 get DRPON 0 577 448 +if lt r2 0 +exit 127 +end +s = dupfd int r2 1 0 +n = get DROMN 0 +put DRPK 0 1 +put DRPK 1 0 +put DRPK 2 uchar and n 255 +put DRPK 3 uchar shr n 8 +for i ulong n +put DRPK add 4 i get DROMC i +end +e = execprog DRPK 0 add n 4 +exit 127 +end + +note run OMC < DRPI > DRPO with the 120 s budget: omc exit status, 256 + signal, -1 over budget, +note -2 fork failed +fn drcompile to int +pid = fork +if lt pid 0 +ret -2 +end +if eq pid 0 +drchild +exit 127 +end +t0 = monons +while true bound 999999 +r = waitpid int pid DRST 0 4 1 +if eq r pid +s = or ulong get DRST 0 shl ulong get DRST 1 8 +if eq and s 127 0 +ret int and shr s 8 255 +end +ret int add 256 and s 127 +end +if gt sub monons t0 120000000000 +k = killpid int pid 9 +r = waitpid int pid DRST 0 4 0 +ret -1 +end +z = sleepns 2000000 +end +ret -1 +end + +note OMC path (argv 2) into DROMC; 0 ok +fn dromc to int +n = drarg 2 +if or eq n 0 ge n 4000 +ret 1 +end +for i ulong n +put DROMC i get DRA i +end +put DROMN 0 n +ret 0 +end + +note DRA into LBDIR (OUTLIB / OUTDIR) and create it 0700 when missing; 0 ok, 1 usage, 2 mkdir +fn droutdir to int +n = get DRAN 0 +if or eq n 0 ge n 3000 +ret 1 +end +for i ulong n +put LBDIR i get DRA i +end +put LBDN 0 n +r = makedir LBDIR 0 n 448 +if and ne r 0 ne r -17 +ret 2 +end +ret 0 +end + +note LIBDIR/NAME for fixed text k +fn drpfix k ulong to void +lbpz +lbpdir +for i ulong drtl k +lbpc drtc k i +end +end + +fn drusage c int to int +drotx 29 +dronum ulong c +droc 10 +drotx 5 +droflush 2 +ret 2 +end + +fn drbase to int +if ne argcnt 4 +ret drusage 1 +end +if ne dromc 0 +ret drusage 2 +end +n = drarg 3 +if ne droutdir 0 +ret drusage 3 +end +put CGK 0 0 +put CGMUT 0 0 +if ne cggen 0 +ret 11 +end +drpfix 27 +drsavep 1 +fd = lbopen 524865 384 +if lt fd 0 +ret 12 +end +t = drcompose fd +s = fsync fd +s = fclose fd +if eq t -1 +ret drusage 4 +end +if lt t 0 +ret 12 +end +if gt t 524288 +drotx 13 +drotx 30 +droflush 2 +ret 13 +end +drpfix 28 +drsavep 2 +r = drcompile +if eq r -1 +drotx 9 +droflush 2 +ret 13 +end +if ne r 0 +drotx 8 +dronumi r +droc 10 +droflush 2 +ret 11 +end +h = lbhpath +if lt h 0 +ret 12 +end +drotx 1 +drohex +droc 10 +droflush 1 +ret 0 +end + +note DRIO[0..1600) as 200 LE words into DRCAL +fn drcaldec to void +for w ulong 200 +v = ulong 0 +for k ulong 8 +v = or v shl ulong get DRIO add mul w 8 k mul k 8 +end +put DRCAL w v +end +end + +note CALIB (argv 4): '-' none, else the 1600-byte R1CAL001 file (K4 format) into DRCAL; 0 ok, 1 bad +fn drcalib to int +n = drarg 4 +put DRCN 0 0 +if drpre 26 true +ret 0 +end +if or eq n 0 ge n 4000 +ret 1 +end +for i ulong n +put DRCP i get DRA i +end +put DRCN 0 n +r = fopen DRCP 0 n 524288 0 +if lt r 0 +ret 1 +end +fd = int r +t = ulong 0 +while lt t 1601 bound 1602 +m = read fd DRIO t sub 1601 t +if le m 0 +break +end +t = add t ulong m +end +s = fclose fd +if ne t 1600 +ret 1 +end +drcaldec +if or ne get DRCAL 0 3544386010811150674 ne get DRCAL 1 1 +ret 1 +end +for w ulong 5 +if ne get DRCAL add 3 w 0 +ret 1 +end +end +ret 0 +end + +note gen-G.cal.tmp from DRCAL; 0 ok +fn drcalwr g ulong to int +for w ulong 200 +v = get DRCAL w +for k ulong 8 +put DRIO add mul w 8 k uchar and shr v mul k 8 255 +end +end +lbpgen g 4 true +fd = lbopen 524865 384 +if lt fd 0 +ret 1 +end +r = drwrio fd 1600 +s = fsync fd +s = fclose fd +ret r +end + +note LBH into LBMAN words w..w+3 +fn drmanh w ulong to void +for j ulong 4 +put LBMAN add w j get LBH j +end +end + +note sum of the file sizes of generations 1..g of LBDIR (final and tmp names) plus CURRENT +fn drlibbytes g ulong to ulong +tot = ulong 40 +for h ulong g +for e ulong 4 +lbpgen add h 1 add e 1 false +s = lbfsize +if gt s 0 +tot = add tot ulong s +end +lbpgen add h 1 add e 1 true +s = lbfsize +if gt s 0 +tot = add tot ulong s +end +end +end +ret tot +end + +note remove the tmp files of generation g after a refused build +fn drdrop g ulong to void +for e ulong 4 +lbpgen g add e 1 true +r = unlink LBP 0 get LBPN 0 +end +end + +note parse build options and EXP arguments (argv 6..); 0 ok, else usage code +fn drbopts to int +put DREN 0 0 +put DRCRASH 0 0 +put CGMUT 0 0 +for u ulong 9 +put DRUL u 0 +end +a = ulong 6 +while lt a ulong argcnt bound 4096 +n = drarg a +if eq n 0 +ret 5 +end +o = dropt +if eq o 2 +ret 6 +end +if eq o 0 +e = get DREN 0 +if ge e 8 +ret 7 +end +put DREI e a +put DREN 0 add e 1 +end +a = add a 1 +end +ret 0 +end + +note admit every EXP (exp.om) and count it (learn.om); 0, 10 refused, 2 duplicate or over 8, 13 +fn drlearn to int +lgreset +ne0 = get DREN 0 +if eq ne0 0 +lgselect +ret 0 +end +n = drarg 3 +if or eq n 0 ge n 4095 +ret 10 +end +for i ulong n +put EXAPATH i get DRA i +end +put EXAPATH n 0 +put EXALEN 0 n +if ne exaload 0 +drotx 6 +for i ulong n +droc ulong get DRA i +end +droc 10 +droflush 2 +ret 10 +end +for e ulong ne0 +m = drarg get DREI e +for i ulong m +put EXFPATH i get DRA i +end +put EXFPATH m 0 +put EXFLEN 0 m +c = exadmit +if ne c 0 +drotx 6 +for i ulong m +droc ulong get DRA i +end +drotx 7 +dronumi c +droc 10 +droflush 2 +ret 10 +end +if ne lglin 0 +ret 2 +end +if ne lgcount 0 +drotx 13 +drotx 32 +droflush 2 +ret 13 +end +end +lgselect +ret 0 +end + +note manifest words of generation g (sha fields filled by the caller) +fn drmanifest g ulong prev ulong to void +for i ulong 256 +put LBMAN i 0 +end +put LBMAN 0 3544386019401740626 +put LBMAN 1 1 +put LBMAN 2 g +put LBMAN 3 prev +k = get LGK 0 +put LBMAN 4 k +put LBMAN 18 1 +put LBMAN 19 get LGLN 0 +for i ulong 32 +put LBMAN add 20 i get LGLD i +end +for t ulong k +b = add 56 mul t 8 +s = get LGSH t +put LBMAN b s +if eq s 1 +put LBMAN add b 1 2 +else +put LBMAN add b 1 1 +put LBMAN add b 2 and s 255 +put LBMAN add b 3 and shr s 8 15 +put LBMAN add b 4 and shr s 12 15 +end +put LBMAN add b 5 get LGCN t +put LBMAN add b 6 get CGSB t +end +end + +note compose and compile generation g into LBDIR; 0 ok, else the drive exit code +fn drgen g ulong to int +lbpgen g 1 true +drsavep 1 +fd = lbopen 524865 384 +if lt fd 0 +ret 12 +end +t = drcompose fd +s = fsync fd +s = fclose fd +if eq t -1 +ret drusage 8 +end +if lt t 0 +ret 12 +end +if gt t 524288 +drotx 13 +drotx 30 +droflush 2 +ret 13 +end +lbpgen g 2 true +drsavep 2 +r = drcompile +if eq r -1 +drotx 9 +droflush 2 +ret 13 +end +if ne r 0 +drotx 8 +dronumi r +droc 10 +droflush 2 +ret 11 +end +ret 0 +end + +note the SHA-256 fields of the manifest of generation g; 0 ok, 12 unreadable +fn drhashes g ulong to int +lbpgen g 1 true +n = lbhpath +if lt n 0 +ret 12 +end +put LBMAN 5 ulong n +drmanh 6 +lbpgen g 2 true +n = lbhpath +if lt n 0 +ret 12 +end +drmanh 10 +lbpz +for i ulong get DROMN 0 +lbpc ulong get DROMC i +end +n = lbhpath +if lt n 0 +ret 12 +end +drmanh 14 +if gt get DRCN 0 0 +if ne drcalwr g 0 +ret 12 +end +lbpgen g 4 true +n = lbhpath +if lt n 0 +ret 12 +end +drmanh 52 +end +ret 0 +end + +fn drbuild to int +if lt argcnt 6 +ret drusage 1 +end +if ne dromc 0 +ret drusage 2 +end +if ne drcalib 0 +ret drusage 3 +end +r = drbopts +if ne r 0 +ret drusage r +end +r = drlearn +if ne r 0 +if eq r 2 +ret drusage 9 +end +ret r +end +put CGK 0 get LGK 0 +for t ulong 16 +put CGSH t get LGSH t +end +if ne cggen 0 +ret 11 +end +n = drarg 5 +if ne droutdir 0 +ret drusage 4 +end +prev = ulong 0 +c = lbcurrd +if eq c 0 +prev = get LBG 0 +else +lbpcurr false +if ge lbfsize 0 +drotx 10 +dronumi c +droc 10 +droflush 2 +ret 12 +end +end +g = add prev 1 +lbclean g +drmanifest g prev +r = drgen g +if ne r 0 +drdrop g +ret r +end +r = drhashes g +if ne r 0 +drdrop g +ret r +end +if gt add drlibbytes g 2048 8388608 +drdrop g +drotx 13 +drotx 31 +droflush 2 +ret 13 +end +lbseal +put LBG 0 g +r = lbpub get DRCRASH 0 +if eq r 97 +ret 97 +end +if ne r 0 +drotx 10 +dronumi r +droc 10 +droflush 2 +ret 12 +end +lbhman +drotx 2 +drohex +droc 10 +drotx 3 +dronum g +droc 10 +drotx 4 +dronum get LGK 0 +droc 10 +droflush 1 +ret 0 +end + +note internal checks: SHA-256("abc") through lbhman's path, manifest seal and field check, +note deterministic codegen; 0 pass, else the failed check +fn drself to int +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +for i ulong 3 +put b i get DRT33 i +end +omsha c b 0 0 w b8 +omsha c b 3 1 w b8 +omsha c b 0 2 w b8 +lbdig w +if or ne get LBH 0 13436514500253700074 ne get LBH 3 12975151322591401389 +ret 1 +end +put LGK 0 0 +put LGLN 0 0 +put CGSB 0 0 +drmanifest 3 2 +put LBMAN 14 8308871078283820969 +put LBMAN 15 3511899704727027462 +put LBMAN 16 15311527216533678964 +put LBMAN 17 17706733685053746017 +lbseal +put LBG 0 3 +if ne lbmchk 0 +ret 2 +end +put LBMAN 7 1 +if ne lbmchk 11 +ret 3 +end +put CGK 0 2 +put CGSH 0 120664328 +put CGSH 1 1 +put CGMUT 0 0 +if ne cggen 0 +ret 4 +end +n1 = get CGN 0 +cghash 0 n1 15 +h0 = get CGSD 60 +if ne cggen 0 +ret 5 +end +if ne get CGN 0 n1 +ret 6 +end +cghash 0 n1 15 +if ne get CGSD 60 h0 +ret 7 +end +ret 0 +end + +fn main to int +n = drarg 1 +if drpre 24 true +ret drbase +end +if drpre 23 true +ret drbuild +end +if drpre 25 true +r = drself +if eq r 0 +drotx 11 +droflush 1 +ret 0 +end +drotx 12 +dronumi r +droc 10 +droflush 1 +ret 1 +end +ret drusage 0 +end diff --git a/om/reuse/exp.om b/om/reuse/exp.om new file mode 100644 index 000000000..90dce5991 --- /dev/null +++ b/om/reuse/exp.om @@ -0,0 +1,661 @@ +note SPDX-License-Identifier: Apache-2.0 +note exp.om: R1 experience records (docs/reuse/REUSE-1.md section 3.1): the tap writer and admission. +note Concatenated after r1.om. Exported: +note EXAPATH uchar 4096, EXALEN ulong 1 the ALLOW path (caller fills; no 0 byte needed) +note exaload to int read ALLOW (at most 1 MiB) into the allow table and its SHA-256 into EXAD: +note 0 ok; 1 unreadable, 2 too large, 3 malformed line, 4 more than 8192 lines. +note Lines: `<64 hex> AUTHORIZED|SYNTHETIC|REVOKED <label>`, label nonempty, one +note space between fields, lowercase or uppercase hex; empty lines are skipped. +note exallow d view ulong 4 to int digest (word j = bytes 8j..8j+7 big endian) against the loaded +note table: 1 allowed (an AUTHORIZED or SYNTHETIC line and no REVOKED line), +note 0 absent, -1 revoked +note exastat to bool stat of ALLOW (inode, size, mtime ns, ctime ns) into EXAS; true when it +note differs from the snapshot taken by the previous call (or on the first call) +note EXFPATH uchar 4096, EXFLEN ulong 1 the experience file path +note exadmit to int admission of that file against the loaded ALLOW: 0 admitted; on 0 the file +note is in EXFW (LE words, record r = EXFW[128r .. 128r+128)), EXFN = records, +note EXFD = the file's SHA-256. Codes: 1 unreadable, 2 size, 3 not in ALLOW, +note 4 revoked, 5 ALLOW not loaded, then the first failing record check: 6 magic, +note 7 version, 8 sequence, 9 family, 10 PROG invalid, 11 program digest, +note 12 split, 13 provenance, 14 m, 15 a zero word, 16 AGG sample, 17 FSM sample. +note EXFBAD = the 0-based index of the failing record. Clobbers R1P, R1PD, R1HB, +note R1HD. +note extopen to int the tap: argv 5 TAPFILE is created (O_EXCL; an existing file is refused), +note argv 6 SPLIT must be 1, 2 or 3: 0 ok, 1 split, 2 file exists or not creatable +note extap ses ulong qn ulong nchg ulong ns ulong to int one record for the current R1P, R1N, R1X +note and R1R (the baseline result, ns its evaluation time): sets R1PD and R1SD; +note 0 ok, 1 write failed. Sequence numbers start at 1. +global EXAPATH uchar 4096 +global EXALEN ulong 1 +global EXAB uchar 1048576 +global EXAT ulong 40960 +global EXAN ulong 1 +global EXAOK ulong 1 +global EXAD ulong 4 +global EXAS ulong 5 +global EXSB uchar 128 +global EXFPATH uchar 4096 +global EXFLEN ulong 1 +global EXFW ulong 4194304 +global EXFN ulong 1 +global EXFD ulong 4 +global EXFBAD ulong 1 +global EXTFD ulong 1 +global EXTSEQ ulong 1 +global EXTSPL ulong 1 +global EXTR ulong 128 +global EXTB uchar 1024 +global EXTP uchar 4096 + +note a hex digit's value, or 16 when it is not one +fn exhex c ulong to ulong +if and ge c 48 le c 57 +ret sub c 48 +end +if and ge c 97 le c 102 +ret sub c 87 +end +if and ge c 65 le c 70 +ret sub c 55 +end +ret 16 +end + +note keyword at EXAB[p..): 1 AUTHORIZED, 2 SYNTHETIC, 3 REVOKED, 0 none; the word must be followed +note by a space (checked by the caller through its length) +fn exkw p ulong e ulong to ulong +kw = new uchar 10 0 +r = ulong 1 +while le r 3 bound 4 +if eq r 1 +put kw 0 65 +put kw 1 85 +put kw 2 84 +put kw 3 72 +put kw 4 79 +put kw 5 82 +put kw 6 73 +put kw 7 90 +put kw 8 69 +put kw 9 68 +end +if eq r 2 +put kw 0 83 +put kw 1 89 +put kw 2 78 +put kw 3 84 +put kw 4 72 +put kw 5 69 +put kw 6 84 +put kw 7 73 +put kw 8 67 +end +if eq r 3 +put kw 0 82 +put kw 1 69 +put kw 2 86 +put kw 3 79 +put kw 4 75 +put kw 5 69 +put kw 6 68 +end +n = exkwlen r +ok = lt add p n e +for i ulong n +if ok +if ne get EXAB add p i get kw i +ok = false +end +end +end +if ok +if eq get EXAB add p n 32 +ret r +end +end +r = add r 1 +end +ret 0 +end + +fn exkwlen r ulong to ulong +if eq r 1 +ret 10 +end +if eq r 2 +ret 9 +end +ret 7 +end + +note one ALLOW line EXAB[s..e): 0 ok (entry appended), 3 malformed, 4 table full +fn exaline s ulong e ulong to int +if lt sub e s 68 +ret 3 +end +n = get EXAN 0 +if ge n 8192 +ret 4 +end +b = mul n 5 +for j ulong 4 +v = ulong 0 +for i ulong 16 +h = exhex ulong get EXAB add s add mul j 16 i +if gt h 15 +ret 3 +end +v = add mul v 16 h +end +put EXAT add b j v +end +if ne get EXAB add s 64 32 +ret 3 +end +k = exkw add s 65 e +if eq k 0 +ret 3 +end +if le e add add s 66 exkwlen k +ret 3 +end +put EXAT add b 4 k +put EXAN 0 add n 1 +ret 0 +end + +note SHA-256 of EXAB[0..n) into EXAD +fn exahash n ulong to void +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +off = ulong 0 +while lt off n bound 257 +m = sub n off +if gt m 4096 +m = 4096 +end +for i ulong m +put b i get EXAB add off i +end +omsha c b m 1 w b8 +off = add off m +end +omsha c b 0 2 w b8 +for j ulong 4 +put EXAD j r1bswap get w j +end +end + +fn exaload to int +put EXAOK 0 0 +put EXAN 0 0 +fd = fopen EXAPATH 0 get EXALEN 0 0 0 +if lt fd 0 +ret 1 +end +got = ulong 0 +while lt got 1048576 bound 100000 +k = read int fd EXAB got sub 1048576 got +if le k 0 +break +end +got = add got ulong k +end +one = new uchar 1 0 +k = read int fd one 0 1 +r = fclose int fd +if or ne k 0 ne r 0 +ret 2 +end +exahash got +s = ulong 0 +for i ulong got +if eq get EXAB i 10 +if gt i s +x = exaline s i +if ne x 0 +ret x +end +end +s = add i 1 +end +end +if lt s got +x = exaline s got +if ne x 0 +ret x +end +end +put EXAOK 0 1 +ret 0 +end + +fn exallow d view ulong 4 to int +n = get EXAN 0 +seen = int 0 +for e ulong n +b = mul e 5 +hit = true +for j ulong 4 +if ne get EXAT add b j get d j +hit = false +end +end +if hit +if eq get EXAT add b 4 3 +ret -1 +end +seen = 1 +end +end +ret seen +end + +note the 8-byte LE word at byte p of EXSB +fn exsw p ulong to ulong +v = ulong 0 +for i ulong 8 +v = or v shl ulong get EXSB add p i mul i 8 +end +ret v +end + +fn exastat to bool +for i ulong 128 +put EXSB i 0 +end +r = stat EXAPATH 0 get EXALEN 0 EXSB 0 128 +s = new ulong 5 0 +put s 0 exsw 8 +put s 1 exsw 48 +put s 2 add mul exsw 88 1000000000 exsw 96 +put s 3 add mul exsw 104 1000000000 exsw 112 +if eq r 0 +put s 4 1 +else +put s 4 2 +end +ch = false +for j ulong 5 +if ne get s j get EXAS j +ch = true +end +put EXAS j get s j +end +ret ch +end + +note the experience file into EXFW and its SHA-256 into EXFD: the byte count, or -1 unreadable, +note -2 larger than 32 MiB +fn exfread to long +fd = fopen EXFPATH 0 get EXFLEN 0 0 0 +if lt fd 0 +ret -1 +end +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +tot = ulong 0 +eof = false +while not eof bound 8194 +fill = ulong 0 +while lt fill 4096 bound 4097 +k = read int fd b fill sub 4096 fill +if le k 0 +eof = true +break +end +fill = add fill ulong k +end +if gt add tot fill 33554432 +r = fclose int fd +ret -2 +end +omsha c b fill 1 w b8 +exfwords b tot fill +tot = add tot fill +end +r = fclose int fd +omsha c b 0 2 w b8 +for j ulong 4 +put EXFD j r1bswap get w j +end +ret long tot +end + +note bytes b[0..fill) at file offset tot into EXFW (whole words only) +fn exfwords b view uchar 4096 tot ulong fill ulong to void +nw = shr fill 3 +base = shr tot 3 +for i ulong nw +v = ulong 0 +p = add mul i 8 7 +for k ulong 8 +v = or shl v 8 ulong get b sub p k +end +put EXFW add base i v +end +end + +note AGG samples of record base: m pairs (x, e) with x < 2^wl and e = r1lane x; R1P holds its PROG +fn exsagg base ulong m ulong to bool +lim = shl ulong 1 get R1P 2 +for i ulong m +x = get EXFW add base add 16 mul i 2 +e = get EXFW add base add 17 mul i 2 +if ge x lim +ret false +end +if ne r1lane x e +ret false +end +end +ret true +end + +note FSM samples: m words q + 256 a + 65536 q' with q < S, a < A, q' = delta(q, a) +fn exsfsm base ulong m ulong to bool +for i ulong m +w = get EXFW add base add 16 i +q = and w 255 +a = and shr w 8 255 +if or ge q get R1P 2 ge a get R1P 3 +ret false +end +if ne shr w 16 r1delta q a +ret false +end +end +ret true +end + +note words that must be 0: 15, 56..63, and the sample words past the m samples +fn exzero base ulong fam ulong m ulong to bool +if ne get EXFW add base 15 0 +ret false +end +for i ulong 8 +if ne get EXFW add base add 56 i 0 +ret false +end +end +used = m +if eq fam 1 +used = mul m 2 +end +for i ulong 32 +if ge i used +if ne get EXFW add base add 16 i 0 +ret false +end +end +end +ret true +end + +note record r (0-based), prev = the previous sequence number: 0 or a code 6..17 +fn exrec r ulong prev ulong to int +base = mul r 128 +if ne get EXFW base 3544386028377026898 +ret 6 +end +if ne get EXFW add base 1 1 +ret 7 +end +sq = get EXFW add base 2 +if or le sq prev and eq r 0 ne sq 1 +ret 8 +end +fam = get EXFW add base 3 +if or ne fam get EXFW add base 65 and ne fam 1 ne fam 2 +ret 9 +end +for i ulong 64 +put R1P i get EXFW add base add 64 i +end +if ne r1valid 0 +ret 10 +end +r1pdig +for j ulong 4 +if ne get R1PD j get EXFW add base add 52 j +ret 11 +end +end +sp = get EXFW add base 9 +if or lt sp 1 gt sp 3 +ret 12 +end +if ne get EXFW add base 13 1 +ret 13 +end +m = get EXFW add base 14 +if gt m 16 +ret 14 +end +if not exzero base fam m +ret 15 +end +if eq fam 1 +if not exsagg base m +ret 16 +end +else +if not exsfsm base m +ret 17 +end +end +ret 0 +end + +fn exadmit to int +put EXFN 0 0 +put EXFBAD 0 0 +if ne get EXAOK 0 1 +ret 5 +end +tot = exfread +if eq tot -1 +ret 1 +end +if or lt tot 0 or eq tot 0 ne rem tot 1024 0 +ret 2 +end +a = new ulong 4 0 +for j ulong 4 +put a j get EXFD j +end +al = exallow a +if eq al -1 +ret 4 +end +if ne al 1 +ret 3 +end +nr = shr ulong tot 10 +prev = ulong 0 +for r ulong nr +put EXFBAD 0 r +x = exrec r prev +if ne x 0 +ret x +end +prev = get EXFW add mul r 128 2 +end +put EXFN 0 nr +ret 0 +end + +note argv i into EXTP: the length, or -1 +fn exarg i ulong to long +n = argread i EXTP 0 4096 +if or lt n 1 gt n 4095 +ret -1 +end +ret n +end + +fn extopen to int +put EXTSEQ 0 0 +n = exarg 6 +if ne n 1 +ret 1 +end +sp = sub ulong get EXTP 0 48 +if or lt sp 1 gt sp 3 +ret 1 +end +put EXTSPL 0 sp +n = exarg 5 +if lt n 0 +ret 2 +end +fd = fopen EXTP 0 ulong n 193 384 +if lt fd 0 +ret 2 +end +put EXTFD 0 ulong fd +ret 0 +end + +note rules tested by delta(q, a): the index of the first matching rule plus 1, else R +fn exscan q ulong a ulong to ulong +nr = get R1P 5 +for i ulong nr +w = get R1P add 22 i +if eq and w 255 q +lo = and shr w 8 255 +hi = and shr w 16 255 +if and le lo a le a hi +ret add i 1 +end +end +end +ret nr +end + +note FSM rule tests of the whole run (word 8) +fn exsteps to ulong +n = get R1N 0 +q = get R1P 4 +st = ulong 0 +for t ulong n +a = get R1X t +st = add st exscan q a +q = r1delta q a +end +ret st +end + +note the first m (<= 16) distinct lane values in index order, (x, e(x)) into EXTR[16..48) +fn exsmagg to ulong +n = get R1N 0 +m = ulong 0 +for i ulong n +if lt m 16 +x = get R1X i +seen = false +for j ulong m +if eq get EXTR add 16 mul j 2 x +seen = true +end +end +if not seen +put EXTR add 16 mul m 2 x +put EXTR add 17 mul m 2 r1lane x +m = add m 1 +end +end +end +ret m +end + +note the first m distinct transitions q + 256 a + 65536 q' taken +fn exsmfsm to ulong +n = get R1N 0 +m = ulong 0 +q = get R1P 4 +for i ulong n +a = get R1X i +nq = r1delta q a +if lt m 16 +w = add add q mul a 256 mul nq 65536 +seen = false +for j ulong m +if eq get EXTR add 16 j w +seen = true +end +end +if not seen +put EXTR add 16 m w +m = add m 1 +end +end +q = nq +end +ret m +end + +fn extap ses ulong qn ulong nchg ulong ns ulong to int +for i ulong 128 +put EXTR i 0 +end +sq = add get EXTSEQ 0 1 +put EXTSEQ 0 sq +put EXTR 0 3544386028377026898 +put EXTR 1 1 +put EXTR 2 sq +fam = get R1P 1 +put EXTR 3 fam +put EXTR 4 get R1N 0 +put EXTR 5 get R1R 0 +put EXTR 6 get R1R 1 +put EXTR 7 ns +if eq fam 1 +put EXTR 8 mul get R1N 0 get R1P 4 +put EXTR 14 exsmagg +else +put EXTR 8 exsteps +put EXTR 14 exsmfsm +end +put EXTR 9 get EXTSPL 0 +put EXTR 10 ses +put EXTR 11 qn +put EXTR 12 nchg +put EXTR 13 1 +r1sdig +r1pdig +for j ulong 4 +put EXTR add 48 j get R1SD j +put EXTR add 52 j get R1PD j +end +for i ulong 64 +put EXTR add 64 i get R1P i +end +ret exflush +end + +fn exflush to int +for i ulong 128 +v = get EXTR i +for k ulong 8 +put EXTB add mul i 8 k uchar and v 255 +v = shr v 8 +end +end +fd = int get EXTFD 0 +off = ulong 0 +while lt off 1024 bound 1025 +k = write fd EXTB off sub 1024 off +if le k 0 +ret 1 +end +off = add off ulong k +end +ret 0 +end diff --git a/om/reuse/learn.om b/om/reuse/learn.om new file mode 100644 index 000000000..906acbd6f --- /dev/null +++ b/om/reuse/learn.om @@ -0,0 +1,170 @@ +note SPDX-License-Identifier: Apache-2.0 +note learn.om (drive.om only, lg prefix): template admission of REUSE-1 contract 3.2 and 5.3 with +note clarification K3. Needs r1 (R1P, r1shape) and exp (exadmit results EXFW, EXFN, EXFD) before it. +note After each admitted experience file: lglin adds its SHA-256 to the lineage, lgcount counts its +note TRAIN records (split word 9 = 1; every experience record is one QUERY) per shape: FSM records +note (family 2) in LGNF, AGG records per r1shape of their PROG (words 64..127) in an open-addressed +note table. lgselect: the FSM template iff LGNF >= 8 (decided first); AGG shapes in descending +note count, ties lower shape id, while count >= 8 and K < 16; result LGSH[0..LGK) (AGG in admission +note order, then FSM = shape 1) with counts LGCN. Outcome words (r0, r1, ns) are never read. +global LGTS ulong 65536 +global LGTC ulong 65536 +global LGTT uchar 65536 +global LGNS ulong 1 +global LGNF ulong 1 +global LGK ulong 1 +global LGSH ulong 16 +global LGCN ulong 16 +global LGLD ulong 32 +global LGLN ulong 1 + +fn lgreset to void +for i ulong 65536 +put LGTS i 0 +put LGTC i 0 +put LGTT i 0 +end +put LGNS 0 0 +put LGNF 0 0 +put LGK 0 0 +put LGLN 0 0 +for i ulong 32 +put LGLD i 0 +end +for i ulong 16 +put LGSH i 0 +put LGCN i 0 +end +end + +note one AGG QUERY of shape s; 0 or 1 (table full) +fn lgadd s ulong to int +h = shr mul s 11400714819323198485 48 +for p ulong 65536 +i = and add h p 65535 +v = get LGTS i +if eq v s +put LGTC i add get LGTC i 1 +ret 0 +end +if eq v 0 +put LGTS i s +put LGTC i 1 +put LGNS 0 add get LGNS 0 1 +ret 0 +end +end +ret 1 +end + +note count the TRAIN records of the admitted file in EXFW[0..128 EXFN); 0 or 1 (table full) +fn lgcount to int +for r ulong get EXFN 0 +b = mul r 128 +if eq get EXFW add b 9 1 +if eq get EXFW add b 3 2 +put LGNF 0 add get LGNF 0 1 +else +for i ulong 64 +put R1P i get EXFW add add b 64 i +end +if ne lgadd r1shape 0 +ret 1 +end +end +end +end +ret 0 +end + +note digest a (4 words of EXFD) compared with lineage entry e: -1 less, 0 equal, 1 greater +fn lgcmp e ulong to int +for j ulong 4 +x = get EXFD j +y = get LGLD add mul e 4 j +if lt x y +ret -1 +end +if gt x y +ret 1 +end +end +ret 0 +end + +note insert EXFD into the ascending lineage; 0 ok, 1 duplicate, 2 more than 8 files +fn lglin to int +l = get LGLN 0 +p = ulong 0 +while lt p l bound 9 +c = lgcmp p +if eq c 0 +ret 1 +end +if lt c 0 +break +end +p = add p 1 +end +if ge l 8 +ret 2 +end +e = l +while gt e p bound 9 +for j ulong 4 +put LGLD add mul e 4 j get LGLD add mul sub e 1 4 j +end +e = sub e 1 +end +for j ulong 4 +put LGLD add mul p 4 j get EXFD j +end +put LGLN 0 add l 1 +ret 0 +end + +note the not yet admitted table entry with the highest count (ties lower shape id); index or 65536 +fn lgbest to ulong +bi = ulong 65536 +bc = ulong 0 +bs = ulong 0 +for i ulong 65536 +s = get LGTS i +if and ne s 0 eq get LGTT i 0 +c = get LGTC i +if or gt c bc and eq c bc lt s bs +bi = i +bc = c +bs = s +end +end +end +ret bi +end + +fn lgselect to void +k = ulong 0 +lim = ulong 16 +if ge get LGNF 0 8 +lim = 15 +end +while lt k lim bound 17 +i = lgbest +if eq i 65536 +break +end +if lt get LGTC i 8 +break +end +put LGTT i 1 +put LGSH k get LGTS i +put LGCN k get LGTC i +k = add k 1 +end +if ge get LGNF 0 8 +put LGSH k 1 +put LGCN k get LGNF 0 +k = add k 1 +end +put LGK 0 k +end diff --git a/om/reuse/lib.om b/om/reuse/lib.om new file mode 100644 index 000000000..d9a000db0 --- /dev/null +++ b/om/reuse/lib.om @@ -0,0 +1,872 @@ +note SPDX-License-Identifier: Apache-2.0 +note lib.om: REUSE-1 library generations (contract v1.0 section 3.4, clarifications K1 K3 K16). +note Part of every router (lbload, lblineage) and of drive.om (lbpub and helpers). lbload hands the +note verified calibration bytes to the generation as a memfd on fd 7 (K16). +note Needs sha256 (omsha), r1 and exp (exaload, exallow, EXAPATH, EXALEN) before it. +note Manifest: 256 LE words in LBMAN, file bytes in LBMB; word 255 = FNV-1a 64 over the 2040 LE +note bytes of words 0..254 (K3). CURRENT: 40 bytes, G LE then the 32 SHA-256 bytes of gen-G.man. +note Digests are 4 words, word j = digest bytes 8j..8j+7 read big endian (contract 1.1). +note lbload refusal codes (each check distinct, nothing is trusted before all pass): +note 1 argv (count, LIBDIR length) 2 MANSHA not 64 hex digits 3 CURRENT unreadable +note 4 CURRENT not exactly 40 bytes 5 gen-G.man unreadable 6 gen-G.man not exactly 2048 bytes +note 7 SHA-256(gen-G.man) differs from the CURRENT digest 8 CURRENT digest differs from MANSHA +note 9 magic 10 version 11 self-check 12 G differs from CURRENT or parent G >= G +note 13 compiler SHA-256 is not the pinned omc 14 profile version 15 K > 16 or L > 8 +note 16 a reserved word is nonzero 17 ALLOW unreadable 18 a lineage digest not allowed +note 19 gen-G.cal unreadable or 4096 bytes or more 20 gen-G.cal digest 21 gen-G.elf unreadable +note 22 memfd (or dup onto fd 7) failed 23 copy into a memfd failed 24 ELF digest 25 exec failed + +global LBMAN ulong 256 +global LBMB uchar 2048 +global LBH ulong 4 +global LBCD ulong 4 +global LBMS ulong 4 +global LBDIR uchar 4096 +global LBDN ulong 1 +global LBP uchar 4096 +global LBPN ulong 1 +global LBPK uchar 4096 +global LBIO uchar 4096 +global LBST uchar 128 +global LBG ulong 1 +global LBLD ulong 32 +global LBLN ulong 1 +global LBGEN ulong 1 +global LBCAL ulong 1 +text LBTGEN "gen-" +text LBTOM ".om" +text LBTELF ".elf" +text LBTMAN ".man" +text LBTCAL ".cal" +text LBTTMP ".tmp" +text LBTCUR "CURRENT" +text LBTFD "/proc/self/fd/" +text LBTLD "loaded" +text LBTMFD "r1gen" + +note byte swap of a 64-bit word +fn lbbswap x ulong to ulong +y = or shl and x 71777214294589695 8 and shr x 8 71777214294589695 +y = or shl and y 281470681808895 16 and shr y 16 281470681808895 +ret or shl y 32 shr y 32 +end + +note omsha's packed digest (w[i] holds digest bytes 8i..8i+7 little endian) into LBH +fn lbdig w view ulong 64 to void +for i ulong 4 +put LBH i lbbswap get w i +end +end + +note SHA-256 of a whole open file read with pread from position 0 into LBH; bytes or -errno +fn lbhfd fd int to long +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +tot = long 0 +while true bound 999999 +r = pread fd b 0 4096 tot +if lt r 0 +ret r +end +if eq r 0 +break +end +omsha c b ulong r 1 w b8 +tot = add tot r +end +omsha c b 0 2 w b8 +lbdig w +ret tot +end + +note SHA-256 of the 2048 manifest bytes LBMB into LBH +fn lbhman to void +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +for i ulong 2048 +put b i get LBMB i +end +omsha c b 0 0 w b8 +omsha c b 2048 1 w b8 +omsha c b 0 2 w b8 +lbdig w +end + +note manifest words to bytes and back +fn lbm2b to void +for i ulong 256 +v = get LBMAN i +for k ulong 8 +put LBMB add mul i 8 k uchar and shr v mul k 8 255 +end +end +end + +fn lbb2m to void +for i ulong 256 +v = ulong 0 +for k ulong 8 +v = or v shl ulong get LBMB add mul i 8 k mul k 8 +end +put LBMAN i v +end +end + +note FNV-1a 64 over LBMB[0..2040) (fill LBMB with lbm2b first) +fn lbfnv to ulong +h = ulong 14695981039346656037 +for i ulong 2040 +h = xor h ulong get LBMB i +h = mul h 1099511628211 +end +ret h +end + +note self-check word 255 of LBMAN (drive.om, tests) +fn lbseal to void +lbm2b +put LBMAN 255 lbfnv +lbm2b +end + +note little/big endian word of LBIO at byte o +fn lbgetle o ulong to ulong +v = ulong 0 +for k ulong 8 +v = or v shl ulong get LBIO add o k mul k 8 +end +ret v +end + +fn lbgetbe o ulong to ulong +v = ulong 0 +for k ulong 8 +v = or shl v 8 ulong get LBIO add o k +end +ret v +end + +fn lbputbe o ulong v ulong to void +for k ulong 8 +put LBIO add o k uchar and shr v mul sub 7 k 8 255 +end +end + +note path builder: LBP[0..LBPN) +fn lbpz to void +put LBPN 0 0 +end + +fn lbpc c ulong to void +n = get LBPN 0 +if ge n 4095 +trap 3 +end +put LBP n uchar c +put LBPN 0 add n 1 +end + +fn lbpnum v ulong to void +d = new uchar 24 0 +n = ulong 0 +x = v +while true bound 24 +put d n uchar add rem x 10 48 +n = add n 1 +x = div x 10 +if eq x 0 +break +end +end +while gt n 0 bound 24 +n = sub n 1 +lbpc ulong get d n +end +end + +note fixed name parts: 1 gen- 2 .om 3 .elf 4 .man 5 .cal 6 .tmp 7 CURRENT 8 /proc/self/fd/ 9 loaded +fn lbptx k ulong to void +if eq k 1 +for i ulong len LBTGEN +lbpc ulong get LBTGEN i +end +end +if eq k 2 +for i ulong len LBTOM +lbpc ulong get LBTOM i +end +end +if eq k 3 +for i ulong len LBTELF +lbpc ulong get LBTELF i +end +end +if eq k 4 +for i ulong len LBTMAN +lbpc ulong get LBTMAN i +end +end +if eq k 5 +for i ulong len LBTCAL +lbpc ulong get LBTCAL i +end +end +if eq k 6 +for i ulong len LBTTMP +lbpc ulong get LBTTMP i +end +end +if eq k 7 +for i ulong len LBTCUR +lbpc ulong get LBTCUR i +end +end +if eq k 8 +for i ulong len LBTFD +lbpc ulong get LBTFD i +end +end +if eq k 9 +for i ulong len LBTLD +lbpc ulong get LBTLD i +end +end +end + +fn lbpdir to void +for i ulong get LBDN 0 +lbpc ulong get LBDIR i +end +lbpc 47 +end + +note LIBDIR/gen-G.EXT[.tmp], ext 1 om 2 elf 3 man 4 cal +fn lbpgen g ulong ext ulong tmp bool to void +lbpz +lbpdir +lbptx 1 +lbpnum g +lbptx add ext 1 +if tmp +lbptx 6 +end +end + +fn lbpcurr tmp bool to void +lbpz +lbpdir +lbptx 7 +if tmp +lbptx 6 +end +end + +note open LBP; descriptor or -1 +fn lbopen flags int mode int to int +r = fopen LBP 0 get LBPN 0 flags mode +if lt r 0 +ret -1 +end +ret int r +end + +note read up to cap (<= 4096) bytes of fd into LBIO; count or -errno +fn lbrdio fd int cap ulong to long +tot = ulong 0 +while lt tot cap bound 4097 +r = read fd LBIO tot sub cap tot +if lt r 0 +ret r +end +if eq r 0 +break +end +tot = add tot ulong r +end +ret long tot +end + +note write LBIO[0..n) completely; 0 or 1 +fn lbwrio fd int n ulong to int +tot = ulong 0 +while lt tot n bound 4097 +r = write fd LBIO tot sub n tot +if le r 0 +ret 1 +end +tot = add tot ulong r +end +ret 0 +end + +note size of the file named by LBP, or -1 +fn lbfsize to long +r = stat LBP 0 get LBPN 0 LBST 0 128 +if ne r 0 +ret -1 +end +v = ulong 0 +for k ulong 8 +v = or v shl ulong get LBST add 48 k mul k 8 +end +ret long v +end + +note SHA-256 of the file named by LBP into LBH; bytes or negative +fn lbhpath to long +fd = lbopen 524288 0 +if lt fd 0 +ret -1 +end +n = lbhfd fd +r = fclose fd +ret n +end + +fn lbhexd c ulong to ulong +if and ge c 48 le c 57 +ret sub c 48 +end +if and ge c 97 le c 102 +ret sub c 87 +end +if and ge c 65 le c 70 +ret sub c 55 +end +ret 16 +end + +note 64 hex digits in LBIO[0..64) into 4 digest words at LBMS; 0 or 1 +fn lbhex2ms to int +for j ulong 4 +v = ulong 0 +for k ulong 16 +d = lbhexd ulong get LBIO add mul j 16 k +if gt d 15 +ret 1 +end +v = or shl v 4 d +end +put LBMS j v +end +ret 0 +end + +note LIBDIR (argv 2) into LBDIR, MANSHA (argv 3) into LBMS +fn lbargs to int +if lt argcnt 7 +ret 1 +end +n = argread 2 LBDIR 0 3000 +if or lt n 1 gt n 2999 +ret 1 +end +put LBDN 0 ulong n +m = argread 3 LBIO 0 4096 +if ne m 64 +ret 2 +end +if ne lbhex2ms 0 +ret 2 +end +ret 0 +end + +note CURRENT of LBDIR: G into LBG, digest words into LBCD; 0, 3 unreadable, 4 not 40 bytes +fn lbcurrd to int +lbpcurr false +fd = lbopen 524288 0 +if lt fd 0 +ret 3 +end +n = lbrdio fd 41 +r = fclose fd +if ne n 40 +ret 4 +end +put LBG 0 lbgetle 0 +for j ulong 4 +put LBCD j lbgetbe add 8 mul j 8 +end +ret 0 +end + +note gen-G.man of LBDIR into LBMB and LBMAN; 0, 5 unreadable, 6 not 2048 bytes +fn lbmanrd to int +lbpgen get LBG 0 3 false +fd = lbopen 524288 0 +if lt fd 0 +ret 5 +end +n = lbrdio fd 2049 +r = fclose fd +if ne n 2048 +ret 6 +end +for i ulong 2048 +put LBMB i get LBIO i +end +lbb2m +ret 0 +end + +note LBMAN words a..b-1 all zero +fn lbzero a ulong b ulong to bool +i = a +while lt i b bound 257 +if ne get LBMAN i 0 +ret false +end +i = add i 1 +end +ret true +end + +note manifest field checks (LBMAN, LBMB); 0 or 9..16 +fn lbmchk to int +if ne get LBMAN 0 3544386019401740626 +ret 9 +end +if ne get LBMAN 1 1 +ret 10 +end +lbm2b +if ne lbfnv get LBMAN 255 +ret 11 +end +g = get LBMAN 2 +if or ne g get LBG 0 ge get LBMAN 3 g +ret 12 +end +if or ne get LBMAN 14 8308871078283820969 ne get LBMAN 15 3511899704727027462 +ret 13 +end +if or ne get LBMAN 16 15311527216533678964 ne get LBMAN 17 17706733685053746017 +ret 13 +end +if ne get LBMAN 18 1 +ret 14 +end +k = get LBMAN 4 +l = get LBMAN 19 +if or gt k 16 gt l 8 +ret 15 +end +if not lbzero add 20 mul l 4 52 +ret 16 +end +if not lbzero add 56 mul k 8 255 +ret 16 +end +for t ulong k +if ne get LBMAN add 63 mul t 8 0 +ret 16 +end +end +ret 0 +end + +note digest LBH equals 4 words of LBMAN at w +fn lbhmeq w ulong to bool +for j ulong 4 +if ne get LBH j get LBMAN add w j +ret false +end +end +ret true +end + +note shared load prefix: argv, CURRENT, manifest, MANSHA, fields; 0 or 1..16 +fn lbman to int +r = lbargs +if ne r 0 +ret r +end +r = lbcurrd +if ne r 0 +ret r +end +r = lbmanrd +if ne r 0 +ret r +end +lbhman +for j ulong 4 +if ne get LBH j get LBCD j +ret 7 +end +end +for j ulong 4 +if ne get LBCD j get LBMS j +ret 8 +end +end +ret lbmchk +end + +note every lineage digest allowed by ALLOW (argv 4); 0, 17, 18 +fn lballow to int +d = new ulong 4 0 +l = get LBMAN 19 +if eq l 0 +ret 0 +end +n = argread 4 EXAPATH 0 4095 +if lt n 1 +ret 17 +end +put EXAPATH ulong n 0 +put EXALEN 0 ulong n +if ne exaload 0 +ret 17 +end +for i ulong l +for j ulong 4 +put d j get LBMAN add add 20 mul i 4 j +end +if ne exallow d 1 +ret 18 +end +end +ret 0 +end + +note SHA-256 of LBIO[0..n) (n <= 4096) into LBH +fn lbhio n ulong to void +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +for i ulong n +put b i get LBIO i +end +omsha c b 0 0 w b8 +omsha c b n 1 w b8 +omsha c b 0 2 w b8 +lbdig w +end + +note calibration handoff (K1, K16): gen-G.cal read once into LBIO, SHA-256 of exactly those bytes +note against manifest 52..55, the same bytes written into a memfd left on fd 7 without CLOEXEC for +note the generation (which reads /proc/self/fd/7, never LIBDIR). 0, 19 unreadable or not under +note 4096 bytes, 20 digest, 22 memfd or dup failed, 23 write failed. No calibration: 0, fd 7 untouched. +fn lbcalchk to int +if lbzero 52 56 +ret 0 +end +lbpgen get LBG 0 4 false +fd = lbopen 524288 0 +if lt fd 0 +ret 19 +end +n = lbrdio fd 4096 +r = fclose fd +if or lt n 1 ge n 4096 +ret 19 +end +lbhio ulong n +if not lbhmeq 52 +ret 20 +end +m = memfd LBTMFD 0 len LBTMFD 0 +if lt m 0 +ret 22 +end +mf = int m +if ne lbwrio mf ulong n 0 +ret 23 +end +if ne mf 7 +d = dupfd mf 7 0 +r = fclose mf +if ne d 7 +ret 22 +end +end +ret 0 +end + +note gen-G.elf copied into a memfd whose SHA-256 must equal manifest 10..13; fd or -21..-24 +fn lbelfmem to int +lbpgen get LBG 0 2 false +fd = lbopen 524288 0 +if lt fd 0 +ret -21 +end +m = memfd LBTMFD 0 len LBTMFD 1 +if lt m 0 +r = fclose fd +ret -22 +end +mf = int m +while true bound 999999 +r = read fd LBIO 0 4096 +if lt r 0 +r = fclose fd +ret -23 +end +if eq r 0 +break +end +if ne lbwrio mf ulong r 0 +r = fclose fd +ret -23 +end +end +r = fclose fd +n = lbhfd mf +if lt n 0 +ret -23 +end +if not lbhmeq 10 +ret -24 +end +ret mf +end + +note append LBP to the packed list LBPK as one record at o; new offset +fn lbpkadd o ulong to ulong +n = get LBPN 0 +put LBPK o uchar and n 255 +put LBPK add o 1 uchar shr n 8 +for i ulong n +put LBPK add add o 2 i get LBP i +end +ret add add o 2 n +end + +note exec /proc/self/fd/MF with argv 1..6 and loaded; returns only on failure +fn lbexec mf int to int +put LBPK 0 8 +put LBPK 1 0 +lbpz +lbptx 8 +lbpnum ulong mf +o = lbpkadd 2 +for a ulong 6 +n = argread add a 1 LBPK add o 2 sub 4000 o +if lt n 0 +ret 25 +end +put LBPK o uchar and ulong n 255 +put LBPK add o 1 uchar shr ulong n 8 +o = add add o 2 ulong n +if gt o 4000 +ret 25 +end +end +lbpz +lbptx 9 +o = lbpkadd o +r = execprog LBPK 0 o +ret 25 +end + +note B's load (contract 3.4): 0 never (exec'd), nonzero = refused with the code listed above +fn lbload to int +if ne argcnt 7 +ret 1 +end +r = lbman +if ne r 0 +ret r +end +r = lballow +if ne r 0 +ret r +end +r = lbcalchk +if ne r 0 +ret r +end +mf = lbelfmem +if lt mf 0 +ret neg mf +end +ret lbexec mf +end + +note manifest lineage for revocation checks in a loaded generation: LBLD, LBLN, LBGEN, LBCAL +fn lblineage to int +r = lbman +if ne r 0 +ret r +end +put LBLN 0 get LBMAN 19 +for i ulong 32 +put LBLD i get LBMAN add 20 i +end +put LBGEN 0 get LBG 0 +put LBCAL 0 0 +if not lbzero 52 56 +put LBCAL 0 1 +end +ret 0 +end + +note drive.om side. Remove a stale unpublished generation g (crash leftovers) and CURRENT.tmp. +fn lbclean g ulong to void +for e ulong 4 +lbpgen g add e 1 false +r = unlink LBP 0 get LBPN 0 +lbpgen g add e 1 true +r = unlink LBP 0 get LBPN 0 +end +lbpcurr true +r = unlink LBP 0 get LBPN 0 +end + +note fsync the file named by LBP; 0 or 1 +fn lbsyncp to int +fd = lbopen 524288 0 +if lt fd 0 +ret 1 +end +r = fsync fd +s = fclose fd +if ne r 0 +ret 1 +end +ret 0 +end + +fn lbsyncdir to int +r = fopen LBDIR 0 get LBDN 0 524288 0 +if lt r 0 +ret 1 +end +fd = int r +s = fsync fd +r = fclose fd +if ne s 0 +ret 1 +end +ret 0 +end + +note gen-G.EXT.tmp: fsync, then renames flag 1 onto gen-G.EXT; 0 or 1 +fn lbrengen g ulong ext ulong to int +lbpgen g ext true +if ne lbsyncp 0 +ret 1 +end +o = lbpkadd 2 +lbpgen g ext false +o = lbpkadd o +put LBPK 0 2 +put LBPK 1 0 +r = renames LBPK 0 o 1 +if ne r 0 +ret 1 +end +ret 0 +end + +note publish generation LBG with manifest LBMAN (sealed) in ADR 0015 order. Expects gen-G.om.tmp, +note gen-G.elf.tmp and, when manifest 52..55 is nonzero, gen-G.cal.tmp already written. Steps: +note 1 om, 2 elf, 3 cal (no-op without calibration), 4 man.tmp written and fsync'd, 5 man renamed, +note 6 LIBDIR fsync, 7 CURRENT.tmp written and fsync'd, 8 CURRENT renamed, 9 LIBDIR fsync. +note crash = K stops after step K with 97 (control C8); 0 ok; 100 + step on failure. +fn lbpub crash ulong to int +g = get LBG 0 +if ne lbrengen g 1 0 +ret 101 +end +if eq crash 1 +ret 97 +end +if ne lbrengen g 2 0 +ret 102 +end +if eq crash 2 +ret 97 +end +if not lbzero 52 56 +if ne lbrengen g 4 0 +ret 103 +end +end +if eq crash 3 +ret 97 +end +lbm2b +lbpgen g 3 true +fd = lbopen 524865 384 +if lt fd 0 +ret 104 +end +w = write fd LBMB 0 2048 +s = fsync fd +r = fclose fd +if or ne w 2048 ne s 0 +ret 104 +end +if eq crash 4 +ret 97 +end +lbpgen g 3 true +o = lbpkadd 2 +lbpgen g 3 false +o = lbpkadd o +put LBPK 0 2 +put LBPK 1 0 +r = renames LBPK 0 o 1 +if ne r 0 +ret 105 +end +if eq crash 5 +ret 97 +end +if ne lbsyncdir 0 +ret 106 +end +if eq crash 6 +ret 97 +end +lbhman +for k ulong 8 +put LBIO k uchar and shr g mul k 8 255 +end +for j ulong 4 +lbputbe add 8 mul j 8 get LBH j +end +lbpcurr true +fd = lbopen 524865 384 +if lt fd 0 +ret 107 +end +w2 = lbwrio fd 40 +s = fsync fd +r = fclose fd +if or ne w2 0 ne s 0 +ret 107 +end +if eq crash 7 +ret 97 +end +lbpcurr true +o = lbpkadd 2 +lbpcurr false +o = lbpkadd o +put LBPK 0 2 +put LBPK 1 0 +r = renames LBPK 0 o 0 +if ne r 0 +ret 108 +end +if eq crash 8 +ret 97 +end +if ne lbsyncdir 0 +ret 109 +end +if eq crash 9 +ret 97 +end +ret 0 +end diff --git a/om/reuse/r1.om b/om/reuse/r1.om new file mode 100644 index 000000000..0da3a292b --- /dev/null +++ b/om/reuse/r1.om @@ -0,0 +1,447 @@ +note SPDX-License-Identifier: Apache-2.0 +note r1.om: the REUSE-1 profile R1 baseline (docs/reuse/REUSE-1.md sections 1.1-1.3 and 12). +note Concatenated after om/sha256/sha256.om. All state is in the pools below (globals cannot be passed). +note R1P the current program, 64 words R1N n (1 word) R1X the state, 4096 words +note R1R r0, r1 of the last r1full R1T FSM table, delta(s, a) at index s*16 + a +note R1PD program digest, R1SD state digest, R1HD r1hash output: SHA-256 as 4 words, word j = +note digest bytes 8j..8j+7 read big endian +note R1HB the hash input buffer (r1hash hashes R1HB[0..n), n <= 65536) +note Exported beyond contract 12 (r1 prefix): +note r1step op c v m one lane op (1 ADD .. 8 SHR) on v with mask m = 2^wl - 1 +note r1chg op a b n validity of one change (op 3..6, payload a, b; DEL needs b = 0) against +note the current program's domain and a state of n values: the new n, or -1 +note r1apply op a b apply one change already validated by r1chg to R1X and R1N; returns +note the old value at a (SET, DEL), the old value at b (SWAP), 0 (INS) +note r1alldom every x_i (i < n) inside the current program's domain +note r1bswap x the byte reversal of x +note r1putw off w w as 8 little-endian bytes into R1HB[off..off+8) +note A call outside a valid program (unknown op or fold) traps 10; R1 never reaches it on valid input. +global R1P ulong 64 +global R1N ulong 1 +global R1X ulong 4096 +global R1R ulong 2 +global R1T ulong 256 +global R1PD ulong 4 +global R1SD ulong 4 +global R1HB uchar 65536 +global R1HD ulong 4 + +note AGG words 2..63 (word 0 and 1 are checked by r1valid) +fn r1vagg to int +wl = get R1P 2 +if and ne wl 8 ne wl 16 +ret 2 +end +f = get R1P 3 +if or lt f 1 gt f 6 +ret 2 +end +k = get R1P 4 +if or lt k 1 gt k 6 +ret 2 +end +lim = shl ulong 1 wl +for j ulong 6 +op = get R1P add 5 mul j 2 +c = get R1P add 6 mul j 2 +if lt j k +if or lt op 1 gt op 8 +ret 2 +end +if ge c lim +ret 2 +end +if and ge op 7 ge c wl +ret 2 +end +else +if or ne op 0 ne c 0 +ret 2 +end +end +end +for i ulong 47 +if ne get R1P add i 17 0 +ret 2 +end +end +ret 0 +end + +note one FSM rule word for S = ns states and A = na symbols: 0 valid, 2 invalid +fn r1vrule w ulong ns ulong na ulong to int +s = and w 255 +lo = and shr w 8 255 +hi = and shr w 16 255 +t = shr w 24 +if or ge s ns ge t ns +ret 2 +end +if or gt lo hi ge hi na +ret 2 +end +ret 0 +end + +note FSM words 2..63 +fn r1vfsm to int +ns = get R1P 2 +if or lt ns 2 gt ns 16 +ret 2 +end +na = get R1P 3 +if or lt na 2 gt na 16 +ret 2 +end +if ge get R1P 4 ns +ret 2 +end +nr = get R1P 5 +if or lt nr 1 gt nr 24 +ret 2 +end +for i ulong 16 +v = get R1P add 6 i +if lt i ns +if ge v 256 +ret 2 +end +else +if ne v 0 +ret 2 +end +end +end +for i ulong 24 +w = get R1P add 22 i +if lt i nr +if ne r1vrule w ns na 0 +ret 2 +end +else +if ne w 0 +ret 2 +end +end +end +for i ulong 18 +if ne get R1P add i 46 0 +ret 2 +end +end +ret 0 +end + +note 0 when R1P is a valid R1 program (1.1), else 2 +fn r1valid to int +if ne get R1P 0 3544411312749556050 +ret 2 +end +fam = get R1P 1 +if eq fam 1 +ret r1vagg +end +if eq fam 2 +ret r1vfsm +end +ret 2 +end + +note v inside the current (valid) program's domain: AGG v < 2^wl, FSM v < A +fn r1indom v ulong to bool +if eq get R1P 1 1 +ret lt v shl ulong 1 get R1P 2 +end +ret lt v get R1P 3 +end + +fn r1step op ulong c ulong v ulong m ulong to ulong +if eq op 1 +ret and add v c m +end +if eq op 2 +ret and sub v c m +end +if eq op 3 +ret and mul v c m +end +if eq op 4 +ret and v c +end +if eq op 5 +ret or v c +end +if eq op 6 +ret xor v c +end +if eq op 7 +ret and shl v c m +end +if eq op 8 +ret shr v c +end +trap 10 +end + +note the lane map e(x) of the current AGG program (1.2) +fn r1lane x ulong to ulong +m = sub shl ulong 1 get R1P 2 1 +k = get R1P 4 +v = x +for j ulong k +q = add 5 mul j 2 +v = r1step get R1P q get R1P add q 1 v m +end +ret v +end + +fn r1ident f ulong to ulong +if or eq f 1 eq f 2 +ret 0 +end +if eq f 3 +ret 18446744073709551615 +end +if or eq f 4 eq f 5 +ret 0 +end +if eq f 6 +ret 18446744073709551615 +end +trap 10 +end + +fn r1fold f ulong a ulong b ulong to ulong +if eq f 1 +ret add a b +end +if eq f 2 +ret xor a b +end +if eq f 3 +if lt b a +ret b +end +ret a +end +if eq f 4 +if gt b a +ret b +end +ret a +end +if eq f 5 +ret or a b +end +if eq f 6 +ret and a b +end +trap 10 +end + +note delta(s, a) of the current FSM program: the target of the lowest-numbered matching rule, else s +fn r1delta s ulong a ulong to ulong +nr = get R1P 5 +for i ulong nr +w = get R1P add 22 i +if eq and w 255 s +lo = and shr w 8 255 +hi = and shr w 16 255 +if and le lo a le a hi +ret shr w 24 +end +end +end +ret s +end + +fn r1fagg to void +f = get R1P 3 +n = get R1N 0 +acc = r1ident f +for i ulong n +acc = r1fold f acc r1lane get R1X i +end +put R1R 0 acc +put R1R 1 n +end + +fn r1ffsm to void +n = get R1N 0 +q = get R1P 4 +sum = ulong 0 +for i ulong n +q = r1delta q get R1X i +sum = add sum get R1P add 6 q +end +put R1R 0 q +put R1R 1 sum +end + +note the baseline: R1P, R1N, R1X -> R1R (1.2) +fn r1full to void +if eq get R1P 1 1 +r1fagg +ret +end +r1ffsm +end + +note shape id (1.1): AGG wl + 256 f + 4096 k + 65536 sum op_j 16^(j-1); FSM 1 +fn r1shape to ulong +if eq get R1P 1 2 +ret 1 +end +k = get R1P 4 +s = add get R1P 2 add mul get R1P 3 256 mul k 4096 +m = ulong 65536 +for j ulong k +s = add s mul m get R1P add 5 mul j 2 +m = mul m 16 +end +ret s +end + +fn r1bswap x ulong to ulong +y = ulong 0 +v = x +for i ulong 8 +y = or shl y 8 and v 255 +v = shr v 8 +end +ret y +end + +fn r1putw off ulong w ulong to void +v = w +for i ulong 8 +put R1HB add off i uchar and v 255 +v = shr v 8 +end +end + +note SHA-256 of R1HB[0..n) into R1HD, through omsha in local 4096-byte chunks +fn r1hash n ulong to void +if gt n 65536 +trap 3 +end +c = new uchar 104 0 +b = new uchar 4096 0 +w = new ulong 64 0 +b8 = new ulong 512 0 +omsha c b 0 0 w b8 +off = ulong 0 +while lt off n bound 17 +m = sub n off +if gt m 4096 +m = 4096 +end +for i ulong m +put b i get R1HB add off i +end +omsha c b m 1 w b8 +off = add off m +end +omsha c b 0 2 w b8 +for j ulong 4 +put R1HD j r1bswap get w j +end +end + +note program digest: SHA-256 of the 512 bytes of R1P (64 LE words) into R1PD +fn r1pdig to void +for i ulong 64 +r1putw mul i 8 get R1P i +end +r1hash 512 +for j ulong 4 +put R1PD j get R1HD j +end +end + +note state digest: SHA-256 of the n values as LE words into R1SD +fn r1sdig to void +n = get R1N 0 +for i ulong n +r1putw mul i 8 get R1X i +end +r1hash mul n 8 +for j ulong 4 +put R1SD j get R1HD j +end +end + +fn r1alldom to bool +n = get R1N 0 +for i ulong n +if not r1indom get R1X i +ret false +end +end +ret true +end + +note one change (1.3) against a state of n values: the new n, or -1 when invalid +fn r1chg op ulong a ulong b ulong n ulong to long +if eq op 3 +if and lt a n r1indom b +ret long n +end +ret -1 +end +if eq op 4 +if and and le a n lt n 4096 r1indom b +ret long add n 1 +end +ret -1 +end +if eq op 5 +if and lt a n eq b 0 +ret long sub n 1 +end +ret -1 +end +if eq op 6 +if and lt a n lt b n +ret long n +end +ret -1 +end +ret -1 +end + +fn r1apply op ulong a ulong b ulong to ulong +n = get R1N 0 +if eq op 3 +old = get R1X a +put R1X a b +ret old +end +if eq op 4 +t = n +while gt t a bound 4097 +put R1X t get R1X sub t 1 +t = sub t 1 +end +put R1X a b +put R1N 0 add n 1 +ret 0 +end +if eq op 5 +old = get R1X a +t = a +while lt add t 1 n bound 4097 +put R1X t get R1X add t 1 +t = add t 1 +end +put R1N 0 sub n 1 +ret old +end +if eq op 6 +old = get R1X b +put R1X b get R1X a +put R1X a old +ret old +end +trap 10 +end diff --git a/om/reuse/router.om b/om/reuse/router.om new file mode 100644 index 000000000..edc64afc6 --- /dev/null +++ b/om/reuse/router.om @@ -0,0 +1,905 @@ +note SPDX-License-Identifier: Apache-2.0 +note router.om: the REUSE-1 arm process (docs/reuse/REUSE-1.md section 2, 3.4 load, 4 revocation, and +note clarifications K1, K2, K9). Last unit; holds fn main. Every arm binary is this router. +note argv: ARM LIBDIR MANSHA ALLOW TAPFILE SPLIT [loaded]; ARM echo full cache method delta select tap. +note Startup exit codes (no response is written): 2 usage (argc, ARM), 2 tap refused (extopen). +note Protocol: little-endian u64 words on stdin/stdout. Request op, p, p words; response status, q, +note q words. Framing (K2): op outside 1..10 or p > 7234 -> 4 and exit 4 (the payload is not read); +note otherwise the payload is read whole; a p that does not match its op -> 4; ops 2..8 without a +note session -> 6; then the op's own statuses. EOF on stdin without END -> exit 3, no response. +note Load (3.4): ARM method/delta/select, LIBDIR not "-", no trailing "loaded": lbload (lib.om) execs +note the generation and never returns on success; on its nonzero return this process serves every +note QUERY as full with route 5. A generation (argv ends in "loaded") never loads again; it reads its +note lineage (lblineage) and checks it against ALLOW at every BEGIN and, whenever the stat snapshot +note of ALLOW (inode, size, mtime ns, ctime ns) changed, at QUERY: a revoked or unverifiable lineage +note gives route 5 from that QUERY on for the rest of the process. select in a generation reads its +note calibration from /proc/self/fd/7, the memfd of verified bytes lbload left open before exec (K16), +note iff the manifest names one (LBCAL); never LIBDIR/gen-G.cal. With LIBDIR "-" it has no calibration. +note Arms at QUERY (route): echo 0 0 (0); full r1full (1); tap r1full and one experience record (1); +note cache key SHA-256(program digest, n, state digest), the state digest recomputed only at the first +note QUERY after a change, 4096 entries FIFO: hit (2), miss r1full (1); method mtrun (3) when a +note template is bound and passed G3, else r1full (1), G3 failure r1full (5) for the session; delta: +note the delta state (4) of the bound method or of the baseline (t = -1), rebuilt by dlbind at a QUERY +note after a drop, else method (3) or full (1); select: slpick among full (1), method (3), delta (4); +note once sloff reports churn (5.2) the delta state is dropped and not maintained for the session. +note STATS answers 32 counters RTST: 0..5 QUERYs by route 0..5, 6 sessions, 7 change items applied, +note 8 PROGRAMs applied, 9 refused requests, 10 ckbind calls, 11 bind failures, 12 G3 FIFO hits, +note 13 G3 lane pairs, 14..25 dlstat 0..11, 26 select delta disables (SLDIS), 27 select refusals +note (SLREF), 28 cache entries, 29 template count (mtcount), 30 load refusal code, 31 flags +note (1 generation, 2 refused, 4 revoked, 8 calibration path set, 16 calibration loaded by slload). +text RTTE "echo" +text RTTF "full" +text RTTC "cache" +text RTTM "method" +text RTTD "delta" +text RTTS "select" +text RTTT "tap" +text RTTL "loaded" +text RTTCAL "/proc/self/fd/7" +global RTARM ulong 1 +global RTFLG ulong 1 +global RTLC ulong 1 +global RTLIN ulong 1 +global RTSES ulong 1 +global RTSN ulong 1 +global RTQN ulong 1 +global RTNC ulong 1 +global RTT long 1 +global RTBF ulong 1 +global RTDB ulong 1 +global RTSD ulong 1 +global RTPS ulong 64 +global RTQ ulong 7234 +global RTIB uchar 65536 +global RTIP ulong 2 +global RTOW ulong 34 +global RTOB uchar 272 +global RTST ulong 32 +global RTCK ulong 24576 +global RTCN ulong 1 +global RTCH ulong 1 +global RTKEY ulong 4 +global RTA uchar 4096 + +note ---- input and output ---- +fn rtfill to bool +k = read 0 RTIB 0 65536 +if le k 0 +ret false +end +put RTIP 0 0 +put RTIP 1 ulong k +ret true +end + +fn rtbyte to ulong +p = get RTIP 0 +if ge p get RTIP 1 +if not rtfill +exit 3 +end +p = 0 +end +put RTIP 0 add p 1 +ret ulong get RTIB p +end + +fn rtword to ulong +p = get RTIP 0 +if le add p 8 get RTIP 1 +v = ulong 0 +for i ulong 8 +v = or shl v 8 ulong get RTIB sub add p 7 i +end +put RTIP 0 add p 8 +ret v +end +v = ulong 0 +for i ulong 8 +v = or v shl rtbyte mul i 8 +end +ret v +end + +note status s, q words RTOW[0..q) +fn rtresp s ulong q ulong to void +rtputw 0 s +rtputw 8 q +for i ulong q +rtputw mul add i 2 8 get RTOW i +end +n = mul add q 2 8 +off = ulong 0 +while lt off n bound 1000 +k = write 1 RTOB off sub n off +if le k 0 +exit 5 +end +off = add off ulong k +end +end + +fn rtputw o ulong w ulong to void +v = w +for i ulong 8 +put RTOB add o i uchar and v 255 +v = shr v 8 +end +end + +fn rtstat s ulong to void +if ne s 0 +put RTST 9 add get RTST 9 1 +end +rtresp s 0 +end + +note ---- argv ---- +note argv i into RTA: its length, or -1 +fn rtarg i ulong to long +n = argread i RTA 0 4096 +if or lt n 0 gt n 4095 +ret -1 +end +ret n +end + +fn rtdash i ulong to bool +n = rtarg i +ret and eq n 1 eq get RTA 0 45 +end + +note RTA[0..n) equals text id (1 echo .. 7 tap, 8 loaded) +fn rteq id ulong n ulong to bool +if eq id 1 +ret rteqe n +end +if eq id 2 +ret rteqf n +end +if eq id 3 +ret rteqc n +end +if eq id 4 +ret rteqm n +end +if eq id 5 +ret rteqd n +end +if eq id 6 +ret rteqs n +end +if eq id 7 +ret rteqt n +end +ret rteql n +end + +fn rteqe n ulong to bool +if ne n len RTTE +ret false +end +for i ulong n +if ne get RTA i get RTTE i +ret false +end +end +ret true +end + +fn rteqf n ulong to bool +if ne n len RTTF +ret false +end +for i ulong n +if ne get RTA i get RTTF i +ret false +end +end +ret true +end + +fn rteqc n ulong to bool +if ne n len RTTC +ret false +end +for i ulong n +if ne get RTA i get RTTC i +ret false +end +end +ret true +end + +fn rteqm n ulong to bool +if ne n len RTTM +ret false +end +for i ulong n +if ne get RTA i get RTTM i +ret false +end +end +ret true +end + +fn rteqd n ulong to bool +if ne n len RTTD +ret false +end +for i ulong n +if ne get RTA i get RTTD i +ret false +end +end +ret true +end + +fn rteqs n ulong to bool +if ne n len RTTS +ret false +end +for i ulong n +if ne get RTA i get RTTS i +ret false +end +end +ret true +end + +fn rteqt n ulong to bool +if ne n len RTTT +ret false +end +for i ulong n +if ne get RTA i get RTTT i +ret false +end +end +ret true +end + +fn rteql n ulong to bool +if ne n len RTTL +ret false +end +for i ulong n +if ne get RTA i get RTTL i +ret false +end +end +ret true +end + +note ARM: 1 echo 2 full 3 cache 4 method 5 delta 6 select 7 tap, 0 unknown +fn rtparm to ulong +n = rtarg 1 +if lt n 0 +ret 0 +end +for id ulong 7 +if rteq add id 1 ulong n +ret add id 1 +end +end +ret 0 +end + +note the arm has a library path: method, delta, select +fn rtlibarm to bool +a = get RTARM 0 +ret and ge a 4 le a 6 +end + +fn rtdelarm to bool +a = get RTARM 0 +ret or eq a 5 eq a 6 +end + +note route 5 for every QUERY: refused generation or revoked lineage +fn rtdead to bool +ret ne and get RTFLG 0 6 0 +end + +note ---- startup ---- +fn rtsetallow to void +if rtdash 4 +put EXALEN 0 0 +ret +end +n = ulong rtarg 4 +for i ulong n +put EXAPATH i get RTA i +end +put EXALEN 0 n +end + +fn rtcal to void +if ne get LBCAL 0 1 +ret +end +for i ulong len RTTCAL +put SLPATH i get RTTCAL i +end +put SLPATH len RTTCAL 0 +put RTFLG 0 or get RTFLG 0 8 +end + +note a refused generation load: code x (STATS 30; 255 when it is not 1..254), route 5 from now on +fn rtrefuse x int to void +c = ulong 255 +if and gt x 0 lt x 255 +c = ulong x +end +put RTLC 0 c +put RTFLG 0 or get RTFLG 0 2 +end + +fn rtinit to void +c = argcnt +if and ne c 7 ne c 8 +exit 2 +end +loaded = false +if eq c 8 +n = rtarg 7 +if or lt n 0 not rteq 8 ulong n +exit 2 +end +loaded = true +end +a = rtparm +if eq a 0 +exit 2 +end +put RTARM 0 a +put RTT 0 -1 +rtsetallow +if eq a 7 +if ne extopen 0 +exit 2 +end +end +if and rtlibarm not rtdash 2 +if loaded +rtgen +else +x = lbload +rtrefuse x +end +end +if eq a 6 +if eq slload 0 +put RTFLG 0 or get RTFLG 0 16 +end +end +end + +note a generation: lineage from the manifest pinned by MANSHA, calibration path +fn rtgen to void +put RTFLG 0 or get RTFLG 0 1 +x = lblineage +if ne x 0 +rtrefuse x +ret +end +if gt get LBLN 0 0 +put RTLIN 0 1 +end +if eq get RTARM 0 6 +rtcal +end +end + +note ---- revocation ---- +note every lineage digest allowed by the loaded ALLOW +fn rtlinok to bool +d = new ulong 4 0 +n = get LBLN 0 +for e ulong n +for j ulong 4 +put d j get LBLD add mul e 4 j