Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: call stack item cleanup #4381

Merged
merged 16 commits into from
Feb 5, 2024
Merged
2 changes: 1 addition & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ library Constants {
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 CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 201;
uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674;
uint256 internal constant CALL_PRIVATE_FUNCTION_RETURN_SIZE = 210;
uint256 internal constant COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
uint256 internal constant NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
uint256 internal constant PUBLIC_DATA_WRITES_NUM_BYTES_PER_BASE_ROLLUP = 1024;
Expand Down
1 change: 1 addition & 0 deletions yarn-project/acir-simulator/src/acvm/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function toACVMField(
* are empty since this is just an execution request, so we don't send them to the circuit.
* @param item - The public call stack item to serialize to be passed onto Noir.
* @returns The fields expected by the enqueue_public_function_call_oracle Aztec.nr function.
* TODO(#4380): Nuke this and replace it with PublicCallRequest.toFields()
*/
export function toAcvmEnqueuePublicFunctionResult(item: PublicCallRequest): ACVMField[] {
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function executePrivateFunction(
publicInputs.unencryptedLogsHash = to2Fields(unencryptedLogs.hash());
publicInputs.unencryptedLogPreimagesLength = new Fr(unencryptedLogs.getSerializedLength());

const callStackItem = new PrivateCallStackItem(contractAddress, functionData, publicInputs, false);
const callStackItem = new PrivateCallStackItem(contractAddress, functionData, publicInputs);
const returnValues = decodeReturnValues(artifact, publicInputs.returnValues);
const readRequestPartialWitnesses = context.getReadRequestPartialWitnesses(publicInputs.readRequests);
const newNotes = context.getNewNotes();
Expand Down
9 changes: 1 addition & 8 deletions yarn-project/aztec-nr/authwit/src/entrypoint.nr
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
use dep::aztec::abi;
use dep::aztec::context::PrivateContext;
use dep::aztec::protocol_types::{
abis::{
call_stack_item::{
PrivateCallStackItem,
PublicCallStackItem,
},
function_selector::FunctionSelector,
},
abis::function_selector::FunctionSelector,
address::AztecAddress,
constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD,
hash::pedersen_hash,
Expand Down
20 changes: 4 additions & 16 deletions yarn-project/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::{
header::get_header_at,
nullifier_key::{get_nullifier_key_pair, NullifierKeyPair},
},
utils::Reader,
};
use dep::protocol_types::{
abis::{
Expand All @@ -24,10 +23,10 @@ use dep::protocol_types::{
function_data::FunctionData,
function_selector::FunctionSelector,
nullifier_key_validation_request::NullifierKeyValidationRequest,
private_call_stack_item::PrivateCallStackItem,
private_circuit_public_inputs::PrivateCircuitPublicInputs,
public_call_stack_item::PublicCallStackItem,
public_circuit_public_inputs::PublicCircuitPublicInputs,
call_stack_item::PrivateCallStackItem,
call_stack_item::PublicCallStackItem,
side_effect::{SideEffect, SideEffectLinkedToNoteHash},
},
address::{
Expand Down Expand Up @@ -58,6 +57,7 @@ use dep::protocol_types::{
hash::hash_args,
header::Header,
state_reference::StateReference,
utils::reader::Reader,
};
use dep::std::{
option::Option,
Expand Down Expand Up @@ -301,22 +301,12 @@ impl PrivateContext {
function_selector: FunctionSelector,
args_hash: Field
) -> [Field; RETURN_VALUES_LENGTH] {
let fields = call_private_function_internal(
let item = call_private_function_internal(
contract_address,
function_selector,
args_hash,
self.side_effect_counter,
);
let mut reader = Reader::new(fields);

let item = PrivateCallStackItem {
contract_address: AztecAddress::from_field(reader.read()),
function_data: reader.read_struct(FunctionData::deserialize),
public_inputs: reader.read_struct(PrivateCircuitPublicInputs::deserialize),
is_execution_request: reader.read() as bool,
};

reader.finish();

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;
Expand All @@ -326,8 +316,6 @@ impl PrivateContext {

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

assert(item.is_execution_request == false);

// Assert that the call context of the enqueued call generated by the oracle matches our request.
// We are issuing a regular call which is not delegate, static, or deployment. We also constrain
// the msg_sender in the nested call to be equal to our address, and the execution context address
Expand Down
17 changes: 11 additions & 6 deletions yarn-project/aztec-nr/aztec/src/oracle/call_private_function.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use dep::protocol_types::{
abis::function_selector::FunctionSelector,
abis::{
function_selector::FunctionSelector,
private_call_stack_item::PrivateCallStackItem,
},
address::AztecAddress,
constants::CALL_PRIVATE_FUNCTION_RETURN_SIZE,
constants::PRIVATE_CALL_STACK_ITEM_LENGTH,
};

#[oracle(callPrivateFunction)]
Expand All @@ -10,18 +13,20 @@ fn call_private_function_oracle(
_function_selector: FunctionSelector,
_args_hash: Field,
_start_side_effect_counter: u32
) -> [Field; CALL_PRIVATE_FUNCTION_RETURN_SIZE] {}
) -> [Field; PRIVATE_CALL_STACK_ITEM_LENGTH] {}

unconstrained pub fn call_private_function_internal(
contract_address: AztecAddress,
function_selector: FunctionSelector,
args_hash: Field,
start_side_effect_counter: u32
) -> [Field; CALL_PRIVATE_FUNCTION_RETURN_SIZE] {
call_private_function_oracle(
) -> PrivateCallStackItem {
let fields = call_private_function_oracle(
contract_address,
function_selector,
args_hash,
start_side_effect_counter
)
);

PrivateCallStackItem::deserialize(fields)
}
43 changes: 0 additions & 43 deletions yarn-project/aztec-nr/aztec/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,3 @@ pub fn full_field_less_than(lhs: Field, rhs: Field) -> bool {
pub fn full_field_greater_than(lhs: Field, rhs: Field) -> bool {
rhs.lt(lhs)
}

// TODO(benesjan): also in circuits. NUKE THIS!
struct Reader<N> {
data: [Field; N],
offset: Field,
}

impl<N> Reader<N> {
pub fn new(data: [Field; N]) -> Self {
Self { data, offset: 0 }
}

pub fn read(&mut self) -> Field {
let result = self.data[self.offset];
self.offset += 1;
result
}

pub fn read_array<K>(&mut self, mut result: [Field; K]) -> [Field; K] {
for i in 0..K {
result[i] = self.data[self.offset + i];
}
self.offset += K;
result
}

// TODO(#4394)
pub fn read_struct<T, K>(&mut self, deserialise: fn([Field; K]) -> T) -> T {
let result = deserialise(self.read_array([0; K]));
result
}

pub fn read_struct_array<T, K, C>(&mut self, deserialise: fn([Field; K]) -> T, mut result: [T; C]) -> [T; C] {
for i in 0..C {
result[i] = self.read_struct(deserialise);
}
result
}

pub fn finish(self) {
assert(self.offset == self.data.len(), "Reader did not read all data");
}
}
43 changes: 0 additions & 43 deletions yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,6 @@ exports[`abis Computes an empty nullifier hash 1`] = `"0x066e6cdc4a6ba5e4781ded

exports[`abis Computes an empty sideeffect hash 1`] = `"0x27b1d0839a5b23baf12a8d195b18ac288fcf401afb2f70b8a4b529ede5fa9fed"`;

exports[`abis compute private call stack item hash 1`] = `
Fr {
"asBigInt": 20809484660315254497535468009354600907499263083154533860853878665971818104219n,
"asBuffer": {
"data": [
46,
1,
192,
60,
242,
95,
113,
243,
178,
218,
79,
199,
55,
206,
59,
194,
234,
24,
158,
215,
200,
157,
95,
167,
67,
42,
204,
172,
237,
37,
5,
155,
],
"type": "Buffer",
},
}
`;

exports[`abis compute public call stack item hash 1`] = `
Fr {
"asBigInt": 4942803204430729321299247114280769628204291639882373728884145897482860239103n,
Expand Down
8 changes: 0 additions & 8 deletions yarn-project/circuits.js/src/abis/abis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import {
makeAztecAddress,
makeEthAddress,
makePrivateCallStackItem,
makePublicCallStackItem,
makeTxRequest,
makeVerificationKey,
Expand All @@ -27,7 +26,6 @@ import {
computeFunctionSelector,
computeFunctionTreeRoot,
computeNullifierHash,
computePrivateCallStackItemHash,
computePublicCallStackItemHash,
computePublicDataTreeLeafSlot,
computePublicDataTreeValue,
Expand Down Expand Up @@ -156,12 +154,6 @@ describe('abis', () => {
expect(hash).toMatchSnapshot();
});

it('compute private call stack item hash', () => {
const item = makePrivateCallStackItem();
const hash = computePrivateCallStackItemHash(item);
expect(hash).toMatchSnapshot();
});

it('compute public call stack item hash', () => {
const item = makePublicCallStackItem();
const hash = computePublicCallStackItemHash(item);
Expand Down
42 changes: 3 additions & 39 deletions yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
FunctionData,
FunctionLeafPreimage,
NewContractData,
PrivateCallStackItem,
PublicCallStackItem,
PublicCircuitPublicInputs,
SideEffect,
Expand Down Expand Up @@ -154,10 +153,7 @@ export function computeFunctionTreeRoot(fnLeaves: Fr[]) {
*/
export function hashConstructor(functionData: FunctionData, argsHash: Fr, constructorVKHash: Buffer): Fr {
return Fr.fromBuffer(
pedersenHash(
[computeFunctionDataHash(functionData).toBuffer(), argsHash.toBuffer(), constructorVKHash],
GeneratorIndex.CONSTRUCTOR,
),
pedersenHash([functionData.hash().toBuffer(), argsHash.toBuffer(), constructorVKHash], GeneratorIndex.CONSTRUCTOR),
);
}

Expand Down Expand Up @@ -294,7 +290,7 @@ export function computeTxHash(txRequest: TxRequest): Fr {
pedersenHash(
[
txRequest.origin.toBuffer(),
computeFunctionDataHash(txRequest.functionData).toBuffer(),
txRequest.functionData.hash().toBuffer(),
txRequest.argsHash.toBuffer(),
computeTxContextHash(txRequest.txContext).toBuffer(),
],
Expand All @@ -303,20 +299,6 @@ export function computeTxHash(txRequest: TxRequest): Fr {
);
}

function computeFunctionDataHash(functionData: FunctionData): Fr {
return Fr.fromBuffer(
pedersenHash(
[
functionData.selector.toBuffer(32),
new Fr(functionData.isInternal).toBuffer(),
new Fr(functionData.isPrivate).toBuffer(),
new Fr(functionData.isConstructor).toBuffer(),
],
GeneratorIndex.FUNCTION_DATA,
),
);
}

function computeTxContextHash(txContext: TxContext): Fr {
return Fr.fromBuffer(
pedersenHash(
Expand Down Expand Up @@ -349,24 +331,6 @@ function computeContractDeploymentDataHash(data: ContractDeploymentData): Fr {
);
}

/**
* Computes a call stack item hash.
* @param callStackItem - The call stack item.
* @returns The call stack item hash.
*/
export function computePrivateCallStackItemHash(callStackItem: PrivateCallStackItem): Fr {
return Fr.fromBuffer(
pedersenHash(
[
callStackItem.contractAddress.toBuffer(),
computeFunctionDataHash(callStackItem.functionData).toBuffer(),
callStackItem.publicInputs.hash().toBuffer(),
],
GeneratorIndex.CALL_STACK_ITEM,
),
);
}

export function computeCommitmentsHash(input: SideEffect) {
return pedersenHash([input.value.toBuffer(), input.counter.toBuffer()], GeneratorIndex.SIDE_EFFECT);
}
Expand Down Expand Up @@ -398,7 +362,7 @@ export function computePublicCallStackItemHash({

return Fr.fromBuffer(
pedersenHash(
[contractAddress.toBuffer(), computeFunctionDataHash(functionData).toBuffer(), publicInputs.hash().toBuffer()],
[contractAddress, functionData.hash(), publicInputs.hash()].map(f => f.toBuffer()),
GeneratorIndex.CALL_STACK_ITEM,
),
);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ 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 CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
export const CONTRACT_STORAGE_READ_LENGTH = 2;
export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 201;
export const GET_NOTES_ORACLE_RETURN_LENGTH = 674;
export const CALL_PRIVATE_FUNCTION_RETURN_SIZE = 210;
export const COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
export const NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
export const PUBLIC_DATA_WRITES_NUM_BYTES_PER_BASE_ROLLUP = 1024;
Expand Down
Loading
Loading