Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Sync from aztec-packages #5222

Merged
merged 7 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved

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
28 changes: 15 additions & 13 deletions compiler/integration-tests/test/node/prove_and_verify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { CompiledCircuit } from '@noir-lang/types';
const assert_lt_program = assert_lt_json as CompiledCircuit;
const fold_fibonacci_program = fold_fibonacci_json as CompiledCircuit;

const backend = new Backend(assert_lt_program);

it('end-to-end proof creation and verification (outer)', async () => {
// Noir.Js part
const inputs = {
Expand All @@ -22,11 +24,11 @@ it('end-to-end proof creation and verification (outer)', async () => {
// bb.js part
//
// Proof creation
const prover = new Backend(assert_lt_program);
const proof = await prover.generateProof(witness);
// const prover = new Backend(assert_lt_program);
const proof = await backend.generateProof(witness);

// Proof verification
const isValid = await prover.verifyProof(proof);
const isValid = await backend.verifyProof(proof);
expect(isValid).to.be.true;
});

Expand Down Expand Up @@ -68,11 +70,11 @@ it('end-to-end proof creation and verification (inner)', async () => {
// bb.js part
//
// Proof creation
const prover = new Backend(assert_lt_program);
const proof = await prover.generateProof(witness);
// const prover = new Backend(assert_lt_program);
const proof = await backend.generateProof(witness);

// Proof verification
const isValid = await prover.verifyProof(proof);
const isValid = await backend.verifyProof(proof);
expect(isValid).to.be.true;
});

Expand All @@ -88,9 +90,9 @@ it('end-to-end proving and verification with different instances', async () => {
const { witness } = await program.execute(inputs);

// bb.js part
const prover = new Backend(assert_lt_program);
// const prover = new Backend(assert_lt_program);

const proof = await prover.generateProof(witness);
const proof = await backend.generateProof(witness);

const verifier = new Backend(assert_lt_program);
const proof_is_valid = await verifier.verifyProof(proof);
Expand Down Expand Up @@ -119,18 +121,18 @@ it('[BUG] -- bb.js null function or function signature mismatch (outer-inner) ',
//
// Proof creation
//
const prover = new Backend(assert_lt_program);
// const prover = new Backend(assert_lt_program);
// Create a proof using both proving systems, the majority of the time
// one would only use outer proofs.
const proofOuter = await prover.generateProof(witness);
const _proofInner = await prover.generateProof(witness);
const proofOuter = await backend.generateProof(witness);
const _proofInner = await backend.generateProof(witness);

// Proof verification
//
const isValidOuter = await prover.verifyProof(proofOuter);
const isValidOuter = await backend.verifyProof(proofOuter);
expect(isValidOuter).to.be.true;
// We can also try verifying an inner proof and it will fail.
const isValidInner = await prover.verifyProof(_proofInner);
const isValidInner = await backend.verifyProof(_proofInner);
expect(isValidInner).to.be.true;
});

Expand Down
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
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ impl<'context> Elaborator<'context> {
self.push_err(DefCollectorErrorKind::MutableReferenceInTraitImpl { span });
}

assert!(trait_impl.trait_id.is_some());
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
if let Some(trait_id) = trait_impl.trait_id {
self.generics = trait_impl.resolved_generics.clone();
self.collect_trait_impl_methods(trait_id, trait_impl);
Expand Down
4 changes: 3 additions & 1 deletion docs/docs/noir/concepts/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,16 @@ See [Lambdas](./lambdas.md) for more details.

Attributes are metadata that can be applied to a function, using the following syntax: `#[attribute(value)]`.

Supported attributes include:
A few supported attributes include:
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved

- **builtin**: the function is implemented by the compiler, for efficiency purposes.
- **deprecated**: mark the function as _deprecated_. Calling the function will generate a warning: `warning: use of deprecated function`
- **field**: Used to enable conditional compilation of code depending on the field size. See below for more details
- **oracle**: mark the function as _oracle_; meaning it is an external unconstrained function, implemented in noir_js. See [Unconstrained](./unconstrained.md) and [NoirJS](../../reference/NoirJS/noir_js/index.md) for more details.
- **test**: mark the function as unit tests. See [Tests](../../tooling/testing.md) for more details

See the Noir compiler for the full list of supported attributes [here](https://github.com/noir-lang/noir/blob/master/compiler/noirc_frontend/src/lexer/token.rs) (inside `let attribute = match &word_segments[..]` at the time of writing).

TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
### Field Attribute

The field attribute defines which field the function is compatible for. The function is conditionally compiled, under the condition that the field attribute matches the Noir native field.
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)
}
Loading
Loading