From ce6f451de105e66512a60026ce51560d1647c521 Mon Sep 17 00:00:00 2001 From: guipublic <47281315+guipublic@users.noreply.github.com> Date: Mon, 5 Feb 2024 14:52:31 +0100 Subject: [PATCH] feat!: Add expression width into acir (#4014) Resolves Noir issue https://github.com/noir-lang/noir/issues/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 --- .../dsl/acir_format/serde/acir.hpp | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) diff --git a/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp b/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp index fe9c4e9f3..fc6ad6b3f 100644 --- a/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp +++ b/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp @@ -1078,6 +1078,29 @@ struct Opcode { static Opcode bincodeDeserialize(std::vector); }; +struct ExpressionWidth { + + struct Unbounded { + friend bool operator==(const Unbounded&, const Unbounded&); + std::vector bincodeSerialize() const; + static Unbounded bincodeDeserialize(std::vector); + }; + + struct Bounded { + uint64_t width; + + friend bool operator==(const Bounded&, const Bounded&); + std::vector bincodeSerialize() const; + static Bounded bincodeDeserialize(std::vector); + }; + + std::variant value; + + friend bool operator==(const ExpressionWidth&, const ExpressionWidth&); + std::vector bincodeSerialize() const; + static ExpressionWidth bincodeDeserialize(std::vector); +}; + struct OpcodeLocation { struct Acir { @@ -1115,6 +1138,7 @@ struct PublicInputs { struct Circuit { uint32_t current_witness_index; std::vector opcodes; + ExpressionWidth expression_width; std::vector private_parameters; PublicInputs public_parameters; PublicInputs return_values; @@ -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; } @@ -5977,6 +6004,7 @@ void serde::Serializable::serialize(const Circuit::Circuit& ob serializer.increase_container_depth(); serde::Serializable::serialize(obj.current_witness_index, serializer); serde::Serializable::serialize(obj.opcodes, serializer); + serde::Serializable::serialize(obj.expression_width, serializer); serde::Serializable::serialize(obj.private_parameters, serializer); serde::Serializable::serialize(obj.public_parameters, serializer); serde::Serializable::serialize(obj.return_values, serializer); @@ -5993,6 +6021,7 @@ Circuit::Circuit serde::Deserializable::deserialize(Deserializ Circuit::Circuit obj; obj.current_witness_index = serde::Deserializable::deserialize(deserializer); obj.opcodes = serde::Deserializable::deserialize(deserializer); + obj.expression_width = serde::Deserializable::deserialize(deserializer); obj.private_parameters = serde::Deserializable::deserialize(deserializer); obj.public_parameters = serde::Deserializable::deserialize(deserializer); obj.return_values = serde::Deserializable::deserialize(deserializer); @@ -6231,6 +6260,144 @@ Circuit::Expression serde::Deserializable::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 ExpressionWidth::bincodeSerialize() const +{ + auto serializer = serde::BincodeSerializer(); + serde::Serializable::serialize(*this, serializer); + return std::move(serializer).bytes(); +} + +inline ExpressionWidth ExpressionWidth::bincodeDeserialize(std::vector input) +{ + auto deserializer = serde::BincodeDeserializer(input); + auto value = serde::Deserializable::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 +void serde::Serializable::serialize(const Circuit::ExpressionWidth& obj, + Serializer& serializer) +{ + serializer.increase_container_depth(); + serde::Serializable::serialize(obj.value, serializer); + serializer.decrease_container_depth(); +} + +template <> +template +Circuit::ExpressionWidth serde::Deserializable::deserialize(Deserializer& deserializer) +{ + deserializer.increase_container_depth(); + Circuit::ExpressionWidth obj; + obj.value = serde::Deserializable::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 ExpressionWidth::Unbounded::bincodeSerialize() const +{ + auto serializer = serde::BincodeSerializer(); + serde::Serializable::serialize(*this, serializer); + return std::move(serializer).bytes(); +} + +inline ExpressionWidth::Unbounded ExpressionWidth::Unbounded::bincodeDeserialize(std::vector input) +{ + auto deserializer = serde::BincodeDeserializer(input); + auto value = serde::Deserializable::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 +void serde::Serializable::serialize(const Circuit::ExpressionWidth::Unbounded& obj, + Serializer& serializer) +{} + +template <> +template +Circuit::ExpressionWidth::Unbounded serde::Deserializable::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 ExpressionWidth::Bounded::bincodeSerialize() const +{ + auto serializer = serde::BincodeSerializer(); + serde::Serializable::serialize(*this, serializer); + return std::move(serializer).bytes(); +} + +inline ExpressionWidth::Bounded ExpressionWidth::Bounded::bincodeDeserialize(std::vector input) +{ + auto deserializer = serde::BincodeDeserializer(input); + auto value = serde::Deserializable::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 +void serde::Serializable::serialize(const Circuit::ExpressionWidth::Bounded& obj, + Serializer& serializer) +{ + serde::Serializable::serialize(obj.width, serializer); +} + +template <> +template +Circuit::ExpressionWidth::Bounded serde::Deserializable::deserialize( + Deserializer& deserializer) +{ + Circuit::ExpressionWidth::Bounded obj; + obj.width = serde::Deserializable::deserialize(deserializer); + return obj; +} + +namespace Circuit { + inline bool operator==(const FunctionInput& lhs, const FunctionInput& rhs) { if (!(lhs.witness == rhs.witness)) {