summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-04-20 13:14:04 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-04-20 13:14:04 +0700
commit5df9602d5139a367083af69c9b9ef3c7145cb6aa (patch)
tree4fef5ad507354df873a75aa2dbad8476ca18c0c9
parentUpdate Kyber public key encoding (diff)
downloadBouncyCastle.NET-ed25519-5df9602d5139a367083af69c9b9ef3c7145cb6aa.tar.xz
Refactoring in BigInteger
-rw-r--r--crypto/src/math/BigInteger.cs112
1 files changed, 33 insertions, 79 deletions
diff --git a/crypto/src/math/BigInteger.cs b/crypto/src/math/BigInteger.cs
index d84680de5..a98955779 100644
--- a/crypto/src/math/BigInteger.cs
+++ b/crypto/src/math/BigInteger.cs
@@ -488,7 +488,7 @@ namespace Org.BouncyCastle.Math
             if ((sbyte)bytes[offset] >= 0)
             {
                 // strip leading zero bytes and return magnitude bytes
-                this.magnitude = MakeMagnitude(bytes, offset, length);
+                this.magnitude = MakeMagnitudeBE(bytes, offset, length);
                 this.sign = this.magnitude.Length > 0 ? 1 : 0;
                 return;
             }
@@ -534,115 +534,69 @@ namespace Org.BouncyCastle.Math
 
             inverse[index]++;
 
-            this.magnitude = MakeMagnitude(inverse);
+            this.magnitude = MakeMagnitudeBE(inverse);
         }
 
-        private static uint[] MakeMagnitude(byte[] bytes)
+        private static uint[] MakeMagnitudeBE(byte[] bytes)
         {
-            return MakeMagnitude(bytes, 0, bytes.Length);
+            return MakeMagnitudeBE(bytes, 0, bytes.Length);
         }
 
-        private static uint[] MakeMagnitude(byte[] bytes, int offset, int length)
+        private static uint[] MakeMagnitudeBE(byte[] bytes, int offset, int length)
         {
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
-            return MakeMagnitude(bytes.AsSpan(offset, length));
+            return MakeMagnitudeBE(bytes.AsSpan(offset, length));
 #else
             int end = offset + length;
 
             // strip leading zeros
-            int firstSignificant;
-            for (firstSignificant = offset; firstSignificant < end && bytes[firstSignificant] == 0; firstSignificant++)
+            int start;
+            for (start = offset; start < end && bytes[start] == 0; start++)
             {
             }
 
-            if (firstSignificant >= end)
+            int nBytes = end - start;
+            if (nBytes <= 0)
                 return ZeroMagnitude;
 
-            int nInts = (end - firstSignificant + 3) / BytesPerInt;
-            int bCount = (end - firstSignificant) % BytesPerInt;
-            if (bCount == 0)
-            {
-                bCount = BytesPerInt;
-            }
-
-            if (nInts < 1)
-                return ZeroMagnitude;
+            int nInts = (nBytes + BytesPerInt - 1) / BytesPerInt;
+            Debug.Assert(nInts > 0);
 
-            uint[] mag = new uint[nInts];
+            uint[] magnitude = new uint[nInts];
 
-            uint v = 0U;
-            int magnitudeIndex = 0;
-            for (int i = firstSignificant; i < end; ++i)
-            {
-                v <<= 8;
-                v |= bytes[i];
-                bCount--;
-                if (bCount <= 0)
-                {
-                    mag[magnitudeIndex] = v;
-                    magnitudeIndex++;
-                    bCount = BytesPerInt;
-                    v = 0U;
-                }
-            }
-
-            if (magnitudeIndex < mag.Length)
-            {
-                mag[magnitudeIndex] = v;
-            }
+            int first = ((nBytes - 1) % BytesPerInt) + 1;
+            magnitude[0] = Pack.BE_To_UInt32_Low(bytes, start, first);
+            Pack.BE_To_UInt32(bytes, start + first, magnitude, 1, nInts - 1);
 
-            return mag;
+            return magnitude;
 #endif
         }
 
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
-        private static uint[] MakeMagnitude(ReadOnlySpan<byte> bytes)
+        private static uint[] MakeMagnitudeBE(ReadOnlySpan<byte> bytes)
         {
             int end = bytes.Length;
 
             // strip leading zeros
-            int firstSignificant;
-            for (firstSignificant = 0; firstSignificant < end && bytes[firstSignificant] == 0; firstSignificant++)
+            int start;
+            for (start = 0; start < end && bytes[start] == 0; start++)
             {
             }
 
-            if (firstSignificant >= end)
+            int nBytes = end - start;
+            if (nBytes <= 0)
                 return ZeroMagnitude;
 
-            int nInts = (end - firstSignificant + 3) / BytesPerInt;
-            int bCount = (end - firstSignificant) % BytesPerInt;
-            if (bCount == 0)
-            {
-                bCount = BytesPerInt;
-            }
-
-            if (nInts < 1)
-                return ZeroMagnitude;
+            int nInts = (nBytes + BytesPerInt - 1) / BytesPerInt;
+            Debug.Assert(nInts > 0);
 
-            uint[] mag = new uint[nInts];
+            uint[] magnitude = new uint[nInts];
 
-            uint v = 0;
-            int magnitudeIndex = 0;
-            for (int i = firstSignificant; i < end; ++i)
-            {
-                v <<= 8;
-                v |= bytes[i];
-                bCount--;
-                if (bCount <= 0)
-                {
-                    mag[magnitudeIndex] = v;
-                    magnitudeIndex++;
-                    bCount = BytesPerInt;
-                    v = 0U;
-                }
-            }
-
-            if (magnitudeIndex < mag.Length)
-            {
-                mag[magnitudeIndex] = v;
-            }
+            int first = ((nBytes - 1) % BytesPerInt) + 1;
+            magnitude[0] = Pack.BE_To_UInt32_Low(bytes.Slice(start, first));
+            Pack.BE_To_UInt32(bytes.Slice(start + first), magnitude.AsSpan(1));
 
-            return mag;
+            return magnitude;
         }
 #endif
 
@@ -664,7 +618,7 @@ namespace Org.BouncyCastle.Math
             else
             {
                 // copy bytes
-                this.magnitude = MakeMagnitude(bytes, offset, length);
+                this.magnitude = MakeMagnitudeBE(bytes, offset, length);
                 this.sign = this.magnitude.Length < 1 ? 0 : sign;
             }
         }
@@ -683,7 +637,7 @@ namespace Org.BouncyCastle.Math
             else
             {
                 // copy bytes
-                this.magnitude = MakeMagnitude(bytes);
+                this.magnitude = MakeMagnitudeBE(bytes);
                 this.sign = this.magnitude.Length < 1 ? 0 : sign;
             }
         }
@@ -719,7 +673,7 @@ namespace Org.BouncyCastle.Math
             int xBits = BitsPerByte * nBytes - sizeInBits;
             b[0] &= (byte)(255U >> xBits);
 
-            this.magnitude = MakeMagnitude(b);
+            this.magnitude = MakeMagnitudeBE(b);
             this.sign = this.magnitude.Length < 1 ? 0 : 1;
         }
 
@@ -766,7 +720,7 @@ namespace Org.BouncyCastle.Math
                 // ensure the trailing bit is 1 (i.e. must be odd)
                 b[nBytes - 1] |= 1;
 
-                this.magnitude = MakeMagnitude(b);
+                this.magnitude = MakeMagnitudeBE(b);
                 this.nBits = -1;
 
                 if (certainty < 1)