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