Skip to content

Commit

Permalink
feat: add meta_hwm to PrivateCircuitPublicInputs (#4341)
Browse files Browse the repository at this point in the history
This PR adds `meta_hwm` to the context and exposes it through the
private circuits so that in the last circuit the side effects created
during private execution can be partitioned into reversible and
irreversible effect sets

Fix #4084

---------


Co-authored-by: Mitchell Tracy <mitchell@aztecprotocol.com>
  • Loading branch information
alexghr and just-mitch committed Feb 5, 2024
1 parent c918d8d commit 4f248b5
Show file tree
Hide file tree
Showing 37 changed files with 5,460 additions and 3,000 deletions.
7 changes: 5 additions & 2 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ library Constants {
uint256 internal constant MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX = 4;
uint256 internal constant NUM_ENCRYPTED_LOGS_HASHES_PER_TX = 1;
uint256 internal constant NUM_UNENCRYPTED_LOGS_HASHES_PER_TX = 1;
uint256 internal constant MAX_NEW_COMMITMENTS_PER_TX_META = 8;
uint256 internal constant MAX_NEW_NULLIFIERS_PER_TX_META = 8;
uint256 internal constant MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX_META = 2;
uint256 internal constant NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP = 16;
uint256 internal constant VK_TREE_HEIGHT = 3;
uint256 internal constant FUNCTION_TREE_HEIGHT = 5;
Expand Down Expand Up @@ -79,8 +82,8 @@ library Constants {
uint256 internal constant HEADER_LENGTH = 18;
uint256 internal constant FUNCTION_DATA_LENGTH = 4;
uint256 internal constant CONTRACT_DEPLOYMENT_DATA_LENGTH = 6;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 204;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 209;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 205;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 210;
uint256 internal constant CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 201;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,14 @@ export class ClientExecutionContext extends ViewDataOracle {
* @param targetContractAddress - The address of the contract to call.
* @param functionSelector - The function selector of the function to call.
* @param argsHash - The packed arguments to pass to the function.
* @param sideffectCounter - The side effect counter at the start of the call.
* @param sideEffectCounter - The side effect counter at the start of the call.
* @returns The execution result.
*/
async callPrivateFunction(
targetContractAddress: AztecAddress,
functionSelector: FunctionSelector,
argsHash: Fr,
sideffectCounter: number,
sideEffectCounter: number,
) {
this.log(
`Calling private function ${this.contractAddress}:${functionSelector} from ${this.callContext.storageContractAddress}`,
Expand All @@ -337,7 +337,7 @@ export class ClientExecutionContext extends ViewDataOracle {
const derivedCallContext = await this.deriveCallContext(
targetContractAddress,
targetArtifact,
sideffectCounter,
sideEffectCounter,
false,
false,
);
Expand Down
46 changes: 25 additions & 21 deletions yarn-project/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ struct PrivateContext {
inputs: PrivateContextInputs,
side_effect_counter: u32,

meta_hwm: u32,

args_hash : Field,
return_values : BoundedVec<Field, RETURN_VALUES_LENGTH>,

Expand Down Expand Up @@ -101,6 +103,7 @@ impl PrivateContext {
PrivateContext {
inputs: inputs,
side_effect_counter: inputs.call_context.start_side_effect_counter,
meta_hwm: 0,

args_hash: args_hash,
return_values: BoundedVec::new(0),
Expand Down Expand Up @@ -172,6 +175,7 @@ impl PrivateContext {
call_context: self.inputs.call_context,
args_hash: self.args_hash,
return_values: self.return_values.storage,
meta_hwm: self.meta_hwm,
read_requests: self.read_requests.storage,
nullifier_key_validation_requests: self.nullifier_key_validation_requests.storage,
new_commitments: self.new_commitments.storage,
Expand Down Expand Up @@ -239,7 +243,7 @@ impl PrivateContext {
}

// docs:start:context_message_portal
pub fn message_portal(&mut self, content: Field)
pub fn message_portal(&mut self, content: Field)
// docs:end:context_message_portal
{
self.new_l2_to_l1_msgs.push(content);
Expand All @@ -254,7 +258,7 @@ impl PrivateContext {
msg_key: Field,
content: Field,
secret: Field
)
)
// docs:end:context_consume_l1_to_l2_message
{
let nullifier = process_l1_to_l2_message(self.historical_header.state.l1_to_l2_message_tree.root, self.this_address(), self.this_portal_address(), self.chain_id(), self.version(), msg_key, content, secret);
Expand All @@ -278,8 +282,8 @@ impl PrivateContext {

pub fn call_private_function<ARGS_COUNT>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args: [Field; ARGS_COUNT]
) -> [Field; RETURN_VALUES_LENGTH] {
let args_hash = hash_args(args);
Expand All @@ -289,8 +293,8 @@ impl PrivateContext {

pub fn call_private_function_no_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
contract_address: AztecAddress,
function_selector: FunctionSelector,
) -> [Field; RETURN_VALUES_LENGTH] {
self.call_private_function_with_packed_args(contract_address, function_selector, 0)
}
Expand All @@ -303,14 +307,14 @@ impl PrivateContext {
) -> [Field; RETURN_VALUES_LENGTH] {
let item = call_private_function_internal(
contract_address,
function_selector,
function_selector,
args_hash,
self.side_effect_counter,
);

assert_eq(item.public_inputs.call_context.start_side_effect_counter, self.side_effect_counter);
self.side_effect_counter = item.public_inputs.end_side_effect_counter + 1;

assert(contract_address.eq(item.contract_address));
assert(function_selector.eq(item.function_data.selector));

Expand All @@ -333,8 +337,8 @@ impl PrivateContext {

pub fn call_public_function<ARGS_COUNT>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args: [Field; ARGS_COUNT]
) {
let args_hash = hash_args(args);
Expand All @@ -344,7 +348,7 @@ impl PrivateContext {

pub fn call_public_function_no_args(
&mut self,
contract_address: AztecAddress,
contract_address: AztecAddress,
function_selector: FunctionSelector,
) {
self.call_public_function_with_packed_args(contract_address, function_selector, 0)
Expand All @@ -357,8 +361,8 @@ impl PrivateContext {
args_hash: Field
) {
let fields = enqueue_public_function_call_internal(
contract_address,
function_selector,
contract_address,
function_selector,
args_hash,
self.side_effect_counter
);
Expand Down Expand Up @@ -395,7 +399,7 @@ impl PrivateContext {
assert_eq(item.public_inputs.call_context.start_side_effect_counter, self.side_effect_counter);
// We increment the sideffect counter by one, to account for the call itself being a side effect.
self.side_effect_counter = self.side_effect_counter + 1;

assert(args_hash == item.public_inputs.args_hash);

// Assert that the call context of the enqueued call generated by the oracle matches our request.
Expand Down Expand Up @@ -457,7 +461,7 @@ impl PublicContext {

new_l2_to_l1_msgs: BoundedVec::new(0),


unencrypted_logs_hash: BoundedVec::new(0),
unencrypted_logs_preimages_length: 0,

Expand Down Expand Up @@ -574,27 +578,27 @@ impl PublicContext {

pub fn call_public_function<ARGS_COUNT>(
_self: Self,
contract_address: AztecAddress,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args: [Field; ARGS_COUNT],
) -> [Field; RETURN_VALUES_LENGTH] {
let args_hash = hash_args(args);
assert(args_hash == arguments::pack_arguments(args));
call_public_function_internal(
contract_address,
function_selector,
contract_address,
function_selector,
args_hash,
)
}

pub fn call_public_function_no_args(
_self: Self,
contract_address: AztecAddress,
contract_address: AztecAddress,
function_selector: FunctionSelector,
) -> [Field; RETURN_VALUES_LENGTH] {
call_public_function_internal(
contract_address,
function_selector,
contract_address,
function_selector,
0,
)
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"clean": "rm -rf ./dest .tsbuildinfo",
"formatting": "run -T prettier --check ./src && run -T eslint ./src",
"formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src",
"remake-constants": "node --loader ts-node/esm src/scripts/constants.in.ts && prettier -w src/constants.gen.ts",
"remake-constants": "node --loader ts-node/esm src/scripts/constants.in.ts && prettier -w src/constants.gen.ts && forge fmt --root ../../l1-contracts",
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --passWithNoTests"
},
"inherits": [
Expand Down
7 changes: 5 additions & 2 deletions yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export const MAX_READ_REQUESTS_PER_TX = 128;
export const MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX = 4;
export const NUM_ENCRYPTED_LOGS_HASHES_PER_TX = 1;
export const NUM_UNENCRYPTED_LOGS_HASHES_PER_TX = 1;
export const MAX_NEW_COMMITMENTS_PER_TX_META = 8;
export const MAX_NEW_NULLIFIERS_PER_TX_META = 8;
export const MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX_META = 2;
export const NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP = 16;
export const VK_TREE_HEIGHT = 3;
export const FUNCTION_TREE_HEIGHT = 5;
Expand Down Expand Up @@ -64,8 +67,8 @@ export const STATE_REFERENCE_LENGTH = 10;
export const HEADER_LENGTH = 18;
export const FUNCTION_DATA_LENGTH = 4;
export const CONTRACT_DEPLOYMENT_DATA_LENGTH = 6;
export const PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 204;
export const PRIVATE_CALL_STACK_ITEM_LENGTH = 209;
export const PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 205;
export const PRIVATE_CALL_STACK_ITEM_LENGTH = 210;
export const CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
export const CONTRACT_STORAGE_READ_LENGTH = 2;
export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 201;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@

exports[`PrivateCallStackItem computes empty item hash 1`] = `
Fr {
"asBigInt": 21230813221792739829032218511346459661622392006403997687170807299887246304465n,
"asBigInt": 2538825319674219123846958071590111740225106671201233263868844578355336664781n,
"asBuffer": {
"data": [
46,
240,
54,
5,
156,
236,
152,
233,
157,
232,
67,
153,
229,
221,
27,
22,
168,
75,
195,
183,
254,
111,
10,
250,
64,
122,
141,
166,
174,
48,
176,
125,
64,
205,
116,
188,
67,
86,
205,
128,
113,
43,
99,
253,
225,
42,
162,
230,
75,
8,
243,
160,
150,
65,
149,
200,
179,
79,
100,
209,
],
"type": "Buffer",
},
Expand All @@ -45,41 +45,41 @@ Fr {

exports[`PrivateCallStackItem computes hash 1`] = `
Fr {
"asBigInt": 14723811775539034985226914197401344473208275906441619992416957589322673569693n,
"asBigInt": 14128890724092381488704229567615295219923311195211320351016242351826479641614n,
"asBuffer": {
"data": [
32,
141,
97,
211,
254,
114,
95,
205,
151,
179,
64,
109,
121,
32,
239,
244,
117,
162,
13,
12,
74,
168,
124,
126,
12,
31,
60,
171,
51,
195,
14,
113,
231,
119,
201,
1,
173,
160,
14,
186,
38,
104,
235,
90,
76,
0,
233,
167,
78,
217,
88,
247,
59,
146,
221,
124,
42,
63,
157,
171,
156,
116,
14,
],
"type": "Buffer",
},
Expand Down
Loading

0 comments on commit 4f248b5

Please sign in to comment.