SHA-384/512/512-224/512-256 streaming on ARMv7 through the shared MdStream code - #606
Merged
Merged
Conversation
Generalize the ARMv7 streaming Merkle-Damgård code and proofs (Impl/MdStream/Arm.lean, Proof/MdStream/Arm/) over the block size B and the length-field size L, as on x86-64, and instantiate them for the SHA-512 family on ARMv7, deleting its bespoke streaming code and proofs. MD5, SHA-1 and SHA-256 (B = 64, L = 8) emit the same code; the SHA-512 family's update now compresses whole blocks straight from the data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019oSrQeJdKT2MeBHirxaPKi
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019oSrQeJdKT2MeBHirxaPKi
Member
Author
|
merge conflict |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019oSrQeJdKT2MeBHirxaPKi
Member
Author
|
merge conflict |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019oSrQeJdKT2MeBHirxaPKi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This came out of a review looking for duplication that points to missing abstractions. MD5, SHA-1 and SHA-256 already streamed through
Impl/MdStream/<T>on every target, but SHA-512 did so only on x86-64. On ARMv7 theMdStreamParamsfixed a 64-byte block and an 8-byte length field, so the SHA-512 family had its own streaming code (Impl/Sha512/Arm/Stream.lean) and about 2,350 lines of its own streaming proofs.This PR generalises ARMv7
MdStreamover the block size and length-field size, as x86-64 already is. The SHA-512 family is then an instance of it, and the bespoke streaming code and proofs are deleted.No changes to
Spec/orTCB/. The contracts are unchanged.The abstraction
Impl/MdStream/Arm.leanParamsgainsB(block size) andL(length-field size).B,B - 1,B - L,L - 1andlog₂ Bwhere it used 64, 63, 56, 7 and 6.outmay writer9andr10.B = 64, L = 8the code is the same as before.Proof/MdStream/Arm/Common.leanShapeandCalleeOkare stated overMd P.B P.N P.L.DimsgainsB ∈ {64, 128},0 < L ≤ 16and the encodability of the new immediates.Dims.{pos, le, lg, mod, mod64, div_eq_zero},shrB,cmp0_shrB,andB,ofNat_shlB,op2_shrB,op2_shlB.Proof/MdStream/Arm/{Update,Finalize}.leanBandL.finalizenames the end of the zero paddinglim P k, so its single symbolic execution stays independent ofk, as before.Impl/Sha512/Arm/Stream.leankeepsinitandoutW. It addsparams:N = 64,B = 128,L = 16, scratch offset 224, a length field written using onlyr9, and the existingoutWdigest.updateandfinalizeare now the generic code.Proof/Sha512/Arm/Stream/{Update,Finalize}.leankeep their module and theorem names (update_verified,finalize_verified,sat,wordBytes_split,writeW_rev,flat_length), so HMAC, PBKDF2 and Ed25519 still find them. They now instantiate the generic proofs:dimsandcalleefor the size parameters and the compression function;shape, made oflen_okandout_ok;taint_decide.B := 64, L := 8to their params.dimsandshapeproofs need one-line adjustments.Whole/Hash.leanunfolds the generic definitions to provenoFrames.Pbkdf2/Md/Arm/Words.lean'sOutOk.ofShapetakesMd P.B P.N P.L, a two-line change.What is removed (line counts against
main)Proof/Sha512/Arm/Stream/Update.leanProof/Sha512/Arm/Stream/Finalize.leanImpl/Sha512/Arm/Stream.leanThe generic proofs grew by about 160 lines. Overall: 18 files changed, 757 insertions, 2667 deletions (net −1910).
Performance
Emitted code
lake env lean --run Emit.leanchanges onlysrc/asm/arm/sha512.rs.vg_sha512_*by symbol.updatenow compresses whole blocks straight from the data. The old code copied every byte through the buffer.Instructions executed on ARMv7
These are SHA-512
init+update+finalizeon ARMv7, counted underqemu-arm -one-insn-per-tb -d exec. Each figure is the difference between 20 and 10 iterations of the hash, divided by 10.updateupdate(two-block padding)updateupdateupdatesupdatesupdateThe benchmarks (
hash_group) hash each message in one call, which is the same or faster at every size. Feeding many tinyupdates costs about 6 instructions more per call.Proof checking
These are
-Dprofiler=true,-j1,Elab.async=false, best of 3 runs on the same idle machine, toolchain and revision. Each cell is elaboration + kernel, then kernel alone, in ms. Hardware counters weren't available in this VM, soperf statcouldn't be used.Proof/MdStream/Arm/CommonProof/MdStream/Arm/UpdateProof/MdStream/Arm/FinalizeProof/Sha512/Arm/Stream/UpdateProof/Sha512/Arm/Stream/FinalizeProof/Sha512/Arm/LitProof/Sha512/Arm/SharedProof/Sha256/Arm/Stream/MdProof/Md5/Arm/Stream/MdProof/Sha1/Arm/Stream/Mdfinalize's constant-time proof. That proof is now the standardtaint_decideover the code, which includes the compression function. The deleted proof was a hand-written relational one.Common's kernel time also rises (+0.24 s), from theDimslemmas.Validation run locally
lake build, both before and after merging the latestmain(which brought in HMAC-SHA-256 and PBKDF2-HMAC-SHA-256 on ARMv7 and x86: the generic contracts and code #583 and HMAC init on ARMv7 and x86: compress the padded key blocks directly #586).lake env lean --run Emit.lean --check: passes. The audits pass, and the committedsrc/asm/is what the emitter generates.check_lean_imports.py,check_lean_speed.py,check_vectors.py,check_arch_gates.py,check_variants.py,check_mcdt.pyandalgorithms_table.py(the README tables are unchanged).cargo fmt --checkandcargo clippy --all-targets -- -D warningson x86-64.WYCHEPROOF_ROOT=… cargo test --target armv7-unknown-linux-gnueabihfunder qemu-user, after the merge. All pass, including SHA-512's incremental tests and the Wycheproof tests.cargo test. No x86-64 code changed.Follow-up
Proof/Pbkdf2/Md/Arm/Sha512.leanproves the same digest code (outW) that the newProof.Sha512.Arm.Stream.Finalize.out_okproves. It can now becomeOutOk.ofShape Proof.Sha512.Arm.Stream.Finalize.shape. I left it alone here because the PBKDF2 code was being changed in parallel.🤖 Generated with Claude Code
https://claude.ai/code/session_019oSrQeJdKT2MeBHirxaPKi
Generated by Claude Code