hash/blake: add traits units
This commit is contained in:
parent
cfc728a3a7
commit
46c0c81918
@ -37,6 +37,11 @@ foreach (c ${components})
|
||||
list (APPEND sources "${c}.cpp" "${c}.hpp")
|
||||
endforeach()
|
||||
|
||||
list (APPEND sources
|
||||
hash/blake/traits.cpp
|
||||
hash/blake/traits.hpp
|
||||
)
|
||||
|
||||
|
||||
option (TESTS "enable unit testing" ON)
|
||||
|
||||
|
@ -18,92 +18,7 @@
|
||||
#include <cstdint>
|
||||
|
||||
using cruft::crypto::hash::blake;
|
||||
using cruft::crypto::hash::traits;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
const std::array<traits<256>::word_t,8>
|
||||
traits<256>::iv
|
||||
{
|
||||
0x6A09E667, // frac(sqrt( 2))
|
||||
0xBB67AE85, // frac(sqrt( 3))
|
||||
0x3C6EF372, // frac(sqrt( 5))
|
||||
0xA54FF53A, // frac(sqrt( 7))
|
||||
0x510E527F, // frac(sqrt(11))
|
||||
0x9B05688C, // frac(sqrt(13))
|
||||
0x1F83D9AB, // frac(sqrt(17))
|
||||
0x5BE0CD19, // frac(sqrt(19))
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const std::array<traits<256>::word_t,16>
|
||||
traits<256>::pi
|
||||
{
|
||||
0x243F6A88,
|
||||
0x85A308D3,
|
||||
0x13198A2E,
|
||||
0x03707344,
|
||||
0xA4093822,
|
||||
0x299F31D0,
|
||||
0x082EFA98,
|
||||
0xEC4E6C89,
|
||||
0x452821E6,
|
||||
0x38D01377,
|
||||
0xBE5466CF,
|
||||
0x34E90C6C,
|
||||
0xC0AC29B7,
|
||||
0xC97C50DD,
|
||||
0x3F84D5B5,
|
||||
0xB5470917,
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const std::array<int,4>
|
||||
traits<256>::rotations {
|
||||
16, 12, 8, 7
|
||||
};
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
const std::array<traits<512>::word_t,8>
|
||||
traits<512>::iv {
|
||||
0x6A09E667F3BCC908,
|
||||
0xBB67AE8584CAA73B,
|
||||
0x3C6EF372FE94F82B,
|
||||
0xA54FF53A5F1D36F1,
|
||||
0x510E527FADE682D1,
|
||||
0x9B05688C2B3E6C1F,
|
||||
0x1F83D9ABFB41BD6B,
|
||||
0x5BE0CD19137E2179,
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const std::array<traits<512>::word_t,16>
|
||||
traits<512>::pi {
|
||||
0x243F6A8885A308D3,
|
||||
0x13198A2E03707344,
|
||||
0xA4093822299F31D0,
|
||||
0x082EFA98EC4E6C89,
|
||||
0x452821E638D01377,
|
||||
0xBE5466CF34E90C6C,
|
||||
0xC0AC29B7C97C50DD,
|
||||
0x3F84D5B5B5470917,
|
||||
0x9216D5D98979FB1B,
|
||||
0xD1310BA698DFB5AC,
|
||||
0x2FFD72DBD01ADFB7,
|
||||
0xB8E1AFED6A267E96,
|
||||
0xBA7C9045F12C7F99,
|
||||
0x24A19947B3916CF7,
|
||||
0x0801F2E2858EFC16,
|
||||
0x636920D871574E69,
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const std::array<int,4> traits<512>::rotations { 32, 25, 16, 11 };
|
||||
using cruft::crypto::hash::detail::blake::traits;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -111,7 +26,7 @@ const std::array<int,4> traits<512>::rotations { 32, 25, 16, 11 };
|
||||
// out a pretty frequent modulus operation.
|
||||
static constexpr
|
||||
int
|
||||
permute[16][16] = {
|
||||
permute[16][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, },
|
||||
@ -208,7 +123,7 @@ blake<width>::operator() (
|
||||
cruft::view<const u08*> data,
|
||||
cruft::view<const u08*> salt
|
||||
) const {
|
||||
std::array<typename traits<width>::word_t, 4> fwd {};
|
||||
std::array<typename detail::blake::traits<width>::word_t, 4> fwd {};
|
||||
|
||||
if (salt.size () > sizeof (fwd))
|
||||
throw std::invalid_argument ("oversized salt");
|
||||
@ -222,9 +137,9 @@ template <int width>
|
||||
typename blake<width>::digest_t
|
||||
blake<width>::operator() (
|
||||
cruft::view<const u08 *> data,
|
||||
const std::array<typename traits<width>::word_t, 4> salt
|
||||
const std::array<typename detail::blake::traits<width>::word_t, 4> salt
|
||||
) const noexcept {
|
||||
auto h = traits<width>::iv;
|
||||
auto h = detail::blake::traits<width>::iv;
|
||||
|
||||
// bounce the message data through d08/dw so we can perform endian
|
||||
// conversion.
|
||||
|
@ -8,6 +8,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "blake/traits.hpp"
|
||||
|
||||
#include <cruft/util/std.hpp>
|
||||
#include <cruft/util/view.hpp>
|
||||
|
||||
@ -19,54 +21,6 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace cruft::crypto::hash {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <int>
|
||||
struct traits {};
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <>
|
||||
struct traits<256>
|
||||
{
|
||||
using word_t = u32;
|
||||
|
||||
static const std::array<word_t,8> iv;
|
||||
static const std::array<word_t,16> pi;
|
||||
static const std::array<int,4> rotations;
|
||||
static constexpr int rounds = 14;
|
||||
};
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <>
|
||||
struct traits<224>
|
||||
{
|
||||
using word_t = traits<256>::word_t;
|
||||
};
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <>
|
||||
struct traits<512>
|
||||
{
|
||||
using word_t = u64;
|
||||
|
||||
static const std::array<word_t,8> iv;
|
||||
static const std::array<word_t,16> pi;
|
||||
static const std::array<int,4> rotations;
|
||||
static constexpr int rounds = 16;
|
||||
};
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <>
|
||||
struct traits<384>
|
||||
{
|
||||
using word_t = traits<512>::word_t;
|
||||
};
|
||||
|
||||
|
||||
/// an implementation of the BLAKE hash function
|
||||
///
|
||||
/// note that this is _not_ BLAKE2, but the original SHA-3 candidate
|
||||
@ -76,7 +30,7 @@ namespace cruft::crypto::hash {
|
||||
template <int width>
|
||||
class blake {
|
||||
public:
|
||||
using word_t = typename traits<width>::word_t;
|
||||
using word_t = typename detail::blake::traits<width>::word_t;
|
||||
|
||||
// size of each round's data block in bytes
|
||||
static const size_t block_size = 16 * sizeof (word_t);
|
||||
|
88
hash/blake/traits.cpp
Normal file
88
hash/blake/traits.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
#include "traits.hpp"
|
||||
|
||||
using cruft::crypto::hash::detail::blake::traits;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
const std::array<traits<256>::word_t,8>
|
||||
traits<256>::iv
|
||||
{
|
||||
0x6A09E667, // frac(sqrt( 2))
|
||||
0xBB67AE85, // frac(sqrt( 3))
|
||||
0x3C6EF372, // frac(sqrt( 5))
|
||||
0xA54FF53A, // frac(sqrt( 7))
|
||||
0x510E527F, // frac(sqrt(11))
|
||||
0x9B05688C, // frac(sqrt(13))
|
||||
0x1F83D9AB, // frac(sqrt(17))
|
||||
0x5BE0CD19, // frac(sqrt(19))
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const std::array<traits<256>::word_t,16>
|
||||
traits<256>::pi
|
||||
{
|
||||
0x243F6A88,
|
||||
0x85A308D3,
|
||||
0x13198A2E,
|
||||
0x03707344,
|
||||
0xA4093822,
|
||||
0x299F31D0,
|
||||
0x082EFA98,
|
||||
0xEC4E6C89,
|
||||
0x452821E6,
|
||||
0x38D01377,
|
||||
0xBE5466CF,
|
||||
0x34E90C6C,
|
||||
0xC0AC29B7,
|
||||
0xC97C50DD,
|
||||
0x3F84D5B5,
|
||||
0xB5470917,
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const std::array<int,4>
|
||||
traits<256>::rotations {
|
||||
16, 12, 8, 7
|
||||
};
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
const std::array<traits<512>::word_t,8>
|
||||
traits<512>::iv {
|
||||
0x6A09E667F3BCC908,
|
||||
0xBB67AE8584CAA73B,
|
||||
0x3C6EF372FE94F82B,
|
||||
0xA54FF53A5F1D36F1,
|
||||
0x510E527FADE682D1,
|
||||
0x9B05688C2B3E6C1F,
|
||||
0x1F83D9ABFB41BD6B,
|
||||
0x5BE0CD19137E2179,
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const std::array<traits<512>::word_t,16>
|
||||
traits<512>::pi {
|
||||
0x243F6A8885A308D3,
|
||||
0x13198A2E03707344,
|
||||
0xA4093822299F31D0,
|
||||
0x082EFA98EC4E6C89,
|
||||
0x452821E638D01377,
|
||||
0xBE5466CF34E90C6C,
|
||||
0xC0AC29B7C97C50DD,
|
||||
0x3F84D5B5B5470917,
|
||||
0x9216D5D98979FB1B,
|
||||
0xD1310BA698DFB5AC,
|
||||
0x2FFD72DBD01ADFB7,
|
||||
0xB8E1AFED6A267E96,
|
||||
0xBA7C9045F12C7F99,
|
||||
0x24A19947B3916CF7,
|
||||
0x0801F2E2858EFC16,
|
||||
0x636920D871574E69,
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
const std::array<int,4> traits<512>::rotations { 32, 25, 16, 11 };
|
||||
|
63
hash/blake/traits.hpp
Normal file
63
hash/blake/traits.hpp
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Copyright 2018-2019 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cruft/util/std.hpp>
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace cruft::crypto::hash::detail {
|
||||
namespace blake {
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
template <int>
|
||||
struct traits {};
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
template <>
|
||||
struct traits<256>
|
||||
{
|
||||
using word_t = u32;
|
||||
|
||||
static const std::array<word_t,8> iv;
|
||||
static const std::array<word_t,16> pi;
|
||||
static const std::array<int,4> rotations;
|
||||
static constexpr int rounds = 14;
|
||||
};
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
template <>
|
||||
struct traits<224>
|
||||
{
|
||||
using word_t = traits<256>::word_t;
|
||||
};
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
template <>
|
||||
struct traits<512>
|
||||
{
|
||||
using word_t = u64;
|
||||
|
||||
static const std::array<word_t,8> iv;
|
||||
static const std::array<word_t,16> pi;
|
||||
static const std::array<int,4> rotations;
|
||||
static constexpr int rounds = 16;
|
||||
};
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
template <>
|
||||
struct traits<384>
|
||||
{
|
||||
using word_t = traits<512>::word_t;
|
||||
};
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user