diff --git a/hash/blake2.cpp b/hash/blake2.cpp index 94c475f..364aff2 100644 --- a/hash/blake2.cpp +++ b/hash/blake2.cpp @@ -17,7 +17,7 @@ using cruft::crypto::hash::blake2; /////////////////////////////////////////////////////////////////////////////// -// blake2b: uint64_t +// blake2b: u64 struct traits { static constexpr int word_bits = 64; using word_t = typename cruft::bits_type::uint; @@ -30,20 +30,26 @@ struct traits { static constexpr std::array iv { - 0x6A09E667F3BCC908, 0xBB67AE8584CAA73B, - 0x3C6EF372FE94F82B, 0xA54FF53A5F1D36F1, - 0x510E527FADE682D1, 0x9B05688C2B3E6C1F, - 0x1F83D9ABFB41BD6B, 0x5BE0CD19137E2179 + 0x6A09E667F3BCC908, + 0xBB67AE8584CAA73B, + 0x3C6EF372FE94F82B, + 0xA54FF53A5F1D36F1, + 0x510E527FADE682D1, + 0x9B05688C2B3E6C1F, + 0x1F83D9ABFB41BD6B, + 0x5BE0CD19137E2179 }; }; +//----------------------------------------------------------------------------- using word_t = traits::word_t; + +//----------------------------------------------------------------------------- static constexpr int - sigma[12][16] { - +sigma[12][16] { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, }, { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3, }, { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4, }, @@ -63,35 +69,32 @@ int // mixing function static constexpr void -G (std::array &v, int a, int b, int c, int d, word_t x, word_t y) +G (word_t v[16], int a, int b, int c, int d, word_t x, word_t y) { - v[a] = v[a] + v[b] + x; - v[d] = cruft::rotater (v[d] ^ v[a], traits::rotations[0]); - v[c] = v[c] + v[d]; - v[b] = cruft::rotater (v[b] ^ v[c], traits::rotations[1]); - - v[a] = v[a] + v[b] + y; - v[d] = cruft::rotater (v[d] ^ v[a], traits::rotations[2]); - - v[c] = v[c] + v[d]; - v[b] = cruft::rotater (v[b] ^ v[c], traits::rotations[3]); + v[a] = v[a] + v[b] + x; v[d] = cruft::rotater (v[d] ^ v[a], traits::rotations[0]); + v[c] = v[c] + v[d]; v[b] = cruft::rotater (v[b] ^ v[c], traits::rotations[1]); + v[a] = v[a] + v[b] + y; v[d] = cruft::rotater (v[d] ^ v[a], traits::rotations[2]); + v[c] = v[c] + v[d]; v[b] = cruft::rotater (v[b] ^ v[c], traits::rotations[3]); } //----------------------------------------------------------------------------- // compression function -std::array -F (std::array h, const word_t m[16], uint64_t t, bool f) +static std::array +F (std::array h, const word_t m[16], u64 t, bool f) { - std::array v { - h[0], h[1], h[2], h[3], - h[4], h[5], h[6], h[7], + word_t v[16] { + h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7], - traits::iv[0], traits::iv[1], - traits::iv[2], traits::iv[3], - traits::iv[4], traits::iv[5], - traits::iv[6], traits::iv[7], + traits::iv[0], + traits::iv[1], + traits::iv[2], + traits::iv[3], + traits::iv[4], + traits::iv[5], + traits::iv[6], + traits::iv[7], }; v[12] ^= t; @@ -123,12 +126,12 @@ F (std::array h, const word_t m[16], uint64_t t, bool f) /////////////////////////////////////////////////////////////////////////////// blake2::blake2 () noexcept: - blake2 (cruft::view{nullptr}) + blake2 (cruft::view{nullptr}) { ; } //----------------------------------------------------------------------------- -blake2::blake2 (cruft::view key) +blake2::blake2 (cruft::view key) { // don't give the user flexibility to provide too much key if (key.size () > ::traits::max_key_bytes) @@ -142,7 +145,7 @@ blake2::blake2 (cruft::view key) //----------------------------------------------------------------------------- blake2::digest_t -blake2::operator() (cruft::view data) const noexcept +blake2::operator() (cruft::view data) const noexcept { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcast-align" @@ -158,7 +161,7 @@ blake2::operator() (cruft::view data) const noexcept h = F (h, zeroes.data (), 0, true); } - uint64_t counter = m_keylen?::traits::block_bytes:0; + u64 counter = m_keylen?::traits::block_bytes:0; auto cursor = data.begin (); while (cursor + ::traits::block_bytes < data.end ()) { @@ -168,7 +171,7 @@ blake2::operator() (cruft::view data) const noexcept } if (cursor != data.cend ()) { - std::array tail {}; + std::array tail {}; memcpy (tail.data(), data.data (), data.cend () - cursor); counter += data.end () - cursor; h = F (h, tail.data (), counter, true); @@ -179,4 +182,3 @@ blake2::operator() (cruft::view data) const noexcept return d; #pragma GCC diagnostic pop } - diff --git a/tools/bench.cpp b/tools/bench.cpp index 729fd94..18bca30 100644 --- a/tools/bench.cpp +++ b/tools/bench.cpp @@ -40,7 +40,7 @@ int main (void) { // 1 GiB of random data; - static constexpr std::size_t SOURCE_SIZE = 1024u * 1024u * 10; + static constexpr std::size_t SOURCE_SIZE = 1024u * 1024u * 1024; std::vector source (SOURCE_SIZE); std::cerr << "generating data\n";