diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp index 52e4d5a0b55..c85f609bbd1 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp @@ -813,28 +813,8 @@ struct Brillig { static Brillig bincodeDeserialize(std::vector); }; -struct QuotientDirective { - Circuit::Expression a; - Circuit::Expression b; - Circuit::Witness q; - Circuit::Witness r; - std::optional predicate; - - friend bool operator==(const QuotientDirective&, const QuotientDirective&); - std::vector bincodeSerialize() const; - static QuotientDirective bincodeDeserialize(std::vector); -}; - struct Directive { - struct Quotient { - Circuit::QuotientDirective value; - - friend bool operator==(const Quotient&, const Quotient&); - std::vector bincodeSerialize() const; - static Quotient bincodeDeserialize(std::vector); - }; - struct ToLeRadix { Circuit::Expression a; std::vector b; @@ -856,7 +836,7 @@ struct Directive { static PermutationSort bincodeDeserialize(std::vector); }; - std::variant value; + std::variant value; friend bool operator==(const Directive&, const Directive&); std::vector bincodeSerialize() const; @@ -5038,53 +5018,6 @@ Circuit::Directive serde::Deserializable::deserialize(Deseri namespace Circuit { -inline bool operator==(const Directive::Quotient& lhs, const Directive::Quotient& rhs) -{ - if (!(lhs.value == rhs.value)) { - return false; - } - return true; -} - -inline std::vector Directive::Quotient::bincodeSerialize() const -{ - auto serializer = serde::BincodeSerializer(); - serde::Serializable::serialize(*this, serializer); - return std::move(serializer).bytes(); -} - -inline Directive::Quotient Directive::Quotient::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::Directive::Quotient& obj, - Serializer& serializer) -{ - serde::Serializable::serialize(obj.value, serializer); -} - -template <> -template -Circuit::Directive::Quotient serde::Deserializable::deserialize( - Deserializer& deserializer) -{ - Circuit::Directive::Quotient obj; - obj.value = serde::Deserializable::deserialize(deserializer); - return obj; -} - -namespace Circuit { - inline bool operator==(const Directive::ToLeRadix& lhs, const Directive::ToLeRadix& rhs) { if (!(lhs.a == rhs.a)) { @@ -6022,76 +5955,6 @@ Circuit::PublicInputs serde::Deserializable::deserialize( namespace Circuit { -inline bool operator==(const QuotientDirective& lhs, const QuotientDirective& rhs) -{ - if (!(lhs.a == rhs.a)) { - return false; - } - if (!(lhs.b == rhs.b)) { - return false; - } - if (!(lhs.q == rhs.q)) { - return false; - } - if (!(lhs.r == rhs.r)) { - return false; - } - if (!(lhs.predicate == rhs.predicate)) { - return false; - } - return true; -} - -inline std::vector QuotientDirective::bincodeSerialize() const -{ - auto serializer = serde::BincodeSerializer(); - serde::Serializable::serialize(*this, serializer); - return std::move(serializer).bytes(); -} - -inline QuotientDirective QuotientDirective::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::QuotientDirective& obj, - Serializer& serializer) -{ - serializer.increase_container_depth(); - serde::Serializable::serialize(obj.a, serializer); - serde::Serializable::serialize(obj.b, serializer); - serde::Serializable::serialize(obj.q, serializer); - serde::Serializable::serialize(obj.r, serializer); - serde::Serializable::serialize(obj.predicate, serializer); - serializer.decrease_container_depth(); -} - -template <> -template -Circuit::QuotientDirective serde::Deserializable::deserialize(Deserializer& deserializer) -{ - deserializer.increase_container_depth(); - Circuit::QuotientDirective obj; - obj.a = serde::Deserializable::deserialize(deserializer); - obj.b = serde::Deserializable::deserialize(deserializer); - obj.q = serde::Deserializable::deserialize(deserializer); - obj.r = serde::Deserializable::deserialize(deserializer); - obj.predicate = serde::Deserializable::deserialize(deserializer); - deserializer.decrease_container_depth(); - return obj; -} - -namespace Circuit { - inline bool operator==(const RegisterIndex& lhs, const RegisterIndex& rhs) { if (!(lhs.value == rhs.value)) { diff --git a/noir/Cargo.lock b/noir/Cargo.lock index 438d74b1b2d..1088bb63004 100644 --- a/noir/Cargo.lock +++ b/noir/Cargo.lock @@ -44,7 +44,6 @@ dependencies = [ "brillig_vm", "indexmap 1.9.3", "num-bigint", - "num-traits", "paste", "proptest", "rand 0.8.5", diff --git a/noir/acvm-repo/acir/codegen/acir.cpp b/noir/acvm-repo/acir/codegen/acir.cpp index 30f6e756337..9b74a8ea631 100644 --- a/noir/acvm-repo/acir/codegen/acir.cpp +++ b/noir/acvm-repo/acir/codegen/acir.cpp @@ -765,28 +765,8 @@ namespace Circuit { static Brillig bincodeDeserialize(std::vector); }; - struct QuotientDirective { - Circuit::Expression a; - Circuit::Expression b; - Circuit::Witness q; - Circuit::Witness r; - std::optional predicate; - - friend bool operator==(const QuotientDirective&, const QuotientDirective&); - std::vector bincodeSerialize() const; - static QuotientDirective bincodeDeserialize(std::vector); - }; - struct Directive { - struct Quotient { - Circuit::QuotientDirective value; - - friend bool operator==(const Quotient&, const Quotient&); - std::vector bincodeSerialize() const; - static Quotient bincodeDeserialize(std::vector); - }; - struct ToLeRadix { Circuit::Expression a; std::vector b; @@ -808,7 +788,7 @@ namespace Circuit { static PermutationSort bincodeDeserialize(std::vector); }; - std::variant value; + std::variant value; friend bool operator==(const Directive&, const Directive&); std::vector bincodeSerialize() const; @@ -4188,44 +4168,6 @@ Circuit::Directive serde::Deserializable::deserialize(Deseri return obj; } -namespace Circuit { - - inline bool operator==(const Directive::Quotient &lhs, const Directive::Quotient &rhs) { - if (!(lhs.value == rhs.value)) { return false; } - return true; - } - - inline std::vector Directive::Quotient::bincodeSerialize() const { - auto serializer = serde::BincodeSerializer(); - serde::Serializable::serialize(*this, serializer); - return std::move(serializer).bytes(); - } - - inline Directive::Quotient Directive::Quotient::bincodeDeserialize(std::vector input) { - auto deserializer = serde::BincodeDeserializer(input); - auto value = serde::Deserializable::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 -void serde::Serializable::serialize(const Circuit::Directive::Quotient &obj, Serializer &serializer) { - serde::Serializable::serialize(obj.value, serializer); -} - -template <> -template -Circuit::Directive::Quotient serde::Deserializable::deserialize(Deserializer &deserializer) { - Circuit::Directive::Quotient obj; - obj.value = serde::Deserializable::deserialize(deserializer); - return obj; -} - namespace Circuit { inline bool operator==(const Directive::ToLeRadix &lhs, const Directive::ToLeRadix &rhs) { @@ -4990,60 +4932,6 @@ Circuit::PublicInputs serde::Deserializable::deserialize( return obj; } -namespace Circuit { - - inline bool operator==(const QuotientDirective &lhs, const QuotientDirective &rhs) { - if (!(lhs.a == rhs.a)) { return false; } - if (!(lhs.b == rhs.b)) { return false; } - if (!(lhs.q == rhs.q)) { return false; } - if (!(lhs.r == rhs.r)) { return false; } - if (!(lhs.predicate == rhs.predicate)) { return false; } - return true; - } - - inline std::vector QuotientDirective::bincodeSerialize() const { - auto serializer = serde::BincodeSerializer(); - serde::Serializable::serialize(*this, serializer); - return std::move(serializer).bytes(); - } - - inline QuotientDirective QuotientDirective::bincodeDeserialize(std::vector input) { - auto deserializer = serde::BincodeDeserializer(input); - auto value = serde::Deserializable::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 -void serde::Serializable::serialize(const Circuit::QuotientDirective &obj, Serializer &serializer) { - serializer.increase_container_depth(); - serde::Serializable::serialize(obj.a, serializer); - serde::Serializable::serialize(obj.b, serializer); - serde::Serializable::serialize(obj.q, serializer); - serde::Serializable::serialize(obj.r, serializer); - serde::Serializable::serialize(obj.predicate, serializer); - serializer.decrease_container_depth(); -} - -template <> -template -Circuit::QuotientDirective serde::Deserializable::deserialize(Deserializer &deserializer) { - deserializer.increase_container_depth(); - Circuit::QuotientDirective obj; - obj.a = serde::Deserializable::deserialize(deserializer); - obj.b = serde::Deserializable::deserialize(deserializer); - obj.q = serde::Deserializable::deserialize(deserializer); - obj.r = serde::Deserializable::deserialize(deserializer); - obj.predicate = serde::Deserializable::deserialize(deserializer); - deserializer.decrease_container_depth(); - return obj; -} - namespace Circuit { inline bool operator==(const RegisterIndex &lhs, const RegisterIndex &rhs) { diff --git a/noir/acvm-repo/acir/src/circuit/directives.rs b/noir/acvm-repo/acir/src/circuit/directives.rs index c3a5b055f19..2486f4cfb83 100644 --- a/noir/acvm-repo/acir/src/circuit/directives.rs +++ b/noir/acvm-repo/acir/src/circuit/directives.rs @@ -1,23 +1,11 @@ use crate::native_types::{Expression, Witness}; use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct QuotientDirective { - pub a: Expression, - pub b: Expression, - pub q: Witness, - pub r: Witness, - pub predicate: Option, -} - #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] /// Directives do not apply any constraints. /// You can think of them as opcodes that allow one to use non-determinism /// In the future, this can be replaced with asm non-determinism blocks pub enum Directive { - //Performs euclidean division of a / b (as integers) and stores the quotient in q and the rest in r - Quotient(QuotientDirective), - //decomposition of a: a=\sum b[i]*radix^i where b is an array of witnesses < radix in little endian form ToLeRadix { a: Expression, diff --git a/noir/acvm-repo/acir/src/circuit/opcodes.rs b/noir/acvm-repo/acir/src/circuit/opcodes.rs index ac5ea0b8a69..5aab9d4d472 100644 --- a/noir/acvm-repo/acir/src/circuit/opcodes.rs +++ b/noir/acvm-repo/acir/src/circuit/opcodes.rs @@ -1,7 +1,4 @@ -use super::{ - brillig::Brillig, - directives::{Directive, QuotientDirective}, -}; +use super::{brillig::Brillig, directives::Directive}; use crate::native_types::{Expression, Witness}; use serde::{Deserialize, Serialize}; @@ -48,21 +45,7 @@ impl std::fmt::Display for Opcode { write!(f, " ]") } - Opcode::Directive(Directive::Quotient(QuotientDirective { a, b, q, r, predicate })) => { - write!(f, "DIR::QUOTIENT ")?; - if let Some(pred) = predicate { - writeln!(f, "PREDICATE = {pred}")?; - } - write!( - f, - "(out : _{}, (_{}, {}), _{})", - a, - q.witness_index(), - b, - r.witness_index() - ) - } Opcode::BlackBoxFuncCall(g) => write!(f, "{g}"), Opcode::Directive(Directive::ToLeRadix { a, b, radix: _ }) => { write!(f, "DIR::TORADIX ")?; diff --git a/noir/acvm-repo/acvm/Cargo.toml b/noir/acvm-repo/acvm/Cargo.toml index f5819d6fa34..be2391a3216 100644 --- a/noir/acvm-repo/acvm/Cargo.toml +++ b/noir/acvm-repo/acvm/Cargo.toml @@ -14,7 +14,6 @@ repository.workspace = true [dependencies] num-bigint.workspace = true -num-traits.workspace = true thiserror.workspace = true tracing.workspace = true diff --git a/noir/acvm-repo/acvm/src/compiler/transformers/mod.rs b/noir/acvm-repo/acvm/src/compiler/transformers/mod.rs index f8351364eb1..306ea1b7c12 100644 --- a/noir/acvm-repo/acvm/src/compiler/transformers/mod.rs +++ b/noir/acvm-repo/acvm/src/compiler/transformers/mod.rs @@ -150,10 +150,6 @@ pub(super) fn transform_internal( } Opcode::Directive(ref directive) => { match directive { - Directive::Quotient(quotient_directive) => { - transformer.mark_solvable(quotient_directive.q); - transformer.mark_solvable(quotient_directive.r); - } Directive::ToLeRadix { b, .. } => { for witness in b { transformer.mark_solvable(*witness); diff --git a/noir/acvm-repo/acvm/src/pwg/directives/mod.rs b/noir/acvm-repo/acvm/src/pwg/directives/mod.rs index cfc458dd611..4605168d98b 100644 --- a/noir/acvm-repo/acvm/src/pwg/directives/mod.rs +++ b/noir/acvm-repo/acvm/src/pwg/directives/mod.rs @@ -1,12 +1,7 @@ use std::cmp::Ordering; -use acir::{ - circuit::directives::{Directive, QuotientDirective}, - native_types::WitnessMap, - FieldElement, -}; +use acir::{circuit::directives::Directive, native_types::WitnessMap, FieldElement}; use num_bigint::BigUint; -use num_traits::Zero; use crate::OpcodeResolutionError; @@ -25,38 +20,6 @@ pub(super) fn solve_directives( directive: &Directive, ) -> Result<(), OpcodeResolutionError> { match directive { - Directive::Quotient(QuotientDirective { a, b, q, r, predicate }) => { - let val_a = get_value(a, initial_witness)?; - let val_b = get_value(b, initial_witness)?; - let int_a = BigUint::from_bytes_be(&val_a.to_be_bytes()); - let int_b = BigUint::from_bytes_be(&val_b.to_be_bytes()); - - // If the predicate is `None`, then we simply return the value 1 - // If the predicate is `Some` but we cannot find a value, then we return unresolved - let pred_value = match predicate { - Some(pred) => get_value(pred, initial_witness)?, - None => FieldElement::one(), - }; - - let (int_r, int_q) = if pred_value.is_zero() || int_b.is_zero() { - (BigUint::zero(), BigUint::zero()) - } else { - (&int_a % &int_b, &int_a / &int_b) - }; - - insert_value( - q, - FieldElement::from_be_bytes_reduce(&int_q.to_bytes_be()), - initial_witness, - )?; - insert_value( - r, - FieldElement::from_be_bytes_reduce(&int_r.to_bytes_be()), - initial_witness, - )?; - - Ok(()) - } Directive::ToLeRadix { a, b, radix } => { let value_a = get_value(a, initial_witness)?; let big_integer = BigUint::from_bytes_be(&value_a.to_be_bytes()); @@ -120,31 +83,3 @@ pub(super) fn solve_directives( } } } - -#[cfg(test)] -mod tests { - use acir::{ - circuit::directives::{Directive, QuotientDirective}, - native_types::{Expression, Witness, WitnessMap}, - FieldElement, - }; - - use super::solve_directives; - - #[test] - fn divisor_is_zero() { - let quotient_directive = QuotientDirective { - a: Expression::zero(), - b: Expression::zero(), - q: Witness(0), - r: Witness(0), - predicate: Some(Expression::one()), - }; - - let mut witness_map = WitnessMap::new(); - witness_map.insert(Witness(0), FieldElement::zero()); - - solve_directives(&mut witness_map, &Directive::Quotient(quotient_directive)) - .expect("expected 0/0 to return 0"); - } -}