Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
blake3s.test.cpp
Go to the documentation of this file.
7#include "blake3s.hpp"
8#include <gtest/gtest.h>
9
10using namespace bb;
11
12template <class Builder> class StdlibBlake3s : public ::testing::Test {};
13
14using BuilderTypes = ::testing::Types<bb::UltraCircuitBuilder, bb::MegaCircuitBuilder>;
16
17namespace {
18std::vector<std::string> test_vectors = { std::string{},
19 "a",
20 "ab",
21 "abc",
22 "abcd",
23 "abcdefg",
24 "abcdefgh",
25 "abcdefghijklmnopqrstuvwxyz01234",
26 "abcdefghijklmnopqrstuvwxyz012345",
27 "abcdefghijklmnopqrstuvwxyz0123456",
28 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0",
29 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01",
30 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz012",
31 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789" };
32} // namespace
33
34TYPED_TEST(StdlibBlake3s, test_single_block)
35{
36 using Builder = TypeParam;
38
40 std::string input = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01";
41 std::vector<uint8_t> input_v(input.begin(), input.end());
42
43 byte_array_ct input_arr(&builder, input_v);
45
46 std::vector<uint8_t> expected = blake3::blake3s(input_v);
47
48 EXPECT_EQ(output.get_value(), expected);
49
50 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
51
52 bool proof_result = CircuitChecker::check(builder);
53 EXPECT_EQ(proof_result, true);
54}
55
56TYPED_TEST(StdlibBlake3s, test_double_block)
57{
58 using Builder = TypeParam;
60
62 std::string input = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789";
63 std::vector<uint8_t> input_v(input.begin(), input.end());
64
65 byte_array_ct input_arr(&builder, input_v);
67
68 std::vector<uint8_t> expected = blake3::blake3s(input_v);
69
70 EXPECT_EQ(output.get_value(), expected);
71
72 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
73
74 bool proof_result = CircuitChecker::check(builder);
75 EXPECT_EQ(proof_result, true);
76}
77
78TYPED_TEST(StdlibBlake3s, test_too_large_input)
79{
80 using Builder = TypeParam;
82
84
85 std::vector<uint8_t> input_v(1025, 0);
86
87 byte_array_ct input_arr(&builder, input_v);
89 "Barretenberg does not support blake3s with input lengths greater than 1024 bytes.");
90}
91
92TYPED_TEST(StdlibBlake3s, test_witness_and_constant)
93{
94 using Builder = TypeParam;
96
98
99 // create a byte array that is a circuit witness
100 std::string witness_str = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz";
101 std::vector<uint8_t> witness_str_vec(witness_str.begin(), witness_str.end());
102
103 // create a byte array that is part circuit witness and part circuit constant
104 // start with the witness part, then append constant padding
105 byte_array_ct input_arr(&builder, witness_str_vec);
108
109 // for expected value calculation
110 std::vector<uint8_t> constant_vec = { '0', '1' };
111
112 // create expected input vector by concatenating witness and constant parts
113 std::vector<uint8_t> input_v;
114 input_v.insert(input_v.end(), witness_str_vec.begin(), witness_str_vec.end());
115 input_v.insert(input_v.end(), constant_vec.begin(), constant_vec.end());
116
117 // Verify the circuit input matches the expected input
118 EXPECT_EQ(input_arr.get_value(), input_v);
119
120 // hash the combined byte array
122
123 // compute expected hash
124 auto expected = blake3::blake3s(input_v);
125
126 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
127
128 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
129
130 bool proof_result = CircuitChecker::check(builder);
131 EXPECT_EQ(proof_result, true);
132}
133
134TYPED_TEST(StdlibBlake3s, test_constant_only)
135{
136 using Builder = TypeParam;
138
140 size_t len = 65;
141
142 // create a byte array that is a circuit constant
144
145 // create expected input vector
146 std::vector<uint8_t> input_v(len, '1');
147
148 // Verify the circuit input matches the expected input
149 EXPECT_EQ(input_arr.get_value(), input_v);
150
151 // hash the byte array
153
154 // compute expected hash
155 auto expected = blake3::blake3s(input_v);
156
157 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
158
159 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
160
161 bool proof_result = CircuitChecker::check(builder);
162 EXPECT_EQ(proof_result, true);
163}
164
165TYPED_TEST(StdlibBlake3s, test_multiple_sized_blocks)
166{
167 using Builder = TypeParam;
169
170 int i = 0;
171
172 for (auto v : test_vectors) {
174
175 std::vector<uint8_t> input_v(v.begin(), v.end());
176
177 byte_array_ct input_arr(&builder, input_v);
179
180 auto expected = blake3::blake3s(input_v);
181
182 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
183
184 info("test vector ", i++, ".: builder gates = ", builder.get_num_finalized_gates_inefficient());
185
186 bool proof_result = CircuitChecker::check(builder);
187 EXPECT_EQ(proof_result, true);
188 }
189}
190
191// Previously, certain inputs were pushing the addition overflows in `g` to beyond 3 bits (where `add_normalize` can
192// tolerate up to 3 bits of overflow), causing failures. This has been addressed by calling `add_normalize` in the
193// second half of every call to `g` to ensure that the overflow doesn't go beyond 3 bits. The edge case that caused
194// addition overflow issues in Blake is tested here. See https://hackmd.io/@aztec-network/SyTHLkAWZx for a detailed
195// description of the addition overflow issue.
196TYPED_TEST(StdlibBlake3s, test_edge_case_addition_overflow)
197{
198 using Builder = TypeParam;
200
201 std::array<uint8_t, 34> v = { 0xC3, 0x2B, 0xC3, 0x91, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0xFF, 0xFF,
202 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
203 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x03, 0x83, 0x83, 0x83, 0x40 };
204
206
207 std::vector<uint8_t> input_v(v.begin(), v.end());
208
209 byte_array_ct input_arr(&builder, input_v);
211
212 auto expected = blake3::blake3s(input_v);
213
214 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
215
216 info(".: builder gates = ", builder.get_num_finalized_gates_inefficient());
217
218 bool proof_result = CircuitChecker::check(builder);
219 EXPECT_EQ(proof_result, true);
220}
#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
test_vector test_vectors[]
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
TYPED_TEST_SUITE(CommitmentKeyTest, Curves)
TYPED_TEST(CommitmentKeyTest, CommitToZeroPoly)
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
::testing::Types< UltraCircuitBuilder, MegaCircuitBuilder > BuilderTypes