Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

[PAN-2842] Blake 2b tweaks #1862

Merged
merged 6 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public int doFinal(final byte[] out, final int offset) {
compress();

for (int i = 0; i < h.length; i++) {
System.arraycopy(Pack.longToBigEndian(h[i]), 0, out, i * 8, 8);
System.arraycopy(Pack.longToLittleEndian(h[i]), 0, out, i * 8, 8);
}

reset();
Expand Down Expand Up @@ -224,7 +224,7 @@ private int bytesToInt(final byte[] bytes) {
}

private long bytesToLong(final byte[] bytes) {
return Pack.bigEndianToLong(bytes, 0);
return Pack.littleEndianToLong(bytes, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty bizarre that the payload is a mix of big endian and little endian values :\

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. But that's what some web3/solidity folks asked for in the EIP and got.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ public class Blake2bfMessageDigestTest {
private Blake2bfMessageDigest messageDigest;

// output when input is all 0
private byte[] blake2bfAllZero =
private static final byte[] BLAKE2F_ALL_ZERO =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these test cases coming from? Is there a spec we can link to? Would the tests be clearer if kept these values as hexary strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are not as useful as the precompiled test vectors. Those have more coverage. As far as I can tell they were calculated from the blake RFC.

new byte[] {
106, 9, -26, 103, -13, -68, -55, 8, -69, 103, -82, -123, -124, -54, -89, 59, 60, 110, -13,
114, -2, -108, -8, 43, -91, 79, -11, 58, 95, 29, 54, -15, 81, 14, 82, 127, -83, -26, -126,
-47, -101, 5, 104, -116, 43, 62, 108, 31, 31, -125, -39, -85, -5, 65, -67, 107, 91, -32,
-51, 25, 19, 126, 33, 121
8, -55, -68, -13, 103, -26, 9, 106, 59, -89, -54, -124, -123, -82, 103, -69, 43, -8, -108,
-2, 114, -13, 110, 60, -15, 54, 29, 95, 58, -11, 79, -91, -47, -126, -26, -83, 127, 82, 14,
81, 31, 108, 62, 43, -116, 104, 5, -101, 107, -67, 65, -5, -85, -39, -125, 31, 121, 33, 126,
19, 25, -51, -32, 91
};

// output when input is all 0 for 2147483648 rounds
private byte[] blake2bfAllZeroNegativeRounds =
// output when input is all 0 for 4294967295 rounds
private static final byte[] BLAKE2F_ALL_ZERO_NEGATIVE_ROUNDS =
new byte[] {
118, 127, 109, 29, 115, -124, -99, -111, 81, 112, 35, 60, -89, 75, 21, 18, -97, -73, 19,
-102, 40, -8, 78, 110, -43, 124, 66, 83, -89, 69, 69, 57, -25, -105, 123, 117, 115, 115, 78,
-92, 123, 87, 14, -127, -94, -1, -74, 25, -125, 48, 54, -78, -82, -75, 84, -26, -38, -42,
-93, 120, -61, 7, -58, 38
-111, -99, -124, 115, 29, 109, 127, 118, 18, 21, 75, -89, 60, 35, 112, 81, 110, 78, -8, 40,
-102, 19, -73, -97, 57, 69, 69, -89, 83, 66, 124, -43, -92, 78, 115, 115, 117, 123, -105,
-25, 25, -74, -1, -94, -127, 14, 87, 123, -26, 84, -75, -82, -78, 54, 48, -125, 38, -58, 7,
-61, 120, -93, -42, -38
};

@Before
Expand All @@ -54,32 +54,32 @@ public void digestIfUpdatedCorrectlyWithBytes() {
for (int i = 0; i < 213; i++) {
messageDigest.update((byte) 0);
}
assertThat(messageDigest.digest()).isEqualTo(blake2bfAllZero);
assertThat(messageDigest.digest()).isEqualTo(BLAKE2F_ALL_ZERO);
}

@Test
public void digestIfUpdatedCorrectlyWithByteArray() {
byte[] update = new byte[213];
final byte[] update = new byte[213];
messageDigest.update(update, 0, 213);
assertThat(messageDigest.digest()).isEqualTo(blake2bfAllZero);
assertThat(messageDigest.digest()).isEqualTo(BLAKE2F_ALL_ZERO);
}

@Test
public void digestIfUpdatedCorrectlyMixed() {
byte[] update = new byte[213];
final byte[] update = new byte[213];
messageDigest.update((byte) 0);
messageDigest.update(update, 2, 211);
messageDigest.update((byte) 0);
assertThat(messageDigest.digest()).isEqualTo(blake2bfAllZero);
assertThat(messageDigest.digest()).isEqualTo(BLAKE2F_ALL_ZERO);
}

@Test
public void digestWithNegativeRounds() {
// equal to Integer.MAX_VALUE + 1 (2147483648) as uint
byte[] rounds = Pack.intToBigEndian(Integer.MIN_VALUE);
public void digestWithMaxRounds() {
// equal to unsigned int max value (4294967295, or signed -1)
final byte[] rounds = Pack.intToBigEndian(Integer.MIN_VALUE);
messageDigest.update(rounds, 0, 4);
messageDigest.update(new byte[213], 0, 209);
assertThat(messageDigest.digest()).isEqualTo(blake2bfAllZeroNegativeRounds);
assertThat(messageDigest.digest()).isEqualTo(BLAKE2F_ALL_ZERO_NEGATIVE_ROUNDS);
}

@Test(expected = IllegalStateException.class)
Expand All @@ -99,14 +99,14 @@ public void throwsIfBufferUpdatedWithMoreThat213Bytes() {

@Test(expected = IllegalArgumentException.class)
public void throwsIfBufferUpdatedLargeByteArray() {
byte[] update = new byte[213];
final byte[] update = new byte[213];
messageDigest.update((byte) 0);
messageDigest.update(update, 0, 213);
}

@Test(expected = IllegalArgumentException.class)
public void throwsIfEmptyBufferUpdatedLargeByteArray() {
byte[] update = new byte[214];
final byte[] update = new byte[214];
messageDigest.update(update, 0, 214);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class HashTest {
"000000016a09e667f2bd8948bb67ae8584caa73b3c6ef372fe94f82ba54ff53a5f1d36f1510e527fade682d19b05688c2b3e6c1f1f83d9abfb41bd6b5be0cd19137e217907060504030201000f0e0d0c0b0a090817161514131211101f1e1d1c1b1a191827262524232221202f2e2d2c2b2a292837363534333231303f3e3d3c3b3a3938000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000001";

private static final String outputBlake2bf =
"62305ad4d48dade8269ef60a1bcb8b7bef6e479e643b5ac1f8017e6422ce89fb62d09ecaa81d095e855540dcbc07bd0feb3d4f5e5e50541260ed930f027cfd8d";
"bd45dd07a6eac13eec520b6aac885a1a89ea882978ae1a07472cc3e8cd358fd1a4d431c1a7d03f4a2bf746506779a970bfc5477026701076eda181e16acc24bc";

/** Validate keccak256 hash. */
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ public Gas gasRequirement(final BytesValue input) {
// Precompile can't be executed so we set its price to 0.
return Gas.ZERO;
}
if ((input.get(212) & 0xFE) != 0) {
// Input is malformed, F value can be only 0 or 1
return Gas.ZERO;
}

byte[] roundsBytes = copyOfRange(input.extractArray(), 0, 4);
BigInteger rounds = new BigInteger(1, roundsBytes);
final byte[] roundsBytes = copyOfRange(input.extractArray(), 0, 4);
final BigInteger rounds = new BigInteger(1, roundsBytes);
return Gas.of(rounds);
}

Expand All @@ -48,6 +52,9 @@ public BytesValue compute(final BytesValue input, final MessageFrame messageFram
if (input.size() != MESSAGE_LENGTH_BYTES) {
return BytesValue.EMPTY;
}
if ((input.get(212) & 0xFE) != 0) {
return BytesValue.EMPTY;
}
return Hash.blake2bf(input);
}
}
Loading