Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
graph_description_aes128.test.cpp
Go to the documentation of this file.
10
11#include <gtest/gtest.h>
12
13using namespace bb;
14using namespace bb::stdlib;
15using namespace cdg;
16
20
31{
32 for (auto& elem : input_vector) {
33 elem.fix_witness();
34 }
35}
36
44TEST(boomerang_stdlib_aes, test_graph_for_aes_64_bytes)
45{
46 uint8_t key[16]{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
47 uint8_t iv[16]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
48 uint8_t in[64]{ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
49 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
50 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
51 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
52
53 auto convert_bytes = [](uint8_t* data) {
54 uint256_t converted(0);
55 for (uint64_t i = 0; i < 16; ++i) {
56 uint256_t to_add = uint256_t((uint64_t)(data[i])) << uint256_t((15 - i) * 8);
57 converted += to_add;
58 }
59 return converted;
60 };
61
62 auto builder = Builder();
63
64 std::vector<field_pt> in_field{
65 witness_pt(&builder, fr(convert_bytes(in))),
66 witness_pt(&builder, fr(convert_bytes(in + 16))),
67 witness_pt(&builder, fr(convert_bytes(in + 32))),
68 witness_pt(&builder, fr(convert_bytes(in + 48))),
69 };
70
71 fix_vector_witness(in_field);
72
73 field_pt key_field(witness_pt(&builder, fr(convert_bytes(key))));
74 field_pt iv_field(witness_pt(&builder, fr(convert_bytes(iv))));
75 key_field.fix_witness();
76 iv_field.fix_witness();
77
78 auto result = stdlib::aes128::encrypt_buffer_cbc(in_field, iv_field, key_field);
79 fix_vector_witness(result);
80
82 auto connected_components = graph.find_connected_components();
83 EXPECT_EQ(connected_components.size(), 1);
84 auto variables_in_one_gate = graph.show_variables_in_one_gate(builder);
85 EXPECT_EQ(variables_in_one_gate.size(), 0);
86}
87
98TEST(boomerang_stdlib_aes, test_variable_gates_count_for_aes128cbc)
99{
100 uint8_t key[16]{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
101 uint8_t iv[16]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
102 uint8_t in[64]{ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
103 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
104 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
105 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
106
107 auto convert_bytes = [](uint8_t* data) {
108 uint256_t converted(0);
109 for (uint64_t i = 0; i < 16; ++i) {
110 uint256_t to_add = uint256_t((uint64_t)(data[i])) << uint256_t((15 - i) * 8);
111 converted += to_add;
112 }
113 return converted;
114 };
115
116 auto builder = Builder();
117
118 std::vector<field_pt> in_field{
119 witness_pt(&builder, fr(convert_bytes(in))),
120 witness_pt(&builder, fr(convert_bytes(in + 16))),
121 witness_pt(&builder, fr(convert_bytes(in + 32))),
122 witness_pt(&builder, fr(convert_bytes(in + 48))),
123 };
124
125 fix_vector_witness(in_field);
126
127 field_pt key_field(witness_pt(&builder, fr(convert_bytes(key))));
128 field_pt iv_field(witness_pt(&builder, fr(convert_bytes(iv))));
129 key_field.fix_witness();
130 iv_field.fix_witness();
131
132 auto result = stdlib::aes128::encrypt_buffer_cbc(in_field, iv_field, key_field);
133 fix_vector_witness(result);
134
136 auto connected_components = graph.find_connected_components();
137 EXPECT_EQ(connected_components.size(), 1);
138 std::unordered_set<uint32_t> variables_in_one_gate = graph.show_variables_in_one_gate(builder);
139 EXPECT_EQ(variables_in_one_gate.size(), 0);
140}
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
const std::vector< FF > data
void fix_vector_witness(std::vector< field_pt > &input_vector)
Fix witness values in a vector to ensure they appear in multiple gates.
stdlib::witness_t< bb::UltraCircuitBuilder > witness_pt
UltraCircuitBuilder Builder
TEST(boomerang_stdlib_aes, test_graph_for_aes_64_bytes)
Test graph description of AES128CBC circuit with 64 bytes of data.
std::vector< field_t< Builder > > encrypt_buffer_cbc(const std::vector< field_t< Builder > > &input, const field_t< Builder > &iv, const field_t< Builder > &key)
Definition aes128.cpp:265
Entry point for Barretenberg command-line interface.
field< Bn254FrParams > fr
Definition fr.hpp:174
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