Keep cipher working state in scalar locals for register residency - #8
Merged
Conversation
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.
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.
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.