44#include " barretenberg/crypto/sha256/sha256.hpp"
55#include " barretenberg/stdlib/hash/sha256/sha256.hpp"
66#include " barretenberg/stdlib/primitives/byte_array/byte_array.hpp"
7- #include " barretenberg/stdlib/primitives/packed_byte_array/packed_byte_array.hpp"
87#include " barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.hpp"
98#include " barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp"
109
@@ -17,28 +16,46 @@ using namespace bb::stdlib;
1716using namespace cdg ;
1817
1918using Builder = UltraCircuitBuilder;
20- using byte_array_pt = byte_array<Builder>;
21- using packed_byte_array_pt = packed_byte_array<Builder>;
22- using field_pt = field_t <Builder>;
19+ using byte_array_ct = byte_array<Builder>;
20+ using field_ct = field_t <Builder>;
21+
22+ /* *
23+ * @brief Given a `byte_array` object, slice it into chunks of size `num_bytes_in_chunk` and compute field elements
24+ * reconstructed from these chunks.
25+ */
26+
27+ std::vector<field_ct> pack_bytes_into_field_elements (const byte_array_ct& input, size_t num_bytes_in_chunk = 4 )
28+ {
29+ std::vector<field_t <Builder>> result;
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+ }
2339
2440/* *
2541 static analyzer usually prints input and output variables as variables in one gate. In tests these variables
2642 are not dangerous and usually we can filter them by adding gate for fixing witness. Then these variables will be
2743 in 2 gates, and static analyzer won't print them. functions fix_vector and fix_byte_array do it
28- for vector of variables and packed_byte_array respectively
44+ for vector of variables and byte_array respectively
2945*/
3046
31- void fix_vector (std::vector<field_pt >& vector)
47+ void fix_vector (std::vector<field_ct >& vector)
3248{
3349 for (auto & elem : vector) {
3450 elem.fix_witness ();
3551 }
3652}
3753
38- void fix_byte_array (packed_byte_array_pt & input)
54+ void fix_byte_array (byte_array_ct & input)
3955{
40- std::vector<field_pt> limbs = input.get_limbs ();
41- fix_vector (limbs);
56+ for (size_t idx = 0 ; idx < input.size (); idx++) {
57+ input[idx].fix_witness ();
58+ }
4259}
4360
4461/* *
@@ -55,12 +72,12 @@ TEST(boomerang_stdlib_sha256, test_graph_for_sha256_55_bytes)
5572 // 55 bytes is the largest number of bytes that can be hashed in a single block,
5673 // accounting for the single padding bit, and the 64 size bits required by the SHA-256 standard.
5774 auto builder = Builder ();
58- packed_byte_array_pt input (&builder, " An 8 character password? Snow White and the 7 Dwarves.." );
75+ byte_array_ct input (&builder, " An 8 character password? Snow White and the 7 Dwarves.." );
5976 fix_byte_array (input);
6077
61- packed_byte_array_pt output_bits = stdlib::SHA256<Builder>::hash (input);
78+ byte_array_ct output_bytes = stdlib::SHA256<Builder>::hash (input);
6279
63- std::vector<field_pt > output = output_bits. to_unverified_byte_slices ( 4 );
80+ std::vector<field_ct > output = pack_bytes_into_field_elements (output_bytes );
6481 fix_vector (output);
6582
6683 StaticAnalyzer graph = StaticAnalyzer (builder);
@@ -87,7 +104,7 @@ HEAVY_TEST(boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_five)
87104{
88105 auto builder = Builder ();
89106
90- packed_byte_array_pt input (
107+ byte_array_ct input (
91108 &builder,
92109 " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
93110 " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
@@ -101,9 +118,9 @@ HEAVY_TEST(boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_five)
101118 " AAAAAAAAAA" );
102119
103120 fix_byte_array (input);
104- packed_byte_array_pt output_bits = stdlib::SHA256<bb::UltraCircuitBuilder>::hash (input);
121+ byte_array_ct output_bytes = stdlib::SHA256<bb::UltraCircuitBuilder>::hash (input);
105122
106- std::vector<field_pt > output = output_bits. to_unverified_byte_slices ( 4 );
123+ std::vector<field_ct > output = pack_bytes_into_field_elements (output_bytes );
107124 fix_vector (output);
108125
109126 StaticAnalyzer graph = StaticAnalyzer (builder);
@@ -126,10 +143,10 @@ HEAVY_TEST(boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_five)
126143TEST (boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_one)
127144{
128145 auto builder = Builder ();
129- packed_byte_array_pt input (&builder, " abc" );
146+ byte_array_ct input (&builder, " abc" );
130147 fix_byte_array (input);
131- packed_byte_array_pt output_bits = stdlib::SHA256<Builder>::hash (input);
132- fix_byte_array (output_bits );
148+ byte_array_ct output_bytes = stdlib::SHA256<Builder>::hash (input);
149+ fix_byte_array (output_bytes );
133150 StaticAnalyzer graph = StaticAnalyzer (builder);
134151 auto connected_components = graph.find_connected_components ();
135152 EXPECT_EQ (connected_components.size (), 1 );
@@ -146,10 +163,10 @@ TEST(boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_one)
146163TEST (boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_two)
147164{
148165 auto builder = Builder ();
149- packed_byte_array_pt input (&builder, " abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" );
166+ byte_array_ct input (&builder, " abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" );
150167 fix_byte_array (input);
151- packed_byte_array_pt output_bits = stdlib::SHA256<Builder>::hash (input);
152- fix_byte_array (output_bits );
168+ byte_array_ct output_bytes = stdlib::SHA256<Builder>::hash (input);
169+ fix_byte_array (output_bytes );
153170 StaticAnalyzer graph = StaticAnalyzer (builder);
154171 auto connected_components = graph.find_connected_components ();
155172 EXPECT_EQ (connected_components.size (), 1 );
@@ -168,10 +185,10 @@ TEST(boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_three)
168185 auto builder = Builder ();
169186
170187 // one byte, 0xbd
171- packed_byte_array_pt input (&builder, std::vector<uint8_t >{ 0xbd });
188+ byte_array_ct input (&builder, std::vector<uint8_t >{ 0xbd });
172189 fix_byte_array (input);
173- packed_byte_array_pt output_bits = stdlib::SHA256<Builder>::hash (input);
174- fix_byte_array (output_bits );
190+ byte_array_ct output_bytes = stdlib::SHA256<Builder>::hash (input);
191+ fix_byte_array (output_bytes );
175192 StaticAnalyzer graph = StaticAnalyzer (builder);
176193 auto connected_components = graph.find_connected_components ();
177194 EXPECT_EQ (connected_components.size (), 1 );
@@ -190,10 +207,10 @@ TEST(boomerang_stdlib_sha256, test_graph_for_sha256_NIST_vector_four)
190207 auto builder = Builder ();
191208
192209 // 4 bytes, 0xc98c8e55
193- packed_byte_array_pt input (&builder, std::vector<uint8_t >{ 0xc9 , 0x8c , 0x8e , 0x55 });
210+ byte_array_ct input (&builder, std::vector<uint8_t >{ 0xc9 , 0x8c , 0x8e , 0x55 });
194211 fix_byte_array (input);
195- packed_byte_array_pt output_bits = stdlib::SHA256<Builder>::hash (input);
196- fix_byte_array (output_bits );
212+ byte_array_ct output_bytes = stdlib::SHA256<Builder>::hash (input);
213+ fix_byte_array (output_bytes );
197214 StaticAnalyzer graph = StaticAnalyzer (builder);
198215 auto connected_components = graph.find_connected_components ();
199216 EXPECT_EQ (connected_components.size (), 1 );
0 commit comments