From 63bbbfd10d706b78213e3c12684dbc423ddb839b Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 1 Mar 2023 21:08:07 +0700 Subject: Refactor GenerateRandomByteArray --- crypto/src/pqc/crypto/bike/BikeEngine.cs | 6 +++--- crypto/src/pqc/crypto/bike/BikeUtilities.cs | 8 ++++---- 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 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) { -- cgit 1.4.1