Skip to content

Commit

Permalink
feat!: Add expression width into acir (AztecProtocol#4014)
Browse files Browse the repository at this point in the history
Resolves Noir issue noir-lang/noir#4012, by
adding ExpressionWidth into acir.
The PR simply account for this new field and update the serialisation.
It also set the width during csat transformation phase.

---------

Co-authored-by: kevaundray <kevtheappdev@gmail.com>
  • Loading branch information
guipublic and kevaundray authored Feb 5, 2024
1 parent 6ef2e59 commit 33f4bef
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 52 deletions.
167 changes: 167 additions & 0 deletions barretenberg/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,29 @@ struct Opcode {
static Opcode bincodeDeserialize(std::vector<uint8_t>);
};

struct ExpressionWidth {

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

struct Bounded {
uint64_t width;

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

std::variant<Unbounded, Bounded> value;

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

struct OpcodeLocation {

struct Acir {
Expand Down Expand Up @@ -1115,6 +1138,7 @@ struct PublicInputs {
struct Circuit {
uint32_t current_witness_index;
std::vector<Opcode> opcodes;
ExpressionWidth expression_width;
std::vector<Witness> private_parameters;
PublicInputs public_parameters;
PublicInputs return_values;
Expand Down Expand Up @@ -5933,6 +5957,9 @@ inline bool operator==(const Circuit& lhs, const Circuit& rhs)
if (!(lhs.opcodes == rhs.opcodes)) {
return false;
}
if (!(lhs.expression_width == rhs.expression_width)) {
return false;
}
if (!(lhs.private_parameters == rhs.private_parameters)) {
return false;
}
Expand Down Expand Up @@ -5977,6 +6004,7 @@ void serde::Serializable<Circuit::Circuit>::serialize(const Circuit::Circuit& ob
serializer.increase_container_depth();
serde::Serializable<decltype(obj.current_witness_index)>::serialize(obj.current_witness_index, serializer);
serde::Serializable<decltype(obj.opcodes)>::serialize(obj.opcodes, serializer);
serde::Serializable<decltype(obj.expression_width)>::serialize(obj.expression_width, serializer);
serde::Serializable<decltype(obj.private_parameters)>::serialize(obj.private_parameters, serializer);
serde::Serializable<decltype(obj.public_parameters)>::serialize(obj.public_parameters, serializer);
serde::Serializable<decltype(obj.return_values)>::serialize(obj.return_values, serializer);
Expand All @@ -5993,6 +6021,7 @@ Circuit::Circuit serde::Deserializable<Circuit::Circuit>::deserialize(Deserializ
Circuit::Circuit obj;
obj.current_witness_index = serde::Deserializable<decltype(obj.current_witness_index)>::deserialize(deserializer);
obj.opcodes = serde::Deserializable<decltype(obj.opcodes)>::deserialize(deserializer);
obj.expression_width = serde::Deserializable<decltype(obj.expression_width)>::deserialize(deserializer);
obj.private_parameters = serde::Deserializable<decltype(obj.private_parameters)>::deserialize(deserializer);
obj.public_parameters = serde::Deserializable<decltype(obj.public_parameters)>::deserialize(deserializer);
obj.return_values = serde::Deserializable<decltype(obj.return_values)>::deserialize(deserializer);
Expand Down Expand Up @@ -6231,6 +6260,144 @@ Circuit::Expression serde::Deserializable<Circuit::Expression>::deserialize(Dese

namespace Circuit {

inline bool operator==(const ExpressionWidth& lhs, const ExpressionWidth& rhs)
{
if (!(lhs.value == rhs.value)) {
return false;
}
return true;
}

inline std::vector<uint8_t> ExpressionWidth::bincodeSerialize() const
{
auto serializer = serde::BincodeSerializer();
serde::Serializable<ExpressionWidth>::serialize(*this, serializer);
return std::move(serializer).bytes();
}

inline ExpressionWidth ExpressionWidth::bincodeDeserialize(std::vector<uint8_t> input)
{
auto deserializer = serde::BincodeDeserializer(input);
auto value = serde::Deserializable<ExpressionWidth>::deserialize(deserializer);
if (deserializer.get_buffer_offset() < input.size()) {
throw_or_abort("Some input bytes were not read");
}
return value;
}

} // end of namespace Circuit

template <>
template <typename Serializer>
void serde::Serializable<Circuit::ExpressionWidth>::serialize(const Circuit::ExpressionWidth& obj,
Serializer& serializer)
{
serializer.increase_container_depth();
serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
serializer.decrease_container_depth();
}

template <>
template <typename Deserializer>
Circuit::ExpressionWidth serde::Deserializable<Circuit::ExpressionWidth>::deserialize(Deserializer& deserializer)
{
deserializer.increase_container_depth();
Circuit::ExpressionWidth obj;
obj.value = serde::Deserializable<decltype(obj.value)>::deserialize(deserializer);
deserializer.decrease_container_depth();
return obj;
}

namespace Circuit {

inline bool operator==(const ExpressionWidth::Unbounded& lhs, const ExpressionWidth::Unbounded& rhs)
{
return true;
}

inline std::vector<uint8_t> ExpressionWidth::Unbounded::bincodeSerialize() const
{
auto serializer = serde::BincodeSerializer();
serde::Serializable<ExpressionWidth::Unbounded>::serialize(*this, serializer);
return std::move(serializer).bytes();
}

inline ExpressionWidth::Unbounded ExpressionWidth::Unbounded::bincodeDeserialize(std::vector<uint8_t> input)
{
auto deserializer = serde::BincodeDeserializer(input);
auto value = serde::Deserializable<ExpressionWidth::Unbounded>::deserialize(deserializer);
if (deserializer.get_buffer_offset() < input.size()) {
throw_or_abort("Some input bytes were not read");
}
return value;
}

} // end of namespace Circuit

template <>
template <typename Serializer>
void serde::Serializable<Circuit::ExpressionWidth::Unbounded>::serialize(const Circuit::ExpressionWidth::Unbounded& obj,
Serializer& serializer)
{}

template <>
template <typename Deserializer>
Circuit::ExpressionWidth::Unbounded serde::Deserializable<Circuit::ExpressionWidth::Unbounded>::deserialize(
Deserializer& deserializer)
{
Circuit::ExpressionWidth::Unbounded obj;
return obj;
}

namespace Circuit {

inline bool operator==(const ExpressionWidth::Bounded& lhs, const ExpressionWidth::Bounded& rhs)
{
if (!(lhs.width == rhs.width)) {
return false;
}
return true;
}

inline std::vector<uint8_t> ExpressionWidth::Bounded::bincodeSerialize() const
{
auto serializer = serde::BincodeSerializer();
serde::Serializable<ExpressionWidth::Bounded>::serialize(*this, serializer);
return std::move(serializer).bytes();
}

inline ExpressionWidth::Bounded ExpressionWidth::Bounded::bincodeDeserialize(std::vector<uint8_t> input)
{
auto deserializer = serde::BincodeDeserializer(input);
auto value = serde::Deserializable<ExpressionWidth::Bounded>::deserialize(deserializer);
if (deserializer.get_buffer_offset() < input.size()) {
throw_or_abort("Some input bytes were not read");
}
return value;
}

} // end of namespace Circuit

template <>
template <typename Serializer>
void serde::Serializable<Circuit::ExpressionWidth::Bounded>::serialize(const Circuit::ExpressionWidth::Bounded& obj,
Serializer& serializer)
{
serde::Serializable<decltype(obj.width)>::serialize(obj.width, serializer);
}

template <>
template <typename Deserializer>
Circuit::ExpressionWidth::Bounded serde::Deserializable<Circuit::ExpressionWidth::Bounded>::deserialize(
Deserializer& deserializer)
{
Circuit::ExpressionWidth::Bounded obj;
obj.width = serde::Deserializable<decltype(obj.width)>::deserialize(deserializer);
return obj;
}

namespace Circuit {

inline bool operator==(const FunctionInput& lhs, const FunctionInput& rhs)
{
if (!(lhs.witness == rhs.witness)) {
Expand Down
142 changes: 142 additions & 0 deletions noir/acvm-repo/acir/codegen/acir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,29 @@ namespace Circuit {
static Opcode bincodeDeserialize(std::vector<uint8_t>);
};

struct ExpressionWidth {

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

struct Bounded {
uint64_t width;

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

std::variant<Unbounded, Bounded> value;

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

struct OpcodeLocation {

struct Acir {
Expand Down Expand Up @@ -1054,6 +1077,7 @@ namespace Circuit {
struct Circuit {
uint32_t current_witness_index;
std::vector<Circuit::Opcode> opcodes;
Circuit::ExpressionWidth expression_width;
std::vector<Circuit::Witness> private_parameters;
Circuit::PublicInputs public_parameters;
Circuit::PublicInputs return_values;
Expand Down Expand Up @@ -4890,6 +4914,7 @@ namespace Circuit {
inline bool operator==(const Circuit &lhs, const Circuit &rhs) {
if (!(lhs.current_witness_index == rhs.current_witness_index)) { return false; }
if (!(lhs.opcodes == rhs.opcodes)) { return false; }
if (!(lhs.expression_width == rhs.expression_width)) { return false; }
if (!(lhs.private_parameters == rhs.private_parameters)) { return false; }
if (!(lhs.public_parameters == rhs.public_parameters)) { return false; }
if (!(lhs.return_values == rhs.return_values)) { return false; }
Expand Down Expand Up @@ -4921,6 +4946,7 @@ void serde::Serializable<Circuit::Circuit>::serialize(const Circuit::Circuit &ob
serializer.increase_container_depth();
serde::Serializable<decltype(obj.current_witness_index)>::serialize(obj.current_witness_index, serializer);
serde::Serializable<decltype(obj.opcodes)>::serialize(obj.opcodes, serializer);
serde::Serializable<decltype(obj.expression_width)>::serialize(obj.expression_width, serializer);
serde::Serializable<decltype(obj.private_parameters)>::serialize(obj.private_parameters, serializer);
serde::Serializable<decltype(obj.public_parameters)>::serialize(obj.public_parameters, serializer);
serde::Serializable<decltype(obj.return_values)>::serialize(obj.return_values, serializer);
Expand All @@ -4936,6 +4962,7 @@ Circuit::Circuit serde::Deserializable<Circuit::Circuit>::deserialize(Deserializ
Circuit::Circuit obj;
obj.current_witness_index = serde::Deserializable<decltype(obj.current_witness_index)>::deserialize(deserializer);
obj.opcodes = serde::Deserializable<decltype(obj.opcodes)>::deserialize(deserializer);
obj.expression_width = serde::Deserializable<decltype(obj.expression_width)>::deserialize(deserializer);
obj.private_parameters = serde::Deserializable<decltype(obj.private_parameters)>::deserialize(deserializer);
obj.public_parameters = serde::Deserializable<decltype(obj.public_parameters)>::deserialize(deserializer);
obj.return_values = serde::Deserializable<decltype(obj.return_values)>::deserialize(deserializer);
Expand Down Expand Up @@ -5126,6 +5153,121 @@ Circuit::Expression serde::Deserializable<Circuit::Expression>::deserialize(Dese
return obj;
}

namespace Circuit {

inline bool operator==(const ExpressionWidth &lhs, const ExpressionWidth &rhs) {
if (!(lhs.value == rhs.value)) { return false; }
return true;
}

inline std::vector<uint8_t> ExpressionWidth::bincodeSerialize() const {
auto serializer = serde::BincodeSerializer();
serde::Serializable<ExpressionWidth>::serialize(*this, serializer);
return std::move(serializer).bytes();
}

inline ExpressionWidth ExpressionWidth::bincodeDeserialize(std::vector<uint8_t> input) {
auto deserializer = serde::BincodeDeserializer(input);
auto value = serde::Deserializable<ExpressionWidth>::deserialize(deserializer);
if (deserializer.get_buffer_offset() < input.size()) {
throw serde::deserialization_error("Some input bytes were not read");
}
return value;
}

} // end of namespace Circuit

template <>
template <typename Serializer>
void serde::Serializable<Circuit::ExpressionWidth>::serialize(const Circuit::ExpressionWidth &obj, Serializer &serializer) {
serializer.increase_container_depth();
serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
serializer.decrease_container_depth();
}

template <>
template <typename Deserializer>
Circuit::ExpressionWidth serde::Deserializable<Circuit::ExpressionWidth>::deserialize(Deserializer &deserializer) {
deserializer.increase_container_depth();
Circuit::ExpressionWidth obj;
obj.value = serde::Deserializable<decltype(obj.value)>::deserialize(deserializer);
deserializer.decrease_container_depth();
return obj;
}

namespace Circuit {

inline bool operator==(const ExpressionWidth::Unbounded &lhs, const ExpressionWidth::Unbounded &rhs) {
return true;
}

inline std::vector<uint8_t> ExpressionWidth::Unbounded::bincodeSerialize() const {
auto serializer = serde::BincodeSerializer();
serde::Serializable<ExpressionWidth::Unbounded>::serialize(*this, serializer);
return std::move(serializer).bytes();
}

inline ExpressionWidth::Unbounded ExpressionWidth::Unbounded::bincodeDeserialize(std::vector<uint8_t> input) {
auto deserializer = serde::BincodeDeserializer(input);
auto value = serde::Deserializable<ExpressionWidth::Unbounded>::deserialize(deserializer);
if (deserializer.get_buffer_offset() < input.size()) {
throw serde::deserialization_error("Some input bytes were not read");
}
return value;
}

} // end of namespace Circuit

template <>
template <typename Serializer>
void serde::Serializable<Circuit::ExpressionWidth::Unbounded>::serialize(const Circuit::ExpressionWidth::Unbounded &obj, Serializer &serializer) {
}

template <>
template <typename Deserializer>
Circuit::ExpressionWidth::Unbounded serde::Deserializable<Circuit::ExpressionWidth::Unbounded>::deserialize(Deserializer &deserializer) {
Circuit::ExpressionWidth::Unbounded obj;
return obj;
}

namespace Circuit {

inline bool operator==(const ExpressionWidth::Bounded &lhs, const ExpressionWidth::Bounded &rhs) {
if (!(lhs.width == rhs.width)) { return false; }
return true;
}

inline std::vector<uint8_t> ExpressionWidth::Bounded::bincodeSerialize() const {
auto serializer = serde::BincodeSerializer();
serde::Serializable<ExpressionWidth::Bounded>::serialize(*this, serializer);
return std::move(serializer).bytes();
}

inline ExpressionWidth::Bounded ExpressionWidth::Bounded::bincodeDeserialize(std::vector<uint8_t> input) {
auto deserializer = serde::BincodeDeserializer(input);
auto value = serde::Deserializable<ExpressionWidth::Bounded>::deserialize(deserializer);
if (deserializer.get_buffer_offset() < input.size()) {
throw serde::deserialization_error("Some input bytes were not read");
}
return value;
}

} // end of namespace Circuit

template <>
template <typename Serializer>
void serde::Serializable<Circuit::ExpressionWidth::Bounded>::serialize(const Circuit::ExpressionWidth::Bounded &obj, Serializer &serializer) {
serde::Serializable<decltype(obj.width)>::serialize(obj.width, serializer);
}

template <>
template <typename Deserializer>
Circuit::ExpressionWidth::Bounded serde::Deserializable<Circuit::ExpressionWidth::Bounded>::deserialize(Deserializer &deserializer) {
Circuit::ExpressionWidth::Bounded obj;
obj.width = serde::Deserializable<decltype(obj.width)>::deserialize(deserializer);
return obj;
}

namespace Circuit {

inline bool operator==(const FunctionInput &lhs, const FunctionInput &rhs) {
Expand Down
Loading

0 comments on commit 33f4bef

Please sign in to comment.