Skip to content

Commit

Permalink
chore: sync noir (#4025)
Browse files Browse the repository at this point in the history
subrepo:
  subdir:   "noir"
  merged:   "0f38b229f"
upstream:
  origin:   "https://github.com/noir-lang/noir"
  branch:   "aztec-packages"
  commit:   "0f38b229f"
git-subrepo:
  version:  "0.4.6"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "110b9eb"

Co-authored-by: kevaundray <[email protected]>
  • Loading branch information
2 people authored and AztecBot committed Jan 15, 2024
1 parent 0f38b22 commit 2d92108
Show file tree
Hide file tree
Showing 7 changed files with 3 additions and 215 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

114 changes: 1 addition & 113 deletions acvm-repo/acir/codegen/acir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,28 +765,8 @@ namespace Circuit {
static Brillig bincodeDeserialize(std::vector<uint8_t>);
};

struct QuotientDirective {
Circuit::Expression a;
Circuit::Expression b;
Circuit::Witness q;
Circuit::Witness r;
std::optional<Circuit::Expression> predicate;

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

struct Directive {

struct Quotient {
Circuit::QuotientDirective value;

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

struct ToLeRadix {
Circuit::Expression a;
std::vector<Circuit::Witness> b;
Expand All @@ -808,7 +788,7 @@ namespace Circuit {
static PermutationSort bincodeDeserialize(std::vector<uint8_t>);
};

std::variant<Quotient, ToLeRadix, PermutationSort> value;
std::variant<ToLeRadix, PermutationSort> value;

friend bool operator==(const Directive&, const Directive&);
std::vector<uint8_t> bincodeSerialize() const;
Expand Down Expand Up @@ -4188,44 +4168,6 @@ Circuit::Directive serde::Deserializable<Circuit::Directive>::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<uint8_t> Directive::Quotient::bincodeSerialize() const {
auto serializer = serde::BincodeSerializer();
serde::Serializable<Directive::Quotient>::serialize(*this, serializer);
return std::move(serializer).bytes();
}

inline Directive::Quotient Directive::Quotient::bincodeDeserialize(std::vector<uint8_t> input) {
auto deserializer = serde::BincodeDeserializer(input);
auto value = serde::Deserializable<Directive::Quotient>::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::Directive::Quotient>::serialize(const Circuit::Directive::Quotient &obj, Serializer &serializer) {
serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
}

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

namespace Circuit {

inline bool operator==(const Directive::ToLeRadix &lhs, const Directive::ToLeRadix &rhs) {
Expand Down Expand Up @@ -4990,60 +4932,6 @@ Circuit::PublicInputs serde::Deserializable<Circuit::PublicInputs>::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<uint8_t> QuotientDirective::bincodeSerialize() const {
auto serializer = serde::BincodeSerializer();
serde::Serializable<QuotientDirective>::serialize(*this, serializer);
return std::move(serializer).bytes();
}

inline QuotientDirective QuotientDirective::bincodeDeserialize(std::vector<uint8_t> input) {
auto deserializer = serde::BincodeDeserializer(input);
auto value = serde::Deserializable<QuotientDirective>::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::QuotientDirective>::serialize(const Circuit::QuotientDirective &obj, Serializer &serializer) {
serializer.increase_container_depth();
serde::Serializable<decltype(obj.a)>::serialize(obj.a, serializer);
serde::Serializable<decltype(obj.b)>::serialize(obj.b, serializer);
serde::Serializable<decltype(obj.q)>::serialize(obj.q, serializer);
serde::Serializable<decltype(obj.r)>::serialize(obj.r, serializer);
serde::Serializable<decltype(obj.predicate)>::serialize(obj.predicate, serializer);
serializer.decrease_container_depth();
}

template <>
template <typename Deserializer>
Circuit::QuotientDirective serde::Deserializable<Circuit::QuotientDirective>::deserialize(Deserializer &deserializer) {
deserializer.increase_container_depth();
Circuit::QuotientDirective obj;
obj.a = serde::Deserializable<decltype(obj.a)>::deserialize(deserializer);
obj.b = serde::Deserializable<decltype(obj.b)>::deserialize(deserializer);
obj.q = serde::Deserializable<decltype(obj.q)>::deserialize(deserializer);
obj.r = serde::Deserializable<decltype(obj.r)>::deserialize(deserializer);
obj.predicate = serde::Deserializable<decltype(obj.predicate)>::deserialize(deserializer);
deserializer.decrease_container_depth();
return obj;
}

namespace Circuit {

inline bool operator==(const RegisterIndex &lhs, const RegisterIndex &rhs) {
Expand Down
12 changes: 0 additions & 12 deletions acvm-repo/acir/src/circuit/directives.rs
Original file line number Diff line number Diff line change
@@ -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<Expression>,
}

#[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,
Expand Down
19 changes: 1 addition & 18 deletions acvm-repo/acir/src/circuit/opcodes.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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 ")?;

Check warning on line 51 in acvm-repo/acir/src/circuit/opcodes.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (TORADIX)
Expand Down
1 change: 0 additions & 1 deletion acvm-repo/acvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ repository.workspace = true

[dependencies]
num-bigint.workspace = true
num-traits.workspace = true
thiserror.workspace = true
tracing.workspace = true

Expand Down
4 changes: 0 additions & 4 deletions acvm-repo/acvm/src/compiler/transformers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
67 changes: 1 addition & 66 deletions acvm-repo/acvm/src/pwg/directives/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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());
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 2d92108

Please sign in to comment.