@Test public void toByteArrayReturnsExpectedResult() { for (int i=0; i<100; i++) { // Arrange: final GroupElement g = MathUtils.getRandomGroupElement(); // Act: final byte[] gBytes = g.toByteArray(); final byte[] bytes = MathUtils.toByteArray(MathUtils.toBigInteger(g.getY())); if (MathUtils.toBigInteger(g.getX()).mod(new BigInteger("2")).equals(BigInteger.ONE)) { bytes[31] |= 0x80; } // Assert: Assert.assertThat(Arrays.equals(gBytes, bytes), IsEqual.equalTo(true)); } }
@Test public void toP2ReturnsExpectedResultIfGroupElementHasP2Representation() { for (int i=0; i<10; i++) { // Arrange: final GroupElement g = MathUtils.toRepresentation(MathUtils.getRandomGroupElement(), GroupElement.Representation.P2); // Act: final GroupElement h = g.toP2(); // Assert: Assert.assertThat(h, IsEqual.equalTo(g)); Assert.assertThat(h.getRepresentation(), IsEqual.equalTo(GroupElement.Representation.P2)); Assert.assertThat(h.getX(), IsEqual.equalTo(g.getX())); Assert.assertThat(h.getY(), IsEqual.equalTo(g.getY())); Assert.assertThat(h.getZ(), IsEqual.equalTo(g.getZ())); Assert.assertThat(h.getT(), IsEqual.equalTo(null)); } }
@Test public void toP3ReturnsExpectedResultIfGroupElementHasP3Representation() { for (int i=0; i<10; i++) { // Arrange: final GroupElement g = MathUtils.getRandomGroupElement(); // Act: final GroupElement h = g.toP3(); // Assert: Assert.assertThat(h, IsEqual.equalTo(g)); Assert.assertThat(h.getRepresentation(), IsEqual.equalTo(GroupElement.Representation.P3)); Assert.assertThat(h, IsEqual.equalTo(g)); Assert.assertThat(h.getX(), IsEqual.equalTo(g.getX())); Assert.assertThat(h.getY(), IsEqual.equalTo(g.getY())); Assert.assertThat(h.getZ(), IsEqual.equalTo(g.getZ())); Assert.assertThat(h.getT(), IsEqual.equalTo(g.getT())); } }
@Test public void toP2ReturnsExpectedResultIfGroupElementHasP3Representation() { for (int i=0; i<10; i++) { // Arrange: final GroupElement g = MathUtils.getRandomGroupElement(); // Act: final GroupElement h1 = g.toP2(); final GroupElement h2 = MathUtils.toRepresentation(g, GroupElement.Representation.P2); // Assert: Assert.assertThat(h1, IsEqual.equalTo(h2)); Assert.assertThat(h1.getRepresentation(), IsEqual.equalTo(GroupElement.Representation.P2)); Assert.assertThat(h1.getX(), IsEqual.equalTo(g.getX())); Assert.assertThat(h1.getY(), IsEqual.equalTo(g.getY())); Assert.assertThat(h1.getZ(), IsEqual.equalTo(g.getZ())); Assert.assertThat(h1.getT(), IsEqual.equalTo(null)); } }
@Test public void toCachedReturnsExpectedResultIfGroupElementHasCachedRepresentation() { for (int i=0; i<10; i++) { // Arrange: final GroupElement g = MathUtils.toRepresentation(MathUtils.getRandomGroupElement(), GroupElement.Representation.CACHED); // Act: final GroupElement h = g.toCached(); // Assert: Assert.assertThat(h, IsEqual.equalTo(g)); Assert.assertThat(h.getRepresentation(), IsEqual.equalTo(GroupElement.Representation.CACHED)); Assert.assertThat(h, IsEqual.equalTo(g)); Assert.assertThat(h.getX(), IsEqual.equalTo(g.getX())); Assert.assertThat(h.getY(), IsEqual.equalTo(g.getY())); Assert.assertThat(h.getZ(), IsEqual.equalTo(g.getZ())); Assert.assertThat(h.getT(), IsEqual.equalTo(g.getT())); } }
@Test public void toCachedReturnsExpectedResultIfGroupElementHasP3Representation() { for (int i=0; i<10; i++) { // Arrange: final GroupElement g = MathUtils.getRandomGroupElement(); // Act: final GroupElement h1 = g.toCached(); final GroupElement h2 = MathUtils.toRepresentation(g, GroupElement.Representation.CACHED); // Assert: Assert.assertThat(h1, IsEqual.equalTo(h2)); Assert.assertThat(h1.getRepresentation(), IsEqual.equalTo(GroupElement.Representation.CACHED)); Assert.assertThat(h1, IsEqual.equalTo(g)); Assert.assertThat(h1.getX(), IsEqual.equalTo(g.getY().add(g.getX()))); Assert.assertThat(h1.getY(), IsEqual.equalTo(g.getY().subtract(g.getX()))); Assert.assertThat(h1.getZ(), IsEqual.equalTo(g.getZ())); Assert.assertThat(h1.getT(), IsEqual.equalTo(g.getT().multiply(curve.get2D()))); } }
/** * Negates a group element. * * @param g The group element. * @return The negated group element. */ public static GroupElement negateGroupElement(final GroupElement g) { if (g.getRepresentation() != GroupElement.Representation.P3) { throw new IllegalArgumentException("g must have representation P3"); } return GroupElement.p3(g.getCurve(), g.getX().negate(), g.getY(), g.getZ(), g.getT().negate()); }
@Test public void toP3ReturnsExpectedResultIfGroupElementHasP1P1Representation() { for (int i=0; i<10; i++) { // Arrange: final GroupElement g = MathUtils.toRepresentation(MathUtils.getRandomGroupElement(), GroupElement.Representation.P1P1); // Act: final GroupElement h1 = g.toP3(); final GroupElement h2 = MathUtils.toRepresentation(g, GroupElement.Representation.P3); // Assert: Assert.assertThat(h1, IsEqual.equalTo(h2)); Assert.assertThat(h1.getRepresentation(), IsEqual.equalTo(GroupElement.Representation.P3)); Assert.assertThat(h1.getX(), IsEqual.equalTo(g.getX().multiply(g.getT()))); Assert.assertThat(h1.getY(), IsEqual.equalTo(g.getY().multiply(g.getZ()))); Assert.assertThat(h1.getZ(), IsEqual.equalTo(g.getZ().multiply(g.getT()))); Assert.assertThat(h1.getT(), IsEqual.equalTo(g.getX().multiply(g.getY()))); } }
@Test public void toP2ReturnsExpectedResultIfGroupElementHasP1P1Representation() { for (int i=0; i<10; i++) { // Arrange: final GroupElement g = MathUtils.toRepresentation(MathUtils.getRandomGroupElement(), GroupElement.Representation.P1P1); // Act: final GroupElement h1 = g.toP2(); final GroupElement h2 = MathUtils.toRepresentation(g, GroupElement.Representation.P2); // Assert: Assert.assertThat(h1, IsEqual.equalTo(h2)); Assert.assertThat(h1.getRepresentation(), IsEqual.equalTo(GroupElement.Representation.P2)); Assert.assertThat(h1.getX(), IsEqual.equalTo(g.getX().multiply(g.getT()))); Assert.assertThat(h1.getY(), IsEqual.equalTo(g.getY().multiply(g.getZ()))); Assert.assertThat(h1.getZ(), IsEqual.equalTo(g.getZ().multiply(g.getT()))); Assert.assertThat(h1.getT(), IsEqual.equalTo(null)); } }
final BigInteger g1Y = toBigInteger(g1.getY().toByteArray()); final BigInteger g1Z = toBigInteger(g1.getZ().toByteArray()); final BigInteger g2X = toBigInteger(g2.getX().toByteArray()); final BigInteger g2Y = toBigInteger(g2.getY().toByteArray()); final BigInteger g2Z = toBigInteger(g2.getZ().toByteArray());
@Test public void isOnCurveReturnsFalseForPointsNotOnTheCurve() { for (int i=0; i<100; i++) { // Arrange: final GroupElement g = MathUtils.getRandomGroupElement(); final GroupElement h = GroupElement.p2(curve, g.getX(), g.getY(), g.getZ().multiply(curve.getField().TWO)); // Assert (can only fail for 5*Z^2=1): Assert.assertThat(h.isOnCurve(), IsEqual.equalTo(false)); } } }
@Test public void toP3PrecomputeDoubleReturnsExpectedResultIfGroupElementHasP1P1Representation() { for (int i=0; i<10; i++) { // Arrange: final GroupElement g = MathUtils.toRepresentation(MathUtils.getRandomGroupElement(), GroupElement.Representation.P1P1); // Act: final GroupElement h1 = g.toP3PrecomputeDouble(); final GroupElement h2 = MathUtils.toRepresentation(g, GroupElement.Representation.P3PrecomputedDouble); // Assert: Assert.assertThat(h1, IsEqual.equalTo(h2)); Assert.assertThat(h1.getRepresentation(), IsEqual.equalTo(GroupElement.Representation.P3)); Assert.assertThat(h1.getX(), IsEqual.equalTo(g.getX().multiply(g.getT()))); Assert.assertThat(h1.getY(), IsEqual.equalTo(g.getY().multiply(g.getZ()))); Assert.assertThat(h1.getZ(), IsEqual.equalTo(g.getZ().multiply(g.getT()))); Assert.assertThat(h1.getT(), IsEqual.equalTo(g.getX().multiply(g.getY()))); Assert.assertThat(h1.precmp, IsNull.nullValue()); Assert.assertThat(h1.dblPrecmp, IsNull.notNullValue()); Assert.assertThat(h1.dblPrecmp, IsEqual.equalTo(h2.dblPrecmp)); } }
BigInteger y; final BigInteger gX = toBigInteger(g.getX().toByteArray()); final BigInteger gY = toBigInteger(g.getY().toByteArray()); final BigInteger gZ = toBigInteger(g.getZ().toByteArray()); final BigInteger gT = null == g.getT()? null : toBigInteger(g.getT().toByteArray());