Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
blake3s.test.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Nishat], commit: 8fb8b041d4c9179f62da56a9c7bbf22c40db46cc}
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
11#include "blake3s.hpp"
12#include <gtest/gtest.h>
13
14using namespace bb;
15
18
19std::vector<std::string> test_vectors = { std::string{},
20 "a",
21 "ab",
22 "abc",
23 "abcd",
24 "abcdefg",
25 "abcdefgh",
26 "abcdefghijklmnopqrstuvwxyz01234",
27 "abcdefghijklmnopqrstuvwxyz012345",
28 "abcdefghijklmnopqrstuvwxyz0123456",
29 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0",
30 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01",
31 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz012",
32 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789" };
33
34TEST(stdlib_blake3s, test_single_block)
35{
36 auto builder = UltraBuilder();
37 std::string input = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01";
38 std::vector<uint8_t> input_v(input.begin(), input.end());
39
40 byte_array_ct input_arr(&builder, input_v);
42
43 std::vector<uint8_t> expected = blake3::blake3s(input_v);
44
45 EXPECT_EQ(output.get_value(), expected);
46
47 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
48
49 bool proof_result = CircuitChecker::check(builder);
50 EXPECT_EQ(proof_result, true);
51}
52
53TEST(stdlib_blake3s, test_double_block)
54{
55 auto builder = UltraBuilder();
56 std::string input = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789";
57 std::vector<uint8_t> input_v(input.begin(), input.end());
58
59 byte_array_ct input_arr(&builder, input_v);
61
62 std::vector<uint8_t> expected = blake3::blake3s(input_v);
63
64 EXPECT_EQ(output.get_value(), expected);
65
66 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
67
68 bool proof_result = CircuitChecker::check(builder);
69 EXPECT_EQ(proof_result, true);
70}
71
72TEST(stdlib_blake3s, test_too_large_input)
73{
74 auto builder = UltraBuilder();
75
76 std::vector<uint8_t> input_v(1025, 0);
77
78 byte_array_ct input_arr(&builder, input_v);
80 "Barretenberg does not support blake3s with input lengths greater than 1024 bytes.");
81}
82
83TEST(stdlib_blake3s, test_witness_and_constant)
84{
86
87 // create a byte array that is a circuit witness
88 std::string witness_str = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz";
89 std::vector<uint8_t> witness_str_vec(witness_str.begin(), witness_str.end());
90
91 // create a byte array that is part circuit witness and part circuit constant
92 // start with the witness part, then append constant padding
93 byte_array_ct input_arr(&builder, witness_str_vec);
96
97 // for expected value calculation
98 std::vector<uint8_t> constant_vec = { '0', '1' };
99
100 // create expected input vector by concatenating witness and constant parts
101 std::vector<uint8_t> input_v;
102 input_v.insert(input_v.end(), witness_str_vec.begin(), witness_str_vec.end());
103 input_v.insert(input_v.end(), constant_vec.begin(), constant_vec.end());
104
105 // Verify the circuit input matches the expected input
106 EXPECT_EQ(input_arr.get_value(), input_v);
107
108 // hash the combined byte array
110
111 // compute expected hash
112 auto expected = blake3::blake3s(input_v);
113
114 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
115
116 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
117
118 bool proof_result = CircuitChecker::check(builder);
119 EXPECT_EQ(proof_result, true);
120}
121
122TEST(stdlib_blake3s, test_constant_only)
123{
125 size_t len = 65;
126
127 // create a byte array that is a circuit constant
129
130 // create expected input vector
131 std::vector<uint8_t> input_v(len, '1');
132
133 // Verify the circuit input matches the expected input
134 EXPECT_EQ(input_arr.get_value(), input_v);
135
136 // hash the byte array
138
139 // compute expected hash
140 auto expected = blake3::blake3s(input_v);
141
142 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
143
144 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
145
146 bool proof_result = CircuitChecker::check(builder);
147 EXPECT_EQ(proof_result, true);
148}
149
150TEST(stdlib_blake3s, test_multiple_sized_blocks)
151{
152 int i = 0;
153
154 for (auto v : test_vectors) {
156
157 std::vector<uint8_t> input_v(v.begin(), v.end());
158
159 byte_array_ct input_arr(&builder, input_v);
161
162 auto expected = blake3::blake3s(input_v);
163
164 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
165
166 info("test vector ", i++, ".: builder gates = ", builder.get_num_finalized_gates_inefficient());
167
168 bool proof_result = CircuitChecker::check(builder);
169 EXPECT_EQ(proof_result, true);
170 }
171}
172
173// Previously, certain inputs were pushing the addition overflows in `g` to beyond 3 bits (where `add_normalize` can
174// tolerate up to 3 bits of overflow), causing failures. This has been addressed by calling `add_normalize` in the
175// second half of every call to `g` to ensure that the overflow doesn't go beyond 3 bits. The edge case that caused
176// addition overflow issues in Blake is tested here. See https://hackmd.io/@aztec-network/SyTHLkAWZx for a detailed
177// description of the addition overflow issue.
178TEST(stdlib_blake3s, test_edge_case_addition_overflow)
179{
180 std::array<uint8_t, 34> v = { 0xC3, 0x2B, 0xC3, 0x91, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0xFF, 0xFF,
181 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
182 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x03, 0x83, 0x83, 0x83, 0x40 };
183
185
186 std::vector<uint8_t> input_v(v.begin(), v.end());
187
188 byte_array_ct input_arr(&builder, input_v);
190
191 auto expected = blake3::blake3s(input_v);
192
193 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
194
195 info(".: builder gates = ", builder.get_num_finalized_gates_inefficient());
196
197 bool proof_result = CircuitChecker::check(builder);
198 EXPECT_EQ(proof_result, true);
199}
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
Definition assert.hpp:193
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
static byte_array_ct hash(const byte_array_ct &input)
Definition blake3s.cpp:182
Represents a dynamic array of bytes in-circuit.
byte_array & write(byte_array const &other)
Appends the contents of another byte_array (other) to the end of this one.
std::vector< uint8_t > get_value() const
A helper converting a byte_array into the vector of its uint8_t values.
static byte_array constant_padding(Builder *parent_context, size_t num_bytes, uint8_t value=0)
#define info(...)
Definition log.hpp:93
AluTraceBuilder builder
Definition alu.test.cpp:124
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
std::vector< uint8_t > blake3s(std::vector< uint8_t > const &input)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
uint8_t len
UltraCircuitBuilder UltraBuilder
std::vector< std::string > test_vectors