/** * Converts a field element to a BigInteger. * * @param f The field element. * @return The BigInteger. */ public static BigInteger toBigInteger(final FieldElement f) { return toBigInteger(f.toByteArray()); }
/** * Test method for {@link FieldElement#toByteArray()}. */ @Test public void testToByteArray() { byte[] zero = ZERO.toByteArray(); assertThat(zero.length, is(equalTo(BYTES_ZERO.length))); assertThat(zero, is(equalTo(BYTES_ZERO))); byte[] one = ONE.toByteArray(); assertThat(one.length, is(equalTo(BYTES_ONE.length))); assertThat(one, is(equalTo(BYTES_ONE))); byte[] ten = new BigIntegerFieldElement(ed25519Field, BigInteger.TEN).toByteArray(); assertThat(ten.length, is(equalTo(BYTES_TEN.length))); assertThat(ten, is(equalTo(BYTES_TEN))); }
@Test public void hashCodesAreEqualForEquivalentObjects() { // Arrange: final FieldElement f1 = getRandomFieldElement(); final FieldElement f2 = getField().getEncoding().decode(f1.toByteArray()); final FieldElement f3 = getRandomFieldElement(); final FieldElement f4 = getRandomFieldElement(); // Assert: Assert.assertThat(f1.hashCode(), IsEqual.equalTo(f2.hashCode())); Assert.assertThat(f1.hashCode(), IsNot.not(IsEqual.equalTo(f3.hashCode()))); Assert.assertThat(f1.hashCode(), IsNot.not(IsEqual.equalTo(f4.hashCode()))); Assert.assertThat(f3.hashCode(), IsNot.not(IsEqual.equalTo(f4.hashCode()))); }
/** * Converts the group element to an encoded point on the curve. * * @return The encoded point as byte array. */ public byte[] toByteArray() { switch (this.repr) { case P2: case P3: FieldElement recip = Z.invert(); FieldElement x = X.multiply(recip); FieldElement y = Y.multiply(recip); byte[] s = y.toByteArray(); s[s.length-1] |= (x.isNegative() ? (byte) 0x80 : 0); return s; default: return toP2().toByteArray(); } }
@Test public void scalarMultiplyBasePointWithOneReturnsBasePoint() { // Arrange: final GroupElement basePoint = ed25519.getB(); // Act: final GroupElement g = basePoint.scalarMultiply(curve.getField().ONE.toByteArray()); // Assert: Assert.assertThat(basePoint, IsEqual.equalTo(g)); }
@Test public void doubleScalarMultiplyVariableTimeReturnsExpectedResult() { for (int i=0; i<10; i++) { // Arrange: final GroupElement basePoint = ed25519.getB(); final GroupElement g = MathUtils.getRandomGroupElement(true); final FieldElement f1 = MathUtils.getRandomFieldElement(); final FieldElement f2 = MathUtils.getRandomFieldElement(); // Act: final GroupElement h1 = basePoint.doubleScalarMultiplyVariableTime(g, f2.toByteArray(), f1.toByteArray()); final GroupElement h2 = MathUtils.doubleScalarMultiplyGroupElements(basePoint, f1, g, f2); // Assert: Assert.assertThat(h1, IsEqual.equalTo(h2)); } }
@Test public void scalarMultiplyBasePointWithZeroReturnsNeutralElement() { // Arrange: final GroupElement basePoint = ed25519.getB(); // Act: final GroupElement g = basePoint.scalarMultiply(curve.getField().ZERO.toByteArray()); // Assert: Assert.assertThat(curve.getZero(GroupElement.Representation.P3), IsEqual.equalTo(g)); }
@Test public void scalarMultiplyBasePointReturnsExpectedResult() { for (int i=0; i<10; i++) { // Arrange: final GroupElement basePoint = ed25519.getB(); final FieldElement f = MathUtils.getRandomFieldElement(); // Act: final GroupElement g = basePoint.scalarMultiply(f.toByteArray()); final GroupElement h = MathUtils.scalarMultiplyGroupElement(basePoint, f); // Assert: Assert.assertThat(g, IsEqual.equalTo(h)); } }
/** * Scalar multiply the group element by the field element. * * @param g The group element. * @param f The field element. * @return The resulting group element. */ public static GroupElement scalarMultiplyGroupElement(final GroupElement g, final FieldElement f) { final byte[] bytes = f.toByteArray(); GroupElement h = curve.getZero(GroupElement.Representation.P3); for (int i=254; i>=0; i--) { h = doubleGroupElement(h); if (Utils.bit(bytes, i) == 1) { h = addGroupElements(h, g); } } return h; }
final BigInteger g1X = toBigInteger(g1.getX().toByteArray()); 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 equalsOnlyReturnsTrueForEquivalentObjects() { // Arrange: final FieldElement f1 = getRandomFieldElement(); final FieldElement f2 = getField().getEncoding().decode(f1.toByteArray()); final FieldElement f3 = getRandomFieldElement(); final FieldElement f4 = getRandomFieldElement(); // Assert: Assert.assertThat(f1, IsEqual.equalTo(f2)); Assert.assertThat(f1, IsNot.not(IsEqual.equalTo(f3))); Assert.assertThat(f1, IsNot.not(IsEqual.equalTo(f4))); Assert.assertThat(f3, IsNot.not(IsEqual.equalTo(f4))); }
BigInteger x; 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());
/** * Converts the group element to an encoded point on the curve. * * @return The encoded point as byte array. */ public byte[] toByteArray() { switch (this.repr) { case P2: case P3: FieldElement recip = Z.invert(); FieldElement x = X.multiply(recip); FieldElement y = Y.multiply(recip); byte[] s = y.toByteArray(); s[s.length-1] |= (x.isNegative() ? (byte) 0x80 : 0); return s; default: return toP2().toByteArray(); } }