Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
graph_description_sha256.test.cpp
Go to the documentation of this file.
9
13
14using namespace bb;
15using namespace bb::stdlib;
16using namespace cdg;
17
21
27std::vector<field_ct> pack_bytes_into_field_elements(const byte_array_ct& input, size_t num_bytes_in_chunk = 4)
28{
30 const size_t byte_len = input.size();
31
32 for (size_t i = 0; i < byte_len; i += num_bytes_in_chunk) {
33 byte_array_ct chunk = input.slice(i, std::min(num_bytes_in_chunk, byte_len - i));
34 result.emplace_back(static_cast<field_ct>(chunk));
35 }
36
37 return result;
38}
39
48{
49 for (auto& elem : vector) {
50 elem.fix_witness();
51 }
52}
53
55{
56 for (size_t idx = 0; idx < input.size(); idx++) {
57 input[idx].fix_witness();
58 }
59}
60
70TEST(boomerang_stdlib_sha256, test_graph_for_sha256_55_bytes)
71{
72 // 55 bytes is the largest number of bytes that can be hashed in a single block,
73 // accounting for the single padding bit, and the 64 size bits required by the SHA-256 standard.
74 auto builder = Builder();
75 byte_array_ct input(&builder, "An 8 character password? Snow White and the 7 Dwarves..");
76 fix_byte_array(input);
77
78 byte_array_ct output_bytes = stdlib::SHA256<Builder>::hash(input);
79
81 fix_vector(output);
82
84 auto connected_components = graph.find_connected_components();
85 EXPECT_EQ(connected_components.size(), 1);
86 auto variables_in_one_gate = graph.show_variables_in_one_gate(builder);
87 EXPECT_EQ(variables_in_one_gate.size(), 0);
88}
89
103HEAVY_TEST(boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_five)
104{
105 auto builder = Builder();
106
107 byte_array_ct input(
108 &builder,
109 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
110 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
111 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
112 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
113 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
114 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
115 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
116 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
117 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
118 "AAAAAAAAAA");
119
120 fix_byte_array(input);
122
124 fix_vector(output);
125
127 auto connected_components = graph.find_connected_components();
128 auto variables_in_one_gate = graph.show_variables_in_one_gate(builder);
129 EXPECT_EQ(variables_in_one_gate.size(), 0);
130 EXPECT_EQ(connected_components.size(), 1);
131}
132
143TEST(boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_one)
144{
145 auto builder = Builder();
146 byte_array_ct input(&builder, "abc");
147 fix_byte_array(input);
148 byte_array_ct output_bytes = stdlib::SHA256<Builder>::hash(input);
149 fix_byte_array(output_bytes);
151 auto connected_components = graph.find_connected_components();
152 EXPECT_EQ(connected_components.size(), 1);
153 std::unordered_set<uint32_t> variables_in_one_gate = graph.show_variables_in_one_gate(builder);
154 EXPECT_EQ(variables_in_one_gate.size(), 0);
155}
156
163TEST(boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_two)
164{
165 auto builder = Builder();
166 byte_array_ct input(&builder, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
167 fix_byte_array(input);
168 byte_array_ct output_bytes = stdlib::SHA256<Builder>::hash(input);
169 fix_byte_array(output_bytes);
171 auto connected_components = graph.find_connected_components();
172 EXPECT_EQ(connected_components.size(), 1);
173 std::unordered_set<uint32_t> variables_in_one_gate = graph.show_variables_in_one_gate(builder);
174 EXPECT_EQ(variables_in_one_gate.size(), 0);
175}
176
183TEST(boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_three)
184{
185 auto builder = Builder();
186
187 // one byte, 0xbd
188 byte_array_ct input(&builder, std::vector<uint8_t>{ 0xbd });
189 fix_byte_array(input);
190 byte_array_ct output_bytes = stdlib::SHA256<Builder>::hash(input);
191 fix_byte_array(output_bytes);
193 auto connected_components = graph.find_connected_components();
194 EXPECT_EQ(connected_components.size(), 1);
195 std::unordered_set<uint32_t> variables_in_one_gate = graph.show_variables_in_one_gate(builder);
196 EXPECT_EQ(variables_in_one_gate.size(), 0);
197}
198
205TEST(boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_four)
206{
207 auto builder = Builder();
208
209 // 4 bytes, 0xc98c8e55
210 byte_array_ct input(&builder, std::vector<uint8_t>{ 0xc9, 0x8c, 0x8e, 0x55 });
211 fix_byte_array(input);
212 byte_array_ct output_bytes = stdlib::SHA256<Builder>::hash(input);
213 fix_byte_array(output_bytes);
215 auto connected_components = graph.find_connected_components();
216 EXPECT_EQ(connected_components.size(), 1);
217 std::unordered_set<uint32_t> variables_in_one_gate = graph.show_variables_in_one_gate(builder);
218 EXPECT_EQ(variables_in_one_gate.size(), 0);
219}
static byte_array< Builder > hash(const byte_array_ct &input)
Definition sha256.cpp:308
Represents a dynamic array of bytes in-circuit.
byte_array slice(size_t offset) const
Slice bytes from the byte array starting at offset. Does not add any constraints.
size_t size() const
std::unordered_set< uint32_t > show_variables_in_one_gate(bb::UltraCircuitBuilder &ultra_circuit_builder)
this method returns a final set of variables that were in one gate
Definition graph.cpp:1190
std::vector< std::vector< uint32_t > > find_connected_components()
this methond finds all connected components in the graph described by adjacency lists
Definition graph.cpp:794
AluTraceBuilder builder
Definition alu.test.cpp:123
void fix_vector(std::vector< field_ct > &vector)
void fix_byte_array(byte_array_ct &input)
std::vector< field_ct > pack_bytes_into_field_elements(const byte_array_ct &input, size_t num_bytes_in_chunk=4)
Given a byte_array object, slice it into chunks of size num_bytes_in_chunk and compute field elements...
TEST(boomerang_stdlib_sha256, test_graph_for_sha256_55_bytes)
Test for SHA256 circuit graph analysis.
UltraCircuitBuilder Builder
Entry point for Barretenberg command-line interface.
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
Definition graph.cpp:11
StaticAnalyzer_< bb::fr > StaticAnalyzer
Definition graph.hpp:201
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
#define HEAVY_TEST(x, y)
Definition test.hpp:9