hash/blake2: style

This commit is contained in:
Danny Robson 2019-02-04 20:18:32 +11:00
parent c0351be4fa
commit b6477214ad
2 changed files with 36 additions and 34 deletions

View File

@ -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<word_bits>::uint;
@ -30,20 +30,26 @@ struct traits {
static constexpr
std::array<word_t,8> 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<word_t,16> &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<word_t,8>
F (std::array<word_t,8> h, const word_t m[16], uint64_t t, bool f)
static std::array<word_t,8>
F (std::array<word_t,8> h, const word_t m[16], u64 t, bool f)
{
std::array<word_t,16> 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<word_t,8> h, const word_t m[16], uint64_t t, bool f)
///////////////////////////////////////////////////////////////////////////////
blake2::blake2 () noexcept:
blake2 (cruft::view<const uint8_t*>{nullptr})
blake2 (cruft::view<const u08*>{nullptr})
{ ; }
//-----------------------------------------------------------------------------
blake2::blake2 (cruft::view<const uint8_t *> key)
blake2::blake2 (cruft::view<const u08 *> 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<const uint8_t *> key)
//-----------------------------------------------------------------------------
blake2::digest_t
blake2::operator() (cruft::view<const uint8_t *> data) const noexcept
blake2::operator() (cruft::view<const u08 *> data) const noexcept
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
@ -158,7 +161,7 @@ blake2::operator() (cruft::view<const uint8_t *> 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<const uint8_t *> data) const noexcept
}
if (cursor != data.cend ()) {
std::array<uint64_t,16> tail {};
std::array<u64,16> 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<const uint8_t *> data) const noexcept
return d;
#pragma GCC diagnostic pop
}

View File

@ -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<u08> source (SOURCE_SIZE);
std::cerr << "generating data\n";