Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
blake2s.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
12#include "blake2s.hpp"
13#include <gtest/gtest.h>
14
15using namespace bb;
16using namespace bb::stdlib;
17
19
24
25std::vector<std::string> test_vectors = { "",
26 "a",
27 "ab",
28 "abc",
29 "abcd",
30 "abcdefg",
31 "abcdefgh",
32 "abcdefghijklmnopqrstuvwxyz01234",
33 "abcdefghijklmnopqrstuvwxyz012345",
34 "abcdefghijklmnopqrstuvwxyz0123456",
35 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0",
36 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01",
37 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz012",
38 "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789" };
39
40TEST(stdlib_blake2s, test_single_block)
41{
43 std::string input = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01";
44 std::vector<uint8_t> input_v(input.begin(), input.end());
45
46 byte_array_ct input_arr(&builder, input_v);
48
49 auto expected = crypto::blake2s(input_v);
50
51 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
52
53 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
54
55 bool proof_result = CircuitChecker::check(builder);
56 EXPECT_EQ(proof_result, true);
57}
58
59TEST(stdlib_blake2s, test_double_block)
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 auto expected = crypto::blake2s(input_v);
69
70 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
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
78TEST(stdlib_blake2s, test_witness_and_constant)
79{
81
82 // create a byte array that is a circuit witness
83 std::string witness_str = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz";
84 std::vector<uint8_t> witness_str_vec(witness_str.begin(), witness_str.end());
85
86 // create a byte array that is part circuit witness and part circuit constant
87 // start with the witness part, then append constant padding
88 byte_array_ct input_arr(&builder, witness_str_vec);
91
92 // for expected value calculation
93 std::vector<uint8_t> constant_vec = { '0', '1' };
94
95 // create expected input vector by concatenating witness and constant parts
96 std::vector<uint8_t> input_v;
97 input_v.insert(input_v.end(), witness_str_vec.begin(), witness_str_vec.end());
98 input_v.insert(input_v.end(), constant_vec.begin(), constant_vec.end());
99
100 // Verify the circuit input matches the expected input
101 EXPECT_EQ(input_arr.get_value(), input_v);
102
103 // hash the combined byte array
105
106 // compute expected hash
107 auto expected = crypto::blake2s(input_v);
108
109 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
110
111 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
112
113 bool proof_result = CircuitChecker::check(builder);
114 EXPECT_EQ(proof_result, true);
115}
116
117TEST(stdlib_blake2s, test_constant_only)
118{
120 size_t len = 65;
121
122 // create a byte array that is a circuit constant
124
125 // create expected input vector
126 std::vector<uint8_t> input_v(len, '1');
127
128 // Verify the circuit input matches the expected input
129 EXPECT_EQ(input_arr.get_value(), input_v);
130
131 // hash the byte array
133
134 // compute expected hash
135 auto expected = crypto::blake2s(input_v);
136
137 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
138
139 info("builder gates = ", builder.get_num_finalized_gates_inefficient());
140
141 bool proof_result = CircuitChecker::check(builder);
142 EXPECT_EQ(proof_result, true);
143}
144
145TEST(stdlib_blake2s, test_multiple_sized_blocks)
146{
147
148 int i = 0;
149
150 for (auto v : test_vectors) {
152
153 std::vector<uint8_t> input_v(v.begin(), v.end());
154
155 byte_array_ct input_arr(&builder, input_v);
157
158 auto expected = crypto::blake2s(input_v);
159
160 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
161
162 info("test vector ", i++, ".: builder gates = ", builder.get_num_finalized_gates_inefficient());
163
164 bool proof_result = CircuitChecker::check(builder);
165 EXPECT_EQ(proof_result, true);
166 }
167}
168
169// Previously, certain inputs were pushing the addition overflows in `g` to beyond 3 bits (where `add_normalize` can
170// tolerate up to 3 bits of overflow), causing failures. This has been addressed by calling `add_normalize` in the
171// second half of every call to `g` to ensure that the overflow doesn't go beyond 3 bits. The edge case that caused
172// addition overflow issues in Blake is tested here. See https://hackmd.io/@aztec-network/SyTHLkAWZx for a detailed
173// description of the addition overflow issue.
174TEST(stdlib_blake2s, test_edge_case_addition_overflow)
175{
176 std::array<uint8_t, 62> v = { 0x0E, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6,
177 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6,
178 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xFF,
179 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6, 0xF6,
180 0xF6, 0xF6, 0xF6, 0xF6, 0xED, 0xC3, 0x00, 0x00, 0x00, 0xED };
181
183
184 std::vector<uint8_t> input_v(v.begin(), v.end());
185
186 byte_array_ct input_arr(&builder, input_v);
188
189 auto expected = crypto::blake2s(input_v);
190
191 EXPECT_EQ(output.get_value(), std::vector<uint8_t>(expected.begin(), expected.end()));
192
193 info(".: builder gates = ", builder.get_num_finalized_gates_inefficient());
194
195 bool proof_result = CircuitChecker::check(builder);
196 EXPECT_EQ(proof_result, true);
197}
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)
#define info(...)
Definition log.hpp:93
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