interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / docs / J3_CRYPTO_REFERENCE.md

154 lines · 11819 bytes Raw Blame History Source

J3: SHA-256 and Ed25519 Reference for the Omega Port (Rust Side)

ADR 0003 section 9 step J3 (issue #55): "Omega port: SHA-256 with the hardware instructions and Ed25519 verification". Acceptance: "Digest and signature test vectors match the Rust crates exactly; all-core throughput reported against the A0 references."

Scope of this change. The Omega implementation itself belongs in the omega repository and is not here; it does not exist yet (no SHA-2 instruction or Ed25519 code was found in omega at 1edf41a5). This change delivers the Synapse half that the acceptance compares against: the sealed vector corpus that defines "match the Rust crates exactly", the Rust crates themselves on the SHA-2 instructions, and their all-core throughput against the A0 references. Issue #55 stays open until the Omega side passes the corpus.

1. Acceptance and Where It Stands

J3 acceptanceSynapse side (this change)Omega side
Digest and signature test vectors match the Rust crates exactlyevidence/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 backendNOT_RUN: no Omega implementation yet; it must decode this file and give every digest and verdict
All-core throughput reported against the A0 referencesSection 4: two sealed runs of the A0 probesNOT_RUN

Red before green: the first ten tests were committed against stubs (8f4975d) and failed for the stated reasons (evidence/j3-crypto-vectors/red-green/red.txt); green in green.txt. Review round: the edge-case and pinned-digest tests failed against stubs (21c90e1, red-green/review-red.txt, which also holds the negative control for the backend check), green in review-green.txt.

2. The Corpus

Format (ADR 0003 P5: fixed width, little endian, length prefixed), documented in crates/synapse-bench/src/crypto_vectors.rs:

header   magic "SYNJ3VEC" | version u16 = 1 | reserved u16 = 0 | count u32
record   kind 1 (SHA-256): len u32 | message | digest[32]
         kind 2 (Ed25519): verdict u8 | public_key[32] | signature[64] | len u32 | message
         verdict bits: 0 key decodes, 1 `verify` accepts, 2 `verify_strict` accepts
trailer  SHA-256 of every preceding byte

Synapse uses both Ed25519 calls (verify in synapse-core, synapse-auth, synapse-radicle; verify_strict in synapse-evidence), and they disagree on small-order points, so a port must reproduce three decisions per case, not one.

SHA-256 cases: the four FIPS 180-4 examples, every length from 1 to 257 bytes, 1,000, 4,096, 11,949 (Gate 4 change), 16,384 (A0 block) and 65,536 bytes, and all-zero and all-one messages. Ed25519 cases: RFC 8032 TEST 1 to 3; eight deterministic keys over messages of 0 to 11,949 bytes; changed message, R, S, high bit of S, wrong key; identity, order-2 and non-canonical identity keys; a key that does not decode; and the edge cases below. The decoder refuses bad magic, version, reserved bits, unknown kinds, oversize lengths (cap 1 MiB, checked before allocation), trailing bytes, checksum mismatch and every truncation, each with a typed error.

2.1 Edge cases and the wrong implementation each one catches

Classes from Chalkias, Garillot and Nikolaenko, "Taming the many EdDSAs" (SSR 2020, IACR ePrint 2020/1244). Points are built with curve25519-dalek; verdicts come from ed25519-dalek. The test each_edge_case_is_in_the_corpus_and_catches_a_wrong_implementation runs, for each case, a model that differs from the right one in one rule and requires its answer to differ from dalek's. The test also requires each wrong model to accept RFC 8032 TEST 1. models_reproduce_dalek_on_the_whole_corpus shows that the right models (cofactorless, canonical S, R compared as bytes; strict adds R decoding and small-order checks on A and R) give dalek's answer on all 299 records.

Caseverifyverify_strictWrong implementation caught (compared with)
noncanonical_r_y_p_plus_1: R = identity as y = p + 1, A = identity, S = 0refuserefusedecodes R and compares points (verify)
noncanonical_r_negative_zero: R = identity with the x sign bit setrefuserefusedecodes R and compares points (verify)
noncanonical_a_y_p: A = order-4 point encoded as y = pacceptrefusehashes the re-encoded A instead of the given bytes (verify)
small_order_a_and_r: A of order 8, R of order 4, S = 0acceptrefusestrict without small-order checks (verify_strict)
small_order_r_valid_key: R = identity under an ordinary keyacceptrefusestrict that checks A but not R (verify_strict)
mixed_order_a_k_not_multiple_of_8: A = aB + T8refuserefusecofactored equation (verify)
mixed_order_a_k_multiple_of_8: A = aB + T8, k a multiple of 8acceptacceptrefuses keys that are not torsion free (verify)
mixed_order_r: R = rB + T8refuserefusecofactored equation (verify), and cofactored with strict checks (verify_strict)
noncanonical_s_plus_l: RFC 8032 TEST 1 with S + Lrefuserefusereduces S modulo L instead of refusing it (verify)

3. The sha2 Build Fix

A0 found that sha2 0.10.9 was built without asm, and on aarch64 that crate then compiles only its software backend. In sha2/src/sha256.rs the aarch64 backend is gated on all(feature = "asm", target_arch = "aarch64"), and asm = ["sha2-asm"] in its Cargo.toml; the only other aarch64 name, asm-aarch64, is an alias for asm. So no narrower switch exists in 0.10: enabling the intrinsics backend also builds sha2-asm (unused on aarch64) and its cc build script. The workspace dependency now enables asm. sha2 then uses sha2::sha256::aarch64::sha256_compress when cpufeatures reports sha2 at run time and its portable code otherwise. ed25519-dalek shares the same sha2, so its SHA-512 also gets the aarch64 backend (gated the same way in sha512.rs, with run-time sha3 detection).

Proof, all in cargo test:

Cargo.lock was updated by cargo, not by hand. It adds sha2-asm 0.6.4, cc, find-msvc-tools and shlex, and curve25519-dalek (already in the graph through ed25519-dalek) becomes a direct dependency of synapse-bench. Building needs a C compiler.

4. All-Core Throughput Against the A0 References

Two runs of the A0 probes (synapse-bench bounds, same instrument and verifier; bounds-verify passes on both). Each protocol was committed and pushed before its run:

Run 2 figures, with the meter edge uncertainty (pkg_edge_sd_j, A0 section 3) beside each energy figure:

ProbeAchieved (max)MedianRangePackage W (median)Package energy per unit (median)Above idle (median)
SHA-256, sha2 crate (asm)37.06 GB/s29.86 GB/s24.46 to 37.0660.531.946 ± 0.080 nJ/B (calc:61f2c7d7)0.801 ± 0.085 nJ/B (calc:162fd42c)
SHA-256, A0's own instruction code26.80 GB/s23.99 GB/s20.99 to 26.8073.553.145 ± 0.102 nJ/B (calc:de7554c5)1.641 ± 0.102 nJ/B (calc:7bc75969)
Ed25519 verify, ed25519-dalek392,966 /s387,709 /s370,159 to 392,96663.37163.9 ± 12.9 µJ (calc:3d33d490)79.3 ± 12.8 µJ (calc:20b787b3)

Each ± figure is pkg_edge_sd_j of the repetition that holds that median, divided by its work units (A0's formula: mean package watts × 0.1 s update × √(2/3)). It does not cover the idle run's own uncertainty or the meter's calibration, which is unpublished.

ReferenceA0 valueBest J3 achievedResult
All-core SHA-25622.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)
All-core Ed25519 verification395,192 /s397,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)

In seconds: at the new SHA-256 reference, digesting the Gate 4 replay's 117,196,023 bytes takes 2.970 ms (calc:d25d95ec). That is now the replay bound (the memory term is 0.429 ms), so Gate 4's measured 344.575 ms replay gives **fraction of bound reached = 0.00862 (bound 2.970 ms, measured 344.575 ms, 116 times the bound)** (calc:a8960c47, calc:4cffeef5). One 11,949-byte change digests in 0.303 µs (calc:b66d96d7). No physical joule bound exists here for SHA-256 or Ed25519, so joules carry no fraction. ADR 0003 section 6.2 and docs/A0_HOST_BOUNDS.md mark their earlier figures as superseded by this section.

5. Limits

6. Reproduction

cargo test -p synapse-bench --lib crypto_vectors     # includes the portable-backend build
synapse-bench crypto-vectors-verify --corpus evidence/j3-crypto-vectors/vectors.bin
synapse-bench bounds-verify --evidence evidence/j3-crypto-throughput-r2
synapse-bench bounds-verify --evidence evidence/j3-crypto-throughput