Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
blake3s.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7/*
8 BLAKE3 reference source code package - C implementations
9
10 Intellectual property:
11
12 The Rust code is copyright Jack O'Connor, 2019-2020.
13 The C code is copyright Samuel Neves and Jack O'Connor, 2019-2020.
14 The assembly code is copyright Samuel Neves, 2019-2020.
15
16 This work is released into the public domain with CC0 1.0. Alternatively, it is licensed under the Apache
17 License 2.0.
18
19 - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
20 - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
21
22 More information about the BLAKE3 hash function can be found at
23 https://github.com/BLAKE3-team/BLAKE3.
24
25
26 NOTE: We have modified the original code from the BLAKE3 reference C implementation.
27 The following code works ONLY for inputs of size less than 1024 bytes. This kind of constraint
28 on the input size greatly simplifies the code and helps us get rid of the recursive merkle-tree
29 like operations on chunks (data of size 1024 bytes). This is because we would always be using BLAKE3
30 hashing for inputs of size 32 bytes (or lesser) in barretenberg. The full C++ version of BLAKE3
31 from the original authors is in the module `../crypto/blake3s_full`.
32
33 Also, the length of the output in this specific implementation is fixed at 32 bytes which is the only
34 version relevant to Barretenberg.
35*/
36#pragma once
37#include <array>
38#include <cstddef>
39#include <cstdint>
40#include <string>
41#include <vector>
42
43namespace blake3 {
44
45// internal flags
47 CHUNK_START = 1 << 0,
48 CHUNK_END = 1 << 1,
49 PARENT = 1 << 2,
50 ROOT = 1 << 3,
51 KEYED_HASH = 1 << 4,
54};
55
56// constants
64
65using key_array = std::array<uint32_t, BLAKE3_KEY_LEN>;
66using block_array = std::array<uint8_t, BLAKE3_BLOCK_LEN>;
69
70static constexpr key_array IV = { 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
71 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL };
72
73static constexpr std::array<uint8_t, 16> MSG_SCHEDULE_0 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
74static constexpr std::array<uint8_t, 16> MSG_SCHEDULE_1 = { 2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8 };
75static constexpr std::array<uint8_t, 16> MSG_SCHEDULE_2 = { 3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1 };
76static constexpr std::array<uint8_t, 16> MSG_SCHEDULE_3 = { 10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6 };
77static constexpr std::array<uint8_t, 16> MSG_SCHEDULE_4 = { 12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4 };
78static constexpr std::array<uint8_t, 16> MSG_SCHEDULE_5 = { 9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7 };
79static constexpr std::array<uint8_t, 16> MSG_SCHEDULE_6 = { 11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13 };
80static constexpr std::array<std::array<uint8_t, 16>, 7> MSG_SCHEDULE = {
81 MSG_SCHEDULE_0, MSG_SCHEDULE_1, MSG_SCHEDULE_2, MSG_SCHEDULE_3, MSG_SCHEDULE_4, MSG_SCHEDULE_5, MSG_SCHEDULE_6,
82};
83
92
93inline const char* blake3_version()
94{
95 static const std::string version = "0.3.7";
96 return version.c_str();
97}
98
99constexpr void blake3_hasher_init(blake3_hasher* self);
100constexpr void blake3_hasher_update(blake3_hasher* self, const uint8_t* input, size_t input_len);
101constexpr void blake3_hasher_finalize(const blake3_hasher* self, uint8_t* out);
102
103constexpr void g(state_array& state, size_t a, size_t b, size_t c, size_t d, uint32_t x, uint32_t y);
104constexpr void round_fn(state_array& state, const uint32_t* msg, size_t round);
105
106constexpr void compress_pre(
107 state_array& state, const key_array& cv, const uint8_t* block, uint8_t block_len, uint8_t flags);
108
109constexpr void blake3_compress_in_place(key_array& cv, const uint8_t* block, uint8_t block_len, uint8_t flags);
110
111constexpr void blake3_compress_xof(
112 const key_array& cv, const uint8_t* block, uint8_t block_len, uint8_t flags, uint8_t* out);
113
114constexpr std::array<uint8_t, BLAKE3_OUT_LEN> blake3s_constexpr(const uint8_t* input, size_t input_size);
115inline std::vector<uint8_t> blake3s(std::vector<uint8_t> const& input);
116
117} // namespace blake3
118
119#include "blake3-impl.hpp"
FF a
FF b
constexpr void blake3_hasher_update(blake3_hasher *self, const uint8_t *input, size_t input_len)
const char * blake3_version()
Definition blake3s.hpp:93
blake3s_constant
Definition blake3s.hpp:57
@ BLAKE3_KEY_LEN
Definition blake3s.hpp:58
@ BLAKE3_BLOCK_LEN
Definition blake3s.hpp:60
@ BLAKE3_MAX_DEPTH
Definition blake3s.hpp:62
@ BLAKE3_OUT_LEN
Definition blake3s.hpp:59
@ BLAKE3_CHUNK_LEN
Definition blake3s.hpp:61
constexpr void g(state_array &state, size_t a, size_t b, size_t c, size_t d, uint32_t x, uint32_t y)
constexpr void blake3_compress_xof(const key_array &cv, const uint8_t *block, uint8_t block_len, uint8_t flags, uint8_t *out)
constexpr void blake3_compress_in_place(key_array &cv, const uint8_t *block, uint8_t block_len, uint8_t flags)
constexpr void round_fn(state_array &state, const uint32_t *msg, size_t round)
std::array< uint32_t, BLAKE3_KEY_LEN > key_array
Definition blake3s.hpp:65
std::vector< uint8_t > blake3s(std::vector< uint8_t > const &input)
constexpr void compress_pre(state_array &state, const key_array &cv, const uint8_t *block, uint8_t block_len, uint8_t flags)
std::array< uint32_t, 16 > state_array
Definition blake3s.hpp:67
blake3_flags
Definition blake3s.hpp:46
@ DERIVE_KEY_CONTEXT
Definition blake3s.hpp:52
@ CHUNK_END
Definition blake3s.hpp:48
@ KEYED_HASH
Definition blake3s.hpp:51
@ PARENT
Definition blake3s.hpp:49
@ DERIVE_KEY_MATERIAL
Definition blake3s.hpp:53
@ CHUNK_START
Definition blake3s.hpp:47
std::array< uint8_t, BLAKE3_OUT_LEN > out_array
Definition blake3s.hpp:68
constexpr void blake3_hasher_finalize(const blake3_hasher *self, uint8_t *out)
std::array< uint8_t, BLAKE3_BLOCK_LEN > block_array
Definition blake3s.hpp:66
constexpr void blake3_hasher_init(blake3_hasher *self)
constexpr std::array< uint8_t, BLAKE3_OUT_LEN > blake3s_constexpr(const uint8_t *input, size_t input_size)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
uint8_t blocks_compressed
Definition blake3s.hpp:89