From bfd227283892e59f9f9684ead850aa00e5937895 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Wed, 7 Oct 2026 18:52:57 -0500 Subject: [PATCH 1/2] sha256: roll sha256_add_to_hash into a word loop (-250 B, every profile) sha256_add_to_hash was eight unrolled copies of "clc; 4 x lda/adc/sta" (297 B). sha256_h0..h7 and sha_a..sha_h are each eight contiguous words in the same order (src/data.s), so one X indexes both: a word loop, high word first, LSB-to-MSB carry ripple inside the word, exactly as before. 47 B. Two lderror asserts pin the contiguity it relies on. Its only caller is `jsr sha256_add_to_hash / rts` at the end of compression, so clobbering X is safe. Bytes freed, tools/measure_margins.py --build, every profile +250 B in the region holding sha256.s's code: ip65 CRYPTO_OVERLAY 45 -> 295, ip65-onchip 11 -> 261 (wiki: same); uci NET_CODE 176 -> 426, uci-onchip 184 -> 434, uci-comb 176 -> 426, uci-onchip-store 184 -> 434, uci-comb-store 38 -> 288, comb demo 38 -> 288. Cycles, per 64 B block, counted from instruction timings (no indexed access crosses a page on any shipped profile: h0 at $B165 ip65 / $A965 uci, sha_a 32 B above): 406 -> 527, +121. Tests: test_sha256 10/10, test_hkdf 12/12, test_keyschedule_steps 9/9, test_chained_hmac N=1..10 OK on ip65; test_hkdf 12/12 and test_keyschedule_steps 9/9 on uci-onchip (C64_SKIP_BUILD=1). Red: `ldx #24` (drops h7 += h), PRG 07019d27..., test_hkdf 12/12 FAILED; sha_c/sha_d swapped in data.s -> lderror "sha_a..sha_h must be contiguous", no PRG. Restored: 9a05a5a3... (uci-onchip), green. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/crypto/sha256.s | 151 +++++++++----------------------------------- 1 file changed, 31 insertions(+), 120 deletions(-) diff --git a/src/crypto/sha256.s b/src/crypto/sha256.s index 6efaba33..44a3187b 100644 --- a/src/crypto/sha256.s +++ b/src/crypto/sha256.s @@ -782,128 +782,39 @@ sha256_maj: ; ============================================================================= -; sha256_add_to_hash - add working variables to hash state -; ============================================================================= +; sha256_add_to_hash - add working variables to hash state (h[i] += a..h[i]) +; +; One word per pass, high word first; within a word bytes go LSB (+3) to MSB +; (+0) with the carry rippling, as the 8 unrolled copies this replaced did. +; Indexes both blocks with one X, so it relies on sha256_h0..h7 and +; sha_a..sha_h each being 8 contiguous words in the same order (src/data.s); +; the asserts below fail the link if either stops being so. +; Clobbers A, X. Runs once per 64 B block, so the loop overhead is noise +; beside the 64 rounds; the 250 B it saves are resident on both backends. +; ============================================================================= +.assert sha256_h1 = sha256_h0 + 4 .and sha256_h2 = sha256_h0 + 8 .and sha256_h3 = sha256_h0 + 12 .and sha256_h4 = sha256_h0 + 16 .and sha256_h5 = sha256_h0 + 20 .and sha256_h6 = sha256_h0 + 24 .and sha256_h7 = sha256_h0 + 28, lderror, "sha256_add_to_hash: sha256_h0..h7 must be contiguous (src/data.s)" +.assert sha_b = sha_a + 4 .and sha_c = sha_a + 8 .and sha_d = sha_a + 12 .and sha_e = sha_a + 16 .and sha_f = sha_a + 20 .and sha_g = sha_a + 24 .and sha_h = sha_a + 28, lderror, "sha256_add_to_hash: sha_a..sha_h must be contiguous (src/data.s)" sha256_add_to_hash: - ; h0 += a - clc - lda sha256_h0+3 - adc sha_a+3 - sta sha256_h0+3 - lda sha256_h0+2 - adc sha_a+2 - sta sha256_h0+2 - lda sha256_h0+1 - adc sha_a+1 - sta sha256_h0+1 - lda sha256_h0 - adc sha_a - sta sha256_h0 - - ; h1 += b - clc - lda sha256_h1+3 - adc sha_b+3 - sta sha256_h1+3 - lda sha256_h1+2 - adc sha_b+2 - sta sha256_h1+2 - lda sha256_h1+1 - adc sha_b+1 - sta sha256_h1+1 - lda sha256_h1 - adc sha_b - sta sha256_h1 - - ; h2 += c - clc - lda sha256_h2+3 - adc sha_c+3 - sta sha256_h2+3 - lda sha256_h2+2 - adc sha_c+2 - sta sha256_h2+2 - lda sha256_h2+1 - adc sha_c+1 - sta sha256_h2+1 - lda sha256_h2 - adc sha_c - sta sha256_h2 - - ; h3 += d - clc - lda sha256_h3+3 - adc sha_d+3 - sta sha256_h3+3 - lda sha256_h3+2 - adc sha_d+2 - sta sha256_h3+2 - lda sha256_h3+1 - adc sha_d+1 - sta sha256_h3+1 - lda sha256_h3 - adc sha_d - sta sha256_h3 - - ; h4 += e - clc - lda sha256_h4+3 - adc sha_e+3 - sta sha256_h4+3 - lda sha256_h4+2 - adc sha_e+2 - sta sha256_h4+2 - lda sha256_h4+1 - adc sha_e+1 - sta sha256_h4+1 - lda sha256_h4 - adc sha_e - sta sha256_h4 - - ; h5 += f + ldx #28 ; word 7 (h7 += h) first, down to word 0 +@word: clc - lda sha256_h5+3 - adc sha_f+3 - sta sha256_h5+3 - lda sha256_h5+2 - adc sha_f+2 - sta sha256_h5+2 - lda sha256_h5+1 - adc sha_f+1 - sta sha256_h5+1 - lda sha256_h5 - adc sha_f - sta sha256_h5 - - ; h6 += g - clc - lda sha256_h6+3 - adc sha_g+3 - sta sha256_h6+3 - lda sha256_h6+2 - adc sha_g+2 - sta sha256_h6+2 - lda sha256_h6+1 - adc sha_g+1 - sta sha256_h6+1 - lda sha256_h6 - adc sha_g - sta sha256_h6 - - ; h7 += h - clc - lda sha256_h7+3 - adc sha_h+3 - sta sha256_h7+3 - lda sha256_h7+2 - adc sha_h+2 - sta sha256_h7+2 - lda sha256_h7+1 - adc sha_h+1 - sta sha256_h7+1 - lda sha256_h7 - adc sha_h - sta sha256_h7 + lda sha256_h0+3,x + adc sha_a+3,x + sta sha256_h0+3,x + lda sha256_h0+2,x + adc sha_a+2,x + sta sha256_h0+2,x + lda sha256_h0+1,x + adc sha_a+1,x + sta sha256_h0+1,x + lda sha256_h0,x + adc sha_a,x + sta sha256_h0,x + txa + sec + sbc #4 + tax + bpl @word rts ; ============================================================================= From 24ceea215636a2d9aa932842ccf148fee8c8e594 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Thu, 8 Oct 2026 13:28:05 -0500 Subject: [PATCH 2/2] sha256: correct the rolled loop's cycle cost (+121 or +128 by placement) The previous commit said +121 cycles per 64 B block "on any shipped profile". Wrong on two: where the taken `bpl @word` crosses a page each taken branch costs one more cycle, 7 per call. Recomputed from where the routine links on all ten measure_margins profiles (same branch, bfd2272): 527 cycles (+121): ip65, ip65-onchip, both ip65 wiki targets, uci, uci-onchip, uci-comb, uci-onchip-store (loop inside one page) 534 cycles (+128): uci-comb-store and the comb demo (routine at $34DC, loop $34DE..$350A: the bpl at $3508 branches back across $3500) No indexed lda/adc crosses a page on any of them (sha256_h0 / sha_a sit 32 B apart inside one page). Placement-dependent, so the comment above the routine now says how the cost arises rather than pinning one number. Comment only: comb-store PRG unchanged (2f9ad380...). Co-Authored-By: Claude Opus 5.5 (1M context) --- src/crypto/sha256.s | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/crypto/sha256.s b/src/crypto/sha256.s index 44a3187b..271a9ac1 100644 --- a/src/crypto/sha256.s +++ b/src/crypto/sha256.s @@ -791,6 +791,11 @@ sha256_maj: ; the asserts below fail the link if either stops being so. ; Clobbers A, X. Runs once per 64 B block, so the loop overhead is noise ; beside the 64 rounds; the 250 B it saves are resident on both backends. +; Cost vs the 406-cycle unrolled copy: 527 cycles (+121) where the loop sits +; inside one page, 534 (+128) where the taken `bpl @word` crosses one (+1 +; per taken branch, 7 per call) -- placement-dependent, so it differs by +; profile (the comb TRUST_STORE images cross today). Any indexed access whose +; base+X crossed a page would add +1 more each; none does at present. ; ============================================================================= .assert sha256_h1 = sha256_h0 + 4 .and sha256_h2 = sha256_h0 + 8 .and sha256_h3 = sha256_h0 + 12 .and sha256_h4 = sha256_h0 + 16 .and sha256_h5 = sha256_h0 + 20 .and sha256_h6 = sha256_h0 + 24 .and sha256_h7 = sha256_h0 + 28, lderror, "sha256_add_to_hash: sha256_h0..h7 must be contiguous (src/data.s)" .assert sha_b = sha_a + 4 .and sha_c = sha_a + 8 .and sha_d = sha_a + 12 .and sha_e = sha_a + 16 .and sha_f = sha_a + 20 .and sha_g = sha_a + 24 .and sha_h = sha_a + 28, lderror, "sha256_add_to_hash: sha_a..sha_h must be contiguous (src/data.s)"