-
Notifications
You must be signed in to change notification settings - Fork 310
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
chore(avm): split up AVM test contract as it was growing too large #5702
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
noir-projects/noir-contracts/contracts/avm_acvm_interop_test_contract/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "avm_acvm_interop_test_contract" | ||
authors = [""] | ||
compiler_version = ">=0.25.0" | ||
type = "contract" | ||
|
||
[dependencies] | ||
aztec = { path = "../../../aztec-nr/aztec" } | ||
authwit = { path = "../../../aztec-nr/authwit" } |
72 changes: 72 additions & 0 deletions
72
noir-projects/noir-contracts/contracts/avm_acvm_interop_test_contract/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use dep::aztec::protocol_types::traits::{Serialize, Deserialize}; | ||
|
||
contract AvmAcvmInteropTest { | ||
// Libs | ||
use dep::aztec::protocol_types::address::AztecAddress; | ||
use dep::aztec::context::gas::GasOpts; | ||
use dep::aztec::context::public_context::FunctionReturns; | ||
use dep::aztec::protocol_types::abis::function_selector::FunctionSelector; | ||
use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; | ||
|
||
// Use the standard context interface to emit a new nullifier | ||
#[aztec(public-vm)] | ||
fn new_nullifier(nullifier: Field) { | ||
context.push_new_nullifier(nullifier, 0); | ||
} | ||
|
||
/************************************************************************ | ||
* ACVM interoperability | ||
************************************************************************/ | ||
#[aztec(public)] | ||
fn constant_field_acvm() -> pub Field { | ||
123456 | ||
} | ||
|
||
#[aztec(public-vm)] | ||
fn constant_field_avm() -> pub Field { | ||
123456 | ||
} | ||
|
||
#[aztec(public)] | ||
fn new_nullifier_acvm(nullifier: Field) -> pub Field { | ||
context.push_new_nullifier(nullifier, 0); | ||
} | ||
|
||
#[aztec(public)] | ||
fn assert_unsiloed_nullifier_acvm(unsiloed_nullifier: Field) { | ||
assert(context.nullifier_exists(unsiloed_nullifier, context.this_address())); | ||
} | ||
|
||
#[aztec(public-vm)] | ||
fn call_acvm_from_avm() -> pub Field { | ||
context.call_public_function( | ||
context.this_address(), | ||
FunctionSelector::from_signature("constant_field_acvm()"), | ||
[], | ||
GasOpts::default() | ||
).deserialize_into() | ||
} | ||
|
||
#[aztec(public)] | ||
fn call_avm_from_acvm() -> pub Field { | ||
context.call_public_function( | ||
context.this_address(), | ||
FunctionSelector::from_signature("constant_field_avm()"), | ||
[], | ||
GasOpts::default() | ||
).deserialize_into() | ||
} | ||
|
||
#[aztec(public-vm)] | ||
fn avm_to_acvm_call(selector: FunctionSelector, args: Field) { | ||
context.call_public_function(context.this_address(), selector, [args], GasOpts::default()).assert_empty(); | ||
} | ||
|
||
/************************************************************************ | ||
* Authwit functions | ||
************************************************************************/ | ||
#[aztec(public-vm)] | ||
fn test_authwit_send_money(from: AztecAddress, _to: AztecAddress, _amount: Field) { | ||
assert_current_call_valid_authwit_public(&mut context, from); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
noir-projects/noir-contracts/contracts/avm_nested_calls_test_contract/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "avm_nested_calls_test_contract" | ||
authors = [""] | ||
compiler_version = ">=0.25.0" | ||
type = "contract" | ||
|
||
[dependencies] | ||
aztec = { path = "../../../aztec-nr/aztec" } |
164 changes: 164 additions & 0 deletions
164
noir-projects/noir-contracts/contracts/avm_nested_calls_test_contract/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
use dep::aztec::protocol_types::traits::{Serialize, Deserialize}; | ||
|
||
contract AvmNestedCallsTest { | ||
// Libs | ||
use dep::aztec::state_vars::PublicMutable; | ||
use dep::aztec::protocol_types::address::AztecAddress; | ||
use dep::aztec::context::gas::GasOpts; | ||
use dep::aztec::context::public_context::FunctionReturns; | ||
use dep::aztec::protocol_types::abis::function_selector::FunctionSelector; | ||
|
||
#[aztec(storage)] | ||
struct Storage { | ||
single: PublicMutable<Field>, | ||
} | ||
|
||
/************************************************************************ | ||
* Storage | ||
************************************************************************/ | ||
#[aztec(public-vm)] | ||
fn set_storage_single(a: Field) { | ||
storage.single.write(a); | ||
} | ||
|
||
#[aztec(public-vm)] | ||
fn add_args_return(arg_a: Field, arg_b: Field) -> pub Field { | ||
arg_a + arg_b | ||
} | ||
|
||
// Use the standard context interface to emit a new nullifier | ||
#[aztec(public-vm)] | ||
fn new_nullifier(nullifier: Field) { | ||
context.push_new_nullifier(nullifier, 0); | ||
} | ||
|
||
// Directly call the external call opcode to initiate a nested call to the add function | ||
#[aztec(public-vm)] | ||
fn raw_nested_call_to_add(arg_a: Field, arg_b: Field) -> pub Field { | ||
let selector = FunctionSelector::from_signature("add_args_return(Field,Field)").to_field(); | ||
|
||
// Nested call | ||
let results = context.call_public_function_raw( | ||
GasOpts::default(), | ||
context.this_address(), | ||
selector, | ||
[arg_a, arg_b] | ||
); | ||
let data_to_return: [Field; 1] = results.0; | ||
// this explicit size is necessary to ensure that ret_size is compile-time | ||
// (ensure the data_to_return is in a HeapArray not a HeapVector) | ||
let success: u8 = results.1; | ||
|
||
assert(success == 1, "Call failed"); | ||
|
||
let add_result = data_to_return[0]; | ||
add_result | ||
} | ||
|
||
// Directly call the external call opcode to initiate a nested call to the add function with user-specified gas | ||
#[aztec(public-vm)] | ||
fn raw_nested_call_to_add_with_gas( | ||
arg_a: Field, | ||
arg_b: Field, | ||
l1_gas: Field, | ||
l2_gas: Field, | ||
da_gas: Field | ||
) -> pub Field { | ||
let selector = FunctionSelector::from_signature("add_args_return(Field,Field)").to_field(); | ||
|
||
// Nested call | ||
let results = context.call_public_function_raw( | ||
GasOpts::new(l1_gas, l2_gas, da_gas), | ||
context.this_address(), | ||
selector, | ||
[arg_a, arg_b] | ||
); | ||
let data_to_return: [Field; 1] = results.0; | ||
// this explicit size is necessary to ensure that ret_size is compile-time | ||
// (ensure the data_to_return is in a HeapArray not a HeapVector) | ||
let _success: u8 = results.1; | ||
|
||
let add_result = data_to_return[0]; | ||
add_result | ||
} | ||
|
||
// Use the `call_public_function` wrapper to initiate a nested call to the add function | ||
#[aztec(public-vm)] | ||
fn nested_call_to_add(arg_a: Field, arg_b: Field) -> pub Field { | ||
let selector = FunctionSelector::from_signature("add_args_return(Field,Field)"); | ||
|
||
// Nested call using standard context interface function | ||
context.call_public_function( | ||
context.this_address(), | ||
selector, | ||
[arg_a, arg_b], | ||
GasOpts::default() | ||
).deserialize_into() | ||
} | ||
|
||
// Directly call_static the external call opcode to initiate a nested call to the add function | ||
#[aztec(public-vm)] | ||
fn raw_nested_static_call_to_add(arg_a: Field, arg_b: Field) -> pub (Field, u8) { | ||
let selector = FunctionSelector::from_signature("add_args_return(Field,Field)").to_field(); | ||
|
||
let (result_data, success): ([Field; 1], u8) = context.static_call_public_function_raw( | ||
GasOpts::default(), | ||
context.this_address(), | ||
selector, | ||
[arg_a, arg_b] | ||
); | ||
|
||
(result_data[0], success) | ||
} | ||
|
||
// Directly call_static `set_storage_single`. Should fail since it's accessing storage. | ||
#[aztec(public-vm)] | ||
fn raw_nested_static_call_to_set_storage() -> pub u8 { | ||
let selector = FunctionSelector::from_signature("set_storage_single(Field)").to_field(); | ||
let calldata: [Field; 1] = [20]; | ||
|
||
let (_data_to_return, success): ([Field; 0], u8) = context.static_call_public_function_raw(GasOpts::default(), context.this_address(), selector, calldata); | ||
|
||
success | ||
} | ||
|
||
// Indirectly call_static the external call opcode to initiate a nested call to the add function | ||
#[aztec(public-vm)] | ||
fn nested_static_call_to_add(arg_a: Field, arg_b: Field) -> pub Field { | ||
let selector = FunctionSelector::from_signature("add_args_return(Field,Field)"); | ||
|
||
context.static_call_public_function( | ||
context.this_address(), | ||
selector, | ||
[arg_a, arg_b], | ||
GasOpts::default() | ||
).deserialize_into() | ||
} | ||
|
||
// Indirectly call_static `set_storage_single`. Should revert since it's accessing storage. | ||
#[aztec(public-vm)] | ||
fn nested_static_call_to_set_storage() { | ||
let selector = FunctionSelector::from_signature("set_storage_single(Field)"); | ||
let calldata: [Field; 1] = [20]; | ||
|
||
context.static_call_public_function(context.this_address(), selector, calldata, GasOpts::default()).assert_empty(); | ||
} | ||
|
||
#[aztec(public-vm)] | ||
fn create_same_nullifier_in_nested_call(nestedAddress: AztecAddress, nullifier: Field) { | ||
context.push_new_nullifier(nullifier, 0); | ||
|
||
let selector = FunctionSelector::from_signature("new_nullifier(Field)"); | ||
let calldata: [Field; 1] = [nullifier]; | ||
let _ : FunctionReturns<0> = context.call_public_function(nestedAddress, selector, calldata, GasOpts::default()); | ||
} | ||
|
||
#[aztec(public-vm)] | ||
fn create_different_nullifier_in_nested_call(nestedAddress: AztecAddress, nullifier: Field) { | ||
context.push_new_nullifier(nullifier, 0); | ||
|
||
let selector = FunctionSelector::from_signature("new_nullifier(Field)"); | ||
let calldata: [Field; 1] = [nullifier + 1]; | ||
let _ : FunctionReturns<0> = context.call_public_function(nestedAddress, selector, calldata, GasOpts::default()); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only new Noir code I added (prep for #4293)
See this comment inline on Graphite.