hash/blake: prefer std types

This commit is contained in:
Danny Robson 2019-02-09 15:45:18 +11:00
parent 103ede647b
commit cfc728a3a7
2 changed files with 20 additions and 30 deletions

View File

@ -147,17 +147,10 @@ G (int i,
const auto j = permute[r][2 * i ]; const auto j = permute[r][2 * i ];
const auto k = permute[r][2 * i + 1]; const auto k = permute[r][2 * i + 1];
a = a + b + (m[j] ^ traits<width>::pi[k]); a = a + b + (m[j] ^ traits<width>::pi[k]); d = cruft::rotater (d ^ a, traits<width>::rotations[0]);
d = cruft::rotater (d ^ a, traits<width>::rotations[0]); c = c + d; b = cruft::rotater (b ^ c, traits<width>::rotations[1]);
a = a + b + (m[k] ^ traits<width>::pi[j]); d = cruft::rotater (d ^ a, traits<width>::rotations[2]);
c = c + d; c = c + d; b = cruft::rotater (b ^ c, traits<width>::rotations[3]);
b = cruft::rotater (b ^ c, traits<width>::rotations[1]);
a = a + b + (m[k] ^ traits<width>::pi[j]);
d = cruft::rotater (d ^ a, traits<width>::rotations[2]);
c = c + d;
b = cruft::rotater (b ^ c, traits<width>::rotations[3]);
} }
@ -169,7 +162,7 @@ compress (
std::array<typename traits<width>::word_t,8> h, std::array<typename traits<width>::word_t,8> h,
const typename traits<width>::word_t m[16], const typename traits<width>::word_t m[16],
const std::array<typename traits<width>::word_t,4> s, const std::array<typename traits<width>::word_t,4> s,
uint64_t t u64 t
) { ) {
typename traits<width>::word_t t0 = t & 0xffffffff; typename traits<width>::word_t t0 = t & 0xffffffff;
typename traits<width>::word_t t1 = (t >> 32u) & 0xffffffff; typename traits<width>::word_t t1 = (t >> 32u) & 0xffffffff;
@ -212,8 +205,8 @@ compress (
template <int width> template <int width>
typename blake<width>::digest_t typename blake<width>::digest_t
blake<width>::operator() ( blake<width>::operator() (
cruft::view<const uint8_t*> data, cruft::view<const u08*> data,
cruft::view<const uint8_t*> salt cruft::view<const u08*> salt
) const { ) const {
std::array<typename traits<width>::word_t, 4> fwd {}; std::array<typename traits<width>::word_t, 4> fwd {};
@ -228,7 +221,7 @@ blake<width>::operator() (
template <int width> template <int width>
typename blake<width>::digest_t typename blake<width>::digest_t
blake<width>::operator() ( blake<width>::operator() (
cruft::view<const uint8_t *> data, cruft::view<const u08 *> data,
const std::array<typename traits<width>::word_t, 4> salt const std::array<typename traits<width>::word_t, 4> salt
) const noexcept { ) const noexcept {
auto h = traits<width>::iv; auto h = traits<width>::iv;
@ -241,10 +234,10 @@ blake<width>::operator() (
// more than simple calls to hton would allow. // more than simple calls to hton would allow.
union { union {
word_t dw[16]; word_t dw[16];
uint8_t d08[16*sizeof(word_t)]; u08 d08[16*sizeof(word_t)];
}; };
uint64_t t = 0; u64 t = 0;
auto cursor = data.cbegin (); auto cursor = data.cbegin ();
// perform the simple case where we're consuming whole blocks // perform the simple case where we're consuming whole blocks
@ -327,7 +320,6 @@ blake<width>::operator() (
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template class cruft::crypto::hash::blake<256>; template class cruft::crypto::hash::blake<256>;
template class cruft::crypto::hash::blake<512>; template class cruft::crypto::hash::blake<512>;

View File

@ -3,12 +3,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
* *
* Copyright 2018 Danny Robson <danny@nerdcruft.net> * Copyright 2018-2019 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_CRYPTO_HASH_BLAKE_HPP #pragma once
#define CRUFT_CRYPTO_HASH_BLAKE_HPP
#include <cruft/util/std.hpp>
#include <cruft/util/view.hpp> #include <cruft/util/view.hpp>
#include <array> #include <array>
@ -29,7 +29,7 @@ namespace cruft::crypto::hash {
template <> template <>
struct traits<256> struct traits<256>
{ {
using word_t = uint32_t; using word_t = u32;
static const std::array<word_t,8> iv; static const std::array<word_t,8> iv;
static const std::array<word_t,16> pi; static const std::array<word_t,16> pi;
@ -50,7 +50,7 @@ namespace cruft::crypto::hash {
template <> template <>
struct traits<512> struct traits<512>
{ {
using word_t = uint64_t; using word_t = u64;
static const std::array<word_t,8> iv; static const std::array<word_t,8> iv;
static const std::array<word_t,16> pi; static const std::array<word_t,16> pi;
@ -83,22 +83,20 @@ namespace cruft::crypto::hash {
// size of the digest in bytes // size of the digest in bytes
static const size_t digest_size = width / 8; static const size_t digest_size = width / 8;
using digest_t = std::array<uint8_t,digest_size>; using digest_t = std::array<u08,digest_size>;
digest_t operator() ( digest_t operator() (
cruft::view<const uint8_t*> data, cruft::view<const u08*> data,
cruft::view<const uint8_t*> salt cruft::view<const u08*> salt
) const; ) const;
digest_t operator() (cruft::view<const uint8_t*> data, digest_t operator() (cruft::view<const u08*> data,
const std::array<word_t,4> salt) const noexcept; const std::array<word_t,4> salt) const noexcept;
digest_t operator() (cruft::view<const uint8_t*> data) const noexcept digest_t operator() (cruft::view<const u08*> data) const noexcept
{ {
return (*this) (data, {0,0,0,0}); return (*this) (data, {0,0,0,0});
} }
}; };
} }
#endif