Skip to content

Commit

Permalink
feat(avm): per function avm run
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Mar 25, 2024
1 parent f06283c commit d45127b
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 28 deletions.
8 changes: 4 additions & 4 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
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 @@ -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 {} {}"
ls target/avm_*.json | parallel "$TRANSPILER {} {}"
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -24,6 +27,7 @@ impl Serialize<FUNCTION_DATA_LENGTH> for FunctionData {
[
self.selector.to_field(),
self.is_private as Field,
self.is_transpiled as Field,
]
}
}
Expand All @@ -33,6 +37,7 @@ impl Deserialize<FUNCTION_DATA_LENGTH> for FunctionData {
Self {
selector: FunctionSelector::from_field(serialized[0]),
is_private: serialized[1] as bool,
is_transpiled: serialized[2] as bool,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
22 changes: 18 additions & 4 deletions yarn-project/circuits.js/src/structs/function_data.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
);
}

Expand All @@ -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}`,
Expand Down Expand Up @@ -66,17 +76,21 @@ 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 {
const reader = FieldReader.asReader(fields);

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 {
Expand Down
3 changes: 1 addition & 2 deletions yarn-project/end-to-end/src/e2e_avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/foundation/src/abi/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ export function mapFunctionDataToNoir(functionData: FunctionData): FunctionDataN
return {
selector: mapFunctionSelectorToNoir(functionData.selector),
is_private: functionData.isPrivate,
is_transpiled: functionData.isTranspiled,
};
}

Expand All @@ -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,
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions yarn-project/types/src/abi/contract_artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit d45127b

Please sign in to comment.