Skip to content

Commit

Permalink
alg-sha256.c: SHA-2 Maj() optimization proposed by Wei Dai.
Browse files Browse the repository at this point in the history
This patch has been cherry-picked from:
openwall/yescrypt@9edf51061b45
  • Loading branch information
besser82 committed Nov 7, 2022
1 parent 239664b commit bb17218
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/alg-sha256.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*-
* Copyright 2005-2016 Colin Percival
* Copyright 2016-2018 Alexander Peslyak
* Copyright 2016-2018,2021 Alexander Peslyak
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -65,7 +65,11 @@ static const uint32_t Krnd[64] = {

/* Elementary functions used by SHA256 */
#define Ch(x, y, z) ((x & (y ^ z)) ^ z)
#define Maj(x, y, z) ((x & (y | z)) | (y & z))
#if 1 /* Explicit caching/reuse of common subexpression between rounds */
#define Maj(x, y, z) (y ^ ((x_xor_y = x ^ y) & y_xor_z))
#else /* Let the compiler cache/reuse or not */
#define Maj(x, y, z) (y ^ ((x ^ y) & (y ^ z)))
#endif
#define SHR(x, n) (x >> n)
#define ROTR(x, n) ((x >> n) | (x << (32 - n)))
#define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
Expand All @@ -77,7 +81,8 @@ static const uint32_t Krnd[64] = {
#define RND(a, b, c, d, e, f, g, h, k) \
h += S1(e) + Ch(e, f, g) + k; \
d += h; \
h += S0(a) + Maj(a, b, c);
h += S0(a) + Maj(a, b, c); \
y_xor_z = x_xor_y;

/* Adjusted round function for rotating state */
#define RNDr(S, W, i, ii) \
Expand Down Expand Up @@ -110,6 +115,7 @@ SHA256_Transform(uint32_t state[static restrict 8],

/* 3. Mix. */
for (i = 0; i <= 48; i += 16) {
uint32_t x_xor_y, y_xor_z = S[(65 - i) % 8] ^ S[(66 - i) % 8];
RNDr(S, W, 0, i);
RNDr(S, W, 1, i);
RNDr(S, W, 2, i);
Expand Down

3 comments on commit bb17218

@solardiz
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, the same kind of optimization would also apply to SHA-512.

@besser82
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, the same kind of optimization would also apply to SHA-512.

@solardiz Applied for SHA-512 in 5982354.

@solardiz
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@besser82 Cool. I wonder whether/how this affected code size and speed for sha512crypt?
Similar changes are also possible for SHA-1 and MD4, but their effect is not so obvious, and we didn't even complete this for JtR yet (need to revisit it): openwall/john#4727

Please sign in to comment.