From d45127bb5ef69c44682b9cdb945e8ff52558615e Mon Sep 17 00:00:00 2001 From: fcarreiro Date: Mon, 25 Mar 2024 11:06:18 +0000 Subject: [PATCH] feat(avm): per function avm run --- .../src/core/libraries/ConstantsGen.sol | 8 +++---- noir-projects/noir-contracts/bootstrap.sh | 2 +- .../crates/types/src/abis/function_data.nr | 7 +++++- .../types/src/abis/public_call_stack_item.nr | 4 ++-- .../crates/types/src/constants.nr | 8 +++---- .../src/tests/fixtures/contract_functions.nr | 2 ++ .../types/src/transaction/tx_request.nr | 2 +- yarn-project/circuits.js/src/constants.gen.ts | 8 +++---- .../circuits.js/src/structs/function_data.ts | 22 +++++++++++++++---- .../end-to-end/src/e2e_avm_simulator.test.ts | 3 +-- yarn-project/foundation/src/abi/abi.ts | 4 ++++ .../src/type_conversion.ts | 7 +++++- .../src/sequencer/abstract_phase_manager.ts | 6 ++--- .../types/src/abi/contract_artifact.ts | 1 + 14 files changed, 56 insertions(+), 28 deletions(-) diff --git a/l1-contracts/src/core/libraries/ConstantsGen.sol b/l1-contracts/src/core/libraries/ConstantsGen.sol index 79c5f786c48..3b0dab86411 100644 --- a/l1-contracts/src/core/libraries/ConstantsGen.sol +++ b/l1-contracts/src/core/libraries/ConstantsGen.sol @@ -104,7 +104,7 @@ library Constants { uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2; uint256 internal constant CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 2; uint256 internal constant ETH_ADDRESS_LENGTH = 1; - uint256 internal constant FUNCTION_DATA_LENGTH = 2; + uint256 internal constant FUNCTION_DATA_LENGTH = 3; uint256 internal constant FUNCTION_LEAF_PREIMAGE_LENGTH = 5; uint256 internal constant GLOBAL_VARIABLES_LENGTH = 6; uint256 internal constant HEADER_LENGTH = 20; @@ -114,13 +114,13 @@ library Constants { uint256 internal constant NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH = 4; uint256 internal constant NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 5; uint256 internal constant PARTIAL_STATE_REFERENCE_LENGTH = 6; - uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 210; + uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 211; uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 207; uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 198; uint256 internal constant STATE_REFERENCE_LENGTH = 8; uint256 internal constant TX_CONTEXT_DATA_LENGTH = 4; - uint256 internal constant TX_REQUEST_LENGTH = 8; - uint256 internal constant ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 11; + uint256 internal constant TX_REQUEST_LENGTH = 9; + uint256 internal constant ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 12; uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674; uint256 internal constant NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP = 2048; uint256 internal constant NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048; diff --git a/noir-projects/noir-contracts/bootstrap.sh b/noir-projects/noir-contracts/bootstrap.sh index 0d372df5515..d843e9cdc7d 100755 --- a/noir-projects/noir-contracts/bootstrap.sh +++ b/noir-projects/noir-contracts/bootstrap.sh @@ -21,4 +21,4 @@ $NARGO compile --silence-warnings echo "Transpiling avm contracts... (only '#[aztec(public-vm)]')" TRANSPILER=${TRANSPILER:-../../avm-transpiler/target/release/avm-transpiler} -ls target/avm_*.json | parallel -L8 "$TRANSPILER {} {}" \ No newline at end of file +ls target/avm_*.json | parallel "$TRANSPILER {} {}" \ No newline at end of file diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_data.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_data.nr index 7917108165c..ab803bc99c3 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_data.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_data.nr @@ -7,12 +7,15 @@ use crate::{ struct FunctionData { selector : FunctionSelector, is_private : bool, + // Remove once the AVM is fully operational. + is_transpiled: bool, } impl Eq for FunctionData { fn eq(self, other: Self) -> bool { self.selector.eq(other.selector) & - self.is_private == other.is_private + self.is_private == other.is_private & + self.is_transpiled == other.is_transpiled } } @@ -24,6 +27,7 @@ impl Serialize for FunctionData { [ self.selector.to_field(), self.is_private as Field, + self.is_transpiled as Field, ] } } @@ -33,6 +37,7 @@ impl Deserialize for FunctionData { Self { selector: FunctionSelector::from_field(serialized[0]), is_private: serialized[1] as bool, + is_transpiled: serialized[2] as bool, } } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_call_stack_item.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_call_stack_item.nr index 7ffb978ddf1..b9f5689e2af 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_call_stack_item.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_call_stack_item.nr @@ -58,7 +58,7 @@ mod tests { #[test] fn compute_call_stack_item_request_hash() { let contract_address = AztecAddress::from_field(1); - let function_data = FunctionData { selector: FunctionSelector::from_u32(2), is_private: false }; + let function_data = FunctionData { selector: FunctionSelector::from_u32(2), is_private: false, is_transpiled: false }; let mut public_inputs: PublicCircuitPublicInputs = dep::std::unsafe::zeroed(); public_inputs.new_note_hashes[0] = SideEffect{ @@ -76,7 +76,7 @@ mod tests { #[test] fn compute_call_stack_item_hash() { let contract_address = AztecAddress::from_field(1); - let function_data = FunctionData { selector: FunctionSelector::from_u32(2), is_private: false }; + let function_data = FunctionData { selector: FunctionSelector::from_u32(2), is_private: false, is_transpiled: false }; let mut public_inputs: PublicCircuitPublicInputs = dep::std::unsafe::zeroed(); public_inputs.new_note_hashes[0] = SideEffect{ diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr index fcbe5e623f5..9c56ed53949 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -154,7 +154,7 @@ global CONTRACT_INSTANCE_LENGTH: u64 = 6; global CONTRACT_STORAGE_READ_LENGTH: u64 = 2; global CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH: u64 = 2; global ETH_ADDRESS_LENGTH = 1; -global FUNCTION_DATA_LENGTH: u64 = 2; +global FUNCTION_DATA_LENGTH: u64 = 3; global FUNCTION_LEAF_PREIMAGE_LENGTH: u64 = 5; global GLOBAL_VARIABLES_LENGTH: u64 = 6; global HEADER_LENGTH: u64 = 20; // 2 for last_archive, 4 for content commitment, 8 for state reference, 6 for global vars @@ -164,7 +164,7 @@ global MAX_BLOCK_NUMBER_LENGTH: u64 = 2; // 1 for the option flag, 1 for the val global NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH = 4; global NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 5; global PARTIAL_STATE_REFERENCE_LENGTH: u64 = 6; -global PRIVATE_CALL_STACK_ITEM_LENGTH: u64 = 210; +global PRIVATE_CALL_STACK_ITEM_LENGTH: u64 = 211; // Change this ONLY if you have changed the PrivateCircuitPublicInputs structure. // In other words, if the structure/size of the public inputs of a function call changes then we should change this // constant as well PRIVATE_CALL_STACK_ITEM_LENGTH @@ -173,9 +173,9 @@ global PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH: u64 = 207; global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: u64 = 198; global STATE_REFERENCE_LENGTH: u64 = 8; // 2 for snap + 8 for partial global TX_CONTEXT_DATA_LENGTH: u64 = 4; -global TX_REQUEST_LENGTH: u64 = 8; // 2 + TX_CONTEXT_DATA_LENGTH + FUNCTION_DATA_LENGTH +global TX_REQUEST_LENGTH: u64 = 9; // 2 + TX_CONTEXT_DATA_LENGTH + FUNCTION_DATA_LENGTH -global ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH: Field = 11; // 2 + FUNCTION_DATA_LENGTH + CALL_CONTEXT_LENGTH +global ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH: Field = 12; // 2 + FUNCTION_DATA_LENGTH + CALL_CONTEXT_LENGTH global GET_NOTES_ORACLE_RETURN_LENGTH: u64 = 674; global NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP: Field = 2048; global NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP: Field = 2048; diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixtures/contract_functions.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixtures/contract_functions.nr index a5f7642811a..247eed10836 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixtures/contract_functions.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixtures/contract_functions.nr @@ -15,6 +15,7 @@ global default_private_function = ContractFunction { data: FunctionData { selector: FunctionSelector { inner: 1010101 }, is_private: true, + is_transpiled: false, }, vk_hash: 0, acir_hash: 1111, @@ -34,6 +35,7 @@ global default_public_function = ContractFunction { data: FunctionData { selector: FunctionSelector { inner: 3030303 }, is_private: false, + is_transpiled: false, }, vk_hash: 0, acir_hash: 3333, diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/transaction/tx_request.nr b/noir-projects/noir-protocol-circuits/crates/types/src/transaction/tx_request.nr index ba6179a05ee..5226fd67bec 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/transaction/tx_request.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/transaction/tx_request.nr @@ -80,7 +80,7 @@ mod tests { origin: AztecAddress::from_field(1), args_hash: 3, tx_context: TxContext { is_fee_payment_tx: false, is_rebate_payment_tx: false, chain_id: 0, version: 0 }, - function_data: FunctionData { selector: FunctionSelector::from_u32(2), is_private: true } + function_data: FunctionData { selector: FunctionSelector::from_u32(2), is_private: true, is_transpiled: false } }; // Value from tx_request.test.ts "compute hash" test let test_data_tx_request_hash = 0x20af6f595c396494f1177fa196d17e98d55a2416b28c262b76e78a36d6c01daa; diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index a1ac57a528d..fca307f324a 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -89,7 +89,7 @@ export const CONTRACT_INSTANCE_LENGTH = 6; export const CONTRACT_STORAGE_READ_LENGTH = 2; export const CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 2; export const ETH_ADDRESS_LENGTH = 1; -export const FUNCTION_DATA_LENGTH = 2; +export const FUNCTION_DATA_LENGTH = 3; export const FUNCTION_LEAF_PREIMAGE_LENGTH = 5; export const GLOBAL_VARIABLES_LENGTH = 6; export const HEADER_LENGTH = 20; @@ -99,13 +99,13 @@ export const MAX_BLOCK_NUMBER_LENGTH = 2; export const NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH = 4; export const NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 5; export const PARTIAL_STATE_REFERENCE_LENGTH = 6; -export const PRIVATE_CALL_STACK_ITEM_LENGTH = 210; +export const PRIVATE_CALL_STACK_ITEM_LENGTH = 211; export const PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 207; export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 198; export const STATE_REFERENCE_LENGTH = 8; export const TX_CONTEXT_DATA_LENGTH = 4; -export const TX_REQUEST_LENGTH = 8; -export const ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 11; +export const TX_REQUEST_LENGTH = 9; +export const ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 12; export const GET_NOTES_ORACLE_RETURN_LENGTH = 674; export const NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP = 2048; export const NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048; diff --git a/yarn-project/circuits.js/src/structs/function_data.ts b/yarn-project/circuits.js/src/structs/function_data.ts index bb8aba83713..a00ccc86521 100644 --- a/yarn-project/circuits.js/src/structs/function_data.ts +++ b/yarn-project/circuits.js/src/structs/function_data.ts @@ -1,6 +1,7 @@ import { FunctionAbi, FunctionSelector, FunctionType } from '@aztec/foundation/abi'; import { pedersenHash } from '@aztec/foundation/crypto'; import { Fr } from '@aztec/foundation/fields'; +import { createDebugLogger } from '@aztec/foundation/log'; import { BufferReader, FieldReader, serializeToBuffer } from '@aztec/foundation/serialize'; import { FUNCTION_DATA_LENGTH, GeneratorIndex } from '../constants.gen.js'; @@ -13,12 +14,19 @@ export class FunctionData { public selector: FunctionSelector, /** Indicates whether the function is private or public. */ public isPrivate: boolean, + /** + * Transitional: whether the function is an AVM function. + * Remove when the AVM is fully operational. + */ + public isTranspiled: boolean = false, ) {} static fromAbi(abi: FunctionAbi | ContractFunctionDao): FunctionData { + createDebugLogger('aztec:fromAbi')(`${abi.name}: ${abi.isTranspiled}`); return new FunctionData( FunctionSelector.fromNameAndParameters(abi.name, abi.parameters), abi.functionType === FunctionType.SECRET, + abi.isTranspiled ?? false, ); } @@ -27,11 +35,13 @@ export class FunctionData { * @returns The buffer. */ toBuffer(): Buffer { - return serializeToBuffer(this.selector, this.isPrivate); + return serializeToBuffer(this.selector, this.isPrivate, this.isTranspiled); + // return serializeToBuffer(this.selector, this.isPrivate); } toFields(): Fr[] { - const fields = [this.selector.toField(), new Fr(this.isPrivate)]; + const fields = [this.selector.toField(), new Fr(this.isPrivate), new Fr(this.isTranspiled)]; + // const fields = [this.selector.toField(), new Fr(this.isPrivate)]; if (fields.length !== FUNCTION_DATA_LENGTH) { throw new Error( `Invalid number of fields for FunctionData. Expected ${FUNCTION_DATA_LENGTH}, got ${fields.length}`, @@ -66,8 +76,10 @@ export class FunctionData { * @returns A new instance of FunctionData. */ static fromBuffer(buffer: Buffer | BufferReader): FunctionData { + createDebugLogger('aztec:fromBuffer')(`!`); const reader = BufferReader.asReader(buffer); - return new FunctionData(reader.readObject(FunctionSelector), reader.readBoolean()); + return new FunctionData(reader.readObject(FunctionSelector), reader.readBoolean(), reader.readBoolean()); + // return new FunctionData(reader.readObject(FunctionSelector), reader.readBoolean()); } static fromFields(fields: Fr[] | FieldReader): FunctionData { @@ -75,8 +87,10 @@ export class FunctionData { const selector = FunctionSelector.fromFields(reader); const isPrivate = reader.readBoolean(); + const isTranspiled = reader.readBoolean(); - return new FunctionData(selector, isPrivate); + return new FunctionData(selector, isPrivate, isTranspiled); + // return new FunctionData(selector, isPrivate); } hash(): Fr { diff --git a/yarn-project/end-to-end/src/e2e_avm_simulator.test.ts b/yarn-project/end-to-end/src/e2e_avm_simulator.test.ts index 579dc24f13d..3dce96dd2a3 100644 --- a/yarn-project/end-to-end/src/e2e_avm_simulator.test.ts +++ b/yarn-project/end-to-end/src/e2e_avm_simulator.test.ts @@ -5,10 +5,9 @@ import { jest } from '@jest/globals'; import { setup } from './fixtures/utils.js'; -process.env.AVM_ENABLED = 'absofrigginlutely'; const TIMEOUT = 100_000; -describe('e2e_nested_contract', () => { +describe('e2e_avm_simulator', () => { jest.setTimeout(TIMEOUT); let wallet: Wallet; diff --git a/yarn-project/foundation/src/abi/abi.ts b/yarn-project/foundation/src/abi/abi.ts index c131a81f926..1b173c428e0 100644 --- a/yarn-project/foundation/src/abi/abi.ts +++ b/yarn-project/foundation/src/abi/abi.ts @@ -156,6 +156,10 @@ export interface FunctionAbi { * Whether the function is flagged as an initializer. */ isInitializer: boolean; + /** + * Transitional: whether the function is an AVM function. + */ + isTranspiled?: boolean; } /** diff --git a/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts b/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts index 62622e993a0..f0c35c66437 100644 --- a/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts +++ b/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts @@ -391,6 +391,7 @@ export function mapFunctionDataToNoir(functionData: FunctionData): FunctionDataN return { selector: mapFunctionSelectorToNoir(functionData.selector), is_private: functionData.isPrivate, + is_transpiled: functionData.isTranspiled, }; } @@ -400,7 +401,11 @@ export function mapFunctionDataToNoir(functionData: FunctionData): FunctionDataN * @returns The function data. */ export function mapFunctionDataFromNoir(functionData: FunctionDataNoir): FunctionData { - return new FunctionData(mapFunctionSelectorFromNoir(functionData.selector), functionData.is_private); + return new FunctionData( + mapFunctionSelectorFromNoir(functionData.selector), + functionData.is_private, + functionData.is_transpiled, + ); } /** diff --git a/yarn-project/sequencer-client/src/sequencer/abstract_phase_manager.ts b/yarn-project/sequencer-client/src/sequencer/abstract_phase_manager.ts index 46474f870ae..5376ae460c2 100644 --- a/yarn-project/sequencer-client/src/sequencer/abstract_phase_manager.ts +++ b/yarn-project/sequencer-client/src/sequencer/abstract_phase_manager.ts @@ -55,8 +55,6 @@ import { } from '@aztec/simulator'; import { MerkleTreeOperations } from '@aztec/world-state'; -import { env } from 'process'; - import { PublicKernelCircuitSimulator } from '../simulator/index.js'; import { HintsBuilder } from './hints_builder.js'; import { lastSideEffectCounter } from './utils.js'; @@ -209,9 +207,9 @@ export abstract class AbstractPhaseManager { const isExecutionRequest = !isPublicExecutionResult(current); const sideEffectCounter = lastSideEffectCounter(tx) + 1; - // NOTE: temporary glue to incorporate avm execution calls + // NOTE: temporary glue to incorporate avm execution calls. const simulator = (execution: PublicExecution, globalVariables: GlobalVariables) => - env.AVM_ENABLED + execution.functionData.isTranspiled ? this.publicExecutor.simulateAvm(execution, globalVariables, sideEffectCounter) : this.publicExecutor.simulate(execution, globalVariables, sideEffectCounter); diff --git a/yarn-project/types/src/abi/contract_artifact.ts b/yarn-project/types/src/abi/contract_artifact.ts index 26694d58a76..b1b4a1d894b 100644 --- a/yarn-project/types/src/abi/contract_artifact.ts +++ b/yarn-project/types/src/abi/contract_artifact.ts @@ -147,6 +147,7 @@ function generateFunctionArtifact(fn: NoirCompiledContractFunction): FunctionArt functionType, isInternal, isInitializer: fn.custom_attributes.includes(AZTEC_INITIALIZER_ATTRIBUTE), + isTranspiled: fn.custom_attributes.includes(AZTEC_PUBLIC_VM_ATTRIBUTE), parameters, returnTypes, bytecode: Buffer.from(fn.bytecode, 'base64'),