spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / om / p256 / README.md
| 1 | # om/p256: the #67 judge signature scheme in seeded .om (library only) |
| 2 | |
| 3 | `p256.om` is ECDSA over NIST P-256 with SHA-256 and RFC 6979 deterministic nonces. It reproduces the |
| 4 | signatures of the spark-rsi judge signer byte for byte. It is a parity-proven library and nothing more: |
| 5 | |
| 6 | - it is **not a signer deployment**: nothing calls it from the judge, the daemon or the promotion gate; |
| 7 | - **no key was read**: every private key used here is a public test key (`[7;32]`, `[9;32]`, the RFC 6979 |
| 8 | A.2.5 key); |
| 9 | - **no receipt was signed**: the INTERPLANE receipts below were only verified, under their published key. |
| 10 | |
| 11 | Refs spark-rsi-mv #67 (judge signer, ".om throughout") and #73 (REUSE-1, whose receipt is unsigned |
| 12 | because no `.om` signer existed). |
| 13 | |
| 14 | ## The scheme it matches |
| 15 | |
| 16 | | Property | Rust judge | Source | |
| 17 | |---|---|---| |
| 18 | | Curve, crate | NIST P-256, RustCrypto `p256` 0.14.0 (`ecdsa` 0.17.0, `rfc6979` 0.6.0) | `Cargo.toml:41`, `Cargo.lock` | |
| 19 | | Key | 32-byte big-endian scalar, `SigningKey::from_slice` (0 and >= n refused) | `src/actor/judge.rs:1070-1082` | |
| 20 | | Message | the 32 raw bytes of `receipt_digest` (hex-decoded) | `src/evaluator/mod.rs:259-264` | |
| 21 | | Hash | `Signer::sign` hashes the message once with SHA-256; ECDSA signs that hash | `ecdsa-0.17.0/src/signing.rs:155-172` | |
| 22 | | Nonce | RFC 6979 section 3.2 HMAC-DRBG over (d, bits2octets(h)), no extra entropy (`ad = []`) | `ecdsa-0.17.0/src/signing.rs:171-172`, `rfc6979-0.6.0/src/lib.rs:73-100` | |
| 23 | | Output | `r \|\| s`, 64 bytes, big endian; the judge prefixes `tpm2-p256:` and hex | `src/evaluator/mod.rs:263` | |
| 24 | | Low-S | **not applied**: `p256` sets `NORMALIZE_S = false`, so s is whatever the equation gives and verification accepts both s and n - s | `p256-0.14.0/src/ecdsa.rs:60`, `ecdsa-0.17.0/src/hazmat.rs:85,142` | |
| 25 | | Public key | uncompressed SEC1, `0x04 \|\| X \|\| Y` (65 bytes) | `src/actor/judge.rs:1088` | |
| 26 | |
| 27 | The low-S row is also checked by data: every parity vector carries a `V` line with s replaced by n - s, |
| 28 | and Rust accepted all 2048 of them (`parity-rust.txt`); `p2vrfy` gives the same verdict. |
| 29 | |
| 30 | ## Files |
| 31 | |
| 32 | | File | What it is | |
| 33 | |---|---| |
| 34 | | `p256.om` | The library (682 lines): field and scalar arithmetic, points, HMAC-SHA-256, RFC 6979, sign, verify, public key. Functions and constants only. | |
| 35 | | `sha256.om` | Byte copy of aien-dev/omega-mv `om/sha256/sha256.om` at main `acb3f15c` (last change `a1cfb4a3`), sha256 `7f034814303a8a40b4bc56383ddd55ab2ae8460291f80793340bc3b0ac024e21`. The same bytes as `om/reuse-eval/sha256.om` on the REUSE-1 branch. Not modified. | |
| 36 | | `tests/run.om` | The runner: self tests, then every vector file named on the command line. Exit 0 only when every check passed. | |
| 37 | | `tests/rfc6979-a25.txt` | RFC 6979 A.2.5, P-256 with SHA-256: the public key, both nonces k and both signatures ("sample", "test"), copied from the RFC text. | |
| 38 | | `tests/parity-rust.txt` | Recorded from Rust `p256` 0.14.0: 2 public keys, 2048 signatures (1024 digests per key), 4864 verify verdicts. | |
| 39 | | `tests/edge-rust.txt` | Recorded from Rust: 3 signatures and 3 verdicts for a message whose SHA-256 is >= n. | |
| 40 | | `tests/interplane.txt` | 3 real judge receipts from aien-dev/interplane-mv `a68300c2`, each followed by a tampered copy, with Rust's verdicts. | |
| 41 | |
| 42 | ## API |
| 43 | |
| 44 | Callers own four arrays and pass them in: `w ulong 1024` (workspace), `c uchar 104` (SHA-256 context), |
| 45 | `b uchar 4096` (byte buffer), `s ulong 64` (SHA-256 schedule). Call `p2init w` once. |
| 46 | |
| 47 | | Call | Inputs | Result | |
| 48 | |---|---|---| |
| 49 | | `p2sign w c b s mlen` | key d at `b[3264..3296)`, message at `b[2048..2048+mlen)`, mlen <= 1024 | 0 and `r \|\| s` at `b[3200..3264)`; 1 when d is 0 or >= n | |
| 50 | | `p2vrfy w c b s mlen` | public key at `b[3072..3137)`, `r \|\| s` at `b[3200..3264)`, message as above | 1 valid, 0 refused | |
| 51 | | `p2pub w b` | key d at `b[3264..3296)` | 0 and `0x04 \|\| X \|\| Y` at `b[3072..3137)`; 1 when d is refused | |
| 52 | |
| 53 | `p2vrfy` refuses a key that is not `0x04 || X || Y` with X, Y < p on the curve, r or s outside 1..n-1, |
| 54 | a sum u1 G + u2 Q equal to the identity, or x mod n different from r. These are the checks of |
| 55 | `VerifyingKey::from_sec1_bytes`, `Signature::from_slice` and `verify_prehashed` (`ecdsa-0.17.0/src/hazmat.rs:136-159`). |
| 56 | |
| 57 | ## How it is built |
| 58 | |
| 59 | - 256-bit numbers are 8 limbs of 32 bits in ulong cells. omc has no 64x64 high product, and a 32-bit limb |
| 60 | product plus two limbs is at most 2^64 - 1, so the Montgomery multiplication (CIOS, R = 2^256) never |
| 61 | wraps a ulong where a carry is meant. |
| 62 | - One Montgomery multiplication serves both moduli (p and n). n0', R^2 and R are derived at `p2init` |
| 63 | (Newton steps, 512 modular doublings), so the only hard-coded constants are p, n, b, Gx and Gy |
| 64 | (SEC 2 v2 section 2.4.2). The self tests check G is on the curve, n G is the identity and |
| 65 | (n - 1) G = -G. |
| 66 | - Points are projective with complete addition for a = -3 (Renes, Costello, Batina 2015, Algorithm 4, |
| 67 | in the grouping of RustCrypto `primeorder` 0.14). The same formula doubles, so there are no special |
| 68 | cases for the identity or equal inputs. |
| 69 | - Scalar multiplication is double-and-add-always over all 256 bits with a masked select. |
| 70 | - Inversion is Fermat (a^(m-2)) with the public exponent m - 2. |
| 71 | |
| 72 | ## Side-channel limits |
| 73 | |
| 74 | This is stated for a future signer deployment; nothing in this PR deploys one. |
| 75 | |
| 76 | - **Branch free with respect to secrets:** |
| 77 | - field and scalar add, sub, mul and the final conditional subtraction (masked select, no branch); |
| 78 | - the loop over the 256 bits of the secret scalar in `p2smul` (fixed trip count, masked select of |
| 79 | the sum, array indices from public counters only); |
| 80 | - inversion (the exponent m - 2 is public). |
| 81 | - **Not constant time:** |
| 82 | 1. The RFC 6979 retry branches on secret-derived values: k >= n, r = 0, s = 0. The Rust crate |
| 83 | does the same, and these happen with probability about 2^-32 or less. |
| 84 | 2. `p2sign` returns early for an invalid key. |
| 85 | 3. omc gives no constant-time guarantee: its instruction selection and the bounds-check branches |
| 86 | it emits are not audited for this. No timing measurement was made against secret inputs. |
| 87 | 4. The key, the nonce and the HMAC-DRBG state stay in the caller's arrays. They are never zeroed: |
| 88 | the language has no zeroize, and the stack is not cleared. |
| 89 | - **Not variable-time optimized:** verification uses the same ladder twice, about 2x slower than a |
| 90 | variable-time multi-scalar method. Its inputs are public, so this costs speed, not safety. |
| 91 | |
| 92 | ## Results (DGX Spark, Cortex-X925/A725, `nice -n 19`, one run) |
| 93 | |
| 94 | ``` |
| 95 | self tests: 3 pass, 0 fail |
| 96 | file om/p256/tests/rfc6979-a25.txt: K 1/1 N 2/2 S 2/2 V 0/0 malformed 0 |
| 97 | file om/p256/tests/parity-rust.txt: K 2/2 N 0/0 S 2048/2048 V 4864/4864 malformed 0 |
| 98 | file om/p256/tests/edge-rust.txt: K 0/0 N 0/0 S 3/3 V 3/3 malformed 0 |
| 99 | file om/p256/tests/interplane.txt: K 0/0 N 0/0 S 0/0 V 6/6 malformed 0 |
| 100 | timing p2sign: calls 2053 mean_us 1230 min_us 1224 max_us 2694 |
| 101 | timing p2vrfy (S lines, valid): calls 2053 mean_us 2337 min_us 2327 max_us 5086 |
| 102 | timing p2vrfy (V lines, mixed): calls 4873 mean_us 2151 min_us 0 max_us 2433 |
| 103 | p256: PASS |
| 104 | wall_s 20.22 user_s 20.22 sys_s 0.00 maxrss_kb 2080 |
| 105 | ``` |
| 106 | |
| 107 | What the lines mean: |
| 108 | |
| 109 | - `S` counts a byte-equal signature **and** a verification of it under the public key derived by `p2pub`. |
| 110 | - `V` lines include s replaced by n - s, a tampered digest, the other key, r or s with one bit flipped, |
| 111 | s = 0, r = n, and a key with Y changed. |
| 112 | - `p2vrfy` refuses some inputs early (range and format checks, timed 0 us). The mixed V-line timing |
| 113 | therefore includes those refusals. |
| 114 | - The first call of a process is about 2x slower (cold caches): 2.7 ms sign, 5.1 ms verify on the RFC |
| 115 | file alone. |
| 116 | |
| 117 | ## Mutants (throwaway copies under /tmp, not committed) |
| 118 | |
| 119 | Every mutant was run over the four vector files. A mutant counts as detected only when the runner printed |
| 120 | at least one `MISMATCH` line and exited nonzero. |
| 121 | |
| 122 | | Mutant | Exit | MISMATCH lines | Detected | |
| 123 | |---|---|---|---| |
| 124 | | M1: HMAC-DRBG V initialised to 0x02 instead of 0x01 | 1 | 2055 | yes | |
| 125 | | M2: one limb of Gy changed by 1 | 1 | 4110 (self tests 0/3) | yes | |
| 126 | | M3: final conditional subtraction removed | 104 (trap 4 on the first `N` line) | 1 (the RFC `K` line) | yes, then trapped | |
| 127 | | M4: low-S normalisation added to signing | 1 | 1059 | yes | |
| 128 | | M5: verification compares x with itself | 1 | 2435 | yes | |
| 129 | | M6: Montgomery reduction step removed | 1 | 4110 | yes | |
| 130 | | M7: h mod n removed before RFC 6979 and z | 1 | 3, all in `edge-rust.txt` (0 in the other files) | yes, only by `edge-rust.txt` | |
| 131 | | M8: on-curve check of the public key removed | 0 | 0 | **no** (see below) | |
| 132 | | M9: HMAC opad 0x5d instead of 0x5c | 1 | 2055 | yes | |
| 133 | |
| 134 | **M8 survives.** Every off-curve key in the vectors is still refused without the check, because the |
| 135 | verification equation fails anyway. |
| 136 | |
| 137 | - No vector here is an off-curve key that would verify. Building one against complete a = -3 |
| 138 | formulas, which use b, is an open problem; it was not attempted. |
| 139 | - The check matches `VerifyingKey::from_sec1_bytes` and stays. |
| 140 | - Wycheproof `ecdsa_secp256r1_sha256` and NIST CAVP SigVer are NOT_RUN. |
| 141 | |
| 142 | ## Reproduce |
| 143 | |
| 144 | ```sh |
| 145 | OMC=/path/to/omc # the pinned seeded omc, sha256 734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 |
| 146 | cat om/p256/sha256.om om/p256/p256.om om/p256/tests/run.om | "$OMC" > /tmp/p256-run.elf |
| 147 | chmod +x /tmp/p256-run.elf |
| 148 | /tmp/p256-run.elf om/p256/tests/rfc6979-a25.txt om/p256/tests/parity-rust.txt \ |
| 149 | om/p256/tests/edge-rust.txt om/p256/tests/interplane.txt |
| 150 | ``` |
| 151 | |
| 152 | Run it from the repo root. Exit 0 means every check passed; 1 means a mismatch or a malformed line; 2 means |
| 153 | a file could not be read. |
| 154 | |
| 155 | ## How the Rust data was recorded |
| 156 | |
| 157 | The Rust data came from a throwaway program under `/tmp`, never committed. It uses `p256` 0.14.0 with |
| 158 | spark-rsi-mv's `Cargo.lock` and signs with `SigningKey::from_slice(key).sign(&msg)`, the same call as |
| 159 | `src/evaluator/mod.rs:262`. It verifies with `VerifyingKey::from_sec1_bytes` + `Signature::from_slice` + |
| 160 | `verify`. |
| 161 | |
| 162 | - **Parity digests:** `msg_i = SHA-256(seed || u32be(i))` for i in 0..1024, where |
| 163 | `seed = SHA-256("spark-rsi-mv om/p256 parity v1")`, signed under both keys. |
| 164 | - **Edge message:** found by brute force over `"spark-rsi-mv p256 h>=n " || u64be(i)` (i = 2825506747). |
| 165 | - **INTERPLANE receipts:** the key is `provenance/fixtures/rsi-eval-v2/judge.pub` at interplane-mv |
| 166 | `a68300c2`, and all three receipts verify under it in both Rust and `.om`. |
| 167 | |
| 168 | ## What remains (not in this PR) |
| 169 | |
| 170 | 1. **Key custody.** The real signing key lives with the judge account (`docs/RECEIPT-V2.md`, "Key |
| 171 | separation"; `/var/lib/aien-judge`), and policy says atlas-vault. A `.om` signer has to load it there, |
| 172 | under that account, without copying it. Nothing here touches it. |
| 173 | 2. **RECEIPT-V2 digest in `.om`.** `compute_digest_v2` (`src/evaluator/mod.rs:143-201`) is still Rust |
| 174 | only. A `.om` successor needs it byte-equal before it can sign what the Rust judge signs. |
| 175 | 3. **REUSE-1 receipt mapping.** The REUSE-1 FINAL receipt (`om/reuse-eval/receipt.om`, identity domain |
| 176 | `spark-rsi.reuse1-receipt.v2-unsigned`) is not a RECEIPT-V2 receipt. Signing it needs either a |
| 177 | RECEIPT-V2-shaped record or a versioned companion domain (#67 item 6), plus the operator action in |
| 178 | item 1. |
| 179 | - Any such signature is written after FINAL and must be labelled so. |
| 180 | 4. **Cutover.** The Rust signer is removed only after a `.om` signer is qualified and the judge and the |
| 181 | promotion gate call it. |
| 182 | 5. **Further vectors.** Wycheproof and NIST CAVP vectors, and an invalid-curve vector that kills M8. |