Skip to content

Commit

Permalink
chore: turn on elaborator (#7451)
Browse files Browse the repository at this point in the history
This PR turns off usage of the legacy name resolution/typechecking code
to instead use the new elaborator.

I'm seeing panics when performing keccak hashes however.

---------

Co-authored-by: thunkar <[email protected]>
  • Loading branch information
TomAFrench and Thunkar authored Jul 16, 2024
1 parent 1e8a597 commit 0599500
Show file tree
Hide file tree
Showing 21 changed files with 69 additions and 61 deletions.
2 changes: 1 addition & 1 deletion avm-transpiler/scripts/compile_then_transpile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fi
shift # remove the compile arg so we can inject --show-artifact-paths

# Forward all arguments to nargo, tee output to console
artifacts_to_transpile=$($NARGO compile --use-legacy --show-artifact-paths $@ | tee /dev/tty | grep -oP 'Saved contract artifact to: \K.*')
artifacts_to_transpile=$($NARGO compile --show-artifact-paths $@ | tee /dev/tty | grep -oP 'Saved contract artifact to: \K.*')

# NOTE: the output that is teed to /dev/tty will normally not be redirectable by the caller.
# If the script is run via docker, however, the user will see this output on stdout and will be able to redirect.
Expand Down
2 changes: 1 addition & 1 deletion aztec-up/bin/aztec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if [ "${1:-}" == "test" ]; then
# Compose file to use
FILE_ARG="-f $HOME/.aztec/docker-compose.test.yml"
# Aztec contract test args for nargo
TEST_ARGS="$@ --silence-warnings --use-legacy --oracle-resolver http://aztec:8081"
TEST_ARGS="$@ --silence-warnings --oracle-resolver http://aztec:8081"
get_compose -p aztec-test $FILE_ARG run -e NARGO_FOREIGN_CALL_TIMEOUT=300000 --workdir $CALLED_FROM --rm -it aztec-nargo $TEST_ARGS
elif [ $# == 2 ] && [ "$1" == "start" ] && [ "$2" == "--sandbox" ]; then
# Change working dir, so relative volume mounts are in the right place.
Expand Down
2 changes: 1 addition & 1 deletion boxes/boxes/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"main": "./dist/index.js",
"scripts": {
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile --use-legacy --silence-warnings",
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile --silence-warnings",
"codegen": "${AZTEC_BUILDER:-aztec-builder} codegen src/contracts/target -o artifacts",
"clean": "rm -rf ./dist .tsbuildinfo ./artifacts ./src/contracts/target",
"prep": "yarn clean && yarn compile && yarn codegen",
Expand Down
2 changes: 1 addition & 1 deletion boxes/boxes/vanilla/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.1.0",
"type": "module",
"scripts": {
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile --use-legacy --silence-warnings",
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile --silence-warnings",
"codegen": "${AZTEC_BUILDER:-aztec-builder} codegen src/contracts/target -o artifacts",
"clean": "rm -rf ./dest .tsbuildinfo ./artifacts ./src/contracts/target",
"prep": "yarn clean && yarn compile && yarn codegen && tsc -b",
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Sandbox commands have been cleaned up and simplified. Doing `aztec-up` now gets

**ADDED**:

* `aztec test [options]`: runs `aztec start --txe && aztec-nargo test --use-legacy --oracle-resolver http://aztec:8081 --silence-warnings [options]` via docker-compose allowing users to easily run contract tests using TXE
* `aztec test [options]`: runs `aztec start --txe && aztec-nargo test --oracle-resolver http://aztec:8081 --silence-warnings [options]` via docker-compose allowing users to easily run contract tests using TXE

## 0.45.0
### [Aztec.nr] Remove unencrypted logs from private
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ RUN ./bootstrap.sh
WORKDIR /usr/src/noir-projects/noir-protocol-circuits
RUN ./bootstrap.sh
WORKDIR /usr/src/noir-projects/aztec-nr
RUN nargo compile --use-legacy --silence-warnings
RUN nargo compile --silence-warnings

FROM scratch
COPY --from=builder /usr/src/noir-projects /usr/src/noir-projects
6 changes: 3 additions & 3 deletions noir-projects/Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ WORKDIR /usr/src/noir-projects
COPY . .

# Build & test
RUN cd ./noir-protocol-circuits && ./bootstrap.sh && nargo test --use-legacy --silence-warnings
RUN cd ./noir-protocol-circuits && ./bootstrap.sh && nargo test --silence-warnings

RUN cd /usr/src/yarn-project/txe && yarn start & \
# Wait for TXE to initialize
Expand All @@ -31,10 +31,10 @@ RUN cd /usr/src/yarn-project/txe && yarn start & \
# We need to increase the timeout since all tests running in parallel hammer TXE at the same time, and processing slows down leading to timeouts
# The only way we currently have to batch tests is via RAYON_NUM_THREADS, which is not ideal
./bootstrap.sh && \
NARGO_FOREIGN_CALL_TIMEOUT=300000 nargo test --use-legacy --silence-warnings --oracle-resolver http://localhost:8080
NARGO_FOREIGN_CALL_TIMEOUT=300000 nargo test --silence-warnings --oracle-resolver http://localhost:8080

RUN cd /usr/src/yarn-project/txe && yarn start & \
# Wait for TXE to initialize
sleep 5 && \
cd ./aztec-nr && \
NARGO_FOREIGN_CALL_TIMEOUT=300000 nargo test --use-legacy --silence-warnings --oracle-resolver http://localhost:8080
NARGO_FOREIGN_CALL_TIMEOUT=300000 nargo test --silence-warnings --oracle-resolver http://localhost:8080
4 changes: 2 additions & 2 deletions noir-projects/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ test:
# Wait for TXE to initialize
sleep 5 && \
cd /usr/src/noir-projects/aztec-nr && \
NARGO_FOREIGN_CALL_TIMEOUT=300000 nargo test --use-legacy --silence-warnings --oracle-resolver http://localhost:8080
NARGO_FOREIGN_CALL_TIMEOUT=300000 nargo test --silence-warnings --oracle-resolver http://localhost:8080

RUN cd /usr/src/yarn-project/txe && yarn start & \
# Wait for TXE to initialize
sleep 5 && \
cd /usr/src/noir-projects/noir-contracts && \
# We need to increase the timeout since all tests running in parallel hammer TXE at the same time and processing slows down, leading to timeouts
# The only way we currently have to batch tests is via RAYON_NUM_THREADS, which is not ideal
NARGO_FOREIGN_CALL_TIMEOUT=300000 nargo test --use-legacy --silence-warnings --oracle-resolver http://localhost:8080
NARGO_FOREIGN_CALL_TIMEOUT=300000 nargo test --silence-warnings --oracle-resolver http://localhost:8080

format:
FROM +build
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/noir-contracts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fi

echo "Compiling contracts..."
NARGO=${NARGO:-../../noir/noir-repo/target/release/nargo}
$NARGO compile --silence-warnings --use-legacy
$NARGO compile --silence-warnings

echo "Transpiling contracts..."
scripts/transpile.sh
2 changes: 1 addition & 1 deletion noir-projects/noir-protocol-circuits/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ node ./generate_variants.js

echo "Compiling protocol circuits..."
NARGO=${NARGO:-../../noir/noir-repo/target/release/nargo}
$NARGO compile --silence-warnings --use-legacy
$NARGO compile --silence-warnings

BB_HASH=${BB_HASH:-$(cd ../../ && git ls-tree -r HEAD | grep 'barretenberg/cpp' | awk '{print $3}' | git hash-object --stdin)}
echo Using BB hash $BB_HASH
Expand Down
69 changes: 38 additions & 31 deletions noir/noir-repo/aztec_macros/src/transforms/contract_interface.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use acvm::acir::AcirField;

use noirc_errors::Location;
use noirc_frontend::ast::{Ident, NoirFunction, UnresolvedTypeData};
use noirc_frontend::{
graph::CrateId,
macros_api::{FileId, HirContext, HirExpression, HirLiteral, HirStatement},
macros_api::{FieldElement, FileId, HirContext, HirExpression, HirLiteral, HirStatement},
parse_program,
parser::SortedModule,
Type,
};

use tiny_keccak::{Hasher, Keccak};

use crate::utils::{
constants::SELECTOR_PLACEHOLDER,
errors::AztecMacroError,
hir_utils::{collect_crate_structs, get_contract_module_data, signature_of_type},
};
Expand Down Expand Up @@ -64,11 +67,6 @@ pub fn stub_function(aztec_visibility: &str, func: &NoirFunction, is_static_call
.join(", ");
let fn_return_type: noirc_frontend::ast::UnresolvedType = func.return_type();

let fn_selector = format!(
"dep::aztec::protocol_types::abis::function_selector::FunctionSelector::from_signature(\"{}\")",
SELECTOR_PLACEHOLDER
);

let parameters = func.parameters();
let is_void = if matches!(fn_return_type.typ, UnresolvedTypeData::Unit) { "Void" } else { "" };
let is_static = if is_static_call { "Static" } else { "" };
Expand Down Expand Up @@ -160,7 +158,7 @@ pub fn stub_function(aztec_visibility: &str, func: &NoirFunction, is_static_call

let fn_body = format!(
"{}
let selector = {};
let selector = dep::aztec::protocol_types::abis::function_selector::FunctionSelector::from_field(0);
dep::aztec::context::{}{}{}CallInterface {{
target_contract: self.target_contract,
selector,
Expand All @@ -172,7 +170,6 @@ pub fn stub_function(aztec_visibility: &str, func: &NoirFunction, is_static_call
{}
}}",
args,
fn_selector,
aztec_visibility,
is_static,
is_void,
Expand Down Expand Up @@ -291,27 +288,34 @@ pub fn generate_contract_interface(
Ok(())
}

fn compute_fn_signature(fn_name: &str, parameters: &[Type]) -> String {
format!(
fn compute_fn_signature_hash(fn_name: &str, parameters: &[Type]) -> u32 {
let signature = format!(
"{}({})",
fn_name,
parameters.iter().map(signature_of_type).collect::<Vec<_>>().join(",")
)
);
let mut keccak = Keccak::v256();
let mut result = [0u8; 32];
keccak.update(signature.as_bytes());
keccak.finalize(&mut result);
// Take the first 4 bytes of the hash and convert them to an integer
// If you change the following value you have to change NUM_BYTES_PER_NOTE_TYPE_ID in l1_note_payload.ts as well
let num_bytes_per_note_type_id = 4;
u32::from_be_bytes(result[0..num_bytes_per_note_type_id].try_into().unwrap())
}

// Updates the function signatures in the contract interface with the actual ones, replacing the placeholder.
// This is done by locating the contract interface struct, its functions (stubs) and assuming the last statement of each
// is the constructor for a <visibility>CallInterface. This constructor has a selector field that holds a
// FunctionSelector::from_signature function that receives the signature as a string literal.
// This is done by locating the contract interface struct, its functions (stubs) and assuming the second to last statement of each
// is a let statement initializing the selector with a FunctionSelector::from_field call.
pub fn update_fn_signatures_in_contract_interface(
crate_id: &CrateId,
context: &mut HirContext,
) -> Result<(), (AztecMacroError, FileId)> {
if let Some((name, _, file_id)) = get_contract_module_data(context, crate_id) {
if let Some((struct_name, _, file_id)) = get_contract_module_data(context, crate_id) {
let maybe_interface_struct =
collect_crate_structs(crate_id, context).iter().find_map(|struct_id| {
let r#struct = context.def_interner.get_struct(*struct_id);
if r#struct.borrow().name.0.contents == name {
if r#struct.borrow().name.0.contents == struct_name {
Some(r#struct)
} else {
None
Expand All @@ -329,7 +333,7 @@ pub fn update_fn_signatures_in_contract_interface(
continue;
}

let fn_signature = compute_fn_signature(
let fn_signature_hash = compute_fn_signature_hash(
name,
&fn_parameters
.iter()
Expand Down Expand Up @@ -381,14 +385,12 @@ pub fn update_fn_signatures_in_contract_interface(
context.def_interner.expression(&current_fn_signature_expression_id);

match current_fn_signature_expression {
HirExpression::Literal(HirLiteral::Str(signature)) => {
if signature != SELECTOR_PLACEHOLDER {
HirExpression::Literal(HirLiteral::Integer(value, _)) => {
if !value.is_zero() {
Err((
AztecMacroError::CouldNotGenerateContractInterface {
secondary_message: Some(format!(
"Function signature argument must be a placeholder: {}",
SELECTOR_PLACEHOLDER
)),
secondary_message: Some(
"Function signature argument must be a placeholder with value 0".to_string()),
},
file_id,
))
Expand All @@ -397,20 +399,25 @@ pub fn update_fn_signatures_in_contract_interface(
}
}
_ => Err((
AztecMacroError::CouldNotAssignStorageSlots {
AztecMacroError::CouldNotGenerateContractInterface {
secondary_message: Some(
"Function signature argument must be a literal string".to_string(),
"Function signature argument must be a literal field element"
.to_string(),
),
},
file_id,
)),
}?;

context
.def_interner
.update_expression(current_fn_signature_expression_id, |expr| {
*expr = HirExpression::Literal(HirLiteral::Str(fn_signature))
});
context.def_interner.update_expression(
current_fn_signature_expression_id,
|expr| {
*expr = HirExpression::Literal(HirLiteral::Integer(
FieldElement::from(fn_signature_hash as u128),
false,
))
},
);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion noir/noir-repo/aztec_macros/src/transforms/note_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ pub fn generate_note_interface_impl(module: &mut SortedModule) -> Result<(), Azt
})
.collect::<Result<Vec<_>, _>>()?;
let [note_serialized_len, note_bytes_len]: [_; 2] =
note_interface_generics.try_into().unwrap();
note_interface_generics.try_into().expect(
"NoteInterface must be generic over 2 types, NOTE_FIELDS_LEN and NOTE_BYTES_LEN",
);

// Automatically inject the header field if it's not present
let (header_field_name, _) = if let Some(existing_header) =
Expand Down
1 change: 0 additions & 1 deletion noir/noir-repo/aztec_macros/src/utils/constants.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub const FUNCTION_TREE_HEIGHT: u32 = 5;
pub const MAX_CONTRACT_PRIVATE_FUNCTIONS: usize = 2_usize.pow(FUNCTION_TREE_HEIGHT);
pub const SELECTOR_PLACEHOLDER: &str = "SELECTOR_PLACEHOLDER";
4 changes: 2 additions & 2 deletions yarn-project/aztec/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ export function injectAztecCommands(program: Command, userLog: LogFn, debugLogge
test [options]: starts a dockerized TXE node via
$ aztec start --txe
then runs
$ aztec-nargo test --silence-warnings --use-legacy --oracle-resolver=<TXE_ADDRESS> [options]
then runs
$ aztec-nargo test --silence-warnings --oracle-resolver=<TXE_ADDRESS> [options]
`,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ describe('e2e_blacklist_token_contract access control', () => {
const newRole = new Role().withAdmin().withAdmin();
await expect(
t.asset.withWallet(t.other).methods.update_roles(AztecAddress.random(), newRole.toNoirStruct()).prove(),
).rejects.toThrow("Assertion failed: caller is not admin 'caller_roles.is_admin'");
).rejects.toThrow(/Assertion failed: caller is not admin .*/);
});

it('revoke minter from non admin', async () => {
const noRole = new Role();
await expect(
t.asset.withWallet(t.other).methods.update_roles(t.admin.getAddress(), noRole.toNoirStruct()).prove(),
).rejects.toThrow("Assertion failed: caller is not admin 'caller_roles.is_admin'");
).rejects.toThrow(/Assertion failed: caller is not admin .*/);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('e2e_blacklist_token_contract burn', () => {

it('burn from blacklisted account', async () => {
await expect(asset.methods.burn_public(blacklisted.getAddress(), 1n, 0).prove()).rejects.toThrow(
"Assertion failed: Blacklisted: Sender '!from_roles.is_blacklisted'",
/Assertion failed: Blacklisted: Sender/,
);
});
});
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('e2e_blacklist_token_contract burn', () => {

it('burn from blacklisted account', async () => {
await expect(asset.methods.burn(blacklisted.getAddress(), 1n, 0).prove()).rejects.toThrow(
"Assertion failed: Blacklisted: Sender '!from_roles.is_blacklisted'",
/Assertion failed: Blacklisted: Sender .*/,
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('e2e_blacklist_token_contract mint', () => {
it('mint to blacklisted entity', async () => {
await expect(
asset.withWallet(wallets[1]).methods.mint_public(blacklisted.getAddress(), 1n).prove(),
).rejects.toThrow("Assertion failed: Blacklisted: Recipient '!to_roles.is_blacklisted'");
).rejects.toThrow(/Assertion failed: Blacklisted: Recipient/);
});
});
});
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('e2e_blacklist_token_contract mint', () => {

it('mint and try to redeem at blacklist', async () => {
await expect(asset.methods.redeem_shield(blacklisted.getAddress(), amount, secret).prove()).rejects.toThrow(
"Assertion failed: Blacklisted: Recipient '!to_roles.is_blacklisted'",
/Assertion failed: Blacklisted: Recipient .*/,
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('e2e_blacklist_token_contract shield + redeem_shield', () => {
it('shielding from blacklisted account', async () => {
await expect(
asset.withWallet(blacklisted).methods.shield(blacklisted.getAddress(), 1n, secretHash, 0).prove(),
).rejects.toThrow("Assertion failed: Blacklisted: Sender '!from_roles.is_blacklisted'");
).rejects.toThrow(/Assertion failed: Blacklisted: Sender .*/);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ describe('e2e_blacklist_token_contract transfer private', () => {
it('transfer from a blacklisted account', async () => {
await expect(
asset.methods.transfer(blacklisted.getAddress(), wallets[0].getAddress(), 1n, 0).prove(),
).rejects.toThrow("Assertion failed: Blacklisted: Sender '!from_roles.is_blacklisted'");
).rejects.toThrow(/Assertion failed: Blacklisted: Sender .*/);
});

it('transfer to a blacklisted account', async () => {
await expect(
asset.methods.transfer(wallets[0].getAddress(), blacklisted.getAddress(), 1n, 0).prove(),
).rejects.toThrow("Assertion failed: Blacklisted: Recipient '!to_roles.is_blacklisted'");
).rejects.toThrow(/Assertion failed: Blacklisted: Recipient .*/);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ describe('e2e_blacklist_token_contract transfer public', () => {
it('transfer from a blacklisted account', async () => {
await expect(
asset.methods.transfer_public(blacklisted.getAddress(), wallets[0].getAddress(), 1n, 0n).prove(),
).rejects.toThrow("Assertion failed: Blacklisted: Sender '!from_roles.is_blacklisted'");
).rejects.toThrow(/Assertion failed: Blacklisted: Sender .*/);
});

it('transfer to a blacklisted account', async () => {
await expect(
asset.methods.transfer_public(wallets[0].getAddress(), blacklisted.getAddress(), 1n, 0n).prove(),
).rejects.toThrow("Assertion failed: Blacklisted: Recipient '!to_roles.is_blacklisted'");
).rejects.toThrow(/Assertion failed: Blacklisted: Recipient .*/);
});
});
});
Loading

0 comments on commit 0599500

Please sign in to comment.