barretenberg
Loading...
Searching...
No Matches
blake3s.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#include <stddef.h>
22#include <stdint.h>
23#include <vector>
24
25namespace blake3_full {
26
27#define BLAKE3_VERSION_STRING "0.3.7"
28
29// internal flags
30enum blake3_flags {
31 CHUNK_START = 1 << 0,
32 CHUNK_END = 1 << 1,
33 PARENT = 1 << 2,
34 ROOT = 1 << 3,
35 KEYED_HASH = 1 << 4,
36 DERIVE_KEY_CONTEXT = 1 << 5,
37 DERIVE_KEY_MATERIAL = 1 << 6,
38};
39
40// constants
41enum blake3s_constant {
42 BLAKE3_KEY_LEN = 32,
43 BLAKE3_OUT_LEN = 32,
44 BLAKE3_BLOCK_LEN = 64,
45 BLAKE3_CHUNK_LEN = 1024,
46 BLAKE3_MAX_DEPTH = 54
47};
48
49// modes
50enum mode { HASH_MODE = 0, KEYED_HASH_MODE = 1, DERIVE_KEY_MODE = 2 };
51
52static const uint32_t IV[8] = { 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
53 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL };
54
55static const uint8_t MSG_SCHEDULE[7][16] = {
56 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, { 2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8 },
57 { 3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1 }, { 10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6 },
58 { 12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4 }, { 9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7 },
59 { 11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13 },
60};
61
62// This struct is a private implementation detail. It has to be here because
63// it's part of blake3_hasher below.
64typedef struct blake3_chunk_state__ {
65 uint32_t cv[8];
66 uint64_t chunk_counter;
67 uint8_t buf[BLAKE3_BLOCK_LEN];
68 uint8_t buf_len;
69 uint8_t blocks_compressed;
70 uint8_t flags;
72
73typedef struct blake3_hasher__ {
74 uint32_t key[8];
76 uint8_t cv_stack_len;
77 // The stack size is MAX_DEPTH + 1 because we do lazy merging. For example,
78 // with 7 chunks, we have 3 entries in the stack. Adding an 8th chunk
79 // requires a 4th entry, rather than merging everything down to 1, because we
80 // don't know whether more input is coming. This is different from how the
81 // reference implementation does things.
82 uint8_t cv_stack[(BLAKE3_MAX_DEPTH + 1) * BLAKE3_OUT_LEN];
84
85const char* blake3_version(void);
86void blake3_hasher_init(blake3_hasher* self);
87void blake3_hasher_init_keyed(blake3_hasher* self, const uint8_t key[BLAKE3_KEY_LEN]);
88
89void blake3_hasher_init_derive_key(blake3_hasher* self, const char* context);
90void blake3_hasher_init_derive_key_raw(blake3_hasher* self, const void* context, size_t context_len);
91
92void blake3_hasher_update(blake3_hasher* self, const void* input, size_t input_len);
93void blake3_hasher_finalize(const blake3_hasher* self, uint8_t* out, size_t out_len);
94void blake3_hasher_finalize_seek(const blake3_hasher* self, uint64_t seek, uint8_t* out, size_t out_len);
95
96void g(uint32_t* state, size_t a, size_t b, size_t c, size_t d, uint32_t x, uint32_t y);
97void round_fn(uint32_t state[16], const uint32_t* msg, size_t round);
98
99void compress_pre(uint32_t state[16],
100 const uint32_t cv[8],
101 const uint8_t block[BLAKE3_BLOCK_LEN],
102 uint8_t block_len,
103 uint64_t counter,
104 uint8_t flags);
105
106void blake3_compress_in_place(
107 uint32_t cv[8], const uint8_t block[BLAKE3_BLOCK_LEN], uint8_t block_len, uint64_t counter, uint8_t flags);
108
109void blake3_compress_xof(const uint32_t cv[8],
110 const uint8_t block[BLAKE3_BLOCK_LEN],
111 uint8_t block_len,
112 uint64_t counter,
113 uint8_t flags,
114 uint8_t out[64]);
115
116void blak3s_hash_one(const uint8_t* input,
117 size_t blocks,
118 const uint32_t key[8],
119 uint64_t counter,
120 uint8_t flags,
121 uint8_t flags_start,
122 uint8_t flags_end,
123 uint8_t out[BLAKE3_OUT_LEN]);
124
125void blake3_hash_many(const uint8_t* const* inputs,
126 size_t num_inputs,
127 size_t blocks,
128 const uint32_t key[8],
129 uint64_t counter,
130 bool increment_counter,
131 uint8_t flags,
132 uint8_t flags_start,
133 uint8_t flags_end,
134 uint8_t* out);
135
136std::vector<uint8_t> blake3s(std::vector<uint8_t> const& input,
137 const mode mode_id = HASH_MODE,
138 const uint8_t key[BLAKE3_KEY_LEN] = nullptr,
139 const char* context = nullptr);
140
141} // namespace blake3_full
Definition: blake3s.hpp:64
Definition: blake3s.hpp:73