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#pragma once
8
9#include "../../primitives/byte_array/byte_array.hpp"
10#include "../../primitives/field/field.hpp"
12
13namespace bb::stdlib {
14template <typename Builder> class Blake3s {
17
18 /*
19 * Constants and more.
20 */
21 // internal flags
23 CHUNK_START = 1 << 0,
24 CHUNK_END = 1 << 1,
25 PARENT = 1 << 2,
26 ROOT = 1 << 3,
27 KEYED_HASH = 1 << 4,
30 };
32
33 static constexpr size_t BLAKE3_CV_WORDS = 8;
34
35 // constants
37
38 static constexpr std::array<uint32_t, 8> IV{ 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
39 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL };
40
50
57 static void compress_pre(field_ct state[BLAKE3_STATE_SIZE],
58 const field_ct cv[BLAKE3_CV_WORDS],
59 const byte_array_ct& block,
60 uint8_t block_len,
61 uint8_t flags);
62
64 const byte_array_ct& block,
65 uint8_t block_len,
66 uint8_t flags);
67
68 static void compress_xof(const field_ct cv[BLAKE3_CV_WORDS],
69 const byte_array_ct& block,
70 uint8_t block_len,
71 uint8_t flags,
72 byte_array_ct& out);
73
74 /*
75 * Blake3s helper functions.
76 *
77 */
78 static uint8_t maybe_start_flag(const blake3_hasher* self)
79 {
80 if (self->blocks_compressed == 0) {
81 return CHUNK_START;
82 } else {
83 return 0;
84 }
85 }
86 static output_t make_output(const field_ct input_cv[BLAKE3_CV_WORDS],
87 const byte_array_ct& block,
88 uint8_t block_len,
89 uint8_t flags);
90
91 static void hasher_init(blake3_hasher* self);
92
93 static void hasher_update(blake3_hasher* self, const byte_array_ct& input, size_t input_len);
94
95 static void hasher_finalize(const blake3_hasher* self, byte_array_ct& out);
96
97 public:
98 static byte_array_ct hash(const byte_array_ct& input);
99};
100
101} // namespace bb::stdlib
static constexpr size_t BLAKE3_CV_WORDS
Definition blake3s.hpp:33
static void hasher_init(blake3_hasher *self)
Definition blake3s.cpp:127
static output_t make_output(const field_ct input_cv[BLAKE3_CV_WORDS], const byte_array_ct &block, uint8_t block_len, uint8_t flags)
Definition blake3s.cpp:107
static byte_array_ct hash(const byte_array_ct &input)
Definition blake3s.cpp:182
static void compress_in_place(field_ct cv[BLAKE3_CV_WORDS], const byte_array_ct &block, uint8_t block_len, uint8_t flags)
Definition blake3s.cpp:55
static constexpr size_t BLAKE3_STATE_SIZE
Definition blake3s.hpp:31
static constexpr std::array< uint32_t, 8 > IV
Definition blake3s.hpp:38
static void hasher_finalize(const blake3_hasher *self, byte_array_ct &out)
Definition blake3s.cpp:170
static void compress_xof(const field_ct cv[BLAKE3_CV_WORDS], const byte_array_ct &block, uint8_t block_len, uint8_t flags, byte_array_ct &out)
Definition blake3s.cpp:77
static uint8_t maybe_start_flag(const blake3_hasher *self)
Definition blake3s.hpp:78
static void compress_pre(field_ct state[BLAKE3_STATE_SIZE], const field_ct cv[BLAKE3_CV_WORDS], const byte_array_ct &block, uint8_t block_len, uint8_t flags)
Definition blake3s.cpp:21
static void hasher_update(blake3_hasher *self, const byte_array_ct &input, size_t input_len)
Definition blake3s.cpp:141
Represents a dynamic array of bytes in-circuit.
field_ct cv[BLAKE3_CV_WORDS]
Definition blake3s.hpp:43
field_ct key[BLAKE3_CV_WORDS]
Definition blake3s.hpp:42
field_ct input_cv[BLAKE3_CV_WORDS]
Definition blake3s.hpp:52