summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-03-01 21:08:07 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-03-01 21:08:07 +0700
commit63bbbfd10d706b78213e3c12684dbc423ddb839b (patch)
tree15bdfb6250020cbcea6c50e9f94f97377e6e36ed /crypto
parentBIKE: fix FunctionH over-allocation (diff)
downloadBouncyCastle.NET-ed25519-63bbbfd10d706b78213e3c12684dbc423ddb839b.tar.xz
Refactor GenerateRandomByteArray
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/pqc/crypto/bike/BikeEngine.cs6
-rw-r--r--crypto/src/pqc/crypto/bike/BikeUtilities.cs8
2 files changed, 7 insertions, 7 deletions
diff --git a/crypto/src/pqc/crypto/bike/BikeEngine.cs b/crypto/src/pqc/crypto/bike/BikeEngine.cs
index 16b5f54bc..8d67541bb 100644
--- a/crypto/src/pqc/crypto/bike/BikeEngine.cs
+++ b/crypto/src/pqc/crypto/bike/BikeEngine.cs
@@ -65,7 +65,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
             byte[] res = new byte[R2_BYTE];
             IXof digest = new ShakeDigest(256);
             digest.BlockUpdate(seed, 0, seed.Length);
-            BikeUtilities.GenerateRandomByteArray(res, (uint)(2 * r), (uint)t, digest);
+            BikeUtilities.GenerateRandomByteArray(res, 2 * r, t, digest);
             return res;
         }
 
@@ -145,8 +145,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
 #endif
 
             // 1. Randomly generate h0, h1
-            BikeUtilities.GenerateRandomByteArray(h0, (uint)r, (uint)hw, digest);
-            BikeUtilities.GenerateRandomByteArray(h1, (uint)r, (uint)hw, digest);
+            BikeUtilities.GenerateRandomByteArray(h0, r, hw, digest);
+            BikeUtilities.GenerateRandomByteArray(h1, r, hw, digest);
 
             ulong[] h0Element = bikeRing.Create();
             ulong[] h1Element = bikeRing.Create();
diff --git a/crypto/src/pqc/crypto/bike/BikeUtilities.cs b/crypto/src/pqc/crypto/bike/BikeUtilities.cs
index 40bd6d148..062ac3e26 100644
--- a/crypto/src/pqc/crypto/bike/BikeUtilities.cs
+++ b/crypto/src/pqc/crypto/bike/BikeUtilities.cs
@@ -70,7 +70,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
             }
         }
 
-        internal static void GenerateRandomByteArray(byte[] res, uint size, uint weight, IXof digest)
+        internal static void GenerateRandomByteArray(byte[] res, int size, int weight, IXof digest)
         {
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
             Span<byte> buf = stackalloc byte[4];
@@ -78,7 +78,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
             byte[] buf = new byte[4];
 #endif
 
-            for (int i = (int)weight - 1; i >= 0; i--)
+            for (int i = weight - 1; i >= 0; i--)
             {
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
                 digest.Output(buf);
@@ -88,8 +88,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
                 ulong temp = Pack.LE_To_UInt32(buf, 0);
 #endif
 
-                temp = temp * (size - (uint)i) >> 32;
-                uint rand_pos = (uint)i + (uint)temp;
+                temp *= (uint)(size - i);
+                uint rand_pos = (uint)i + (uint)(temp >> 32);
 
                 if (CheckBit(res, rand_pos) != 0)
                 {