Skip to content

Commit

Permalink
refactored f2m m check.
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Dec 22, 2023
1 parent b24dcaa commit fee80dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
10 changes: 5 additions & 5 deletions core/src/main/java/org/bouncycastle/math/ec/ECCurve.java
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,11 @@ public static BigInteger inverse(int m, int[] ks, BigInteger x)

private static FiniteField buildField(int m, int k1, int k2, int k3)
{
if (m > Properties.asInteger("org.bouncycastle.ec.max_f2m_field_size", 1142)) // twice 571
{
throw new IllegalArgumentException("field size out of range: " + m);
}

int[] exponents = (k2 | k3) == 0
? new int[]{ 0, k1, m }
: new int[]{ 0, k1, k2, k3, m };
Expand Down Expand Up @@ -1006,11 +1011,6 @@ protected ECFieldElement solveQuadraticEquation(ECFieldElement beta)
}

int m = this.getFieldSize();

if (m > Properties.asInteger("org.bouncycastle.ec.max_f2m_field_size", 1142)) // twice 571
{
throw new IllegalStateException("field size out of range: " + m);
}

// For odd m, use the half-trace
if (0 != (m & 1))
Expand Down
19 changes: 19 additions & 0 deletions core/src/test/java/org/bouncycastle/math/ec/test/ECPointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,25 @@ private void implTestAdd(ECPoint[] p, ECPoint infinity)
}
}

public void testLargeMInF2m()
{
int m = 2048;
int k1 = 1;
BigInteger aTpb = new BigInteger("1000", 2);
BigInteger bTpb = new BigInteger("1001", 2);
BigInteger n = new BigInteger("23");
BigInteger h = new BigInteger("1");

try
{
ECCurve.F2m curve = new ECCurve.F2m(m, k1, aTpb, bTpb, n, h);
}
catch (IllegalArgumentException e)
{
assertEquals("field size out of range: 2048", e.getMessage());
}
}

/**
* Calls <code>implTestAdd()</code> for <code>Fp</code> and
* <code>F2m</code>.
Expand Down

0 comments on commit fee80dd

Please sign in to comment.