diff options
-rw-r--r-- | crypto/src/crypto/agreement/DHStandardGroups.cs | 4 | ||||
-rw-r--r-- | crypto/src/crypto/generators/DHParametersHelper.cs | 18 | ||||
-rw-r--r-- | crypto/src/math/BigInteger.cs | 71 | ||||
-rw-r--r-- | crypto/src/math/ec/abc/Tnaf.cs | 4 |
4 files changed, 50 insertions, 47 deletions
diff --git a/crypto/src/crypto/agreement/DHStandardGroups.cs b/crypto/src/crypto/agreement/DHStandardGroups.cs index e334489c8..08afb801c 100644 --- a/crypto/src/crypto/agreement/DHStandardGroups.cs +++ b/crypto/src/crypto/agreement/DHStandardGroups.cs @@ -9,8 +9,6 @@ namespace Org.BouncyCastle.Crypto.Agreement /// <summary>Standard Diffie-Hellman groups from various IETF specifications.</summary> public class DHStandardGroups { - private static readonly BigInteger Two = BigInteger.ValueOf(2); - private static BigInteger FromHex(string hex) { return new BigInteger(1, Hex.DecodeStrict(hex)); @@ -30,7 +28,7 @@ namespace Org.BouncyCastle.Crypto.Agreement { // NOTE: A group using a safe prime (i.e. q = (p-1)/2), and generator g = 2 BigInteger p = FromHex(hexP); - return new DHParameters(p, Two, p.ShiftRight(1), l); + return new DHParameters(p, BigInteger.Two, p.ShiftRight(1), l); } /* diff --git a/crypto/src/crypto/generators/DHParametersHelper.cs b/crypto/src/crypto/generators/DHParametersHelper.cs index 385690430..a05918944 100644 --- a/crypto/src/crypto/generators/DHParametersHelper.cs +++ b/crypto/src/crypto/generators/DHParametersHelper.cs @@ -9,21 +9,9 @@ namespace Org.BouncyCastle.Crypto.Generators { internal class DHParametersHelper { - private static readonly BigInteger Six = BigInteger.ValueOf(6); - private static readonly int[][] primeLists = BigInteger.primeLists; private static readonly int[] primeProducts = BigInteger.primeProducts; - private static readonly BigInteger[] BigPrimeProducts = ConstructBigPrimeProducts(primeProducts); - - private static BigInteger[] ConstructBigPrimeProducts(int[] primeProducts) - { - BigInteger[] bpp = new BigInteger[primeProducts.Length]; - for (int i = 0; i < bpp.Length; ++i) - { - bpp[i] = BigInteger.ValueOf(primeProducts[i]); - } - return bpp; - } + private static readonly BigInteger[] BigPrimeProducts = Array.ConvertAll(primeProducts, BigInteger.ValueOf); /* * Finds a pair of prime BigInteger's {p, q: p = 2q + 1} @@ -83,7 +71,7 @@ namespace Org.BouncyCastle.Crypto.Generators int qRem = test % prime; if (qRem == 0 || qRem == (prime >> 1)) { - q = q.Add(Six); + q = q.Add(BigInteger.Six); goto retry; } } @@ -146,7 +134,7 @@ namespace Org.BouncyCastle.Crypto.Generators { BigInteger h = BigIntegers.CreateRandomInRange(BigInteger.Two, pMinusTwo, random); - g = h.ModPow(BigInteger.Two, p); + g = h.Square().Mod(p); } while (g.Equals(BigInteger.One)); diff --git a/crypto/src/math/BigInteger.cs b/crypto/src/math/BigInteger.cs index 7da886c4f..d61d702c1 100644 --- a/crypto/src/math/BigInteger.cs +++ b/crypto/src/math/BigInteger.cs @@ -139,6 +139,8 @@ namespace Org.BouncyCastle.Math public static readonly BigInteger Two; public static readonly BigInteger Three; public static readonly BigInteger Four; + public static readonly BigInteger Five; + public static readonly BigInteger Six; public static readonly BigInteger Ten; #if !NETCOREAPP3_0_OR_GREATER @@ -181,27 +183,34 @@ namespace Org.BouncyCastle.Math static BigInteger() { Zero = new BigInteger(0, ZeroMagnitude, false); - Zero.nBits = 0; Zero.nBitLength = 0; + Zero.nBits = 0; + Zero.nBitLength = 0; SMALL_CONSTANTS[0] = Zero; for (uint i = 1; i < SMALL_CONSTANTS.Length; ++i) { - SMALL_CONSTANTS[i] = CreateUValueOf(i); + var sc = CreateUValueOf(i); + sc.nBits = Integers.PopCount(i); + sc.nBitLength = BitLen(i); + + SMALL_CONSTANTS[i] = sc; } One = SMALL_CONSTANTS[1]; Two = SMALL_CONSTANTS[2]; Three = SMALL_CONSTANTS[3]; Four = SMALL_CONSTANTS[4]; + Five = SMALL_CONSTANTS[5]; + Six = SMALL_CONSTANTS[6]; Ten = SMALL_CONSTANTS[10]; - radix2 = ValueOf(2); + radix2 = Two; radix2E = radix2.Pow(chunk2); radix8 = ValueOf(8); radix8E = radix8.Pow(chunk8); - radix10 = ValueOf(10); + radix10 = Ten; radix10E = radix10.Pow(chunk10); radix16 = ValueOf(16); @@ -3607,47 +3616,55 @@ namespace Org.BouncyCastle.Math sb.Append(s); } + private static BigInteger CreateUValueOf(uint value) + { + if (value == 0) + return Zero; + + return new BigInteger(1, new uint[]{ value }, false); + } + private static BigInteger CreateUValueOf(ulong value) { uint msw = (uint)(value >> 32); uint lsw = (uint)value; - if (msw != 0) - return new BigInteger(1, new uint[]{ msw, lsw }, false); - - if (lsw != 0) - { - BigInteger n = new BigInteger(1, new uint[]{ lsw }, false); - // Check for a power of two - if ((lsw & -lsw) == lsw) - { - n.nBits = 1; - } - return n; - } + if (msw == 0) + return CreateUValueOf(lsw); - return Zero; + return new BigInteger(1, new uint[]{ msw, lsw }, false); } - private static BigInteger CreateValueOf(long value) + public static BigInteger ValueOf(int value) { - if (value < 0) + if (value >= 0) { - if (value == long.MinValue) - return CreateValueOf(~value).Not(); + if (value < SMALL_CONSTANTS.Length) + return SMALL_CONSTANTS[value]; - return CreateValueOf(-value).Negate(); + return CreateUValueOf((uint)value); } - return CreateUValueOf((ulong)value); + if (value == int.MinValue) + return CreateUValueOf((uint)~value).Not(); + + return ValueOf(-value).Negate(); } public static BigInteger ValueOf(long value) { - if (value >= 0 && value < SMALL_CONSTANTS.Length) - return SMALL_CONSTANTS[value]; + if (value >= 0L) + { + if (value < SMALL_CONSTANTS.Length) + return SMALL_CONSTANTS[value]; + + return CreateUValueOf((ulong)value); + } + + if (value == long.MinValue) + return CreateUValueOf((ulong)~value).Not(); - return CreateValueOf(value); + return ValueOf(-value).Negate(); } public int GetLowestSetBit() diff --git a/crypto/src/math/ec/abc/Tnaf.cs b/crypto/src/math/ec/abc/Tnaf.cs index 88a4eeb96..d8e9b6ae0 100644 --- a/crypto/src/math/ec/abc/Tnaf.cs +++ b/crypto/src/math/ec/abc/Tnaf.cs @@ -500,12 +500,12 @@ namespace Org.BouncyCastle.Math.EC.Abc { if (mu == 1) { - return BigInteger.ValueOf(6); + return BigInteger.Six; } else { // mu == -1 - return BigInteger.ValueOf(10); + return BigInteger.Ten; } } else |