Skip to content

Commit

Permalink
feat: Sync from aztec-packages (#5222)
Browse files Browse the repository at this point in the history
Automated pull of Noir development from
[aztec-packages](https://github.com/AztecProtocol/aztec-packages).
BEGIN_COMMIT_OVERRIDE
chore: add example for recursion on the CLI
(AztecProtocol/aztec-packages#6389)
feat(nargo): hidden option to show contract artifact paths written by
`nargo compile`
(AztecProtocol/aztec-packages#6131)
chore: add bench programs
(AztecProtocol/aztec-packages#6566)
END_COMMIT_OVERRIDE

---------

Co-authored-by: TomAFrench <tom@tomfren.ch>
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 11, 2024
1 parent ff7bb72 commit ff67e14
Show file tree
Hide file tree
Showing 40 changed files with 588 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .aztec-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1d785fd1087d7387fc29213ca3be50b2fc9c4725
86a33140f9a65e518003b3f4c60f97d132f85b89
2 changes: 1 addition & 1 deletion acvm-repo/acvm_js/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function run_if_available {
require_command jq
require_command cargo
require_command wasm-bindgen
# require_command wasm-opt
require_command wasm-opt

self_path=$(dirname "$(readlink -f "$0")")
pname=$(cargo read-manifest | jq -r '.name')
Expand Down
34 changes: 24 additions & 10 deletions aztec_macros/src/transforms/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,30 @@ pub fn export_fn_abi(
///
/// Inserts the following code at the beginning of an unconstrained function
/// ```noir
/// let storage = Storage::init(Context::none());
/// let context = UnconstrainedContext::new();
/// let storage = Storage::init(context);
/// ```
///
/// This will allow developers to access their contract' storage struct in unconstrained functions
pub fn transform_unconstrained(func: &mut NoirFunction, storage_struct_name: String) {
// let context = UnconstrainedContext::new();
let let_context = assignment(
"context", // Assigned to
call(
variable_path(chained_dep!(
"aztec",
"context",
"unconstrained_context",
"UnconstrainedContext",
"new"
)),
vec![],
),
);

// We inject the statements at the beginning, in reverse order.
func.def.body.statements.insert(0, abstract_storage(storage_struct_name, true));
func.def.body.statements.insert(0, let_context);
}

/// Helper function that returns what the private context would look like in the ast
Expand Down Expand Up @@ -597,30 +615,26 @@ fn abstract_return_values(func: &NoirFunction) -> Result<Option<Vec<Statement>>,
/// ```noir
/// #[aztec(private)]
/// fn lol() {
/// let storage = Storage::init(context);
/// let storage = Storage::init(&mut context);
/// }
/// ```
///
/// For public functions:
/// ```noir
/// #[aztec(public)]
/// fn lol() {
/// let storage = Storage::init(context);
/// let storage = Storage::init(&mut context);
/// }
/// ```
///
/// For unconstrained functions:
/// ```noir
/// unconstrained fn lol() {
/// let storage = Storage::init(());
/// let storage = Storage::init(context);
/// }
fn abstract_storage(storage_struct_name: String, unconstrained: bool) -> Statement {
let context_expr = if unconstrained {
// Note that the literal unit type (i.e. '()') is not the same as a tuple with zero elements
expression(ExpressionKind::Literal(Literal::Unit))
} else {
mutable_reference("context")
};
let context_expr =
if unconstrained { variable("context") } else { mutable_reference("context") };

assignment(
"storage", // Assigned to
Expand Down
12 changes: 6 additions & 6 deletions compiler/integration-tests/test/node/prove_and_verify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ it('end-to-end proof creation and verification (outer) -- Verifier API', async (
const { witness } = await program.execute(inputs);

// Generate proof
const backend = new Backend(assert_lt_program);
const proof = await backend.generateProof(witness);
const prover = new Backend(assert_lt_program);
const proof = await prover.generateProof(witness);

const verificationKey = await backend.getVerificationKey();
const verificationKey = await prover.getVerificationKey();

// Proof verification
const verifier = new Verifier();
Expand Down Expand Up @@ -147,10 +147,10 @@ it('end-to-end proof creation and verification for multiple ACIR circuits (inner
// bb.js part
//
// Proof creation
const backend = new Backend(fold_fibonacci_program);
const proof = await backend.generateProof(witness);
const prover = new Backend(fold_fibonacci_program);
const proof = await prover.generateProof(witness);

// Proof verification
const isValid = await backend.verifyProof(proof);
const isValid = await prover.verifyProof(proof);
expect(isValid).to.be.true;
});
4 changes: 4 additions & 0 deletions compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ pub struct CompileOptions {
/// Enable the experimental elaborator pass
#[arg(long, hide = true)]
pub use_elaborator: bool,

/// Outputs the paths to any modified artifacts
#[arg(long, hide = true)]
pub show_artifact_paths: bool,
}

fn parse_expression_width(input: &str) -> Result<ExpressionWidth, std::io::Error> {
Expand Down
2 changes: 2 additions & 0 deletions examples/recursion/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
recurse_leaf/Prover.toml
recurse_node/Prover.toml
2 changes: 2 additions & 0 deletions examples/recursion/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["recurse_leaf", "recurse_node", "sum"]
61 changes: 61 additions & 0 deletions examples/recursion/generate_recursive_proof.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash
set -eu

BACKEND=${BACKEND:-bb}

nargo execute sum_witness --package sum
$BACKEND prove -b ./target/sum.json -w ./target/sum_witness.gz -o ./target/sum_proof

# Once we have generated our inner proof, we must use this to generate inputs to `recurse_leaf``

$BACKEND write_vk -b ./target/sum.json -o ./target/sum_key
$BACKEND vk_as_fields -k ./target/sum_key -o ./target/sum_vk_as_fields
VK_HASH=$(jq -r '.[0]' ./target/sum_vk_as_fields)
VK_AS_FIELDS=$(jq -r '.[1:]' ./target/sum_vk_as_fields)

FULL_PROOF_AS_FIELDS="$($BACKEND proof_as_fields -p ./target/sum_proof -k ./target/sum_key -o -)"
# sum has 3 public inputs
PUBLIC_INPUTS=$(echo $FULL_PROOF_AS_FIELDS | jq -r '.[:3]')
PROOF_AS_FIELDS=$(echo $FULL_PROOF_AS_FIELDS | jq -r '.[3:]')

RECURSE_LEAF_PROVER_TOML=./recurse_leaf/Prover.toml
echo "num = 2" > $RECURSE_LEAF_PROVER_TOML
echo "key_hash = \"$VK_HASH\"" >> $RECURSE_LEAF_PROVER_TOML
echo "verification_key = $VK_AS_FIELDS" >> $RECURSE_LEAF_PROVER_TOML
echo "proof = $PROOF_AS_FIELDS" >> $RECURSE_LEAF_PROVER_TOML
echo "public_inputs = $PUBLIC_INPUTS" >> $RECURSE_LEAF_PROVER_TOML

# We can now execute and prove `recurse_leaf`

nargo execute recurse_leaf_witness --package recurse_leaf
$BACKEND prove -b ./target/recurse_leaf.json -w ./target/recurse_leaf_witness.gz -o ./target/recurse_leaf_proof

# Let's do a sanity check that the proof we've generated so far is valid.
$BACKEND write_vk -b ./target/recurse_leaf.json -o ./target/recurse_leaf_key
$BACKEND verify -p ./target/recurse_leaf_proof -k ./target/recurse_leaf_key

# Now we generate the final `recurse_node` proof similarly to how we did for `recurse_leaf`.

$BACKEND vk_as_fields -k ./target/recurse_leaf_key -o ./target/recurse_leaf_vk_as_fields
VK_HASH=$(jq -r '.[0]' ./target/recurse_leaf_vk_as_fields)
VK_AS_FIELDS=$(jq -r '.[1:]' ./target/recurse_leaf_vk_as_fields)

FULL_PROOF_AS_FIELDS="$($BACKEND proof_as_fields -p ./target/recurse_leaf_proof -k ./target/recurse_leaf_key -o -)"
# recurse_leaf has 4 public inputs (excluding aggregation object)
PUBLIC_INPUTS=$(echo $FULL_PROOF_AS_FIELDS | jq -r '.[:4]')
PROOF_AS_FIELDS=$(echo $FULL_PROOF_AS_FIELDS | jq -r '.[4:]')

RECURSE_NODE_PROVER_TOML=./recurse_node/Prover.toml
echo "key_hash = \"$VK_HASH\"" > $RECURSE_NODE_PROVER_TOML
echo "verification_key = $VK_AS_FIELDS" >> $RECURSE_NODE_PROVER_TOML
echo "proof = $PROOF_AS_FIELDS" >> $RECURSE_NODE_PROVER_TOML
echo "public_inputs = $PUBLIC_INPUTS" >> $RECURSE_NODE_PROVER_TOML

# We can now execute and prove `recurse_node`

nargo execute recurse_node_witness --package recurse_node
$BACKEND prove -b ./target/recurse_node.json -w ./target/recurse_node_witness.gz -o ./target/recurse_node_proof

# We finally verify that the generated recursive proof is valid.
$BACKEND write_vk -b ./target/recurse_node.json -o ./target/recurse_node_key
$BACKEND verify -p ./target/recurse_node_proof -k ./target/recurse_node_key
7 changes: 7 additions & 0 deletions examples/recursion/recurse_leaf/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "recurse_leaf"
type = "bin"
authors = [""]
compiler_version = ">=0.26.0"

[dependencies]
20 changes: 20 additions & 0 deletions examples/recursion/recurse_leaf/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use dep::std;

#[recursive]
fn main(
verification_key: [Field; 114],
public_inputs: pub [Field; 3],
key_hash: Field,
proof: [Field; 93],
num: u64
) -> pub u64 {
// verify sum so far was computed correctly
std::verify_proof(
verification_key.as_slice(),
proof.as_slice(),
public_inputs.as_slice(),
key_hash
);
// Take output of previous proof and add another number to it.
public_inputs[2] as u64 + num
}
7 changes: 7 additions & 0 deletions examples/recursion/recurse_node/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "recurse_node"
type = "bin"
authors = [""]
compiler_version = ">=0.26.0"

[dependencies]
17 changes: 17 additions & 0 deletions examples/recursion/recurse_node/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use dep::std;

fn main(
verification_key: [Field; 114],
public_inputs: pub [Field; 4],
key_hash: Field,
proof: [Field; 109]
) -> pub u64 {
// verify sum was computed correctly
std::verify_proof(
verification_key.as_slice(),
proof.as_slice(),
public_inputs.as_slice(),
key_hash
);
public_inputs[3] as u64
}
7 changes: 7 additions & 0 deletions examples/recursion/sum/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "sum"
type = "bin"
authors = [""]
compiler_version = ">=0.26.0"

[dependencies]
2 changes: 2 additions & 0 deletions examples/recursion/sum/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a = 1
b = 2
4 changes: 4 additions & 0 deletions examples/recursion/sum/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[recursive]
fn main(a: pub u64, b: pub u64) -> pub u64 {
a + b
}
8 changes: 8 additions & 0 deletions examples/recursion/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -eu

# This file is used for Noir CI and is not required.

BACKEND=${BACKEND:-bb}

./generate_recursive_proof.sh
7 changes: 7 additions & 0 deletions test_programs/benchmarks/bench_eddsa_poseidon/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "bench_eddsa_poseidon"
version = "0.1.0"
type = "bin"
authors = [""]

[dependencies]
6 changes: 6 additions & 0 deletions test_programs/benchmarks/bench_eddsa_poseidon/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
msg = 789
pub_key_x = "0x16b051f37589e0dcf4ad3c415c090798c10d3095bedeedabfcc709ad787f3507"
pub_key_y = "0x062800ac9e60839fab9218e5ed9d541f4586e41275f4071816a975895d349a5e"
r8_x = "0x163814666f04c4d2969059a6b63ee26a0f9f0f81bd5957b0796e2e8f4a8a2f06"
r8_y = "0x1255b17d9e4bfb81831625b788f8a1665128079ac4b6c8c3cd1b857666a05a54"
s = "1230930278088778318663840827871215383007447616379808164955640681455510074924"
12 changes: 12 additions & 0 deletions test_programs/benchmarks/bench_eddsa_poseidon/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use dep::std::eddsa::{eddsa_poseidon_verify};

fn main(
msg: pub Field,
pub_key_x: Field,
pub_key_y: Field,
r8_x: Field,
r8_y: Field,
s: Field
) -> pub bool {
eddsa_poseidon_verify(pub_key_x, pub_key_y, s, r8_x, r8_y, msg)
}
7 changes: 7 additions & 0 deletions test_programs/benchmarks/bench_poseidon_hash/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "bench_poseidon_hash"
version = "0.1.0"
type = "bin"
authors = [""]

[dependencies]
1 change: 1 addition & 0 deletions test_programs/benchmarks/bench_poseidon_hash/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
input = [1,2]
5 changes: 5 additions & 0 deletions test_programs/benchmarks/bench_poseidon_hash/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use dep::std::hash::poseidon;

fn main(input: [Field; 2]) -> pub Field {
poseidon::bn254::hash_2(input)
}
7 changes: 7 additions & 0 deletions test_programs/benchmarks/bench_poseidon_hash_100/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "bench_poseidon_hash_100"
version = "0.1.0"
type = "bin"
authors = [""]

[dependencies]
Loading

0 comments on commit ff67e14

Please sign in to comment.