Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
aes128_constraint.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Khashayar], commit: 21476601b111f046f023474465598843e4cfd8ac}
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
10#include <cstdint>
11#include <cstdio>
12#include <span>
13
14using namespace bb;
15
16namespace acir_format {
17
18template <typename Builder> void create_aes128_constraints(Builder& builder, const AES128Constraint& constraint)
19{
20
22 // Packs 16 bytes from the inputs (plaintext, iv, key) into a field element
23 // Note that noir-stdlib already pads the inputs in accordance with PKCS7 padding scheme.
24 BB_ASSERT(constraint.inputs.size() % 16 == 0, "Inputs must be a multiple of 16");
27 field_ct converted = 0;
28 for (size_t i = 0; i < 16; ++i) {
29 converted *= 256;
31 // Noir enforces bytes to be in the range [0, 255] by type declarations, however, if inputs are taken
32 // from
33 // ACIR directly, these ranges should be enforced by the range constraint. In case these range
34 // constraints already exist we won't be paying for the extra constraint.
35 byte.create_range_constraint(8);
36 converted += byte;
37 }
38 return converted;
39 };
40
41 // Packs 16 bytes from the outputs (witness indexes) into a field element for comparison
42 const auto convert_output = [&](std::span<const uint32_t, 16> outputs) {
43 field_ct converted = 0;
44 for (const auto& output : outputs) {
45 converted *= 256;
47 // Noir enforces bytes to be in the range [0, 255] by type declarations, however, if inputs are taken from
48 // ACIR directly, these ranges should be enforced by the range constraint. In case these range constraints
49 // already exist we won't be paying for the extra constraint.
50 byte.create_range_constraint(8);
51 converted += byte;
52 }
53 return converted;
54 };
55
56 // Perform the conversions from array of bytes to field elements
57 std::vector<field_ct> converted_inputs;
58 for (size_t i = 0; i < constraint.inputs.size(); i += 16) {
59 field_ct to_add;
60
61 to_add = convert_input(
62 std::span<const WitnessOrConstant<bb::fr>, std::dynamic_extent>{ &constraint.inputs[i], 16 }, builder);
63
64 converted_inputs.emplace_back(to_add);
65 }
66
67 std::vector<field_ct> converted_outputs;
68 for (size_t i = 0; i < constraint.outputs.size(); i += 16) {
69 std::span<const uint32_t, 16> outputs{ &constraint.outputs[i], 16 };
70 converted_outputs.emplace_back(convert_output(outputs));
71 }
72
73 const std::vector<field_ct> output_bytes = bb::stdlib::aes128::encrypt_buffer_cbc<Builder>(
74 converted_inputs, convert_input(constraint.iv, builder), convert_input(constraint.key, builder));
75
76 for (size_t i = 0; i < output_bytes.size(); ++i) {
77 output_bytes[i].assert_equal(converted_outputs[i]);
78 }
79}
80
82 const AES128Constraint& constraint);
83
85 const AES128Constraint& constraint);
86
87} // namespace acir_format
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
static field_t from_witness_index(Builder *ctx, uint32_t witness_index)
Definition field.cpp:67
AluTraceBuilder builder
Definition alu.test.cpp:124
AvmProvingInputs inputs
template void create_aes128_constraints< MegaCircuitBuilder >(MegaCircuitBuilder &builder, const AES128Constraint &constraint)
template void create_aes128_constraints< UltraCircuitBuilder >(UltraCircuitBuilder &builder, const AES128Constraint &constraint)
void create_aes128_constraints(Builder &builder, const AES128Constraint &constraint)
bb::stdlib::field_t< Builder > to_field_ct(const WitnessOrConstant< typename Builder::FF > &input, Builder &builder)
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::array< WitnessOrConstant< bb::fr >, 16 > iv
std::vector< uint32_t > outputs
std::vector< WitnessOrConstant< bb::fr > > inputs
std::array< WitnessOrConstant< bb::fr >, 16 > key