summary refs log tree commit diff
path: root/crypto/src/math
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-28 12:55:26 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-28 12:55:26 +0700
commit0f951361cae9243b8d1a77d8f4333a602197d50d (patch)
treec5f2adc22a0b8ef42d5964df49268b73b3371a30 /crypto/src/math
parentGenerics migration in Cms (diff)
downloadBouncyCastle.NET-ed25519-0f951361cae9243b8d1a77d8f4333a602197d50d.tar.xz
Generics migration in Crmf, Crypto, Math
Diffstat (limited to 'crypto/src/math')
-rw-r--r--crypto/src/math/BigInteger.cs14
-rw-r--r--crypto/src/math/ec/ECCurve.cs57
-rw-r--r--crypto/src/math/ec/ECPoint.cs5
3 files changed, 34 insertions, 42 deletions
diff --git a/crypto/src/math/BigInteger.cs b/crypto/src/math/BigInteger.cs
index c6e760db9..98d1fcb1d 100644
--- a/crypto/src/math/BigInteger.cs
+++ b/crypto/src/math/BigInteger.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.Globalization;
 using System.Text;
@@ -3258,7 +3258,7 @@ namespace Org.BouncyCastle.Math
                 int mask = (1 << 30) - 1;
                 BigInteger u = this.Abs();
                 int bits = u.BitLength;
-                IList S = Platform.CreateArrayList();
+                var S = new List<string>();
                 while (bits > 30)
                 {
                     S.Add(Convert.ToString(u.IntValue & mask, 8));
@@ -3268,7 +3268,7 @@ namespace Org.BouncyCastle.Math
                 sb.Append(Convert.ToString(u.IntValue, 8));
                 for (int i = S.Count - 1; i >= 0; --i)
                 {
-                    AppendZeroExtendedString(sb, (string)S[i], 10);
+                    AppendZeroExtendedString(sb, S[i], 10);
                 }
                 break;
             }
@@ -3294,8 +3294,8 @@ namespace Org.BouncyCastle.Math
                 }
 
                 // TODO Could cache the moduli for each radix (soft reference?)
-                IList moduli = Platform.CreateArrayList();
-                BigInteger R = BigInteger.ValueOf(radix);
+                var moduli = new List<BigInteger>();
+                BigInteger R = ValueOf(radix);
                 while (R.CompareTo(q) <= 0)
                 {
                     moduli.Add(R);
@@ -3314,7 +3314,7 @@ namespace Org.BouncyCastle.Math
             return sb.ToString();
         }
 
-        private static void ToString(StringBuilder sb, int radix, IList moduli, int scale, BigInteger pos)
+        private static void ToString(StringBuilder sb, int radix, IList<BigInteger> moduli, int scale, BigInteger pos)
         {
             if (pos.BitLength < 64)
             {
@@ -3330,7 +3330,7 @@ namespace Org.BouncyCastle.Math
                 return;
             }
 
-            BigInteger[] qr = pos.DivideAndRemainder((BigInteger)moduli[--scale]);
+            BigInteger[] qr = pos.DivideAndRemainder(moduli[--scale]);
 
             ToString(sb, radix, moduli, scale, qr[0]);
             ToString(sb, radix, moduli, scale, qr[1]);
diff --git a/crypto/src/math/ec/ECCurve.cs b/crypto/src/math/ec/ECCurve.cs
index 838e407e3..8b078c2a8 100644
--- a/crypto/src/math/ec/ECCurve.cs
+++ b/crypto/src/math/ec/ECCurve.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 
 using Org.BouncyCastle.Math.EC.Abc;
 using Org.BouncyCastle.Math.EC.Endo;
@@ -148,7 +148,7 @@ namespace Org.BouncyCastle.Math.EC
         {
             CheckPoint(point);
 
-            IDictionary table;
+            IDictionary<string, PreCompInfo> table;
             lock (point)
             {
                 table = point.m_preCompTable;
@@ -159,7 +159,7 @@ namespace Org.BouncyCastle.Math.EC
 
             lock (table)
             {
-                return (PreCompInfo)table[name];
+                return table.TryGetValue(name, out var preCompInfo) ? preCompInfo : null;
             }
         }
 
@@ -179,19 +179,19 @@ namespace Org.BouncyCastle.Math.EC
         {
             CheckPoint(point);
 
-            IDictionary table;
+            IDictionary<string, PreCompInfo> table;
             lock (point)
             {
                 table = point.m_preCompTable;
                 if (null == table)
                 {
-                    point.m_preCompTable = table = Platform.CreateHashtable(4);
+                    point.m_preCompTable = table = new Dictionary<string, PreCompInfo>();
                 }
             }
 
             lock (table)
             {
-                PreCompInfo existing = (PreCompInfo)table[name];
+                PreCompInfo existing = table.TryGetValue(name, out var preCompInfo) ? preCompInfo : null;
                 PreCompInfo result = callback.Precompute(existing);
 
                 if (result != existing)
@@ -659,7 +659,7 @@ namespace Org.BouncyCastle.Math.EC
     {
         private const int FP_DEFAULT_COORDS = COORD_JACOBIAN_MODIFIED;
 
-        private static readonly IDictionary knownQs = Platform.CreateHashtable();
+        private static readonly HashSet<BigInteger> KnownQs = new HashSet<BigInteger>();
         private static readonly SecureRandom random = new SecureRandom();
 
         protected readonly BigInteger m_q, m_r;
@@ -679,38 +679,31 @@ namespace Org.BouncyCastle.Math.EC
         internal FpCurve(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger cofactor, bool isInternal)
             : base(q)
         {
-            if (isInternal)
+            if (!isInternal)
             {
-                this.m_q = q;
-                if (!knownQs.Contains(q))
-                {
-                    knownQs.Add(q, q);
-                }
-            }
-            else if (knownQs.Contains(q))
-            {
-                this.m_q = q;
-            }
-            else
-            {
-                int maxBitLength = AsInteger("Org.BouncyCastle.EC.Fp_MaxSize", 1042); // 2 * 521
-                int certainty = AsInteger("Org.BouncyCastle.EC.Fp_Certainty", 100);
+                bool unknownQ;
+                lock (KnownQs) unknownQ = !KnownQs.Contains(q);
 
-                int qBitLength = q.BitLength;
-                if (maxBitLength < qBitLength)
+                if (unknownQ)
                 {
-                    throw new ArgumentException("Fp q value out of range");
-                }
+                    int maxBitLength = AsInteger("Org.BouncyCastle.EC.Fp_MaxSize", 1042); // 2 * 521
+                    int certainty = AsInteger("Org.BouncyCastle.EC.Fp_Certainty", 100);
 
-                if (Primes.HasAnySmallFactors(q) || !Primes.IsMRProbablePrime(
-                    q, random, GetNumberOfIterations(qBitLength, certainty)))
-                {
-                    throw new ArgumentException("Fp q value not prime");
-                }
+                    int qBitLength = q.BitLength;
+                    if (maxBitLength < qBitLength)
+                        throw new ArgumentException("Fp q value out of range");
 
-                this.m_q = q;
+                    if (Primes.HasAnySmallFactors(q) ||
+                        !Primes.IsMRProbablePrime(q, random, GetNumberOfIterations(qBitLength, certainty)))
+                    {
+                        throw new ArgumentException("Fp q value not prime");
+                    }
+                }
             }
 
+            lock (KnownQs) KnownQs.Add(q);
+            this.m_q = q;
+
             this.m_r = FpFieldElement.CalculateResidue(q);
             this.m_infinity = new FpPoint(this, null, null);
 
diff --git a/crypto/src/math/ec/ECPoint.cs b/crypto/src/math/ec/ECPoint.cs
index f32376455..dcda5abfc 100644
--- a/crypto/src/math/ec/ECPoint.cs
+++ b/crypto/src/math/ec/ECPoint.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.Text;
 
 using Org.BouncyCastle.Math.EC.Multiplier;
@@ -51,8 +51,7 @@ namespace Org.BouncyCastle.Math.EC
         protected internal readonly ECFieldElement m_x, m_y;
         protected internal readonly ECFieldElement[] m_zs;
 
-        // Dictionary is (string -> PreCompInfo)
-        protected internal IDictionary m_preCompTable = null;
+        protected internal IDictionary<string, PreCompInfo> m_preCompTable = null;
 
         protected ECPoint(ECCurve curve, ECFieldElement	x, ECFieldElement y)
             : this(curve, x, y, GetInitialZCoords(curve))