summary refs log tree commit diff
path: root/crypto/src/math/ec/ECCurve.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/math/ec/ECCurve.cs')
-rw-r--r--crypto/src/math/ec/ECCurve.cs57
1 files changed, 25 insertions, 32 deletions
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);