Skip to content

Keep cipher working state in scalar locals for register residency - #8

Merged
schultz-is merged 1 commit into
trunkfrom
perf/scalar-register-state
Aug 3, 2026
Merged

Keep cipher working state in scalar locals for register residency#8
schultz-is merged 1 commit into
trunkfrom
perf/scalar-register-state

Conversation

@schultz-is

Copy link
Copy Markdown
Owner

The Encrypt/Decrypt hot loops held the working block state in a heap-free array (in/ct [numWords]uint64). Current Go (1.23+) spills that array to the stack across the unrolled rounds instead of keeping it in registers, adding a load+store per word per round; the effect is severe for the small block sizes whose state used to live entirely in registers (256-bit regressed ~2.9x versus Go 1.17, while the 16-word 1024-bit path, which never fit in registers, was unaffected).

Replace the working-state arrays with named scalar locals (b0..bN), which the SSA backend promotes to registers far more reliably than array elements. The fixed word permutation becomes a tuple assignment of those locals. This is the same technique the pure-Go cores in golang.org/x/crypto use.

Purely mechanical (array element -> scalar; every changed line is one such substitution) and verified by the unchanged known-answer vectors round-tripping through both directions. Measured on Apple M1 Ultra, Go 1.26 (ns/op):

Threefish256/encrypt 232 -> 78 (3.0x)
Threefish256/decrypt 277 -> 132 (2.1x)
Threefish512/encrypt 494 -> 118 (4.2x)
Threefish512/decrypt 470 -> 168 (2.8x)
Threefish1024/encrypt 655 -> 216 (3.0x)
Threefish1024/decrypt 678 -> 274 (2.5x)

The code remains pure ARX with no data-dependent branches or memory access, so the constant-time property is preserved. Gains on amd64 (16 GP registers) may be smaller for 1024-bit due to higher register pressure.

The Encrypt/Decrypt hot loops held the working block state in a heap-free
array (in/ct [numWords]uint64). Current Go (1.23+) spills that array to the
stack across the unrolled rounds instead of keeping it in registers, adding a
load+store per word per round; the effect is severe for the small block sizes
whose state used to live entirely in registers (256-bit regressed ~2.9x versus
Go 1.17, while the 16-word 1024-bit path, which never fit in registers, was
unaffected).

Replace the working-state arrays with named scalar locals (b0..bN), which the
SSA backend promotes to registers far more reliably than array elements. The
fixed word permutation becomes a tuple assignment of those locals. This is the
same technique the pure-Go cores in golang.org/x/crypto use.

Purely mechanical (array element -> scalar; every changed line is one such
substitution) and verified by the unchanged known-answer vectors round-tripping
through both directions. Measured on Apple M1 Ultra, Go 1.26 (ns/op):

  Threefish256/encrypt   232 -> 78   (3.0x)
  Threefish256/decrypt   277 -> 132  (2.1x)
  Threefish512/encrypt   494 -> 118  (4.2x)
  Threefish512/decrypt   470 -> 168  (2.8x)
  Threefish1024/encrypt  655 -> 216  (3.0x)
  Threefish1024/decrypt  678 -> 274  (2.5x)

The code remains pure ARX with no data-dependent branches or memory access, so
the constant-time property is preserved. Gains on amd64 (16 GP registers) may be
smaller for 1024-bit due to higher register pressure.
@schultz-is
schultz-is merged commit ff17f6a into trunk Aug 3, 2026
3 checks passed
@schultz-is
schultz-is deleted the perf/scalar-register-state branch August 3, 2026 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant