Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
blake2s.test.cpp
Go to the documentation of this file.
6#include "blake2s.hpp"
7#include <gtest/gtest.h>
8
9using namespace bb;
10using namespace bb::stdlib;
11
13
18
19std::vector<std::string> test_vectors = { "",
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_blake2s, test_single_block)
35{
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 auto expected = crypto::blake2s(input_v);
44
45 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
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_blake2s, test_double_block)
54{
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 auto expected = crypto::blake2s(input_v);
63
64 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
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_blake2s, test_witness_and_constant)
73{
75
76 // create a byte array that is a circuit witness
77 std::string witness_str = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz";
78 std::vector<uint8_t> witness_str_vec(witness_str.begin(), witness_str.end());
79
80 // create a byte array that is part circuit witness and part circuit constant
81 // start with the witness part, then append constant padding
82 byte_array_ct input_arr(&builder, witness_str_vec);
85
86 // for expected value calculation
87 std::vector<uint8_t> constant_vec = { '0', '1' };
88
89 // create expected input vector by concatenating witness and constant parts
90 std::vector<uint8_t> input_v;
91 input_v.insert(input_v.end(), witness_str_vec.begin(), witness_str_vec.end());
92 input_v.insert(input_v.end(), constant_vec.begin(), constant_vec.end());
93
94 // Verify the circuit input matches the expected input
95 EXPECT_EQ(input_arr.get_value(), input_v);
96
97 // hash the combined byte array
99
100 // compute expected hash
101 auto expected = crypto::blake2s(input_v);
102
103 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
104
105 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
106
107 bool proof_result = CircuitChecker::check(builder);
108 EXPECT_EQ(proof_result, true);
109}
110
111TEST(stdlib_blake2s, test_constant_only)
112{
114 size_t len = 65;
115
116 // create a byte array that is a circuit constant
118
119 // create expected input vector
120 std::vector<uint8_t> input_v(len, '1');
121
122 // Verify the circuit input matches the expected input
123 EXPECT_EQ(input_arr.get_value(), input_v);
124
125 // hash the byte array
127
128 // compute expected hash
129 auto expected = crypto::blake2s(input_v);
130
131 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
132
133 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
134
135 bool proof_result = CircuitChecker::check(builder);
136 EXPECT_EQ(proof_result, true);
137}
138
139TEST(stdlib_blake2s, test_multiple_sized_blocks)
140{
141
142 int i = 0;
143
144 for (auto v : test_vectors) {
146
147 std::vector<uint8_t> input_v(v.begin(), v.end());
148
149 byte_array_ct input_arr(&builder, input_v);
151
152 auto expected = crypto::blake2s(input_v);
153
154 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
155
156 info("test vector ", i++, ".: builder gates = ", builder.get_num_finalized_gates_inefficient());
157
158 bool proof_result = CircuitChecker::check(builder);
159 EXPECT_EQ(proof_result, true);
160 }
161}
162
163// Previously, certain inputs were pushing the addition overflows in `g` to beyond 3 bits (where `add_normalize` can
164// tolerate up to 3 bits of overflow), causing failures. This has been addressed by calling `add_normalize` in the
165// second half of every call to `g` to ensure that the overflow doesn't go beyond 3 bits. The edge case that caused
166// addition overflow issues in Blake is tested here. See https://hackmd.io/@aztec-network/SyTHLkAWZx for a detailed
167// description of the addition overflow issue.
168TEST(stdlib_blake2s, test_edge_case_addition_overflow)
169{
170 std::array<uint8_t, 62> v = { 0x0E, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6,
171 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6,
172 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xFF,
173 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6,
174 0xF6, 0xF6, 0xF6, 0xF6, 0xED, 0xC3, 0x00, 0x00, 0x00, 0xED };
175
177
178 std::vector<uint8_t> input_v(v.begin(), v.end());
179
180 byte_array_ct input_arr(&builder, input_v);
182
183 auto expected = crypto::blake2s(input_v);
184
185 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
186
187 info(".: builder gates = ", builder.get_num_finalized_gates_inefficient());
188
189 bool proof_result = CircuitChecker::check(builder);
190 EXPECT_EQ(proof_result, true);
191}
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:133
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
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
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
uint8_t len
std::vector< std::string > test_vectors