-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #5866 Adds AES128 as a blackbox function, exposing the existing functionality from barretenberg. Data is padded using PKCS#7 --------- Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
- Loading branch information
1 parent
ead54c4
commit e4b97a8
Showing
51 changed files
with
773 additions
and
65 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
barretenberg/cpp/src/barretenberg/dsl/acir_format/aes128_constraint.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "aes128_constraint.hpp" | ||
#include "barretenberg/stdlib/encryption/aes128/aes128.hpp" | ||
#include <cstdint> | ||
#include <cstdio> | ||
#include <span> | ||
|
||
namespace acir_format { | ||
|
||
template <typename Builder> void create_aes128_constraints(Builder& builder, const AES128Constraint& constraint) | ||
{ | ||
|
||
using field_ct = bb::stdlib::field_t<Builder>; | ||
|
||
// Packs 16 bytes from the inputs (plaintext, iv, key) into a field element | ||
const auto convert_input = [&](std::span<const AES128Input, std::dynamic_extent> inputs, size_t padding) { | ||
field_ct converted = 0; | ||
for (size_t i = 0; i < 16 - padding; ++i) { | ||
converted *= 256; | ||
field_ct byte = field_ct::from_witness_index(&builder, inputs[i].witness); | ||
converted += byte; | ||
} | ||
for (size_t i = 0; i < padding; ++i) { | ||
converted *= 256; | ||
field_ct byte = padding; | ||
converted += byte; | ||
} | ||
return converted; | ||
}; | ||
|
||
// Packs 16 bytes from the outputs (witness indexes) into a field element for comparison | ||
const auto convert_output = [&](std::span<const uint32_t, 16> outputs) { | ||
field_ct converted = 0; | ||
for (const auto& output : outputs) { | ||
converted *= 256; | ||
field_ct byte = field_ct::from_witness_index(&builder, output); | ||
converted += byte; | ||
} | ||
return converted; | ||
}; | ||
|
||
const size_t padding_size = 16 - constraint.inputs.size() % 16; | ||
|
||
// Perform the conversions from array of bytes to field elements | ||
std::vector<field_ct> converted_inputs; | ||
for (size_t i = 0; i < constraint.inputs.size(); i += 16) { | ||
field_ct to_add; | ||
if (i + 16 > constraint.inputs.size()) { | ||
to_add = convert_input( | ||
std::span<const AES128Input, std::dynamic_extent>{ &constraint.inputs[i], 16 - padding_size }, | ||
padding_size); | ||
} else { | ||
to_add = convert_input(std::span<const AES128Input, 16>{ &constraint.inputs[i], 16 }, 0); | ||
} | ||
converted_inputs.emplace_back(to_add); | ||
} | ||
|
||
std::vector<field_ct> converted_outputs; | ||
for (size_t i = 0; i < constraint.outputs.size(); i += 16) { | ||
std::span<const uint32_t, 16> outputs{ &constraint.outputs[i], 16 }; | ||
converted_outputs.emplace_back(convert_output(outputs)); | ||
} | ||
|
||
const std::vector<field_ct> output_bytes = bb::stdlib::aes128::encrypt_buffer_cbc<Builder>( | ||
converted_inputs, convert_input(constraint.iv, 0), convert_input(constraint.key, 0)); | ||
|
||
for (size_t i = 0; i < output_bytes.size(); ++i) { | ||
builder.assert_equal(output_bytes[i].normalize().witness_index, converted_outputs[i].normalize().witness_index); | ||
} | ||
} | ||
|
||
template void create_aes128_constraints<UltraCircuitBuilder>(UltraCircuitBuilder& builder, | ||
const AES128Constraint& constraint); | ||
|
||
template void create_aes128_constraints<GoblinUltraCircuitBuilder>(GoblinUltraCircuitBuilder& builder, | ||
const AES128Constraint& constraint); | ||
|
||
} // namespace acir_format |
31 changes: 31 additions & 0 deletions
31
barretenberg/cpp/src/barretenberg/dsl/acir_format/aes128_constraint.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
#include "barretenberg/dsl/types.hpp" | ||
#include "barretenberg/serialize/msgpack.hpp" | ||
#include <cstdint> | ||
#include <vector> | ||
|
||
namespace acir_format { | ||
|
||
struct AES128Input { | ||
uint32_t witness; | ||
uint32_t num_bits; | ||
|
||
// For serialization, update with any new fields | ||
MSGPACK_FIELDS(witness, num_bits); | ||
friend bool operator==(AES128Input const& lhs, AES128Input const& rhs) = default; | ||
}; | ||
|
||
struct AES128Constraint { | ||
std::vector<AES128Input> inputs; | ||
std::array<AES128Input, 16> iv; | ||
std::array<AES128Input, 16> key; | ||
std::vector<uint32_t> outputs; | ||
|
||
// For serialization, update with any new fields | ||
MSGPACK_FIELDS(inputs, iv, key, outputs); | ||
friend bool operator==(AES128Constraint const& lhs, AES128Constraint const& rhs) = default; | ||
}; | ||
|
||
template <typename Builder> void create_aes128_constraints(Builder& builder, const AES128Constraint& constraint); | ||
|
||
} // namespace acir_format |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.