Skip to content

Commit

Permalink
feat!: start using fixed size arrays for black box functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Apr 8, 2024
1 parent 9a97a65 commit 363072e
Show file tree
Hide file tree
Showing 29 changed files with 314 additions and 276 deletions.
10 changes: 10 additions & 0 deletions avm-transpiler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Blake2sInput {

struct Blake2sConstraint {
std::vector<Blake2sInput> inputs;
std::vector<uint32_t> result;
std::array<uint32_t, 32> result;

// For serialization, update with any new fields
MSGPACK_FIELDS(inputs, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Blake3Input {

struct Blake3Constraint {
std::vector<Blake3Input> inputs;
std::vector<uint32_t> result;
std::array<uint32_t, 32> result;

// For serialization, update with any new fields
MSGPACK_FIELDS(inputs, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace acir_format {
using namespace bb::plonk;

template <typename Builder>
crypto::ecdsa_signature ecdsa_convert_signature(Builder& builder, std::vector<uint32_t> signature)
crypto::ecdsa_signature ecdsa_convert_signature(Builder& builder, std::array<uint32_t, 64> signature)
{

crypto::ecdsa_signature signature_cr;
Expand Down Expand Up @@ -63,9 +63,9 @@ secp256k1_ct::g1_ct ecdsa_convert_inputs(Builder* ctx, const secp256k1::g1::affi
// vector of bytes here, assumes that the witness indices point to a field element which can be represented
// with just a byte.
// notice that this function truncates each field_element to a byte
template <typename Builder>
bb::stdlib::byte_array<Builder> ecdsa_vector_of_bytes_to_byte_array(Builder& builder,
std::vector<uint32_t> vector_of_bytes)
template <std::size_t SIZE, typename Builder>
bb::stdlib::byte_array<Builder> ecdsa_array_of_bytes_to_byte_array(Builder& builder,
std::array<uint32_t, SIZE> vector_of_bytes)
{
using byte_array_ct = bb::stdlib::byte_array<Builder>;
using field_ct = bb::stdlib::field_t<Builder>;
Expand Down Expand Up @@ -106,9 +106,9 @@ void create_ecdsa_k1_verify_constraints(Builder& builder,

auto new_sig = ecdsa_convert_signature(builder, input.signature);

byte_array_ct message = ecdsa_vector_of_bytes_to_byte_array(builder, input.hashed_message);
auto pub_key_x_byte_arr = ecdsa_vector_of_bytes_to_byte_array(builder, input.pub_x_indices);
auto pub_key_y_byte_arr = ecdsa_vector_of_bytes_to_byte_array(builder, input.pub_y_indices);
byte_array_ct message = ecdsa_array_of_bytes_to_byte_array(builder, input.hashed_message);
auto pub_key_x_byte_arr = ecdsa_array_of_bytes_to_byte_array(builder, input.pub_x_indices);
auto pub_key_y_byte_arr = ecdsa_array_of_bytes_to_byte_array(builder, input.pub_y_indices);

auto pub_key_x_fq = typename secp256k1_ct::fq_ct(pub_key_x_byte_arr);
auto pub_key_y_fq = typename secp256k1_ct::fq_ct(pub_key_y_byte_arr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ namespace acir_format {

struct EcdsaSecp256k1Constraint {
// This is the byte representation of the hashed message.
std::vector<uint32_t> hashed_message;
std::array<uint32_t, 32> hashed_message;

// This is the computed signature
//
std::vector<uint32_t> signature;
std::array<uint32_t, 64> signature;

// This is the supposed public key which signed the
// message, giving rise to the signature.
// Since Fr does not have enough bits to represent
// the prime field in secp256k1, a byte array is used.
// Can also use low and hi where lo=128 bits
std::vector<uint32_t> pub_x_indices;
std::vector<uint32_t> pub_y_indices;
std::array<uint32_t, 32> pub_x_indices;
std::array<uint32_t, 32> pub_y_indices;

// This is the result of verifying the signature
uint32_t result;
Expand All @@ -38,10 +38,10 @@ void create_ecdsa_k1_verify_constraints(Builder& builder,
template <typename Builder> void dummy_ecdsa_constraint(Builder& builder, EcdsaSecp256k1Constraint const& input);

template <typename Builder>
crypto::ecdsa_signature ecdsa_convert_signature(Builder& builder, std::vector<uint32_t> signature);
crypto::ecdsa_signature ecdsa_convert_signature(Builder& builder, std::array<uint32_t, 64> signature);
witness_ct ecdsa_index_to_witness(Builder& builder, uint32_t index);
template <typename Builder>
bb::stdlib::byte_array<Builder> ecdsa_vector_of_bytes_to_byte_array(Builder& builder,
std::vector<uint32_t> vector_of_bytes);
template <std::size_t SIZE, typename Builder>
bb::stdlib::byte_array<Builder> ecdsa_array_of_bytes_to_byte_array(Builder& builder,
std::array<uint32_t, SIZE> vector_of_bytes);

} // namespace acir_format
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ void create_ecdsa_r1_verify_constraints(Builder& builder,

auto new_sig = ecdsa_convert_signature(builder, input.signature);

auto message = ecdsa_vector_of_bytes_to_byte_array(builder, input.hashed_message);
auto pub_key_x_byte_arr = ecdsa_vector_of_bytes_to_byte_array(builder, input.pub_x_indices);
auto pub_key_y_byte_arr = ecdsa_vector_of_bytes_to_byte_array(builder, input.pub_y_indices);
auto message = ecdsa_array_of_bytes_to_byte_array(builder, input.hashed_message);
auto pub_key_x_byte_arr = ecdsa_array_of_bytes_to_byte_array(builder, input.pub_x_indices);
auto pub_key_y_byte_arr = ecdsa_array_of_bytes_to_byte_array(builder, input.pub_y_indices);

auto pub_key_x_fq = typename secp256r1_ct::fq_ct(pub_key_x_byte_arr);
auto pub_key_y_fq = typename secp256r1_ct::fq_ct(pub_key_y_byte_arr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ namespace acir_format {

struct EcdsaSecp256r1Constraint {
// This is the byte representation of the hashed message.
std::vector<uint32_t> hashed_message;
std::array<uint32_t, 32> hashed_message;

// This is the supposed public key which signed the
// message, giving rise to the signature.
// Since Fr does not have enough bits to represent
// the prime field in secp256r1, a byte array is used.
// Can also use low and hi where lo=128 bits
std::vector<uint32_t> pub_x_indices;
std::vector<uint32_t> pub_y_indices;
std::array<uint32_t, 32> pub_x_indices;
std::array<uint32_t, 32> pub_y_indices;

// This is the result of verifying the signature
uint32_t result;

// This is the computed signature
//
std::vector<uint32_t> signature;
std::array<uint32_t, 64> signature;

friend bool operator==(EcdsaSecp256r1Constraint const& lhs, EcdsaSecp256r1Constraint const& rhs) = default;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ struct HashInput {
};

struct Keccakf1600 {
std::vector<uint32_t> state;
std::vector<uint32_t> result;
std::array<uint32_t, 25> state;
std::array<uint32_t, 25> result;

// For serialization, update with any new fields
MSGPACK_FIELDS(state, result);
Expand All @@ -26,7 +26,7 @@ struct Keccakf1600 {

struct KeccakConstraint {
std::vector<HashInput> inputs;
std::vector<uint32_t> result;
std::array<uint32_t, 32> result;
uint32_t var_message_size;

// For serialization, update with any new fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace acir_format {
using namespace bb::stdlib;

template <typename Builder>
crypto::schnorr_signature convert_signature(Builder& builder, std::vector<uint32_t> signature)
crypto::schnorr_signature convert_signature(Builder& builder, std::array<uint32_t, 64> signature)
{

crypto::schnorr_signature signature_cr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct SchnorrConstraint {

// This is the computed signature
//
std::vector<uint32_t> signature;
std::array<uint32_t, 64> signature;

friend bool operator==(SchnorrConstraint const& lhs, SchnorrConstraint const& rhs) = default;
};
Expand Down
36 changes: 18 additions & 18 deletions barretenberg/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct BlackBoxFuncCall {

struct SHA256 {
std::vector<Program::FunctionInput> inputs;
std::vector<Program::Witness> outputs;
std::array<Program::Witness, 32> outputs;

friend bool operator==(const SHA256&, const SHA256&);
std::vector<uint8_t> bincodeSerialize() const;
Expand All @@ -63,7 +63,7 @@ struct BlackBoxFuncCall {

struct Blake2s {
std::vector<Program::FunctionInput> inputs;
std::vector<Program::Witness> outputs;
std::array<Program::Witness, 32> outputs;

friend bool operator==(const Blake2s&, const Blake2s&);
std::vector<uint8_t> bincodeSerialize() const;
Expand All @@ -72,7 +72,7 @@ struct BlackBoxFuncCall {

struct Blake3 {
std::vector<Program::FunctionInput> inputs;
std::vector<Program::Witness> outputs;
std::array<Program::Witness, 32> outputs;

friend bool operator==(const Blake3&, const Blake3&);
std::vector<uint8_t> bincodeSerialize() const;
Expand All @@ -82,7 +82,7 @@ struct BlackBoxFuncCall {
struct SchnorrVerify {
Program::FunctionInput public_key_x;
Program::FunctionInput public_key_y;
std::vector<Program::FunctionInput> signature;
std::array<Program::FunctionInput, 64> signature;
std::vector<Program::FunctionInput> message;
Program::Witness output;

Expand Down Expand Up @@ -112,10 +112,10 @@ struct BlackBoxFuncCall {
};

struct EcdsaSecp256k1 {
std::vector<Program::FunctionInput> public_key_x;
std::vector<Program::FunctionInput> public_key_y;
std::vector<Program::FunctionInput> signature;
std::vector<Program::FunctionInput> hashed_message;
std::array<Program::FunctionInput, 32> public_key_x;
std::array<Program::FunctionInput, 32> public_key_y;
std::array<Program::FunctionInput, 64> signature;
std::array<Program::FunctionInput, 32> hashed_message;
Program::Witness output;

friend bool operator==(const EcdsaSecp256k1&, const EcdsaSecp256k1&);
Expand All @@ -124,10 +124,10 @@ struct BlackBoxFuncCall {
};

struct EcdsaSecp256r1 {
std::vector<Program::FunctionInput> public_key_x;
std::vector<Program::FunctionInput> public_key_y;
std::vector<Program::FunctionInput> signature;
std::vector<Program::FunctionInput> hashed_message;
std::array<Program::FunctionInput, 32> public_key_x;
std::array<Program::FunctionInput, 32> public_key_y;
std::array<Program::FunctionInput, 64> signature;
std::array<Program::FunctionInput, 32> hashed_message;
Program::Witness output;

friend bool operator==(const EcdsaSecp256r1&, const EcdsaSecp256r1&);
Expand Down Expand Up @@ -160,16 +160,16 @@ struct BlackBoxFuncCall {
struct Keccak256 {
std::vector<Program::FunctionInput> inputs;
Program::FunctionInput var_message_size;
std::vector<Program::Witness> outputs;
std::array<Program::Witness, 32> outputs;

friend bool operator==(const Keccak256&, const Keccak256&);
std::vector<uint8_t> bincodeSerialize() const;
static Keccak256 bincodeDeserialize(std::vector<uint8_t>);
};

struct Keccakf1600 {
std::vector<Program::FunctionInput> inputs;
std::vector<Program::Witness> outputs;
std::array<Program::FunctionInput, 25> inputs;
std::array<Program::Witness, 25> outputs;

friend bool operator==(const Keccakf1600&, const Keccakf1600&);
std::vector<uint8_t> bincodeSerialize() const;
Expand Down Expand Up @@ -257,9 +257,9 @@ struct BlackBoxFuncCall {
};

struct Sha256Compression {
std::vector<Program::FunctionInput> inputs;
std::vector<Program::FunctionInput> hash_values;
std::vector<Program::Witness> outputs;
std::array<Program::FunctionInput, 16> inputs;
std::array<Program::FunctionInput, 8> hash_values;
std::array<Program::Witness, 8> outputs;

friend bool operator==(const Sha256Compression&, const Sha256Compression&);
std::vector<uint8_t> bincodeSerialize() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ struct Sha256Input {

struct Sha256Constraint {
std::vector<Sha256Input> inputs;
std::vector<uint32_t> result;
std::array<uint32_t, 32> result;

friend bool operator==(Sha256Constraint const& lhs, Sha256Constraint const& rhs) = default;
// for serialization, update with any new fields
MSGPACK_FIELDS(inputs, result);
};

struct Sha256Compression {
std::vector<Sha256Input> inputs;
std::vector<Sha256Input> hash_values;
std::vector<uint32_t> result;
std::array<Sha256Input, 16> inputs;
std::array<Sha256Input, 8> hash_values;
std::array<uint32_t, 8> result;

friend bool operator==(Sha256Compression const& lhs, Sha256Compression const& rhs) = default;
// for serialization, update with any new fields
Expand Down
10 changes: 10 additions & 0 deletions noir/noir-repo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions noir/noir-repo/acvm-repo/acir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ thiserror.workspace = true
flate2.workspace = true
bincode.workspace = true
base64.workspace = true
serde-big-array = "0.5.1"

[dev-dependencies]
serde_json = "1.0"
Expand Down
Loading

0 comments on commit 363072e

Please sign in to comment.