barretenberg
Loading...
Searching...
No Matches
blake3-impl.hpp
1#pragma once
2/*
3 BLAKE3 reference source code package - C implementations
4
5 Intellectual property:
6
7 The Rust code is copyright Jack O'Connor, 2019-2020.
8 The C code is copyright Samuel Neves and Jack O'Connor, 2019-2020.
9 The assembly code is copyright Samuel Neves, 2019-2020.
10
11 This work is released into the public domain with CC0 1.0. Alternatively, it is licensed under the Apache
12 License 2.0.
13
14 - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
15 - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
16
17 More information about the BLAKE3 hash function can be found at
18 https://github.com/BLAKE3-team/BLAKE3.
19*/
20
21#ifndef BLAKE3_IMPL_H
22#define BLAKE3_IMPL_H
23
24#include <cstddef>
25#include <cstdint>
26#include <cstring>
27
28#include "blake3s.hpp"
29
30namespace blake3 {
31
32// Right rotates 32 bit inputs
33constexpr uint32_t rotr32(uint32_t w, uint32_t c)
34{
35 return (w >> c) | (w << (32 - c));
36}
37
38constexpr uint32_t load32(const uint8_t* src)
39{
40 return (static_cast<uint32_t>(src[0]) << 0) | (static_cast<uint32_t>(src[1]) << 8) |
41 (static_cast<uint32_t>(src[2]) << 16) | (static_cast<uint32_t>(src[3]) << 24);
42}
43
44constexpr void load_key_words(const std::array<uint8_t, BLAKE3_KEY_LEN>& key, key_array& key_words)
45{
46 key_words[0] = load32(&key[0]);
47 key_words[1] = load32(&key[4]);
48 key_words[2] = load32(&key[8]);
49 key_words[3] = load32(&key[12]);
50 key_words[4] = load32(&key[16]);
51 key_words[5] = load32(&key[20]);
52 key_words[6] = load32(&key[24]);
53 key_words[7] = load32(&key[28]);
54}
55
56constexpr void store32(uint8_t* dst, uint32_t w)
57{
58 dst[0] = static_cast<uint8_t>(w >> 0);
59 dst[1] = static_cast<uint8_t>(w >> 8);
60 dst[2] = static_cast<uint8_t>(w >> 16);
61 dst[3] = static_cast<uint8_t>(w >> 24);
62}
63
64constexpr void store_cv_words(out_array& bytes_out, key_array& cv_words)
65{
66 store32(&bytes_out[0], cv_words[0]);
67 store32(&bytes_out[4], cv_words[1]);
68 store32(&bytes_out[8], cv_words[2]);
69 store32(&bytes_out[12], cv_words[3]);
70 store32(&bytes_out[16], cv_words[4]);
71 store32(&bytes_out[20], cv_words[5]);
72 store32(&bytes_out[24], cv_words[6]);
73 store32(&bytes_out[28], cv_words[7]);
74}
75
76} // namespace blake3
77
78#include "blake3s.tcc"
79
80#endif