1
1
#include " barretenberg/boomerang_value_detection/graph.hpp"
2
2
#include " barretenberg/circuit_checker/circuit_checker.hpp"
3
+ #include " barretenberg/common/test.hpp"
3
4
#include " barretenberg/crypto/sha256/sha256.hpp"
4
5
#include " barretenberg/stdlib/hash/sha256/sha256.hpp"
5
6
#include " barretenberg/stdlib/primitives/byte_array/byte_array.hpp"
@@ -25,6 +26,29 @@ using packed_byte_array_ct = packed_byte_array<Builder>;
25
26
using witness_ct = stdlib::witness_t <Builder>;
26
27
using field_ct = field_t <Builder>;
27
28
29
+ bool check_in_byte_array (const uint32_t & real_var_index, const packed_byte_array_ct& byte_array)
30
+ {
31
+ std::vector<field_t <Builder>> limbs = byte_array.get_limbs ();
32
+ for (const auto & elem : limbs) {
33
+ if (elem.witness_index == real_var_index) {
34
+ return true ;
35
+ }
36
+ }
37
+ return false ;
38
+ }
39
+
40
+ bool check_in_range_lists (const uint32_t & real_var_index, const uint64_t & target_range, const Builder& builder)
41
+ {
42
+ auto range_lists = builder.range_lists ;
43
+ auto target_list = range_lists[target_range];
44
+ for (const auto elem : target_list.variable_indices ) {
45
+ if (elem == real_var_index) {
46
+ return true ;
47
+ }
48
+ }
49
+ return false ;
50
+ }
51
+
28
52
TEST (ultra_circuit_constructor, test_variables_gate_counts_for_sha256_55_bytes)
29
53
{
30
54
// 55 bytes is the largest number of bytes that can be hashed in a single block,
@@ -41,34 +65,124 @@ TEST(ultra_circuit_constructor, test_variables_gate_counts_for_sha256_55_bytes)
41
65
std::vector<uint32_t > vector_variables_in_on_gate (variables_in_on_gate.begin (), variables_in_on_gate.end ());
42
66
std::sort (vector_variables_in_on_gate.begin (), vector_variables_in_on_gate.end ());
43
67
for (const auto & elem : vector_variables_in_on_gate) {
44
- info (" elem == " , elem);
68
+ bool result1 = check_in_byte_array (elem, input);
69
+ bool result2 = check_in_byte_array (elem, output_bits);
70
+ bool result3 = check_in_range_lists (elem, 3 , builder);
71
+ bool check = (result1 == 1 ) || (result2 == 1 ) || (result3 == 1 );
72
+ EXPECT_EQ (check, true );
73
+ }
74
+ }
75
+
76
+ HEAVY_TEST (ultra_circuit_constructor, test_variable_gates_count_for_sha256_NIST_vector_five)
77
+ {
78
+ auto builder = Builder ();
79
+
80
+ packed_byte_array_ct input (
81
+ &builder,
82
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
83
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
84
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
85
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
86
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
87
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
88
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
89
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
90
+ " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
91
+ " AAAAAAAAAA" );
92
+
93
+ packed_byte_array_ct output_bits = stdlib::sha256 (input);
94
+ Graph graph = Graph (builder);
95
+ std::unordered_set<uint32_t > variables_in_on_gate = graph.show_variables_in_one_gate (builder);
96
+ for (const auto & elem : variables_in_on_gate) {
97
+ bool result1 = check_in_byte_array (elem, input);
98
+ bool result2 = check_in_byte_array (elem, output_bits);
99
+ bool result3 = check_in_range_lists (elem, 3 , builder);
100
+ bool check = (result1 == 1 ) || (result2 == 1 ) || (result3 == 1 );
101
+ if (check == false ) {
102
+ info (" elem == " , elem);
103
+ info (result1);
104
+ info (result2);
105
+ info (result3);
106
+ info ();
107
+ }
108
+ EXPECT_EQ (check, true );
45
109
}
46
110
}
47
111
48
- TEST (ultra_circuit_constructor, test_boomerang_value_in_sha256 )
112
+ TEST (ultra_circuit_constructor, test_variable_gates_count_for_sha256_NIST_vector_one )
49
113
{
50
114
auto builder = Builder ();
51
- std::array<field_t <Builder>, 16 > input;
52
- for (size_t i = 0 ; i < 16 ; i++) {
53
- auto variable = fr::random_element ();
54
- auto var_slice = uint256_t (variable).slice (0 , 32 );
55
- field_ct elt (witness_ct (&builder, fr (var_slice)));
56
- builder.fix_witness (elt.witness_index , elt.get_value ());
57
- input[i] = elt;
115
+ packed_byte_array_ct input (&builder, " abc" );
116
+ packed_byte_array_ct output_bits = stdlib::sha256 (input);
117
+
118
+ Graph graph = Graph (builder);
119
+ std::unordered_set<uint32_t > variables_in_one_gate = graph.show_variables_in_one_gate (builder);
120
+ for (const auto & elem : variables_in_one_gate) {
121
+ bool result1 = check_in_byte_array (elem, input);
122
+ bool result2 = check_in_byte_array (elem, output_bits);
123
+ bool result3 = check_in_range_lists (elem, 3 , builder);
124
+ bool check = (result1 == 1 ) || (result2 == 1 ) || (result3 == 1 );
125
+ EXPECT_EQ (check, true );
58
126
}
59
- std::array<field_t <Builder>, 64 > w_ext = sha256_plookup::extend_witness (input);
60
- bool result1 = CircuitChecker::check (builder);
61
- EXPECT_EQ (result1, true );
62
- while (true ) {
63
- auto change_variable = fr::random_element ();
64
- auto change_var_slice = uint256_t (change_variable).slice (0 , 32 );
65
- field_t change_elt (&builder, fr (change_var_slice));
66
- uint32_t variable_index = w_ext[62 ].witness_index ;
67
- if (builder.variables [builder.real_variable_index [variable_index]] != fr (change_var_slice)) {
68
- builder.variables [builder.real_variable_index [variable_index]] = fr (change_var_slice);
69
- bool result2 = CircuitChecker::check (builder);
70
- EXPECT_EQ (result2, true );
71
- break ;
127
+ }
128
+
129
+ TEST (ultra_circuit_constructor, test_variable_gates_count_for_sha256_NIST_vector_two)
130
+ {
131
+ auto builder = Builder ();
132
+
133
+ packed_byte_array_ct input (&builder, " abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" );
134
+
135
+ packed_byte_array_ct output_bits = stdlib::sha256 (input);
136
+ Graph graph = Graph (builder);
137
+ std::unordered_set<uint32_t > variables_in_one_gate = graph.show_variables_in_one_gate (builder);
138
+ for (const auto & elem : variables_in_one_gate) {
139
+ bool result1 = check_in_byte_array (elem, input);
140
+ bool result2 = check_in_byte_array (elem, output_bits);
141
+ bool result3 = check_in_range_lists (elem, 3 , builder);
142
+ bool check = (result1 == 1 ) || (result2 == 1 ) || (result3 == 1 );
143
+ if (check == false ) {
144
+ info (" elem == " , elem);
145
+ info (result1);
146
+ info (result2);
147
+ info (result3);
148
+ info ();
72
149
}
150
+ EXPECT_EQ (check, true );
151
+ }
152
+ }
153
+
154
+ TEST (ultra_circuit_constructor, test_variable_gates_count_sha256_NIST_vector_three)
155
+ {
156
+ auto builder = Builder ();
157
+
158
+ // one byte, 0xbd
159
+ packed_byte_array_ct input (&builder, std::vector<uint8_t >{ 0xbd });
160
+ packed_byte_array_ct output_bits = stdlib::sha256 (input);
161
+ Graph graph = Graph (builder);
162
+ std::unordered_set<uint32_t > variables_in_one_gate = graph.show_variables_in_one_gate (builder);
163
+ for (const auto & elem : variables_in_one_gate) {
164
+ bool result1 = check_in_byte_array (elem, input);
165
+ bool result2 = check_in_byte_array (elem, output_bits);
166
+ bool result3 = check_in_range_lists (elem, 3 , builder);
167
+ bool check = (result1 == 1 ) || (result2 == 1 ) || (result3 == 1 );
168
+ EXPECT_EQ (check, true );
169
+ }
170
+ }
171
+
172
+ TEST (ultra_circuit_constructor, test_variable_gates_count_sha256_NIST_vector_four)
173
+ {
174
+ auto builder = Builder ();
175
+
176
+ // 4 bytes, 0xc98c8e55
177
+ packed_byte_array_ct input (&builder, std::vector<uint8_t >{ 0xc9 , 0x8c , 0x8e , 0x55 });
178
+ packed_byte_array_ct output_bits = stdlib::sha256<Builder>(input);
179
+ Graph graph = Graph (builder);
180
+ std::unordered_set<uint32_t > variables_in_one_gate = graph.show_variables_in_one_gate (builder);
181
+ for (const auto & elem : variables_in_one_gate) {
182
+ bool result1 = check_in_byte_array (elem, input);
183
+ bool result2 = check_in_byte_array (elem, output_bits);
184
+ bool result3 = check_in_range_lists (elem, 3 , builder);
185
+ bool check = (result1 == 1 ) || (result2 == 1 ) || (result3 == 1 );
186
+ EXPECT_EQ (check, true );
73
187
}
74
188
}
0 commit comments