Skip to content

Commit

Permalink
chore: git subrepo commit (merge) noir (#3955)
Browse files Browse the repository at this point in the history
subrepo:
  subdir:   "noir"
  merged:   "6bdab5c1e4"
upstream:
  origin:   "https://github.com/noir-lang/noir"
  branch:   "aztec-packages"
  commit:   "75c83348e6"
git-subrepo:
  version:  "0.4.6"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "110b9eb"

---------

Co-authored-by: ludamad <adam@aztecprotocol.com>
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
3 people authored and AztecBot committed Jan 11, 2024
1 parent 3416229 commit 5646354
Show file tree
Hide file tree
Showing 32 changed files with 716 additions and 26 deletions.
286 changes: 284 additions & 2 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
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
8 changes: 8 additions & 0 deletions acvm-repo/acvm/src/compiler/transformers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ 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 Down
6 changes: 6 additions & 0 deletions acvm-repo/acvm/src/pwg/blackbox/mod.rs
Original file line number Diff line number Diff line change
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
12 changes: 12 additions & 0 deletions acvm-repo/blackbox_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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 fn sha256(inputs: &[u8]) -> Result<[u8; 32], BlackBoxResolutionError> {
Expand Down
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!();
}
}
12 changes: 12 additions & 0 deletions acvm-repo/brillig/src/black_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub enum BlackBoxOp {
Sha256 { message: HeapVector, output: HeapArray },
/// Calculates the Blake2s hash of the inputs.
Blake2s { message: HeapVector, output: HeapArray },
/// Calculates the Blake3 hash of the inputs.
Blake3 { message: HeapVector, output: HeapArray },
/// Calculates the Keccak256 hash of the inputs.
Keccak256 { message: HeapVector, output: HeapArray },
/// Verifies a ECDSA signature over the secp256k1 curve.
Expand Down Expand Up @@ -41,4 +43,14 @@ pub enum BlackBoxOp {
PedersenHash { inputs: HeapVector, domain_separator: RegisterIndex, output: RegisterIndex },
/// Performs scalar multiplication over the embedded curve.
FixedBaseScalarMul { low: RegisterIndex, high: RegisterIndex, result: HeapArray },
/// Performs addtion over the embedded curve.
EmbeddedCurveAdd {
input1_x: RegisterIndex,
input1_y: RegisterIndex,
input2_x: RegisterIndex,
input2_y: RegisterIndex,
result: HeapArray,
},
/// Performs point doubling over the embedded curve.
EmbeddedCurveDouble { input1_x: RegisterIndex, input1_y: RegisterIndex, result: HeapArray },
}
27 changes: 26 additions & 1 deletion acvm-repo/brillig_vm/src/black_box.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use acir::brillig::{BlackBoxOp, HeapArray, HeapVector, Value};
use acir::{BlackBoxFunc, FieldElement};
use acvm_blackbox_solver::{
blake2s, ecdsa_secp256k1_verify, ecdsa_secp256r1_verify, keccak256, sha256,
blake2s, blake3, ecdsa_secp256k1_verify, ecdsa_secp256r1_verify, keccak256, sha256,
BlackBoxFunctionSolver, BlackBoxResolutionError,
};

Expand Down Expand Up @@ -58,6 +58,12 @@ pub(crate) fn evaluate_black_box<Solver: BlackBoxFunctionSolver>(
memory.write_slice(registers.get(output.pointer).to_usize(), &to_value_vec(&bytes));
Ok(())
}
BlackBoxOp::Blake3 { message, output } => {
let message = to_u8_vec(read_heap_vector(memory, registers, message));
let bytes = blake3(message.as_slice())?;
memory.write_slice(registers.get(output.pointer).to_usize(), &to_value_vec(&bytes));
Ok(())
}
BlackBoxOp::Keccak256 { message, output } => {
let message = to_u8_vec(read_heap_vector(memory, registers, message));
let bytes = keccak256(message.as_slice())?;
Expand Down Expand Up @@ -136,6 +142,22 @@ pub(crate) fn evaluate_black_box<Solver: BlackBoxFunctionSolver>(
memory.write_slice(registers.get(result.pointer).to_usize(), &[x.into(), y.into()]);
Ok(())
}
BlackBoxOp::EmbeddedCurveAdd { input1_x, input1_y, input2_x, input2_y, result } => {
let input1_x = registers.get(*input1_x).to_field();
let input1_y = registers.get(*input1_y).to_field();
let input2_x = registers.get(*input2_x).to_field();
let input2_y = registers.get(*input2_y).to_field();
let (x, y) = solver.ec_add(&input1_x, &input1_y, &input2_x, &input2_y)?;
memory.write_slice(registers.get(result.pointer).to_usize(), &[x.into(), y.into()]);
Ok(())
}
BlackBoxOp::EmbeddedCurveDouble { input1_x, input1_y, result } => {
let input1_x = registers.get(*input1_x).to_field();
let input1_y = registers.get(*input1_y).to_field();
let (x, y) = solver.ec_double(&input1_x, &input1_y)?;
memory.write_slice(registers.get(result.pointer).to_usize(), &[x.into(), y.into()]);
Ok(())
}
BlackBoxOp::PedersenCommitment { inputs, domain_separator, output } => {
let inputs: Vec<FieldElement> =
read_heap_vector(memory, registers, inputs).iter().map(|x| x.to_field()).collect();
Expand Down Expand Up @@ -171,13 +193,16 @@ fn black_box_function_from_op(op: &BlackBoxOp) -> BlackBoxFunc {
match op {
BlackBoxOp::Sha256 { .. } => BlackBoxFunc::SHA256,
BlackBoxOp::Blake2s { .. } => BlackBoxFunc::Blake2s,
BlackBoxOp::Blake3 { .. } => BlackBoxFunc::Blake3,
BlackBoxOp::Keccak256 { .. } => BlackBoxFunc::Keccak256,
BlackBoxOp::EcdsaSecp256k1 { .. } => BlackBoxFunc::EcdsaSecp256k1,
BlackBoxOp::EcdsaSecp256r1 { .. } => BlackBoxFunc::EcdsaSecp256r1,
BlackBoxOp::SchnorrVerify { .. } => BlackBoxFunc::SchnorrVerify,
BlackBoxOp::PedersenCommitment { .. } => BlackBoxFunc::PedersenCommitment,
BlackBoxOp::PedersenHash { .. } => BlackBoxFunc::PedersenHash,
BlackBoxOp::FixedBaseScalarMul { .. } => BlackBoxFunc::FixedBaseScalarMul,
BlackBoxOp::EmbeddedCurveAdd { .. } => BlackBoxFunc::EmbeddedCurveAdd,
BlackBoxOp::EmbeddedCurveDouble { .. } => BlackBoxFunc::EmbeddedCurveDouble,
}
}

Expand Down
16 changes: 16 additions & 0 deletions acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,22 @@ impl BlackBoxFunctionSolver for DummyBlackBoxSolver {
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
Ok((4_u128.into(), 5_u128.into()))
}
fn ec_add(
&self,
_input1_x: &FieldElement,
_input1_y: &FieldElement,
_input2_x: &FieldElement,
_input2_y: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
Ok((5_u128.into(), 6_u128.into()))
}
fn ec_double(
&self,
_input1_x: &FieldElement,
_input1_y: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
Ok((7_u128.into(), 8_u128.into()))
}
}

#[cfg(test)]
Expand Down
31 changes: 30 additions & 1 deletion aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,21 @@ fn create_context(ty: &str, params: &[Param]) -> Vec<Statement> {
UnresolvedTypeData::Integer(..) | UnresolvedTypeData::Bool => {
add_cast_to_hasher(identifier)
}
_ => unreachable!("[Aztec Noir] Provided parameter type is not supported"),
UnresolvedTypeData::String(..) => {
let (var_bytes, id) = str_to_bytes(identifier);
injected_expressions.push(var_bytes);
add_array_to_hasher(
&id,
&UnresolvedType {
typ: UnresolvedTypeData::Integer(Signedness::Unsigned, 32),
span: None,
},
)
}
_ => panic!(
"[Aztec Noir] Provided parameter type: {:?} is not supported",
unresolved_type
),
};
injected_expressions.push(expression);
}
Expand Down Expand Up @@ -909,6 +923,21 @@ fn add_struct_to_hasher(identifier: &Ident) -> Statement {
)))
}

fn str_to_bytes(identifier: &Ident) -> (Statement, Ident) {
// let identifier_as_bytes = identifier.as_bytes();
let var = variable_ident(identifier.clone());
let contents = if let ExpressionKind::Variable(p) = &var.kind {
p.segments.first().cloned().unwrap_or_else(|| panic!("No segments")).0.contents
} else {
panic!("Unexpected identifier type")
};
let bytes_name = format!("{}_bytes", contents);
let var_bytes = assignment(&bytes_name, method_call(var, "as_bytes", vec![]));
let id = Ident::new(bytes_name, Span::default());

(var_bytes, id)
}

fn create_loop_over(var: Expression, loop_body: Vec<Statement>) -> Statement {
// If this is an array of primitive types (integers / fields) we can add them each to the hasher
// casted to a field
Expand Down
10 changes: 10 additions & 0 deletions bootstrap_cache.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -eu

cd "$(dirname "$0")"
source ../build-system/scripts/setup_env '' '' mainframe_$USER > /dev/null

echo -e "\033[1mRetrieving noir packages from remote cache...\033[0m"
extract_repo noir-packages /usr/src/noir/packages ./noir
echo -e "\033[1mRetrieving nargo from remote cache...\033[0m"
extract_repo noir /usr/src/noir/target/release ./noir/target
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ pub(crate) fn convert_black_box_call(
unreachable!("ICE: Blake2s expects one array argument and one array result")
}
}
BlackBoxFunc::Blake3 => {
if let ([message], [BrilligVariable::BrilligArray(result_array)]) =
(function_arguments, function_results)
{
let message_vector = convert_array_or_vector(brillig_context, message, bb_func);
brillig_context.black_box_op_instruction(BlackBoxOp::Blake3 {
message: message_vector.to_heap_vector(),
output: result_array.to_heap_array(),
});
} else {
unreachable!("ICE: Blake3 expects one array argument and one array result")
}
}
BlackBoxFunc::Keccak256 => {
if let (
[message, BrilligVariable::Simple(array_size)],
Expand Down Expand Up @@ -169,6 +182,42 @@ pub(crate) fn convert_black_box_call(
)
}
}
BlackBoxFunc::EmbeddedCurveAdd => {
if let (
[BrilligVariable::Simple(input1_x), BrilligVariable::Simple(input1_y), BrilligVariable::Simple(input2_x), BrilligVariable::Simple(input2_y)],
[BrilligVariable::BrilligArray(result_array)],
) = (function_arguments, function_results)
{
brillig_context.black_box_op_instruction(BlackBoxOp::EmbeddedCurveAdd {
input1_x: *input1_x,
input1_y: *input1_y,
input2_x: *input2_x,
input2_y: *input2_y,
result: result_array.to_heap_array(),
});
} else {
unreachable!(
"ICE: EmbeddedCurveAdd expects four register arguments and one array result"
)
}
}
BlackBoxFunc::EmbeddedCurveDouble => {
if let (
[BrilligVariable::Simple(input1_x), BrilligVariable::Simple(input1_y)],
[BrilligVariable::BrilligArray(result_array)],
) = (function_arguments, function_results)
{
brillig_context.black_box_op_instruction(BlackBoxOp::EmbeddedCurveDouble {
input1_x: *input1_x,
input1_y: *input1_y,
result: result_array.to_heap_array(),
});
} else {
unreachable!(
"ICE: EmbeddedCurveAdd expects two register arguments and one array result"
)
}
}
BlackBoxFunc::AND => {
unreachable!("ICE: `BlackBoxFunc::AND` calls should be transformed into a `BinaryOp`")
}
Expand All @@ -181,9 +230,6 @@ pub(crate) fn convert_black_box_call(
BlackBoxFunc::RecursiveAggregation => unimplemented!(
"ICE: `BlackBoxFunc::RecursiveAggregation` is not implemented by the Brillig VM"
),
BlackBoxFunc::Blake3 => {
unimplemented!("ICE: `BlackBoxFunc::Blake3` is not implemented by the Brillig VM")
}
BlackBoxFunc::Keccakf1600 => {
unimplemented!("ICE: `BlackBoxFunc::Keccakf1600` is not implemented by the Brillig VM")
}
Expand Down
Loading

0 comments on commit 5646354

Please sign in to comment.