From d72274554502e72412170580390a09305b15e9d0 Mon Sep 17 00:00:00 2001 From: ivaylo Date: Thu, 4 Jul 2019 16:53:37 +0100 Subject: [PATCH] Add tests --- .../crypto/Blake2fMessageDigestTest.java | 112 ++++++++++++++++++ .../pegasys/pantheon/crypto/HashTest.java | 14 +++ 2 files changed, 126 insertions(+) create mode 100644 crypto/src/test/java/tech/pegasys/pantheon/crypto/Blake2fMessageDigestTest.java diff --git a/crypto/src/test/java/tech/pegasys/pantheon/crypto/Blake2fMessageDigestTest.java b/crypto/src/test/java/tech/pegasys/pantheon/crypto/Blake2fMessageDigestTest.java new file mode 100644 index 0000000000..94685e80f0 --- /dev/null +++ b/crypto/src/test/java/tech/pegasys/pantheon/crypto/Blake2fMessageDigestTest.java @@ -0,0 +1,112 @@ +/* + * Copyright 2019 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.crypto; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.bouncycastle.util.Pack; +import org.junit.Before; +import org.junit.Test; + +/** + * Test vectors adapted from + * https://github.com/keep-network/blake2b/blob/master/compression/f_test.go + */ +public class Blake2fMessageDigestTest { + + private Blake2fMessageDigest messageDigest; + + // output when input is all 0 + private byte[] blake2fAllZero = + 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 + }; + + // output when input is all 0 for 2147483648 rounds + private byte[] blake2fAllZeroNegativeRounds = + 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 + }; + + @Before + public void setUp() { + messageDigest = new Blake2fMessageDigest(); + } + + @Test + public void digestIfUpdatedCorrectlyWithBytes() { + for (int i = 0; i < 213; i++) { + messageDigest.update((byte) 0); + } + assertThat(messageDigest.digest()).isEqualTo(blake2fAllZero); + } + + @Test + public void digestIfUpdatedCorrectlyWithByteArray() { + byte[] update = new byte[213]; + messageDigest.update(update, 0, 213); + assertThat(messageDigest.digest()).isEqualTo(blake2fAllZero); + } + + @Test + public void digestIfUpdatedCorrectlyMixed() { + byte[] update = new byte[213]; + messageDigest.update((byte) 0); + messageDigest.update(update, 2, 211); + messageDigest.update((byte) 0); + assertThat(messageDigest.digest()).isEqualTo(blake2fAllZero); + } + + @Test + public void digestWithNegativeRounds() { + // equal to Integer.MAX_VALUE + 1 (2147483648) as uint + byte[] rounds = Pack.intToBigEndian(Integer.MIN_VALUE); + messageDigest.update(rounds, 0, 4); + messageDigest.update(new byte[213], 0, 209); + assertThat(messageDigest.digest()).isEqualTo(blake2fAllZeroNegativeRounds); + } + + @Test(expected = IllegalStateException.class) + public void throwsIfBufferUpdatedWithLessThat213Bytes() { + for (int i = 0; i < 212; i++) { + messageDigest.update((byte) 0); + } + messageDigest.digest(); + } + + @Test(expected = IllegalArgumentException.class) + public void throwsIfBufferUpdatedWithMoreThat213Bytes() { + for (int i = 0; i < 214; i++) { + messageDigest.update((byte) 0); + } + } + + @Test(expected = IllegalArgumentException.class) + public void throwsIfBufferUpdatedLargeByteArray() { + 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]; + messageDigest.update(update, 0, 214); + } +} diff --git a/crypto/src/test/java/tech/pegasys/pantheon/crypto/HashTest.java b/crypto/src/test/java/tech/pegasys/pantheon/crypto/HashTest.java index 12bed28bdd..50cf542137 100644 --- a/crypto/src/test/java/tech/pegasys/pantheon/crypto/HashTest.java +++ b/crypto/src/test/java/tech/pegasys/pantheon/crypto/HashTest.java @@ -17,6 +17,7 @@ import tech.pegasys.pantheon.util.bytes.BytesValue; +import org.bouncycastle.util.encoders.Hex; import org.junit.Test; public class HashTest { @@ -26,6 +27,12 @@ public class HashTest { private static final String horseKeccak256 = "c87f65ff3f271bf5dc8643484f66b200109caffe4bf98c4cb393dc35740b28c0"; + private static final String inputBlake2f = + "000000016a09e667f2bd8948bb67ae8584caa73b3c6ef372fe94f82ba54ff53a5f1d36f1510e527fade682d19b05688c2b3e6c1f1f83d9abfb41bd6b5be0cd19137e217907060504030201000f0e0d0c0b0a090817161514131211101f1e1d1c1b1a191827262524232221202f2e2d2c2b2a292837363534333231303f3e3d3c3b3a3938000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000001"; + + private static final String outputBlake2f = + "62305ad4d48dade8269ef60a1bcb8b7bef6e479e643b5ac1f8017e6422ce89fb62d09ecaa81d095e855540dcbc07bd0feb3d4f5e5e50541260ed930f027cfd8d"; + /** Validate keccak256 hash. */ @Test public void keccak256Hash() { @@ -35,4 +42,11 @@ public void keccak256Hash() { final BytesValue resultCow = Hash.keccak256(BytesValue.wrap("cow".getBytes(UTF_8))); assertEquals(BytesValue.fromHexString(cowKeccak256), resultCow); } + + /** Validate blake2f compression digest. */ + @Test + public void blake2fCompression() { + final BytesValue result = Hash.blake2f(BytesValue.wrap(Hex.decode(inputBlake2f))); + assertEquals(BytesValue.fromHexString(outputBlake2f), result); + } }