Skip to content

Commit

Permalink
refactor(avm)!: rollback FunctionAbi isTranspiled changes (#5561)
Browse files Browse the repository at this point in the history
I'm rolling back the `FunctionAbi/Data` changes in this
[PR](#5421) and
replacing it with a hackier but less intrusive and more powerful
approach.

In following PRs I'll make it possible for ACVM<->AVM functions to call
each other as external public calls.
  • Loading branch information
fcarreiro authored Apr 3, 2024
1 parent 07794ee commit 150932a
Show file tree
Hide file tree
Showing 25 changed files with 113 additions and 80 deletions.
14 changes: 14 additions & 0 deletions avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec<u8> {
}
}

// TEMPORARY: Add a "magic number" instruction to the end of the program.
// This makes it possible to know that the bytecode corresponds to the AVM.
// We are adding a MOV instruction that moves a value to itself.
// This should therefore not affect the program's execution.
avm_instrs.push(AvmInstruction {
opcode: AvmOpcode::MOV,
indirect: Some(ALL_DIRECT),
operands: vec![
AvmOperand::U32 { value: 0x18ca },
AvmOperand::U32 { value: 0x18ca },
],
..Default::default()
});

dbg_print_avm_program(&avm_instrs);

// Constructing bytecode from instructions
Expand Down
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 @@ -96,7 +96,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 = 3;
uint256 internal constant FUNCTION_DATA_LENGTH = 2;
uint256 internal constant FUNCTION_LEAF_PREIMAGE_LENGTH = 5;
uint256 internal constant GLOBAL_VARIABLES_LENGTH = 6;
uint256 internal constant HEADER_LENGTH = 20;
Expand All @@ -106,13 +106,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 = 211;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 210;
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 = 9;
uint256 internal constant ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 12;
uint256 internal constant TX_REQUEST_LENGTH = 8;
uint256 internal constant ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 11;
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ 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_transpiled == other.is_transpiled)
self.is_private == other.is_private
}
}

Expand All @@ -27,7 +24,6 @@ impl Serialize<FUNCTION_DATA_LENGTH> for FunctionData {
[
self.selector.to_field(),
self.is_private as Field,
self.is_transpiled as Field,
]
}
}
Expand All @@ -37,7 +33,6 @@ 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 All @@ -62,6 +57,6 @@ fn empty_hash() {
let hash = data.hash();

// Value from function_data.test.ts "computes empty item hash" test
let test_data_empty_hash = 0x066e6cdc4a6ba5e4781deda650b0be6c12f975f064fc38df72c1060716759b17;
let test_data_empty_hash = 0x27b1d0839a5b23baf12a8d195b18ac288fcf401afb2f70b8a4b529ede5fa9fed;
assert_eq(hash, test_data_empty_hash);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ fn empty_hash() {
let hash = item.hash();

// Value from private_call_stack_item.test.ts "computes empty item hash" test
let test_data_empty_hash = 0x06b381be873c42defb570287e3ba23900175838afb6047a27c7dce1f653b4ac5;
let test_data_empty_hash = 0x13f518365c690d1b96d31454afed495ad29fe530939caf7189dd44f9bd63ef89;
assert_eq(hash, test_data_empty_hash);
}
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, is_transpiled: false };
let function_data = FunctionData { selector: FunctionSelector::from_u32(2), is_private: false };

let mut public_inputs: PublicCircuitPublicInputs = dep::std::unsafe::zeroed();
public_inputs.new_note_hashes[0] = SideEffect{
Expand All @@ -69,14 +69,14 @@ mod tests {
let call_stack_item = PublicCallStackItem { contract_address, public_inputs, is_execution_request: true, function_data };

// Value from public_call_stack_item.test.ts "Computes a callstack item request hash" test
let test_data_call_stack_item_request_hash = 0x09bd19d3ebcda705ab1ed598db287340aed3efda0ad3bbbf3296737bda731fa9;
let test_data_call_stack_item_request_hash = 0x141bbf6bc30f0a19640983354528288239b68edd5c1edd9955a007801230d7b6;
assert_eq(call_stack_item.hash(), test_data_call_stack_item_request_hash);
}

#[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, is_transpiled: false };
let function_data = FunctionData { selector: FunctionSelector::from_u32(2), is_private: false };

let mut public_inputs: PublicCircuitPublicInputs = dep::std::unsafe::zeroed();
public_inputs.new_note_hashes[0] = SideEffect{
Expand All @@ -87,7 +87,7 @@ mod tests {
let call_stack_item = PublicCallStackItem { contract_address, public_inputs, is_execution_request: false, function_data };

// Value from public_call_stack_item.test.ts "Computes a callstack item hash" test
let test_data_call_stack_item_hash = 0x1fe20dd657d73941763bf8437471ac000be6efcb046895014b1a84b5aaf55905;
let test_data_call_stack_item_hash = 0x05e9e448563aa811c209cc557136ac56b55f9f2f31ee54d41b697389fd45dc1c;
assert_eq(call_stack_item.hash(), test_data_call_stack_item_hash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,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 = 3;
global FUNCTION_DATA_LENGTH: u64 = 2;
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 @@ -151,7 +151,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 = 211;
global PRIVATE_CALL_STACK_ITEM_LENGTH: u64 = 210;
// 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 @@ -160,9 +160,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 = 9; // 2 + TX_CONTEXT_DATA_LENGTH + FUNCTION_DATA_LENGTH
global TX_REQUEST_LENGTH: u64 = 8; // 2 + TX_CONTEXT_DATA_LENGTH + FUNCTION_DATA_LENGTH

global ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH: Field = 12; // 2 + FUNCTION_DATA_LENGTH + CALL_CONTEXT_LENGTH
global ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH: Field = 11; // 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,7 +15,6 @@ 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 @@ -35,7 +34,6 @@ 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,10 +80,10 @@ 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, is_transpiled: false }
function_data: FunctionData { selector: FunctionSelector::from_u32(2), is_private: true }
};
// Value from tx_request.test.ts "compute hash" test
let test_data_tx_request_hash = 0x08e435f993536fd7a9d6faabfc03a18c4314a54d3d90d15a15275fcfe77bc5a3;
let test_data_tx_request_hash = 0x20af6f595c396494f1177fa196d17e98d55a2416b28c262b76e78a36d6c01daa;
assert(tx_request.hash() == test_data_tx_request_hash);
}
}
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 @@ -81,7 +81,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 = 3;
export const FUNCTION_DATA_LENGTH = 2;
export const FUNCTION_LEAF_PREIMAGE_LENGTH = 5;
export const GLOBAL_VARIABLES_LENGTH = 6;
export const HEADER_LENGTH = 20;
Expand All @@ -91,13 +91,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 = 211;
export const PRIVATE_CALL_STACK_ITEM_LENGTH = 210;
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 = 9;
export const ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 12;
export const TX_REQUEST_LENGTH = 8;
export const ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 11;
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`FunctionData computes empty inputs hash 1`] = `Fr<0x066e6cdc4a6ba5e4781deda650b0be6c12f975f064fc38df72c1060716759b17>`;
exports[`FunctionData computes empty inputs hash 1`] = `Fr<0x27b1d0839a5b23baf12a8d195b18ac288fcf401afb2f70b8a4b529ede5fa9fed>`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PrivateCallStackItem computes empty item hash 1`] = `Fr<0x06b381be873c42defb570287e3ba23900175838afb6047a27c7dce1f653b4ac5>`;
exports[`PrivateCallStackItem computes empty item hash 1`] = `Fr<0x13f518365c690d1b96d31454afed495ad29fe530939caf7189dd44f9bd63ef89>`;

exports[`PrivateCallStackItem computes hash 1`] = `Fr<0x22654b49c43ac7bb9abc1a84d46ba1a6c67a9d354b9b4c5aba2496e82f03315f>`;
exports[`PrivateCallStackItem computes hash 1`] = `Fr<0x1b1f2424b9b38679cd1520ad44392630ac60f1efd1e73e5a0682d999fdee5f91>`;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PublicCallStackItem Computes a callstack item hash 1`] = `"0x1fe20dd657d73941763bf8437471ac000be6efcb046895014b1a84b5aaf55905"`;
exports[`PublicCallStackItem Computes a callstack item hash 1`] = `"0x05e9e448563aa811c209cc557136ac56b55f9f2f31ee54d41b697389fd45dc1c"`;

exports[`PublicCallStackItem Computes a callstack item request hash 1`] = `"0x09bd19d3ebcda705ab1ed598db287340aed3efda0ad3bbbf3296737bda731fa9"`;
exports[`PublicCallStackItem Computes a callstack item request hash 1`] = `"0x141bbf6bc30f0a19640983354528288239b68edd5c1edd9955a007801230d7b6"`;

exports[`PublicCallStackItem computes hash 1`] = `Fr<0x199dfba7eaee0977e755d4b7b575845e94f37b94bffe27b03ad45dfafe53177c>`;
exports[`PublicCallStackItem computes hash 1`] = `Fr<0x2bd387b49bd2cf3794f9352624447a438529fe53031367e6ce3790c4a2050f95>`;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`TxRequest compute hash 1`] = `"0x08e435f993536fd7a9d6faabfc03a18c4314a54d3d90d15a15275fcfe77bc5a3"`;
exports[`TxRequest compute hash 1`] = `"0x20af6f595c396494f1177fa196d17e98d55a2416b28c262b76e78a36d6c01daa"`;
15 changes: 4 additions & 11 deletions yarn-project/circuits.js/src/structs/function_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@ 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 {
return new FunctionData(
FunctionSelector.fromNameAndParameters(abi.name, abi.parameters),
abi.functionType === FunctionType.SECRET,
abi.isTranspiled ?? false,
);
}

Expand All @@ -33,11 +27,11 @@ export class FunctionData {
* @returns The buffer.
*/
toBuffer(): Buffer {
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), 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 @@ -73,17 +67,16 @@ export class FunctionData {
*/
static fromBuffer(buffer: Buffer | BufferReader): FunctionData {
const reader = BufferReader.asReader(buffer);
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, isTranspiled);
return new FunctionData(selector, isPrivate);
}

hash(): Fr {
Expand Down
4 changes: 0 additions & 4 deletions yarn-project/foundation/src/abi/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,6 @@ 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
Loading

0 comments on commit 150932a

Please sign in to comment.