summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-03-02 01:57:24 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-03-02 01:57:24 +0700
commita4d0d5db2e92c61bd9672a3f0c01559bc45677fa (patch)
treedcb062aa499c063bd7262c4243c424da93142636
parentBIKE: reduce allocations (diff)
downloadBouncyCastle.NET-ed25519-a4d0d5db2e92c61bd9672a3f0c01559bc45677fa.tar.xz
Refactor KeccakDigest
-rw-r--r--crypto/src/crypto/digests/KeccakDigest.cs34
1 files changed, 25 insertions, 9 deletions
diff --git a/crypto/src/crypto/digests/KeccakDigest.cs b/crypto/src/crypto/digests/KeccakDigest.cs
index 9f4a36b88..a6e60c8e8 100644
--- a/crypto/src/crypto/digests/KeccakDigest.cs
+++ b/crypto/src/crypto/digests/KeccakDigest.cs
@@ -176,7 +176,11 @@ namespace Org.BouncyCastle.Crypto.Digests
             dataQueue[bitsInQueue >> 3] = data;
             if ((bitsInQueue += 8) == rate)
             {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                KeccakAbsorb(dataQueue);
+#else
                 KeccakAbsorb(dataQueue, 0);
+#endif
                 bitsInQueue = 0;
             }
         }
@@ -204,13 +208,21 @@ namespace Org.BouncyCastle.Crypto.Digests
             {
                 Array.Copy(data, off, dataQueue, bytesInQueue, available);
                 count += available;
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                KeccakAbsorb(dataQueue);
+#else
                 KeccakAbsorb(dataQueue, 0);
+#endif
             }
 
             int remaining;
             while ((remaining = (len - count)) >= rateBytes)
             {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                KeccakAbsorb(data.AsSpan(off + count));
+#else
                 KeccakAbsorb(data, off + count);
+#endif
                 count += rateBytes;
             }
 
@@ -243,7 +255,7 @@ namespace Org.BouncyCastle.Crypto.Digests
             {
                 data[..available].CopyTo(dataQueue.AsSpan(bytesInQueue));
                 count += available;
-                KeccakAbsorb(dataQueue, 0);
+                KeccakAbsorb(dataQueue);
             }
 
             int remaining;
@@ -282,7 +294,11 @@ namespace Org.BouncyCastle.Crypto.Digests
 
             if (++bitsInQueue == rate)
             {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                KeccakAbsorb(dataQueue);
+#else
                 KeccakAbsorb(dataQueue, 0);
+#endif
             }
             else
             {
@@ -353,25 +369,25 @@ namespace Org.BouncyCastle.Crypto.Digests
         }
 #endif
 
-        private void KeccakAbsorb(byte[] data, int off)
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        private void KeccakAbsorb(ReadOnlySpan<byte> data)
         {
-            int count = rate >> 6;
+            int count = rate >> 6, off = 0;
             for (int i = 0; i < count; ++i)
             {
-                state[i] ^= Pack.LE_To_UInt64(data, off);
+                state[i] ^= Pack.LE_To_UInt64(data[off..]);
                 off += 8;
             }
 
             KeccakPermutation();
         }
-
-#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
-        private void KeccakAbsorb(ReadOnlySpan<byte> data)
+#else
+        private void KeccakAbsorb(byte[] data, int off)
         {
-            int count = rate >> 6, off = 0;
+            int count = rate >> 6;
             for (int i = 0; i < count; ++i)
             {
-                state[i] ^= Pack.LE_To_UInt64(data[off..]);
+                state[i] ^= Pack.LE_To_UInt64(data, off);
                 off += 8;
             }