public FieldElement fromByteArray(byte[] x) { return enc.decode(x); }
@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 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)); }
public FieldElement fromByteArray(byte[] x) { return enc.decode(x); }
@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))); }