Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
blake2s.test.cpp
Go to the documentation of this file.
8#include "blake2s.hpp"
9#include <gtest/gtest.h>
10
11using namespace bb;
12using namespace bb::stdlib;
13
14template <class Builder> class StdlibBlake2s : public ::testing::Test {};
15
16using BuilderTypes = ::testing::Types<bb::UltraCircuitBuilder, bb::MegaCircuitBuilder>;
18
19namespace {
20std::vector<std::string> test_vectors = { "",
21 "a",
22 "ab",
23 "abc",
24 "abcd",
25 "abcdefg",
26 "abcdefgh",
27 "abcdefghijklmnopqrstuvwxyz01234",
28 "abcdefghijklmnopqrstuvwxyz012345",
29 "abcdefghijklmnopqrstuvwxyz0123456",
30 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0",
31 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01",
32 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz012",
33 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789" };
34} // namespace
35
36TYPED_TEST(StdlibBlake2s, test_single_block)
37{
38 using Builder = TypeParam;
40
42 std::string input = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01";
43 std::vector<uint8_t> input_v(input.begin(), input.end());
44
45 byte_array_ct input_arr(&builder, input_v);
47
48 auto expected = crypto::blake2s(input_v);
49
50 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
51
52 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
53
54 bool proof_result = CircuitChecker::check(builder);
55 EXPECT_EQ(proof_result, true);
56}
57
58TYPED_TEST(StdlibBlake2s, test_double_block)
59{
60 using Builder = TypeParam;
62
64 std::string input = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789";
65 std::vector<uint8_t> input_v(input.begin(), input.end());
66
67 byte_array_ct input_arr(&builder, input_v);
69
70 auto expected = crypto::blake2s(input_v);
71
72 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
73
74 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
75
76 bool proof_result = CircuitChecker::check(builder);
77 EXPECT_EQ(proof_result, true);
78}
79
80TYPED_TEST(StdlibBlake2s, test_witness_and_constant)
81{
82 using Builder = TypeParam;
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 = crypto::blake2s(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
122TYPED_TEST(StdlibBlake2s, test_constant_only)
123{
124 using Builder = TypeParam;
126
128 size_t len = 65;
129
130 // create a byte array that is a circuit constant
132
133 // create expected input vector
134 std::vector<uint8_t> input_v(len, '1');
135
136 // Verify the circuit input matches the expected input
137 EXPECT_EQ(input_arr.get_value(), input_v);
138
139 // hash the byte array
141
142 // compute expected hash
143 auto expected = crypto::blake2s(input_v);
144
145 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
146
147 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
148
149 bool proof_result = CircuitChecker::check(builder);
150 EXPECT_EQ(proof_result, true);
151}
152
153TYPED_TEST(StdlibBlake2s, test_multiple_sized_blocks)
154{
155 using Builder = TypeParam;
157
158 int i = 0;
159
160 for (auto v : test_vectors) {
162
163 std::vector<uint8_t> input_v(v.begin(), v.end());
164
165 byte_array_ct input_arr(&builder, input_v);
167
168 auto expected = crypto::blake2s(input_v);
169
170 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
171
172 info("test vector ", i++, ".: builder gates = ", builder.get_num_finalized_gates_inefficient());
173
174 bool proof_result = CircuitChecker::check(builder);
175 EXPECT_EQ(proof_result, true);
176 }
177}
178
179// Previously, certain inputs were pushing the addition overflows in `g` to beyond 3 bits (where `add_normalize` can
180// tolerate up to 3 bits of overflow), causing failures. This has been addressed by calling `add_normalize` in the
181// second half of every call to `g` to ensure that the overflow doesn't go beyond 3 bits. The edge case that caused
182// addition overflow issues in Blake is tested here. See https://hackmd.io/@aztec-network/SyTHLkAWZx for a detailed
183// description of the addition overflow issue.
184TYPED_TEST(StdlibBlake2s, test_edge_case_addition_overflow)
185{
186 using Builder = TypeParam;
188
189 std::array<uint8_t, 62> v = { 0x0E, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6,
190 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6,
191 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xFF,
192 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6,
193 0xF6, 0xF6, 0xF6, 0xF6, 0xED, 0xC3, 0x00, 0x00, 0x00, 0xED };
194
196
197 std::vector<uint8_t> input_v(v.begin(), v.end());
198
199 byte_array_ct input_arr(&builder, input_v);
201
202 auto expected = crypto::blake2s(input_v);
203
204 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
205
206 info(".: builder gates = ", builder.get_num_finalized_gates_inefficient());
207
208 bool proof_result = CircuitChecker::check(builder);
209 EXPECT_EQ(proof_result, true);
210}
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
static byte_array_ct hash(const byte_array_ct &input)
Definition blake2s.cpp:135
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[]
std::array< uint8_t, BLAKE2S_OUTBYTES > blake2s(std::vector< uint8_t > const &input)
Definition blake2s.cpp:232
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
TYPED_TEST_SUITE(CommitmentKeyTest, Curves)
TYPED_TEST(CommitmentKeyTest, CommitToZeroPoly)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
uint8_t len
::testing::Types< UltraCircuitBuilder, MegaCircuitBuilder > BuilderTypes