Skip to content

Commit 24d4529

Browse files
committed
update for sha256 for PR
1 parent 3facaab commit 24d4529

File tree

4 files changed

+161
-93
lines changed

4 files changed

+161
-93
lines changed

barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph.cpp

+19-65
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ inline void Graph_<FF>::remove_unnecessary_decompose_variables(bb::UltraCircuitB
441441
}
442442

443443
template <typename FF>
444-
void Graph_<FF>::remove_unnecessary_plookup_variables(bb::UltraCircuitBuilder& ultra_circuit_builder,
445-
std::unordered_set<uint32_t>& variables_in_one_gate)
444+
inline void Graph_<FF>::remove_unnecessary_plookup_variables(bb::UltraCircuitBuilder& ultra_circuit_builder,
445+
std::unordered_set<uint32_t>& variables_in_one_gate)
446446
{
447447
auto& lookup_block = ultra_circuit_builder.blocks.lookup;
448448
if (lookup_block.size() > 0) {
@@ -470,6 +470,8 @@ inline void Graph_<FF>::process_current_plookup_gate(bb::UltraCircuitBuilder& ul
470470
std::set<bb::fr> column_2(table.column_2.begin(), table.column_2.end());
471471
std::set<bb::fr> column_3(table.column_3.begin(), table.column_3.end());
472472
bb::plookup::BasicTableId id = table.id;
473+
// in some table we don't use variables from third column. So, these variables are false-case for a
474+
// analyzer, and we have to remove them false cases for AES
473475
if (id == bb::plookup::AES_SBOX_MAP || id == bb::plookup::AES_SPARSE_MAP) {
474476
uint32_t real_out_idx = to_real(lookup_block.w_o()[gate_index]);
475477
uint32_t real_right_idx = to_real(lookup_block.w_r()[gate_index]);
@@ -490,74 +492,22 @@ inline void Graph_<FF>::process_current_plookup_gate(bb::UltraCircuitBuilder& ul
490492
}
491493
}
492494
}
493-
if (id == bb::plookup::SHA256_WITNESS_SLICE_3) {
495+
// false cases for sha256
496+
if (id == bb::plookup::SHA256_WITNESS_SLICE_3 || id == bb::plookup::SHA256_WITNESS_SLICE_7_ROTATE_4 ||
497+
id == bb::plookup::SHA256_WITNESS_SLICE_8_ROTATE_7 ||
498+
id == bb::plookup::SHA256_WITNESS_SLICE_14_ROTATE_1 || id == bb::plookup::SHA256_BASE16_ROTATE2 ||
499+
id == bb::plookup::SHA256_BASE16 || id == bb::plookup::SHA256_BASE28_ROTATE6 ||
500+
id == bb::plookup::SHA256_BASE28_ROTATE3) {
494501
uint32_t real_right_idx = to_real(lookup_block.w_r()[gate_index]);
495502
uint32_t real_out_idx = to_real(lookup_block.w_o()[gate_index]);
496503
if (variables_gate_counts[real_out_idx] != 1 || variables_gate_counts[real_right_idx] != 1) {
497504
auto q_m = lookup_block.q_m()[gate_index];
498505
auto q_c = lookup_block.q_c()[gate_index];
499506
bool find_out = find_position(real_out_idx);
500507
bool find_right = find_position(real_right_idx);
501-
if (q_c == 0) {
502-
if (find_out) {
503-
variables_in_one_gate.erase(real_out_idx);
504-
}
505-
}
506-
if (q_m == 0) {
507-
if (find_right) {
508-
variables_in_one_gate.erase(real_right_idx);
509-
}
510-
}
511-
}
512-
}
513-
if (id == bb::plookup::SHA256_WITNESS_SLICE_7_ROTATE_4) {
514-
uint32_t real_right_idx = to_real(lookup_block.w_r()[gate_index]);
515-
uint32_t real_out_idx = to_real(lookup_block.w_o()[gate_index]);
516-
if (variables_gate_counts[real_out_idx] != 1 || variables_gate_counts[real_right_idx] != 1) {
517-
auto q_m = lookup_block.q_m()[gate_index];
518-
auto q_c = lookup_block.q_c()[gate_index];
519-
bool find_out = find_position(real_out_idx);
520-
bool find_right = find_position(real_right_idx);
521-
if (q_c == 0) {
522-
if (find_out) {
523-
variables_in_one_gate.erase(real_out_idx);
524-
}
525-
}
526-
if (q_m == 0) {
527-
if (find_right) {
528-
variables_in_one_gate.erase(real_right_idx);
529-
}
508+
if (real_out_idx == 1504) {
509+
info("it's selector == ", q_m);
530510
}
531-
}
532-
}
533-
if (id == bb::plookup::SHA256_WITNESS_SLICE_8_ROTATE_7) {
534-
uint32_t real_right_idx = to_real(lookup_block.w_r()[gate_index]);
535-
uint32_t real_out_idx = to_real(lookup_block.w_o()[gate_index]);
536-
if (variables_gate_counts[real_out_idx] != 1 || variables_gate_counts[real_right_idx] != 1) {
537-
auto q_m = lookup_block.q_m()[gate_index];
538-
auto q_c = lookup_block.q_c()[gate_index];
539-
bool find_out = find_position(real_out_idx);
540-
bool find_right = find_position(real_right_idx);
541-
if (q_c == 0) {
542-
if (find_out) {
543-
variables_in_one_gate.erase(real_out_idx);
544-
}
545-
}
546-
if (q_m == 0) {
547-
if (find_right) {
548-
variables_in_one_gate.erase(real_right_idx);
549-
}
550-
}
551-
}
552-
}
553-
if (id == bb::plookup::SHA256_WITNESS_SLICE_14_ROTATE_1) {
554-
uint32_t real_right_idx = to_real(lookup_block.w_r()[gate_index]);
555-
uint32_t real_out_idx = to_real(lookup_block.w_o()[gate_index]);
556-
if (variables_gate_counts[real_out_idx] != 1 || variables_gate_counts[real_right_idx] != 1) {
557-
auto q_m = lookup_block.q_m()[gate_index];
558-
auto q_c = lookup_block.q_c()[gate_index];
559-
bool find_out = find_position(real_out_idx);
560-
bool find_right = find_position(real_right_idx);
561511
if (q_c == 0) {
562512
if (find_out) {
563513
variables_in_one_gate.erase(real_out_idx);
@@ -568,6 +518,12 @@ inline void Graph_<FF>::process_current_plookup_gate(bb::UltraCircuitBuilder& ul
568518
variables_in_one_gate.erase(real_right_idx);
569519
}
570520
}
521+
} else {
522+
info("gates_count: ",
523+
variables_gate_counts[real_out_idx],
524+
" ",
525+
variables_gate_counts[real_right_idx]);
526+
info("variable indices: ", real_right_idx, " ", real_out_idx);
571527
}
572528
}
573529
if (column_1.size() == 1) {
@@ -661,16 +617,14 @@ template <typename FF> void Graph_<FF>::print_variables_gate_counts()
661617
{
662618
for (const auto& it : variables_gate_counts) {
663619
info("number of gates with variables ", it.first, " == ", it.second);
664-
info(it.second);
665620
}
666621
}
667622

668623
template <typename FF> void Graph_<FF>::print_variables_edge_counts()
669624
{
670625
for (const auto& it : variables_degree) {
671626
if (it.first != 0) {
672-
info("variable index = ", it.first);
673-
info("number of edges for this variables = ", it.second);
627+
info("variable index = ", it.first, "number of edges for this variable = ", it.second);
674628
}
675629
}
676630
}

barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph_description_sha256.test.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ TEST(ultra_circuit_constructor, test_sha256_55_bytes)
3737
Graph graph = Graph(builder);
3838
auto connected_components = graph.find_connected_components();
3939
EXPECT_EQ(connected_components.size(), 1);
40-
// graph.print_connected_components();
4140
}
4241

4342
HEAVY_TEST(ultra_circuit_constructor, test_graph_for_sha256_NIST_vector_five)

barretenberg/cpp/src/barretenberg/boomerang_value_detection/variables_gate_counts.sha256.test.cpp

+136-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "barretenberg/boomerang_value_detection/graph.hpp"
22
#include "barretenberg/circuit_checker/circuit_checker.hpp"
3+
#include "barretenberg/common/test.hpp"
34
#include "barretenberg/crypto/sha256/sha256.hpp"
45
#include "barretenberg/stdlib/hash/sha256/sha256.hpp"
56
#include "barretenberg/stdlib/primitives/byte_array/byte_array.hpp"
@@ -25,6 +26,29 @@ using packed_byte_array_ct = packed_byte_array<Builder>;
2526
using witness_ct = stdlib::witness_t<Builder>;
2627
using field_ct = field_t<Builder>;
2728

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+
2852
TEST(ultra_circuit_constructor, test_variables_gate_counts_for_sha256_55_bytes)
2953
{
3054
// 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)
4165
std::vector<uint32_t> vector_variables_in_on_gate(variables_in_on_gate.begin(), variables_in_on_gate.end());
4266
std::sort(vector_variables_in_on_gate.begin(), vector_variables_in_on_gate.end());
4367
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);
45109
}
46110
}
47111

48-
TEST(ultra_circuit_constructor, test_boomerang_value_in_sha256)
112+
TEST(ultra_circuit_constructor, test_variable_gates_count_for_sha256_NIST_vector_one)
49113
{
50114
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);
58126
}
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();
72149
}
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);
73187
}
74188
}

barretenberg/cpp/src/barretenberg/stdlib/hash/sha256/sha256_plookup.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ std::array<field_t<Builder>, 64> extend_witness(const std::array<field_t<Builder
139139
} else {
140140
w_out = witness_t<Builder>(
141141
ctx, fr(w_out_raw.get_value().from_montgomery_form().data[0] & (uint64_t)0xffffffffULL));
142+
constexpr fr inv_pow_two = fr(2).pow(32).invert();
143+
field_pt w_out_raw_inv_pow_two = w_out_raw * inv_pow_two;
144+
field_pt w_out_inv_pow_two = w_out * inv_pow_two;
145+
field_pt divisor = (w_out_raw_inv_pow_two - w_out_inv_pow_two).normalize();
146+
ctx->create_new_range_constraint(divisor.witness_index, 3);
142147
}
143-
constexpr fr inv_pow_two = fr(2).pow(32).invert();
144-
field_pt w_out_raw_inv_pow_two = w_out_raw * inv_pow_two;
145-
field_pt w_out_inv_pow_two = w_out * inv_pow_two;
146-
field_pt divisor = (w_out_raw_inv_pow_two - w_out_inv_pow_two).normalize();
147-
ctx->create_new_range_constraint(divisor.witness_index, 3);
148148
w_sparse[i] = sparse_witness_limbs(w_out);
149149
}
150150

@@ -271,6 +271,7 @@ std::array<field_t<Builder>, 8> sha256_block(const std::array<field_t<Builder>,
271271
auto a = map_into_maj_sparse_form(h_init[0]);
272272
auto b = map_into_maj_sparse_form(h_init[1]);
273273
auto c = map_into_maj_sparse_form(h_init[2]);
274+
// auto d = sparse_value<Builder>(h_init[3]);
274275
auto d = map_into_maj_sparse_form(h_init[3]);
275276
auto e = map_into_choose_sparse_form(h_init[4]);
276277
auto f = map_into_choose_sparse_form(h_init[5]);

0 commit comments

Comments
 (0)