Skip to content

Commit

Permalink
feat: Sync aztec-packages (#4011)
Browse files Browse the repository at this point in the history
Development from Aztec.

---------

Co-authored-by: ludamad <adam.domurad@gmail.com>
Co-authored-by: ludamad <adam@aztecprotocol.com>
Co-authored-by: kevaundray <kevtheappdev@gmail.com>
Co-authored-by: sirasistant <sirasistant@gmail.com>
Co-authored-by: Gregorio Juliana <gregojquiros@gmail.com>
Co-authored-by: Tom French <tom@tomfren.ch>
Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
Co-authored-by: Jan Beneš <janbenes1234@gmail.com>
Co-authored-by: Charlie Lye <karl.lye@gmail.com>
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
Co-authored-by: ledwards2225 <l.edwards.d@gmail.com>
Co-authored-by: ledwards2225 <98505400+ledwards2225@users.noreply.github.com>
  • Loading branch information
13 people authored Jan 17, 2024
1 parent 71d6514 commit fee2452
Show file tree
Hide file tree
Showing 43 changed files with 811 additions and 275 deletions.
3 changes: 3 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ plugins:
spec: "@yarnpkg/plugin-workspace-tools"

yarnPath: .yarn/releases/yarn-3.6.3.cjs
logFilters:
- code: YN0013
level: discard
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.

19 changes: 10 additions & 9 deletions Dockerfile.packages
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ FROM rust:alpine3.17
RUN apk update \
&& apk upgrade \
&& apk add --no-cache \
build-base \
pkgconfig \
openssl-dev \
npm \
yarn \
bash \
jq \
git
build-base \
pkgconfig \
openssl-dev \
npm \
yarn \
bash \
jq \
git \
curl

WORKDIR /usr/src/noir
COPY . .
Expand All @@ -18,4 +19,4 @@ RUN ./scripts/bootstrap_packages.sh
FROM scratch
COPY --from=0 /usr/src/noir/packages /usr/src/noir/packages
# For some unknown reason, on alpine only, we need this to exist.
COPY --from=0 /usr/src/noir/node_modules/@noir-lang /usr/src/noir/node_modules/@noir-lang
COPY --from=0 /usr/src/noir/node_modules/@noir-lang /usr/src/noir/node_modules/@noir-lang
450 changes: 335 additions & 115 deletions acvm-repo/acir/codegen/acir.cpp

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions acvm-repo/acir/src/circuit/black_box_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub enum BlackBoxFunc {
/// Compute a recursive aggregation object when verifying a proof inside another circuit.
/// This outputted aggregation object will then be either checked in a top-level verifier or aggregated upon again.
RecursiveAggregation,
/// Addition over the embedded curve on which [`FieldElement`][acir_field::FieldElement] is defined.
EmbeddedCurveAdd,
/// Point doubling over the embedded curve on which [`FieldElement`][acir_field::FieldElement] is defined.
EmbeddedCurveDouble,
}

impl std::fmt::Display for BlackBoxFunc {
Expand All @@ -64,6 +68,8 @@ impl BlackBoxFunc {
BlackBoxFunc::PedersenHash => "pedersen_hash",
BlackBoxFunc::EcdsaSecp256k1 => "ecdsa_secp256k1",
BlackBoxFunc::FixedBaseScalarMul => "fixed_base_scalar_mul",
BlackBoxFunc::EmbeddedCurveAdd => "ec_add",
BlackBoxFunc::EmbeddedCurveDouble => "ec_double",
BlackBoxFunc::AND => "and",
BlackBoxFunc::XOR => "xor",
BlackBoxFunc::RANGE => "range",
Expand All @@ -84,6 +90,8 @@ impl BlackBoxFunc {
"ecdsa_secp256k1" => Some(BlackBoxFunc::EcdsaSecp256k1),
"ecdsa_secp256r1" => Some(BlackBoxFunc::EcdsaSecp256r1),
"fixed_base_scalar_mul" => Some(BlackBoxFunc::FixedBaseScalarMul),
"ec_add" => Some(BlackBoxFunc::EmbeddedCurveAdd),
"ec_double" => Some(BlackBoxFunc::EmbeddedCurveDouble),
"and" => Some(BlackBoxFunc::AND),
"xor" => Some(BlackBoxFunc::XOR),
"range" => Some(BlackBoxFunc::RANGE),
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 ")?;
Expand Down
24 changes: 23 additions & 1 deletion acvm-repo/acir/src/circuit/opcodes/black_box_function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ pub enum BlackBoxFuncCall {
high: FunctionInput,
outputs: (Witness, Witness),
},
EmbeddedCurveAdd {
input1_x: FunctionInput,
input1_y: FunctionInput,
input2_x: FunctionInput,
input2_y: FunctionInput,
outputs: (Witness, Witness),
},
EmbeddedCurveDouble {
input_x: FunctionInput,
input_y: FunctionInput,
outputs: (Witness, Witness),
},
Keccak256 {
inputs: Vec<FunctionInput>,
outputs: Vec<Witness>,
Expand Down Expand Up @@ -125,6 +137,8 @@ impl BlackBoxFuncCall {
BlackBoxFuncCall::EcdsaSecp256k1 { .. } => BlackBoxFunc::EcdsaSecp256k1,
BlackBoxFuncCall::EcdsaSecp256r1 { .. } => BlackBoxFunc::EcdsaSecp256r1,
BlackBoxFuncCall::FixedBaseScalarMul { .. } => BlackBoxFunc::FixedBaseScalarMul,
BlackBoxFuncCall::EmbeddedCurveAdd { .. } => BlackBoxFunc::EmbeddedCurveAdd,
BlackBoxFuncCall::EmbeddedCurveDouble { .. } => BlackBoxFunc::EmbeddedCurveDouble,
BlackBoxFuncCall::Keccak256 { .. } => BlackBoxFunc::Keccak256,
BlackBoxFuncCall::Keccak256VariableLength { .. } => BlackBoxFunc::Keccak256,
BlackBoxFuncCall::Keccakf1600 { .. } => BlackBoxFunc::Keccakf1600,
Expand All @@ -149,6 +163,12 @@ impl BlackBoxFuncCall {
vec![*lhs, *rhs]
}
BlackBoxFuncCall::FixedBaseScalarMul { low, high, .. } => vec![*low, *high],
BlackBoxFuncCall::EmbeddedCurveAdd {
input1_x, input1_y, input2_x, input2_y, ..
} => vec![*input1_x, *input1_y, *input2_x, *input2_y],
BlackBoxFuncCall::EmbeddedCurveDouble { input_x, input_y, .. } => {
vec![*input_x, *input_y]
}
BlackBoxFuncCall::RANGE { input } => vec![*input],
BlackBoxFuncCall::SchnorrVerify {
public_key_x,
Expand Down Expand Up @@ -237,7 +257,9 @@ impl BlackBoxFuncCall {
| BlackBoxFuncCall::PedersenHash { output, .. }
| BlackBoxFuncCall::EcdsaSecp256r1 { output, .. } => vec![*output],
BlackBoxFuncCall::FixedBaseScalarMul { outputs, .. }
| BlackBoxFuncCall::PedersenCommitment { outputs, .. } => vec![outputs.0, outputs.1],
| BlackBoxFuncCall::PedersenCommitment { outputs, .. }
| BlackBoxFuncCall::EmbeddedCurveAdd { outputs, .. }
| BlackBoxFuncCall::EmbeddedCurveDouble { outputs, .. } => vec![outputs.0, outputs.1],
BlackBoxFuncCall::RANGE { .. } | BlackBoxFuncCall::RecursiveAggregation { .. } => {
vec![]
}
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
11 changes: 7 additions & 4 deletions acvm-repo/acvm/src/compiler/transformers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ pub(super) fn transform_internal(
outputs,
..
}
| acir::circuit::opcodes::BlackBoxFuncCall::EmbeddedCurveAdd {
outputs, ..
}
| acir::circuit::opcodes::BlackBoxFuncCall::EmbeddedCurveDouble {
outputs,
..
}
| acir::circuit::opcodes::BlackBoxFuncCall::PedersenCommitment {
outputs,
..
Expand All @@ -143,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
10 changes: 8 additions & 2 deletions acvm-repo/acvm/src/pwg/blackbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ pub(crate) fn solve(
let lane = witness_assignment.try_to_u64();
state[i] = lane.unwrap();
}
let state = keccakf1600(state)?;
for (output_witness, value) in outputs.iter().zip(state.into_iter()) {
let output_state = keccakf1600(state)?;
for (output_witness, value) in outputs.iter().zip(output_state.into_iter()) {
insert_value(output_witness, FieldElement::from(value as u128), initial_witness)?;
}
Ok(())
Expand Down Expand Up @@ -177,6 +177,12 @@ pub(crate) fn solve(
BlackBoxFuncCall::FixedBaseScalarMul { low, high, outputs } => {
fixed_base_scalar_mul(backend, initial_witness, *low, *high, *outputs)
}
BlackBoxFuncCall::EmbeddedCurveAdd { .. } => {
todo!();
}
BlackBoxFuncCall::EmbeddedCurveDouble { .. } => {
todo!();
}
// Recursive aggregation will be entirely handled by the backend and is not solved by the ACVM
BlackBoxFuncCall::RecursiveAggregation { .. } => Ok(()),
}
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");
}
}
28 changes: 28 additions & 0 deletions acvm-repo/blackbox_solver/src/curve_specific_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ pub trait BlackBoxFunctionSolver {
low: &FieldElement,
high: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError>;
fn ec_add(
&self,
input1_x: &FieldElement,
input1_y: &FieldElement,
input2_x: &FieldElement,
input2_y: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError>;
fn ec_double(
&self,
input_x: &FieldElement,
input_x: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError>;
}

pub struct StubbedBlackBoxSolver;
Expand Down Expand Up @@ -73,4 +85,20 @@ impl BlackBoxFunctionSolver for StubbedBlackBoxSolver {
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
Err(Self::fail(BlackBoxFunc::FixedBaseScalarMul))
}
fn ec_add(
&self,
_input1_x: &FieldElement,
_input1_y: &FieldElement,
_input2_x: &FieldElement,
_input2_y: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
Err(Self::fail(BlackBoxFunc::EmbeddedCurveAdd))
}
fn ec_double(
&self,
_input_x: &FieldElement,
_input_y: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
Err(Self::fail(BlackBoxFunc::EmbeddedCurveDouble))
}
}
18 changes: 18 additions & 0 deletions acvm-repo/bn254_blackbox_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,22 @@ impl BlackBoxFunctionSolver for Bn254BlackBoxSolver {
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
fixed_base_scalar_mul(low, high)
}

fn ec_add(
&self,
_input1_x: &FieldElement,
_input1_y: &FieldElement,
_input2_x: &FieldElement,
_input2_y: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
todo!();
}

fn ec_double(
&self,
_input_x: &FieldElement,
_input_y: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
todo!();
}
}
Loading

0 comments on commit fee2452

Please sign in to comment.