synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / docs / J3_CRYPTO_REFERENCE.md
| 1 | # J3: SHA-256 and Ed25519 Reference for the Omega Port (Rust Side) |
| 2 | |
| 3 | ADR 0003 section 9 step J3 (issue #55): "Omega port: SHA-256 with the hardware instructions and Ed25519 |
| 4 | verification". Acceptance: "Digest and signature test vectors match the Rust crates exactly; all-core |
| 5 | throughput reported against the A0 references." |
| 6 | |
| 7 | **Scope of this change.** The Omega implementation itself belongs in the `omega` repository and is not |
| 8 | here; it does not exist yet (no SHA-2 instruction or Ed25519 code was found in `omega` at `1edf41a5`). |
| 9 | This change delivers the Synapse half that the acceptance compares against: the sealed vector corpus |
| 10 | that defines "match the Rust crates exactly", the Rust crates themselves on the SHA-2 instructions, and |
| 11 | their all-core throughput against the A0 references. Issue #55 stays open until the Omega side passes |
| 12 | the corpus. |
| 13 | |
| 14 | ## 1. Acceptance and Where It Stands |
| 15 | |
| 16 | | J3 acceptance | Synapse side (this change) | Omega side | |
| 17 | | :--- | :--- | :--- | |
| 18 | | Digest and signature test vectors match the Rust crates exactly | `evidence/j3-crypto-vectors/vectors.bin`: 299 records written by the Rust crates, SHA-256 `523a6199b9dcc383573c38e3dac9c1480ef85ad3c03ec436786207978721c94b`, pinned as `CORPUS_SHA256` and checked by test `sealed_corpus_digest_is_pinned`. Every record is recomputed by `ed25519-dalek` and `sha2` (`sealed_corpus_matches_the_rust_crates_exactly`). FIPS 180-4 and RFC 8032 published answers are checked against the crates and required in the corpus. Nine Ed25519 edge cases each catch a named wrong implementation (section 2.1). `cargo test` also builds and runs the corpus test on `sha2`'s portable backend | NOT_RUN: no Omega implementation yet; it must decode this file and give every digest and verdict | |
| 19 | | All-core throughput reported against the A0 references | Section 4: two sealed runs of the A0 probes | NOT_RUN | |
| 20 | |
| 21 | Red before green: the first ten tests were committed against stubs (`8f4975d`) and failed for the stated |
| 22 | reasons (`evidence/j3-crypto-vectors/red-green/red.txt`); green in `green.txt`. Review round: the edge-case |
| 23 | and pinned-digest tests failed against stubs (`21c90e1`, `red-green/review-red.txt`, which also holds the |
| 24 | negative control for the backend check), green in `review-green.txt`. |
| 25 | |
| 26 | ## 2. The Corpus |
| 27 | |
| 28 | Format (ADR 0003 P5: fixed width, little endian, length prefixed), documented in |
| 29 | `crates/synapse-bench/src/crypto_vectors.rs`: |
| 30 | |
| 31 | ```text |
| 32 | header magic "SYNJ3VEC" | version u16 = 1 | reserved u16 = 0 | count u32 |
| 33 | record kind 1 (SHA-256): len u32 | message | digest[32] |
| 34 | kind 2 (Ed25519): verdict u8 | public_key[32] | signature[64] | len u32 | message |
| 35 | verdict bits: 0 key decodes, 1 `verify` accepts, 2 `verify_strict` accepts |
| 36 | trailer SHA-256 of every preceding byte |
| 37 | ``` |
| 38 | |
| 39 | Synapse uses both Ed25519 calls (`verify` in `synapse-core`, `synapse-auth`, `synapse-radicle`; |
| 40 | `verify_strict` in `synapse-evidence`), and they disagree on small-order points, so a port must reproduce |
| 41 | three decisions per case, not one. |
| 42 | |
| 43 | SHA-256 cases: the four FIPS 180-4 examples, every length from 1 to 257 bytes, 1,000, 4,096, 11,949 |
| 44 | (Gate 4 change), 16,384 (A0 block) and 65,536 bytes, and all-zero and all-one messages. Ed25519 cases: |
| 45 | RFC 8032 TEST 1 to 3; eight deterministic keys over messages of 0 to 11,949 bytes; changed message, R, S, |
| 46 | high bit of S, wrong key; identity, order-2 and non-canonical identity keys; a key that does not decode; |
| 47 | and the edge cases below. The decoder refuses bad magic, version, reserved bits, unknown kinds, oversize |
| 48 | lengths (cap 1 MiB, checked before allocation), trailing bytes, checksum mismatch and every truncation, |
| 49 | each with a typed error. |
| 50 | |
| 51 | ### 2.1 Edge cases and the wrong implementation each one catches |
| 52 | |
| 53 | Classes from Chalkias, Garillot and Nikolaenko, "Taming the many EdDSAs" (SSR 2020, IACR ePrint |
| 54 | 2020/1244). Points are built with `curve25519-dalek`; verdicts come from `ed25519-dalek`. The test |
| 55 | `each_edge_case_is_in_the_corpus_and_catches_a_wrong_implementation` runs, for each case, a model that |
| 56 | differs from the right one in one rule and requires its answer to differ from dalek's. The test also |
| 57 | requires each wrong model to accept RFC 8032 TEST 1. `models_reproduce_dalek_on_the_whole_corpus` shows |
| 58 | that the right models (cofactorless, canonical S, R compared as bytes; strict adds R decoding and |
| 59 | small-order checks on A and R) give dalek's answer on all 299 records. |
| 60 | |
| 61 | | Case | verify | verify_strict | Wrong implementation caught (compared with) | |
| 62 | | :--- | :---: | :---: | :--- | |
| 63 | | `noncanonical_r_y_p_plus_1`: R = identity as y = p + 1, A = identity, S = 0 | refuse | refuse | decodes R and compares points (`verify`) | |
| 64 | | `noncanonical_r_negative_zero`: R = identity with the x sign bit set | refuse | refuse | decodes R and compares points (`verify`) | |
| 65 | | `noncanonical_a_y_p`: A = order-4 point encoded as y = p | accept | refuse | hashes the re-encoded A instead of the given bytes (`verify`) | |
| 66 | | `small_order_a_and_r`: A of order 8, R of order 4, S = 0 | accept | refuse | strict without small-order checks (`verify_strict`) | |
| 67 | | `small_order_r_valid_key`: R = identity under an ordinary key | accept | refuse | strict that checks A but not R (`verify_strict`) | |
| 68 | | `mixed_order_a_k_not_multiple_of_8`: A = aB + T8 | refuse | refuse | cofactored equation (`verify`) | |
| 69 | | `mixed_order_a_k_multiple_of_8`: A = aB + T8, k a multiple of 8 | accept | accept | refuses keys that are not torsion free (`verify`) | |
| 70 | | `mixed_order_r`: R = rB + T8 | refuse | refuse | cofactored equation (`verify`), and cofactored with strict checks (`verify_strict`) | |
| 71 | | `noncanonical_s_plus_l`: RFC 8032 TEST 1 with S + L | refuse | refuse | reduces S modulo L instead of refusing it (`verify`) | |
| 72 | |
| 73 | ## 3. The `sha2` Build Fix |
| 74 | |
| 75 | A0 found that `sha2` 0.10.9 was built without `asm`, and on aarch64 that crate then compiles only its |
| 76 | software backend. In `sha2/src/sha256.rs` the aarch64 backend is gated on |
| 77 | `all(feature = "asm", target_arch = "aarch64")`, and `asm = ["sha2-asm"]` in its `Cargo.toml`; the only |
| 78 | other aarch64 name, `asm-aarch64`, is an alias for `asm`. So no narrower switch exists in 0.10: enabling |
| 79 | the intrinsics backend also builds `sha2-asm` (unused on aarch64) and its `cc` build script. The |
| 80 | workspace dependency now enables `asm`. `sha2` then uses `sha2::sha256::aarch64::sha256_compress` when |
| 81 | `cpufeatures` reports `sha2` at run time and its portable code otherwise. `ed25519-dalek` shares the |
| 82 | same `sha2`, so its SHA-512 also gets the aarch64 backend (gated the same way in `sha512.rs`, with |
| 83 | run-time `sha3` detection). |
| 84 | |
| 85 | Proof, all in `cargo test`: |
| 86 | - `workspace_sha2_is_built_with_its_aarch64_instruction_backend` reads the resolved features from |
| 87 | `cargo metadata`. It then checks the backend actually in use: the instruction function is linked into |
| 88 | the running test binary, and the CPU reports `sha2`. It fails without cargo. |
| 89 | - `portable_sha2_backend_gives_the_sealed_corpus` builds the crate again with `sha2/force-soft` in a |
| 90 | separate target directory. It runs the sealed-corpus test there, plus a check that the portable backend |
| 91 | is the one in use. `sha2` 0.10 fixes its backend at build time and `cpufeatures` caches the hardware |
| 92 | answer, so the portable code cannot run inside the same process. The negative control (the portable |
| 93 | check run in the instruction build fails) is in `red-green/review-red.txt`. |
| 94 | - `red-green/backend-disassembly.txt`: the default build links `sha256_compress` with `sha256h`; the |
| 95 | `force-soft` build does not. |
| 96 | |
| 97 | `Cargo.lock` was updated by cargo, not by hand. It adds `sha2-asm` 0.6.4, `cc`, `find-msvc-tools` and |
| 98 | `shlex`, and `curve25519-dalek` (already in the graph through `ed25519-dalek`) becomes a direct |
| 99 | dependency of `synapse-bench`. Building needs a C compiler. |
| 100 | |
| 101 | ## 4. All-Core Throughput Against the A0 References |
| 102 | |
| 103 | Two runs of the A0 probes (`synapse-bench bounds`, same instrument and verifier; `bounds-verify` passes on |
| 104 | both). Each protocol was committed and pushed before its run: |
| 105 | - `evidence/j3-crypto-throughput/` (`000db4e`, A0's protocol byte for byte). The SHA-256 windows lasted |
| 106 | 0.50 to 0.88 s (7 of 14 below A0's 0.7 s floor), so **its energy figures are not used**; its times |
| 107 | are valid (1 ns clock). |
| 108 | - `evidence/j3-crypto-throughput-r2/` (`1f25721`). Only change: `sha256.blocks_per_thread` 60,000 to |
| 109 | 180,000, giving SHA-256 windows of 1.59 to 2.81 s, inside A0's calibrated 0.7 to 3.3 s. Ed25519 |
| 110 | windows were 1.02 to 1.08 s. One-minute load average 9.1 to 14.2 at the starts of the SHA-256 and Ed25519 repetitions. |
| 111 | |
| 112 | Run 2 figures, with the meter edge uncertainty (`pkg_edge_sd_j`, A0 section 3) beside each energy figure: |
| 113 | |
| 114 | | Probe | Achieved (max) | Median | Range | Package W (median) | Package energy per unit (median) | Above idle (median) | |
| 115 | | :--- | ---: | ---: | :--- | ---: | ---: | ---: | |
| 116 | | SHA-256, `sha2` crate (`asm`) | 37.06 GB/s | 29.86 GB/s | 24.46 to 37.06 | 60.53 | 1.946 ± 0.080 nJ/B (calc:61f2c7d7) | 0.801 ± 0.085 nJ/B (calc:162fd42c) | |
| 117 | | SHA-256, A0's own instruction code | 26.80 GB/s | 23.99 GB/s | 20.99 to 26.80 | 73.55 | 3.145 ± 0.102 nJ/B (calc:de7554c5) | 1.641 ± 0.102 nJ/B (calc:7bc75969) | |
| 118 | | Ed25519 verify, `ed25519-dalek` | 392,966 /s | 387,709 /s | 370,159 to 392,966 | 63.37 | 163.9 ± 12.9 µJ (calc:3d33d490) | 79.3 ± 12.8 µJ (calc:20b787b3) | |
| 119 | |
| 120 | Each ± figure is `pkg_edge_sd_j` of the repetition that holds that median, divided by its work units |
| 121 | (A0's formula: mean package watts × 0.1 s update × √(2/3)). It does not cover the idle run's own |
| 122 | uncertainty or the meter's calibration, which is unpublished. |
| 123 | |
| 124 | | Reference | A0 value | Best J3 achieved | Result | |
| 125 | | :--- | ---: | ---: | :--- | |
| 126 | | All-core SHA-256 | 22.25 GB/s (A0 instruction code) | 39.46 GB/s (`sha2` crate, run 1) | Above the A0 reference, so under the ADR 6.2 replacement rule the reference becomes 39.46 GB/s and its fraction is 1 by definition. A0's figure is 0.564 of it (calc:e7d9638b); run 2's maximum is 0.939 of it (calc:c80a9ab8). Before this fix the crate's A0 maximum, 6.167 GB/s, was 0.277 of the A0 reference (calc:aa382756) | |
| 127 | | All-core Ed25519 verification | 395,192 /s | 397,560 /s (run 1) | Above by less than A0's own spread (298,004 to 395,192), so it is replaced by the same rule. A0's figure is 0.994 of it (calc:771221f6) and run 2's is 0.988 (calc:4b62da69). That is 2.515 µs of all-core time per verification (calc:4f355c4c) | |
| 128 | |
| 129 | In seconds: at the new SHA-256 reference, digesting the Gate 4 replay's 117,196,023 bytes takes |
| 130 | 2.970 ms (calc:d25d95ec). That is now the replay bound (the memory term is 0.429 ms), so Gate 4's |
| 131 | measured 344.575 ms replay gives **fraction of bound reached = 0.00862 (bound 2.970 ms, measured |
| 132 | 344.575 ms, 116 times the bound)** (calc:a8960c47, calc:4cffeef5). One 11,949-byte change digests in |
| 133 | 0.303 µs (calc:b66d96d7). No physical joule bound exists here for SHA-256 or Ed25519, so joules carry no |
| 134 | fraction. ADR 0003 section 6.2 and `docs/A0_HOST_BOUNDS.md` mark their earlier figures as superseded |
| 135 | by this section. |
| 136 | |
| 137 | ## 5. Limits |
| 138 | |
| 139 | - The host was shared and busier in run 2 (load 9.1 to 14.2) than in run 1 (4.3 to 8.8), the likely (untested) reason |
| 140 | run 2's SHA-256 median is below run 1's. The reference uses the best repetition, per the A0 bound |
| 141 | statistic. |
| 142 | - Energy is SoC package only and uncalibrated, as in A0. The device and memory rows of both runs are in |
| 143 | their `report.json` files but are not J3 results. |
| 144 | - The corpus pins the Rust crates' decisions. It does not claim they are the only correct Ed25519 |
| 145 | semantics. |
| 146 | |
| 147 | ## 6. Reproduction |
| 148 | |
| 149 | ``` |
| 150 | cargo test -p synapse-bench --lib crypto_vectors # includes the portable-backend build |
| 151 | synapse-bench crypto-vectors-verify --corpus evidence/j3-crypto-vectors/vectors.bin |
| 152 | synapse-bench bounds-verify --evidence evidence/j3-crypto-throughput-r2 |
| 153 | synapse-bench bounds-verify --evidence evidence/j3-crypto-throughput |
| 154 | ``` |