Skip to content

Commit a20b027

Browse files
committed
Improve sqrt tests
1 parent 2a3ffb2 commit a20b027

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

core/src/test/java/org/bouncycastle/math/ec/test/ECPointTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,15 +431,25 @@ private void implSqrtTest(ECCurve c)
431431
BigInteger pMinusOne = p.subtract(ECConstants.ONE);
432432
BigInteger legendreExponent = p.shiftRight(1);
433433

434-
int count = 0;
435-
while (count < 10)
434+
ECFieldElement zero = c.fromBigInteger(BigInteger.ZERO);
435+
assertEquals(zero, zero.sqrt());
436+
437+
ECFieldElement one = c.fromBigInteger(BigInteger.ONE);
438+
assertEquals(one, one.sqrt());
439+
440+
for (int i = 0; i < 20; ++i)
436441
{
437-
BigInteger nonSquare = BigIntegers.createRandomInRange(ECConstants.TWO, pMinusOne, secRand);
438-
if (!nonSquare.modPow(legendreExponent, p).equals(ECConstants.ONE))
442+
BigInteger x = BigIntegers.createRandomInRange(ECConstants.TWO, pMinusOne, secRand);
443+
ECFieldElement fe = c.fromBigInteger(x);
444+
ECFieldElement root = fe.sqrt();
445+
446+
if (root == null)
447+
{
448+
assertEquals(pMinusOne, x.modPow(legendreExponent, p));
449+
}
450+
else
439451
{
440-
ECFieldElement root = c.fromBigInteger(nonSquare).sqrt();
441-
assertNull(root);
442-
++count;
452+
assertEquals(fe, root.square());
443453
}
444454
}
445455
}

0 commit comments

Comments
 (0)