summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/pqc/crypto/bike/BikeEngine.cs35
-rw-r--r--crypto/src/pqc/crypto/bike/BikeRing.cs19
2 files changed, 31 insertions, 23 deletions
diff --git a/crypto/src/pqc/crypto/bike/BikeEngine.cs b/crypto/src/pqc/crypto/bike/BikeEngine.cs
index fdb568b01..7e01bdb6f 100644
--- a/crypto/src/pqc/crypto/bike/BikeEngine.cs
+++ b/crypto/src/pqc/crypto/bike/BikeEngine.cs
@@ -154,10 +154,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
             bikeRing.DecodeBytes(h1, h1Element);
 
             // 2. Compute h
-            ulong[] hElement = bikeRing.Create();
-            bikeRing.Inv(h0Element, hElement);
-            bikeRing.Multiply(hElement, h1Element, hElement);
-            bikeRing.EncodeBytes(hElement, h);
+            ulong[] t = bikeRing.Create();
+            bikeRing.Inv(h0Element, t);
+            bikeRing.Multiply(t, h1Element, t);
+            bikeRing.EncodeBytes(t, h);
 
             //3. Parse seed2 as sigma
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
@@ -192,19 +192,15 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
 
             ulong[] e0Element = bikeRing.Create();
             ulong[] e1Element = bikeRing.Create();
-
             bikeRing.DecodeBytes(e0Bytes, e0Element);
             bikeRing.DecodeBytes(e1Bytes, e1Element);
 
-            ulong[] hElement = bikeRing.Create();
-            bikeRing.DecodeBytes(h, hElement);
-
             // 3. Calculate c
-            // calculate c0
-            ulong[] c0Element = bikeRing.Create();
-            bikeRing.Multiply(e1Element, hElement, c0Element);
-            bikeRing.Add(c0Element, e0Element, c0Element);
-            bikeRing.EncodeBytes(c0Element, c0);
+            ulong[] t = bikeRing.Create();
+            bikeRing.DecodeBytes(h, t);
+            bikeRing.Multiply(t, e1Element, t);
+            bikeRing.Add(t, e0Element, t);
+            bikeRing.EncodeBytes(t, c0);
 
             //calculate c1
             FunctionL(e0Bytes, e1Bytes, c1);
@@ -264,13 +260,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
 
         private byte[] ComputeSyndrome(byte[] c0, byte[] h0)
         {
-            ulong[] c0Element = bikeRing.Create();
-            ulong[] h0Element = bikeRing.Create();
-            bikeRing.DecodeBytes(c0, c0Element);
-            bikeRing.DecodeBytes(h0, h0Element);
-            ulong[] sElement = bikeRing.Create();
-            bikeRing.Multiply(c0Element, h0Element, sElement);
-            return bikeRing.EncodeBitsTransposed(sElement);
+            ulong[] t = bikeRing.Create();
+            ulong[] u = bikeRing.Create();
+            bikeRing.DecodeBytes(c0, t);
+            bikeRing.DecodeBytes(h0, u);
+            bikeRing.Multiply(t, u, t);
+            return bikeRing.EncodeBitsTransposed(t);
         }
 
         private byte[] BGFDecoder(byte[] s, int[] h0Compact, int[] h1Compact)
diff --git a/crypto/src/pqc/crypto/bike/BikeRing.cs b/crypto/src/pqc/crypto/bike/BikeRing.cs
index 7455fac06..a98cc9975 100644
--- a/crypto/src/pqc/crypto/bike/BikeRing.cs
+++ b/crypto/src/pqc/crypto/bike/BikeRing.cs
@@ -75,9 +75,15 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
         internal void DecodeBytes(byte[] bs, ulong[] z)
         {
             int partialBits = m_bits & 63;
+            int partialBytes = (partialBits + 7) >> 3;
             Pack.LE_To_UInt64(bs, 0, z, 0, Size - 1);
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            Span<byte> last = stackalloc byte[8];
+            bs.AsSpan((Size - 1) << 3, partialBytes).CopyTo(last);
+#else
             byte[] last = new byte[8];
-            Array.Copy(bs, (Size - 1) << 3, last, 0, (partialBits + 7) >> 3);
+            Array.Copy(bs, (Size - 1) << 3, last, 0, partialBytes);
+#endif
             z[Size - 1] = Pack.LE_To_UInt64(last);
             Debug.Assert((z[Size - 1] >> partialBits) == 0UL);
         }
@@ -96,13 +102,20 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
         internal void EncodeBytes(ulong[] x, byte[] bs)
         {
             int partialBits = m_bits & 63;
+            int partialBytes = (partialBits + 7) >> 3;
             Debug.Assert((x[Size - 1] >> partialBits) == 0UL);
             Pack.UInt64_To_LE(x, 0, Size - 1, bs, 0);
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            Span<byte> last = stackalloc byte[8];
+            Pack.UInt64_To_LE(x[Size - 1], last);
+            last[..partialBytes].CopyTo(bs.AsSpan((Size - 1) << 3));
+#else
             byte[] last = new byte[8];
             Pack.UInt64_To_LE(x[Size - 1], last);
-            Array.Copy(last, 0, bs, (Size - 1) << 3, (partialBits + 7) >> 3);
+            Array.Copy(last, 0, bs, (Size - 1) << 3, partialBytes);
+#endif
         }
-        
+
         internal void Inv(ulong[] a, ulong[] z)
         {
             ulong[] f = Create();