/** * Encode a FieldElement in its (b-1)-bit encoding. * @return the (b-1)-bit encoding of this FieldElement. */ public byte[] toByteArray() { return f.getEncoding().encode(this); }
public boolean isNegative() { return f.getEncoding().isNegative(this); }
@Test public void toStringReturnsCorrectRepresentation() { // Arrange: final byte[] bytes = new byte[32]; for (int i=0; i<32; i++) { bytes[i] = (byte)(i+1); } final FieldElement f = MathUtils.getField().getEncoding().decode(bytes); // Act: final String fAsString = f.toString(); final StringBuilder builder = new StringBuilder(); builder.append("[Ed25519FieldElement val="); for (byte b : bytes) { builder.append(String.format("%02x", b)); } builder.append("]"); // Assert: Assert.assertThat(fAsString, IsEqual.equalTo(builder.toString())); }
/** * Converts a BigInteger to a field element. * * @param b The BigInteger. * @return The field element. */ public static FieldElement toFieldElement(final BigInteger b) { return getField().getEncoding().decode(toByteArray(b)); }
@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()))); }
@Test public void isNegativeReturnsCorrectResult() { for (int i=0; i<10000; i++) { // Arrange: final int[] t = new int[10]; for (int j=0; j<10; j++) { t[j] = random.nextInt(1 << 28) - (1 << 27); } final boolean isNegative = MathUtils.toBigInteger(t).mod(MathUtils.getQ()).mod(new BigInteger("2")).equals(BigInteger.ONE); final FieldElement f = new Ed25519FieldElement(MathUtils.getField(), t); // Assert: Assert.assertThat(MathUtils.getField().getEncoding().isNegative(f), IsEqual.equalTo(isNegative)); } } }
@Test public void decodeReturnsCorrectFieldElement() { for (int i=0; i<10000; i++) { // Arrange: final byte[] bytes = new byte[32]; random.nextBytes(bytes); bytes[31] = (byte)(bytes[31] & 0x7f); final BigInteger b1 = MathUtils.toBigInteger(bytes); // Act: final Ed25519FieldElement f = (Ed25519FieldElement)MathUtils.getField().getEncoding().decode(bytes); final BigInteger b2 = MathUtils.toBigInteger(f.t).mod(MathUtils.getQ()); // Assert: Assert.assertThat(b2, IsEqual.equalTo(b1)); } }
@Test public void decodeReturnsCorrectFieldElementForSimpleByteArrays() { // Arrange: final byte[] bytes1 = new byte[32]; final byte[] bytes2 = new byte[32]; bytes2[0] = 1; // Act: final Ed25519FieldElement f1 = (Ed25519FieldElement)MathUtils.getField().getEncoding().decode(bytes1); final Ed25519FieldElement f2 = (Ed25519FieldElement)MathUtils.getField().getEncoding().decode(bytes2); final BigInteger b1 = MathUtils.toBigInteger(f1.t); final BigInteger b2 = MathUtils.toBigInteger(f2.t); // Assert: Assert.assertThat(b1, IsEqual.equalTo(BigInteger.ZERO)); Assert.assertThat(b2, IsEqual.equalTo(BigInteger.ONE)); }
@Test public void encodeReturnsCorrectByteArray() { for (int i=0; i<10000; i++){ // Arrange: final int[] t = new int[10]; for (int j=0; j<10; j++) { t[j] = random.nextInt(1 << 28) - (1 << 27); } final FieldElement fieldElement1 = new Ed25519FieldElement(MathUtils.getField(), t); final BigInteger b = MathUtils.toBigInteger(t); // Act: final byte[] bytes = MathUtils.getField().getEncoding().encode(fieldElement1); // Assert: Assert.assertThat(bytes, IsEqual.equalTo(MathUtils.toByteArray(b.mod(MathUtils.getQ())))); } }
@Test public void encodeReturnsCorrectByteArrayForSimpleFieldElements() { // Arrange: final int[] t1 = new int[10]; final int[] t2 = new int[10]; t2[0] = 1; final FieldElement fieldElement1 = new Ed25519FieldElement(MathUtils.getField(), t1); final FieldElement fieldElement2 = new Ed25519FieldElement(MathUtils.getField(), t2); // Act: final byte[] bytes1 = MathUtils.getField().getEncoding().encode(fieldElement1); final byte[] bytes2 = MathUtils.getField().getEncoding().encode(fieldElement2); // Assert: Assert.assertThat(bytes1, IsEqual.equalTo(MathUtils.toByteArray(BigInteger.ZERO))); Assert.assertThat(bytes2, IsEqual.equalTo(MathUtils.toByteArray(BigInteger.ONE))); }
@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))); }
/** * Encode a FieldElement in its $(b-1)$-bit encoding. * @return the $(b-1)$-bit encoding of this FieldElement. */ public byte[] toByteArray() { return f.getEncoding().encode(this); }
public boolean isNegative() { return f.getEncoding().isNegative(this); }