Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
blake3s.test.cpp
Go to the documentation of this file.
5#include "blake3s.hpp"
6#include <gtest/gtest.h>
7
8using namespace bb;
9
12
13std::vector<std::string> test_vectors = { std::string{},
14 "a",
15 "ab",
16 "abc",
17 "abcd",
18 "abcdefg",
19 "abcdefgh",
20 "abcdefghijklmnopqrstuvwxyz01234",
21 "abcdefghijklmnopqrstuvwxyz012345",
22 "abcdefghijklmnopqrstuvwxyz0123456",
23 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0",
24 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01",
25 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz012",
26 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789" };
27
28TEST(stdlib_blake3s, test_single_block)
29{
30 auto builder = UltraBuilder();
31 std::string input = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01";
32 std::vector<uint8_t> input_v(input.begin(), input.end());
33
34 byte_array_ct input_arr(&builder, input_v);
36
37 std::vector<uint8_t> expected = blake3::blake3s(input_v);
38
39 EXPECT_EQ(output.get_value(), expected);
40
41 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
42
43 bool proof_result = CircuitChecker::check(builder);
44 EXPECT_EQ(proof_result, true);
45}
46
47TEST(stdlib_blake3s, test_double_block)
48{
49 auto builder = UltraBuilder();
50 std::string input = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789";
51 std::vector<uint8_t> input_v(input.begin(), input.end());
52
53 byte_array_ct input_arr(&builder, input_v);
55
56 std::vector<uint8_t> expected = blake3::blake3s(input_v);
57
58 EXPECT_EQ(output.get_value(), expected);
59
60 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
61
62 bool proof_result = CircuitChecker::check(builder);
63 EXPECT_EQ(proof_result, true);
64}
65
66TEST(stdlib_blake3s, test_too_large_input)
67{
68 auto builder = UltraBuilder();
69
70 std::vector<uint8_t> input_v(1025, 0);
71
72 byte_array_ct input_arr(&builder, input_v);
74 "Barretenberg does not support blake3s with input lengths greater than 1024 bytes.");
75}
76
77TEST(stdlib_blake3s, test_witness_and_constant)
78{
80
81 // create a byte array that is a circuit witness
82 std::string witness_str = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz";
83 std::vector<uint8_t> witness_str_vec(witness_str.begin(), witness_str.end());
84
85 // create a byte array that is part circuit witness and part circuit constant
86 // start with the witness part, then append constant padding
87 byte_array_ct input_arr(&builder, witness_str_vec);
90
91 // for expected value calculation
92 std::vector<uint8_t> constant_vec = { '0', '1' };
93
94 // create expected input vector by concatenating witness and constant parts
95 std::vector<uint8_t> input_v;
96 input_v.insert(input_v.end(), witness_str_vec.begin(), witness_str_vec.end());
97 input_v.insert(input_v.end(), constant_vec.begin(), constant_vec.end());
98
99 // Verify the circuit input matches the expected input
100 EXPECT_EQ(input_arr.get_value(), input_v);
101
102 // hash the combined byte array
104
105 // compute expected hash
106 auto expected = blake3::blake3s(input_v);
107
108 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
109
110 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
111
112 bool proof_result = CircuitChecker::check(builder);
113 EXPECT_EQ(proof_result, true);
114}
115
116TEST(stdlib_blake3s, test_constant_only)
117{
119 size_t len = 65;
120
121 // create a byte array that is a circuit constant
123
124 // create expected input vector
125 std::vector<uint8_t> input_v(len, '1');
126
127 // Verify the circuit input matches the expected input
128 EXPECT_EQ(input_arr.get_value(), input_v);
129
130 // hash the byte array
132
133 // compute expected hash
134 auto expected = blake3::blake3s(input_v);
135
136 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
137
138 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
139
140 bool proof_result = CircuitChecker::check(builder);
141 EXPECT_EQ(proof_result, true);
142}
143
144TEST(stdlib_blake3s, test_multiple_sized_blocks)
145{
146 int i = 0;
147
148 for (auto v : test_vectors) {
150
151 std::vector<uint8_t> input_v(v.begin(), v.end());
152
153 byte_array_ct input_arr(&builder, input_v);
155
156 auto expected = blake3::blake3s(input_v);
157
158 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
159
160 info("test vector ", i++, ".: builder gates = ", builder.get_num_finalized_gates_inefficient());
161
162 bool proof_result = CircuitChecker::check(builder);
163 EXPECT_EQ(proof_result, true);
164 }
165}
166
167// Previously, certain inputs were pushing the addition overflows in `g` to beyond 3 bits (where `add_normalize` can
168// tolerate up to 3 bits of overflow), causing failures. This has been addressed by calling `add_normalize` in the
169// second half of every call to `g` to ensure that the overflow doesn't go beyond 3 bits. The edge case that caused
170// addition overflow issues in Blake is tested here. See https://hackmd.io/@aztec-network/SyTHLkAWZx for a detailed
171// description of the addition overflow issue.
172TEST(stdlib_blake3s, test_edge_case_addition_overflow)
173{
174 std::array<uint8_t, 34> v = { 0xC3, 0x2B, 0xC3, 0x91, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0xFF, 0xFF,
175 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
176 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x03, 0x83, 0x83, 0x83, 0x40 };
177
179
180 std::vector<uint8_t> input_v(v.begin(), v.end());
181
182 byte_array_ct input_arr(&builder, input_v);
184
185 auto expected = blake3::blake3s(input_v);
186
187 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
188
189 info(".: builder gates = ", builder.get_num_finalized_gates_inefficient());
190
191 bool proof_result = CircuitChecker::check(builder);
192 EXPECT_EQ(proof_result, true);
193}
#define EXPECT_THROW_OR_ABORT(statement, matcher)
Definition assert.hpp:174
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)
void info(Args... args)
Definition log.hpp:75
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