From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
"Jason A . Donenfeld" <Jason@zx2c4.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
x86@kernel.org, Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 4/4] lib/crypto: x86/sha512-avx2: Optimize SHA512_Round
Date: Sun, 22 Nov 2026 19:13:21 -0800 [thread overview]
Message-ID: <20261123031321.403308-5-ebiggers@kernel.org> (raw)
In-Reply-To: <20261123031321.403308-1-ebiggers@kernel.org>
Make two related optimizations to the SHA-512 round computation.
First, replace the Majority function computation:
Maj(a, b, c) = ((a | c) & b) | (a & c)
... with the equivalent computation:
Maj(a, b, c) = ((b | c) & a) | (b & c)
This is helpful because it uses 'a' later, matching its later
availability. ('a' was just computed by the previous round, whereas 'b'
and 'c' were computed by the two rounds before that.) This reduces the
latency of Maj(a, b, c) from 'a' from 3 cycles to 2 cycles.
Second, now that Maj() is less likely to be a bottleneck, reassociate
the additions. Instead of:
h += w + k
d += h
h += S0(a)
t = S1(e) + Ch(e, f, g)
d += t
h += t
h += Maj(a, b, c)
Do the following (which conveniently also more closely matches the way
the SHA-2 specification describes the algorithm):
t1 = h + w + k + Ch(e, f, g) + S1(e)
d += t1
t2 = Maj(a, b, c) + S0(a)
h = t1 + t2
The CPU can compute t2 in parallel with t1, shortening the critical path
for updating 'h' (the next round's 'a') from 6 cycles to 5 cycles from
'a' and 'e' -- assuming that 'xor', 'add', and 'rorx' have a latency of
1 cycle, as they do on current CPUs. The critical path for updating 'd'
(the next round's 'e') remains at 5 cycles from 'e'. The new code also
has one fewer instruction per round: one of the 'add' instructions is
gone. Finally, the new code also uses one fewer scratch register: %rsi
(alias y3) is no longer used, so INP no longer needs to be spilled.
This commit makes SHA-512 about 5% faster on AMD Ryzen 9 9950X (Zen 5)
and about 6% faster on Intel Sapphire Rapids.
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
lib/crypto/x86/sha512-avx2-asm.S | 44 ++++++++++++++------------------
1 file changed, 19 insertions(+), 25 deletions(-)
diff --git a/lib/crypto/x86/sha512-avx2-asm.S b/lib/crypto/x86/sha512-avx2-asm.S
index f64ef3adcd6c1..2b6151cebd852 100644
--- a/lib/crypto/x86/sha512-avx2-asm.S
+++ b/lib/crypto/x86/sha512-avx2-asm.S
@@ -86,7 +86,6 @@ NUM_BLKS = %rdx
c = %rcx
d = %r8
e = %rdx
-y3 = %rsi
TBL = %rdi # clobbers CTX1
@@ -105,14 +104,12 @@ y2 = %r15
# Local variables (stack frame)
XFER_SIZE = 4*8
SRND_SIZE = 1*8
-INP_SIZE = 1*8
INPEND_SIZE = 1*8
CTX_SIZE = 1*8
frame_XFER = 0
frame_SRND = frame_XFER + XFER_SIZE
-frame_INP = frame_SRND + SRND_SIZE
-frame_INPEND = frame_INP + INP_SIZE
+frame_INPEND = frame_SRND + SRND_SIZE
frame_CTX = frame_INPEND + INPEND_SIZE
frame_size = frame_CTX + CTX_SIZE
@@ -162,38 +159,38 @@ frame_size = frame_CTX + CTX_SIZE
# Do one SHA-512 round using the state a-h and \wk = W[t] + K[t]. Updates a-h
# and rotates their aliases to match what the next round expects.
.macro SHA512_Round wk
+ add \wk, h # h = h + w + k
mov f, y2 # y2 = f
rorx $41, e, y0 # y0 = e ror 41
rorx $18, e, y1 # y1 = e ror 18
xor g, y2 # y2 = f ^ g
- add \wk, h # h = h + w + k
xor y1, y0 # y0 = (e ror 41) ^ (e ror 18)
rorx $14, e, y1 # y1 = e ror 14
and e, y2 # y2 = (f ^ g) & e
- add h, d # d = d + h + w + k
xor y1, y0 # y0 = S1(e)
xor g, y2 # y2 = Ch(e, f, g) = ((f ^ g) & e) ^ g
- rorx $34, a, T1 # T1 = a ror 34
- rorx $39, a, y1 # y1 = a ror 39
- mov a, y3 # y3 = a
+ add y2, h # h = h + w + k + Ch(e, f, g)
- xor T1, y1 # y1 = (a ror 39) ^ (a ror 34)
- rorx $28, a, T1 # T1 = a ror 28
- or c, y3 # y3 = a | c
+ mov b, y2 # y2 = b
+ mov b, T1 # T1 = b
+ or c, y2 # y2 = b | c
+ and c, T1 # T1 = b & c
+ add y0, h # h = t1 = h + w + k + Ch(e, f, g) + S1(e)
- xor T1, y1 # y1 = S0(a)
- mov a, T1 # T1 = a
- and b, y3 # y3 = (a | c) & b
- and c, T1 # T1 = a & c
- add y0, y2 # y2 = S1(e) + Ch(e, f, g)
+ rorx $39, a, y1 # y1 = a ror 39
+ and a, y2 # y2 = (b | c) & a
+ rorx $34, a, y0 # y0 = a ror 34
+ or T1, y2 # y2 = Maj(a, b, c) = ((b | c) & a) | (b & c)
+
+ xor y0, y1 # y1 = (a ror 39) ^ (a ror 34)
+ rorx $28, a, y0 # y0 = a ror 28
+ add h, d # d = d + t1
+ xor y0, y1 # y1 = S0(a)
- or T1, y3 # y3 = Maj(a, b, c) = ((a | c) & b) | (a & c)
- add y1, h # h = h + w + k + S0(a)
- add y2, d # d = d + t1
- add y2, h # h = t1 + S0(a)
- add y3, h # h = t1 + S0(a) + Maj(a, b, c)
+ add y1, y2 # y2 = t2 = Maj(a, b, c) + S0(a)
+ add y2, h # h = t1 + t2
RotateState
.endm
@@ -326,8 +323,6 @@ SYM_FUNC_START(sha512_transform_rorx)
vpshufb YBSWAP, Y_\i, Y_\i
.endr
- mov INP, frame_INP(%rsp)
-
# Do rounds 0..63 and compute W[16..79] (interleaved) in four loop
# iterations, each of which does four groups of four rounds.
movq $4, frame_SRND(%rsp)
@@ -382,7 +377,6 @@ SYM_FUNC_START(sha512_transform_rorx)
addm 8*6(CTX2), g
addm 8*7(CTX2), h
- mov frame_INP(%rsp), INP
add $128, INP
cmp frame_INPEND(%rsp), INP
jne .Lloop0
--
2.56.1
prev parent reply other threads:[~2026-11-23 3:13 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 3:13 [PATCH 0/4] lib/crypto: Clean up and optimize sha512-avx2-asm.S Eric Biggers
2026-11-23 3:13 ` [PATCH 1/4] lib/crypto: x86/sha512-avx2: Clean up loading and byte-swapping the data Eric Biggers
2026-11-23 3:13 ` [PATCH 2/4] lib/crypto: x86/sha512-avx2: Optimize message schedule Eric Biggers
2026-11-23 3:13 ` [PATCH 3/4] lib/crypto: x86/sha512-avx2: Factor out SHA512_Round Eric Biggers
2026-11-23 3:13 ` Eric Biggers [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261123031321.403308-5-ebiggers@kernel.org \
--to=ebiggers@kernel.org \
--cc=Jason@zx2c4.com \
--cc=ardb@kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox