Branch data Line data Source code
# 1 : : // Copyright (c) 2013-2020 The Bitcoin Core developers
# 2 : : // Distributed under the MIT software license, see the accompanying
# 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 4 : :
# 5 : : #include <clientversion.h>
# 6 : : #include <crypto/siphash.h>
# 7 : : #include <hash.h>
# 8 : : #include <test/util/setup_common.h>
# 9 : : #include <util/strencodings.h>
# 10 : :
# 11 : : #include <boost/test/unit_test.hpp>
# 12 : :
# 13 : : BOOST_FIXTURE_TEST_SUITE(hash_tests, BasicTestingSetup)
# 14 : :
# 15 : : BOOST_AUTO_TEST_CASE(murmurhash3)
# 16 : 2 : {
# 17 : :
# 18 : 28 : #define T(expected, seed, data) BOOST_CHECK_EQUAL(MurmurHash3(seed, ParseHex(data)), expected)
# 19 : :
# 20 : : // Test MurmurHash3 with various inputs. Of course this is retested in the
# 21 : : // bloom filter tests - they would fail if MurmurHash3() had any problems -
# 22 : : // but is useful for those trying to implement Bitcoin libraries as a
# 23 : : // source of test data for their MurmurHash3() primitive during
# 24 : : // development.
# 25 : : //
# 26 : : // The magic number 0xFBA4C795 comes from CBloomFilter::Hash()
# 27 : :
# 28 : 2 : T(0x00000000U, 0x00000000, "");
# 29 : 2 : T(0x6a396f08U, 0xFBA4C795, "");
# 30 : 2 : T(0x81f16f39U, 0xffffffff, "");
# 31 : :
# 32 : 2 : T(0x514e28b7U, 0x00000000, "00");
# 33 : 2 : T(0xea3f0b17U, 0xFBA4C795, "00");
# 34 : 2 : T(0xfd6cf10dU, 0x00000000, "ff");
# 35 : :
# 36 : 2 : T(0x16c6b7abU, 0x00000000, "0011");
# 37 : 2 : T(0x8eb51c3dU, 0x00000000, "001122");
# 38 : 2 : T(0xb4471bf8U, 0x00000000, "00112233");
# 39 : 2 : T(0xe2301fa8U, 0x00000000, "0011223344");
# 40 : 2 : T(0xfc2e4a15U, 0x00000000, "001122334455");
# 41 : 2 : T(0xb074502cU, 0x00000000, "00112233445566");
# 42 : 2 : T(0x8034d2a0U, 0x00000000, "0011223344556677");
# 43 : 2 : T(0xb4698defU, 0x00000000, "001122334455667788");
# 44 : :
# 45 : 2 : #undef T
# 46 : 2 : }
# 47 : :
# 48 : : /*
# 49 : : SipHash-2-4 output with
# 50 : : k = 00 01 02 ...
# 51 : : and
# 52 : : in = (empty string)
# 53 : : in = 00 (1 byte)
# 54 : : in = 00 01 (2 bytes)
# 55 : : in = 00 01 02 (3 bytes)
# 56 : : ...
# 57 : : in = 00 01 02 ... 3e (63 bytes)
# 58 : :
# 59 : : from: https://131002.net/siphash/siphash24.c
# 60 : : */
# 61 : : uint64_t siphash_4_2_testvec[] = {
# 62 : : 0x726fdb47dd0e0e31, 0x74f839c593dc67fd, 0x0d6c8009d9a94f5a, 0x85676696d7fb7e2d,
# 63 : : 0xcf2794e0277187b7, 0x18765564cd99a68d, 0xcbc9466e58fee3ce, 0xab0200f58b01d137,
# 64 : : 0x93f5f5799a932462, 0x9e0082df0ba9e4b0, 0x7a5dbbc594ddb9f3, 0xf4b32f46226bada7,
# 65 : : 0x751e8fbc860ee5fb, 0x14ea5627c0843d90, 0xf723ca908e7af2ee, 0xa129ca6149be45e5,
# 66 : : 0x3f2acc7f57c29bdb, 0x699ae9f52cbe4794, 0x4bc1b3f0968dd39c, 0xbb6dc91da77961bd,
# 67 : : 0xbed65cf21aa2ee98, 0xd0f2cbb02e3b67c7, 0x93536795e3a33e88, 0xa80c038ccd5ccec8,
# 68 : : 0xb8ad50c6f649af94, 0xbce192de8a85b8ea, 0x17d835b85bbb15f3, 0x2f2e6163076bcfad,
# 69 : : 0xde4daaaca71dc9a5, 0xa6a2506687956571, 0xad87a3535c49ef28, 0x32d892fad841c342,
# 70 : : 0x7127512f72f27cce, 0xa7f32346f95978e3, 0x12e0b01abb051238, 0x15e034d40fa197ae,
# 71 : : 0x314dffbe0815a3b4, 0x027990f029623981, 0xcadcd4e59ef40c4d, 0x9abfd8766a33735c,
# 72 : : 0x0e3ea96b5304a7d0, 0xad0c42d6fc585992, 0x187306c89bc215a9, 0xd4a60abcf3792b95,
# 73 : : 0xf935451de4f21df2, 0xa9538f0419755787, 0xdb9acddff56ca510, 0xd06c98cd5c0975eb,
# 74 : : 0xe612a3cb9ecba951, 0xc766e62cfcadaf96, 0xee64435a9752fe72, 0xa192d576b245165a,
# 75 : : 0x0a8787bf8ecb74b2, 0x81b3e73d20b49b6f, 0x7fa8220ba3b2ecea, 0x245731c13ca42499,
# 76 : : 0xb78dbfaf3a8d83bd, 0xea1ad565322a1a0b, 0x60e61c23a3795013, 0x6606d7e446282b93,
# 77 : : 0x6ca4ecb15c5f91e1, 0x9f626da15c9625f3, 0xe51b38608ef25f57, 0x958a324ceb064572
# 78 : : };
# 79 : :
# 80 : : BOOST_AUTO_TEST_CASE(siphash)
# 81 : 2 : {
# 82 : 2 : CSipHasher hasher(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL);
# 83 : 2 : BOOST_CHECK_EQUAL(hasher.Finalize(), 0x726fdb47dd0e0e31ull);
# 84 : 2 : static const unsigned char t0[1] = {0};
# 85 : 2 : hasher.Write(t0, 1);
# 86 : 2 : BOOST_CHECK_EQUAL(hasher.Finalize(), 0x74f839c593dc67fdull);
# 87 : 2 : static const unsigned char t1[7] = {1,2,3,4,5,6,7};
# 88 : 2 : hasher.Write(t1, 7);
# 89 : 2 : BOOST_CHECK_EQUAL(hasher.Finalize(), 0x93f5f5799a932462ull);
# 90 : 2 : hasher.Write(0x0F0E0D0C0B0A0908ULL);
# 91 : 2 : BOOST_CHECK_EQUAL(hasher.Finalize(), 0x3f2acc7f57c29bdbull);
# 92 : 2 : static const unsigned char t2[2] = {16,17};
# 93 : 2 : hasher.Write(t2, 2);
# 94 : 2 : BOOST_CHECK_EQUAL(hasher.Finalize(), 0x4bc1b3f0968dd39cull);
# 95 : 2 : static const unsigned char t3[9] = {18,19,20,21,22,23,24,25,26};
# 96 : 2 : hasher.Write(t3, 9);
# 97 : 2 : BOOST_CHECK_EQUAL(hasher.Finalize(), 0x2f2e6163076bcfadull);
# 98 : 2 : static const unsigned char t4[5] = {27,28,29,30,31};
# 99 : 2 : hasher.Write(t4, 5);
# 100 : 2 : BOOST_CHECK_EQUAL(hasher.Finalize(), 0x7127512f72f27cceull);
# 101 : 2 : hasher.Write(0x2726252423222120ULL);
# 102 : 2 : BOOST_CHECK_EQUAL(hasher.Finalize(), 0x0e3ea96b5304a7d0ull);
# 103 : 2 : hasher.Write(0x2F2E2D2C2B2A2928ULL);
# 104 : 2 : BOOST_CHECK_EQUAL(hasher.Finalize(), 0xe612a3cb9ecba951ull);
# 105 : :
# 106 : 2 : BOOST_CHECK_EQUAL(SipHashUint256(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL, uint256S("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100")), 0x7127512f72f27cceull);
# 107 : :
# 108 : : // Check test vectors from spec, one byte at a time
# 109 : 2 : CSipHasher hasher2(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL);
# 110 [ + + ]: 130 : for (uint8_t x=0; x<std::size(siphash_4_2_testvec); ++x)
# 111 : 128 : {
# 112 : 128 : BOOST_CHECK_EQUAL(hasher2.Finalize(), siphash_4_2_testvec[x]);
# 113 : 128 : hasher2.Write(&x, 1);
# 114 : 128 : }
# 115 : : // Check test vectors from spec, eight bytes at a time
# 116 : 2 : CSipHasher hasher3(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL);
# 117 [ + + ]: 18 : for (uint8_t x=0; x<std::size(siphash_4_2_testvec); x+=8)
# 118 : 16 : {
# 119 : 16 : BOOST_CHECK_EQUAL(hasher3.Finalize(), siphash_4_2_testvec[x]);
# 120 : 16 : hasher3.Write(uint64_t(x)|(uint64_t(x+1)<<8)|(uint64_t(x+2)<<16)|(uint64_t(x+3)<<24)|
# 121 : 16 : (uint64_t(x+4)<<32)|(uint64_t(x+5)<<40)|(uint64_t(x+6)<<48)|(uint64_t(x+7)<<56));
# 122 : 16 : }
# 123 : :
# 124 : 2 : CHashWriter ss(SER_DISK, CLIENT_VERSION);
# 125 : 2 : CMutableTransaction tx;
# 126 : : // Note these tests were originally written with tx.nVersion=1
# 127 : : // and the test would be affected by default tx version bumps if not fixed.
# 128 : 2 : tx.nVersion = 1;
# 129 : 2 : ss << tx;
# 130 : 2 : BOOST_CHECK_EQUAL(SipHashUint256(1, 2, ss.GetHash()), 0x79751e980c2a0a35ULL);
# 131 : :
# 132 : : // Check consistency between CSipHasher and SipHashUint256[Extra].
# 133 : 2 : FastRandomContext ctx;
# 134 [ + + ]: 34 : for (int i = 0; i < 16; ++i) {
# 135 : 32 : uint64_t k1 = ctx.rand64();
# 136 : 32 : uint64_t k2 = ctx.rand64();
# 137 : 32 : uint256 x = InsecureRand256();
# 138 : 32 : uint32_t n = ctx.rand32();
# 139 : 32 : uint8_t nb[4];
# 140 : 32 : WriteLE32(nb, n);
# 141 : 32 : CSipHasher sip256(k1, k2);
# 142 : 32 : sip256.Write(x.begin(), 32);
# 143 : 32 : CSipHasher sip288 = sip256;
# 144 : 32 : sip288.Write(nb, 4);
# 145 : 32 : BOOST_CHECK_EQUAL(SipHashUint256(k1, k2, x), sip256.Finalize());
# 146 : 32 : BOOST_CHECK_EQUAL(SipHashUint256Extra(k1, k2, x, n), sip288.Finalize());
# 147 : 32 : }
# 148 : 2 : }
# 149 : :
# 150 : : BOOST_AUTO_TEST_SUITE_END()
|