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))
|