sha/keccak1600.c: implement bit interleaving optimization.
authorAndy Polyakov <appro@openssl.org>
Mon, 29 May 2017 15:12:54 +0000 (17:12 +0200)
committerAndy Polyakov <appro@openssl.org>
Tue, 30 May 2017 17:52:50 +0000 (19:52 +0200)
This targets 32-bit processors and is discussed in section 2.1 of
"Keccak implementation overview".

Reviewed-by: Rich Salz <rsalz@openssl.org>
crypto/sha/keccak1600.c

index 9deba8f12a9db13cfacc02ca0baa0051d84698b9..2682fdb1db9b2e406cfd69fdf6468f6e39a9cd83 100644 (file)
 #include <string.h>
 #include <assert.h>
 
 #include <string.h>
 #include <assert.h>
 
-#define ROL64(a, offset) ((offset) ? (((a) << offset) | ((a) >> (64-offset))) \
-                                   : a)
+#define ROL32(a, offset) (((a) << (offset)) | ((a) >> ((32 - (offset)) & 31)))
+
+static uint64_t ROL64(uint64_t val, int offset)
+{
+    if (offset == 0) {
+        return val;
+    } else if (sizeof(void *) == 8) {
+        return (val << offset) | (val >> (64-offset));
+    } else {
+        uint32_t hi = (uint32_t)(val >> 32), lo = (uint32_t)val;
+
+        if (offset & 1) {
+            uint32_t tmp = hi;
+
+            offset >>= 1;
+            hi = ROL32(lo, offset);
+            lo = ROL32(tmp, offset + 1);
+        } else {
+            offset >>= 1;
+            lo = ROL32(lo, offset);
+            hi = ROL32(hi, offset);
+        }
+
+        return ((uint64_t)hi << 32) | lo;
+    }
+}
+
+static const unsigned char rhotates[5][5] = {
+    {  0,  1, 62, 28, 27 },
+    { 36, 44,  6, 55, 20 },
+    {  3, 10, 43, 25, 39 },
+    { 41, 45, 15, 21,  8 },
+    { 18,  2, 61, 56, 14 }
+};
+
+static const uint64_t iotas[] = {
+    sizeof(void *) == 8 ? 0x0000000000000001U : 0x0000000000000001U,
+    sizeof(void *) == 8 ? 0x0000000000008082U : 0x0000008900000000U,
+    sizeof(void *) == 8 ? 0x800000000000808aU : 0x8000008b00000000U,
+    sizeof(void *) == 8 ? 0x8000000080008000U : 0x8000808000000000U,
+    sizeof(void *) == 8 ? 0x000000000000808bU : 0x0000008b00000001U,
+    sizeof(void *) == 8 ? 0x0000000080000001U : 0x0000800000000001U,
+    sizeof(void *) == 8 ? 0x8000000080008081U : 0x8000808800000001U,
+    sizeof(void *) == 8 ? 0x8000000000008009U : 0x8000008200000001U,
+    sizeof(void *) == 8 ? 0x000000000000008aU : 0x0000000b00000000U,
+    sizeof(void *) == 8 ? 0x0000000000000088U : 0x0000000a00000000U,
+    sizeof(void *) == 8 ? 0x0000000080008009U : 0x0000808200000001U,
+    sizeof(void *) == 8 ? 0x000000008000000aU : 0x0000800300000000U,
+    sizeof(void *) == 8 ? 0x000000008000808bU : 0x0000808b00000001U,
+    sizeof(void *) == 8 ? 0x800000000000008bU : 0x8000000b00000001U,
+    sizeof(void *) == 8 ? 0x8000000000008089U : 0x8000008a00000001U,
+    sizeof(void *) == 8 ? 0x8000000000008003U : 0x8000008100000001U,
+    sizeof(void *) == 8 ? 0x8000000000008002U : 0x8000008100000000U,
+    sizeof(void *) == 8 ? 0x8000000000000080U : 0x8000000800000000U,
+    sizeof(void *) == 8 ? 0x000000000000800aU : 0x0000008300000000U,
+    sizeof(void *) == 8 ? 0x800000008000000aU : 0x8000800300000000U,
+    sizeof(void *) == 8 ? 0x8000000080008081U : 0x8000808800000001U,
+    sizeof(void *) == 8 ? 0x8000000000008080U : 0x8000008800000000U,
+    sizeof(void *) == 8 ? 0x0000000080000001U : 0x0000800000000001U,
+    sizeof(void *) == 8 ? 0x8000000080008008U : 0x8000808200000000U
+};
 
 #if defined(KECCAK_REF)
 /*
 
 #if defined(KECCAK_REF)
 /*
@@ -60,13 +119,6 @@ static void Theta(uint64_t A[5][5])
 
 static void Rho(uint64_t A[5][5])
 {
 
 static void Rho(uint64_t A[5][5])
 {
-    static const unsigned char rhotates[5][5] = {
-        {  0,  1, 62, 28, 27 },
-        { 36, 44,  6, 55, 20 },
-        {  3, 10, 43, 25, 39 },
-        { 41, 45, 15, 21,  8 },
-        { 18,  2, 61, 56, 14 }
-    };
     size_t y;
 
     for (y = 0; y < 5; y++) {
     size_t y;
 
     for (y = 0; y < 5; y++) {
@@ -141,17 +193,6 @@ static void Chi(uint64_t A[5][5])
 
 static void Iota(uint64_t A[5][5], size_t i)
 {
 
 static void Iota(uint64_t A[5][5], size_t i)
 {
-    static const uint64_t iotas[] = {
-        0x0000000000000001U, 0x0000000000008082U, 0x800000000000808aU,
-        0x8000000080008000U, 0x000000000000808bU, 0x0000000080000001U,
-        0x8000000080008081U, 0x8000000000008009U, 0x000000000000008aU,
-        0x0000000000000088U, 0x0000000080008009U, 0x000000008000000aU,
-        0x000000008000808bU, 0x800000000000008bU, 0x8000000000008089U,
-        0x8000000000008003U, 0x8000000000008002U, 0x8000000000000080U,
-        0x000000000000800aU, 0x800000008000000aU, 0x8000000080008081U,
-        0x8000000000008080U, 0x0000000080000001U, 0x8000000080008008U
-    };
-
     assert(i < (sizeof(iotas) / sizeof(iotas[0])));
     A[0][0] ^= iotas[i];
 }
     assert(i < (sizeof(iotas) / sizeof(iotas[0])));
     A[0][0] ^= iotas[i];
 }
@@ -183,23 +224,6 @@ void KeccakF1600(uint64_t A[5][5])
 static void Round(uint64_t A[5][5], size_t i)
 {
     uint64_t C[5], D[5], T[2][5];
 static void Round(uint64_t A[5][5], size_t i)
 {
     uint64_t C[5], D[5], T[2][5];
-    static const unsigned char rhotates[5][5] = {
-        {  0,  1, 62, 28, 27 },
-        { 36, 44,  6, 55, 20 },
-        {  3, 10, 43, 25, 39 },
-        { 41, 45, 15, 21,  8 },
-        { 18,  2, 61, 56, 14 }
-    };
-    static const uint64_t iotas[] = {
-        0x0000000000000001U, 0x0000000000008082U, 0x800000000000808aU,
-        0x8000000080008000U, 0x000000000000808bU, 0x0000000080000001U,
-        0x8000000080008081U, 0x8000000000008009U, 0x000000000000008aU,
-        0x0000000000000088U, 0x0000000080008009U, 0x000000008000000aU,
-        0x000000008000808bU, 0x800000000000008bU, 0x8000000000008089U,
-        0x8000000000008003U, 0x8000000000008002U, 0x8000000000000080U,
-        0x000000000000800aU, 0x800000008000000aU, 0x8000000080008081U,
-        0x8000000000008080U, 0x0000000080000001U, 0x8000000080008008U
-    };
 
     assert(i < (sizeof(iotas) / sizeof(iotas[0])));
 
 
     assert(i < (sizeof(iotas) / sizeof(iotas[0])));
 
@@ -310,23 +334,6 @@ void KeccakF1600(uint64_t A[5][5])
 static void Round(uint64_t R[5][5], uint64_t A[5][5], size_t i)
 {
     uint64_t C[5], D[5];
 static void Round(uint64_t R[5][5], uint64_t A[5][5], size_t i)
 {
     uint64_t C[5], D[5];
-    static const unsigned char rhotates[5][5] = {
-        {  0,  1, 62, 28, 27 },
-        { 36, 44,  6, 55, 20 },
-        {  3, 10, 43, 25, 39 },
-        { 41, 45, 15, 21,  8 },
-        { 18,  2, 61, 56, 14 }
-    };
-    static const uint64_t iotas[] = {
-        0x0000000000000001U, 0x0000000000008082U, 0x800000000000808aU,
-        0x8000000080008000U, 0x000000000000808bU, 0x0000000080000001U,
-        0x8000000080008081U, 0x8000000000008009U, 0x000000000000008aU,
-        0x0000000000000088U, 0x0000000080008009U, 0x000000008000000aU,
-        0x000000008000808bU, 0x800000000000008bU, 0x8000000000008089U,
-        0x8000000000008003U, 0x8000000000008002U, 0x8000000000000080U,
-        0x000000000000800aU, 0x800000008000000aU, 0x8000000080008081U,
-        0x8000000000008080U, 0x0000000080000001U, 0x8000000080008008U
-    };
 
     assert(i < (sizeof(iotas) / sizeof(iotas[0])));
 
 
     assert(i < (sizeof(iotas) / sizeof(iotas[0])));
 
@@ -425,23 +432,6 @@ void KeccakF1600(uint64_t A[5][5])
 static void FourRounds(uint64_t A[5][5], size_t i)
 {
     uint64_t B[5], C[5], D[5];
 static void FourRounds(uint64_t A[5][5], size_t i)
 {
     uint64_t B[5], C[5], D[5];
-    static const unsigned char rhotates[5][5] = {
-        {  0,  1, 62, 28, 27 },
-        { 36, 44,  6, 55, 20 },
-        {  3, 10, 43, 25, 39 },
-        { 41, 45, 15, 21,  8 },
-        { 18,  2, 61, 56, 14 }
-    };
-    static const uint64_t iotas[] = {
-        0x0000000000000001U, 0x0000000000008082U, 0x800000000000808aU,
-        0x8000000080008000U, 0x000000000000808bU, 0x0000000080000001U,
-        0x8000000080008081U, 0x8000000000008009U, 0x000000000000008aU,
-        0x0000000000000088U, 0x0000000080008009U, 0x000000008000000aU,
-        0x000000008000808bU, 0x800000000000008bU, 0x8000000000008089U,
-        0x8000000000008003U, 0x8000000000008002U, 0x8000000000000080U,
-        0x000000000000800aU, 0x800000008000000aU, 0x8000000080008081U,
-        0x8000000000008080U, 0x0000000080000001U, 0x8000000080008008U
-    };
 
     assert(i <= (sizeof(iotas) / sizeof(iotas[0]) - 4));
 
 
     assert(i <= (sizeof(iotas) / sizeof(iotas[0]) - 4));
 
@@ -731,6 +721,39 @@ void KeccakF1600(uint64_t A[5][5])
 
 #endif
 
 
 #endif
 
+static uint64_t BitInterleave(uint64_t Ai)
+{
+    if (sizeof(void *) < 8) {
+        uint32_t hi = 0, lo = 0;
+        int j;
+
+        for (j = 0; j < 32; j++) {
+            lo |= ((uint32_t)(Ai >> (2 * j))     & 1) << j;
+            hi |= ((uint32_t)(Ai >> (2 * j + 1)) & 1) << j;
+        }
+
+        Ai = ((uint64_t)hi << 32) | lo;
+    }
+
+    return Ai;
+}
+
+static uint64_t BitDeinterleave(uint64_t Ai)
+{
+    if (sizeof(void *) < 8) {
+        uint32_t hi = (uint32_t)(Ai >> 32), lo = (uint32_t)Ai;
+        int j;
+
+        Ai = 0;
+        for (j = 0; j < 32; j++) {
+            Ai |= (uint64_t)((lo >> j) & 1) << (2 * j);
+            Ai |= (uint64_t)((hi >> j) & 1) << (2 * j + 1);
+        }
+    }
+
+    return Ai;
+}
+
 /*
  * SHA3_absorb can be called multiple times, but at each invocation
  * largest multiple of |r| out of |len| bytes are processed. Then
 /*
  * SHA3_absorb can be called multiple times, but at each invocation
  * largest multiple of |r| out of |len| bytes are processed. Then
@@ -751,11 +774,13 @@ size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len,
 
     while (len >= r) {
         for (i = 0; i < w; i++) {
 
     while (len >= r) {
         for (i = 0; i < w; i++) {
-            A_flat[i] ^= (uint64_t)inp[0]       | (uint64_t)inp[1] << 8  |
-                         (uint64_t)inp[2] << 16 | (uint64_t)inp[3] << 24 |
-                         (uint64_t)inp[4] << 32 | (uint64_t)inp[5] << 40 |
-                         (uint64_t)inp[6] << 48 | (uint64_t)inp[7] << 56;
+            uint64_t Ai = (uint64_t)inp[0]       | (uint64_t)inp[1] << 8  |
+                          (uint64_t)inp[2] << 16 | (uint64_t)inp[3] << 24 |
+                          (uint64_t)inp[4] << 32 | (uint64_t)inp[5] << 40 |
+                          (uint64_t)inp[6] << 48 | (uint64_t)inp[7] << 56;
             inp += 8;
             inp += 8;
+
+            A_flat[i] ^= BitInterleave(Ai);
         }
         KeccakF1600(A);
         len -= r;
         }
         KeccakF1600(A);
         len -= r;
@@ -777,7 +802,7 @@ void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r)
 
     while (len >= r) {
         for (i = 0; i < w; i++) {
 
     while (len >= r) {
         for (i = 0; i < w; i++) {
-            uint64_t Ai = A_flat[i];
+            uint64_t Ai = BitDeinterleave(A_flat[i]);
 
             out[0] = (unsigned char)(Ai);
             out[1] = (unsigned char)(Ai >> 8);
 
             out[0] = (unsigned char)(Ai);
             out[1] = (unsigned char)(Ai >> 8);
@@ -798,7 +823,7 @@ void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r)
     len /= 8;
 
     for (i = 0; i < len; i++) {
     len /= 8;
 
     for (i = 0; i < len; i++) {
-        uint64_t Ai = A_flat[i];
+        uint64_t Ai = BitDeinterleave(A_flat[i]);
 
         out[0] = (unsigned char)(Ai);
         out[1] = (unsigned char)(Ai >> 8);
 
         out[0] = (unsigned char)(Ai);
         out[1] = (unsigned char)(Ai >> 8);
@@ -812,7 +837,7 @@ void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r)
     }
 
     if (rem) {
     }
 
     if (rem) {
-        uint64_t Ai = A_flat[i];
+        uint64_t Ai = BitDeinterleave(A_flat[i]);
 
         for (i = 0; i < rem; i++) {
             *out++ = (unsigned char)Ai;
 
         for (i = 0; i < rem; i++) {
             *out++ = (unsigned char)Ai;