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

feat: introduce avm circuit public inputs #9759

Merged
merged 12 commits into from
Nov 7, 2024
2 changes: 1 addition & 1 deletion barretenberg/cpp/pil/avm/constants_gen.pil
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace constants(256);
pol MAX_NOTE_HASHES_PER_CALL = 16;
pol MAX_NULLIFIERS_PER_CALL = 16;
pol MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL = 16;
pol MAX_ENQUEUED_CALLS_PER_CALL = 16;
pol MAX_L2_TO_L1_MSGS_PER_CALL = 2;
pol MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL = 64;
pol MAX_PUBLIC_DATA_READS_PER_CALL = 64;
Expand Down
3 changes: 2 additions & 1 deletion barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#define MAX_NOTE_HASHES_PER_CALL 16
#define MAX_NULLIFIERS_PER_CALL 16
#define MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL 16
#define MAX_ENQUEUED_CALLS_PER_CALL 16
#define MAX_L2_TO_L1_MSGS_PER_CALL 2
#define MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL 64
#define MAX_PUBLIC_DATA_READS_PER_CALL 64
Expand Down Expand Up @@ -35,6 +35,7 @@
#define HEADER_LENGTH 24
#define PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH 866
#define PUBLIC_CONTEXT_INPUTS_LENGTH 41
#define AVM_ACCUMULATED_DATA_LENGTH 318
#define AVM_VERIFICATION_KEY_LENGTH_IN_FIELDS 86
#define AVM_PROOF_LENGTH_IN_FIELDS 4176
#define AVM_PUBLIC_COLUMN_MAX_SIZE 1024
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/vm/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ inline const uint32_t PUBLIC_CALLSTACK_PCPI_OFFSET =
PUBLIC_DATA_READ_PCPI_OFFSET + (MAX_PUBLIC_DATA_READS_PER_CALL * CONTRACT_STORAGE_READ_LENGTH);

inline const uint32_t NEW_NOTE_HASHES_PCPI_OFFSET =
PUBLIC_CALLSTACK_PCPI_OFFSET + (MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL * PUBLIC_INNER_CALL_REQUEST_LENGTH);
PUBLIC_CALLSTACK_PCPI_OFFSET + (MAX_ENQUEUED_CALLS_PER_CALL * PUBLIC_INNER_CALL_REQUEST_LENGTH);

inline const uint32_t NEW_NULLIFIERS_PCPI_OFFSET =
NEW_NOTE_HASHES_PCPI_OFFSET + (MAX_NOTE_HASHES_PER_CALL * NOTE_HASH_LENGTH);
Expand Down
28 changes: 18 additions & 10 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ library Constants {
uint256 internal constant MAX_NOTE_HASHES_PER_CALL = 16;
uint256 internal constant MAX_NULLIFIERS_PER_CALL = 16;
uint256 internal constant MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL = 4;
uint256 internal constant MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL = 16;
uint256 internal constant MAX_ENQUEUED_CALLS_PER_CALL = 16;
uint256 internal constant MAX_L2_TO_L1_MSGS_PER_CALL = 2;
uint256 internal constant MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL = 64;
uint256 internal constant MAX_PUBLIC_DATA_READS_PER_CALL = 64;
Expand Down Expand Up @@ -56,7 +56,7 @@ library Constants {
uint256 internal constant MAX_NOTE_HASHES_PER_TX = 64;
uint256 internal constant MAX_NULLIFIERS_PER_TX = 64;
uint256 internal constant MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX = 8;
uint256 internal constant MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX = 32;
uint256 internal constant MAX_ENQUEUED_CALLS_PER_TX = 32;
uint256 internal constant PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 1;
uint256 internal constant MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 63;
Expand Down Expand Up @@ -194,17 +194,20 @@ library Constants {
uint256 internal constant SCOPED_NOTE_HASH_LENGTH = 3;
uint256 internal constant NULLIFIER_LENGTH = 3;
uint256 internal constant SCOPED_NULLIFIER_LENGTH = 4;
uint256 internal constant PUBLIC_DATA_WRITE_LENGTH = 2;
uint256 internal constant PUBLIC_CALL_STACK_ITEM_COMPRESSED_LENGTH = 12;
uint256 internal constant PRIVATE_CALL_REQUEST_LENGTH = 8;
uint256 internal constant PUBLIC_CALL_REQUEST_LENGTH = 6;
uint256 internal constant PUBLIC_CALL_REQUEST_LENGTH = 5;
uint256 internal constant COUNTED_PUBLIC_CALL_REQUEST_LENGTH = 6;
uint256 internal constant PUBLIC_INNER_CALL_REQUEST_LENGTH = 13;
uint256 internal constant ROLLUP_VALIDATION_REQUESTS_LENGTH = 2;
uint256 internal constant STATE_REFERENCE_LENGTH = 8;
uint256 internal constant TREE_SNAPSHOTS_LENGTH = 8;
uint256 internal constant TX_CONTEXT_LENGTH = 9;
uint256 internal constant TX_REQUEST_LENGTH = 13;
uint256 internal constant TOTAL_FEES_LENGTH = 1;
uint256 internal constant HEADER_LENGTH = 24;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 501;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 500;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 866;
uint256 internal constant PRIVATE_CONTEXT_INPUTS_LENGTH = 38;
uint256 internal constant PUBLIC_CONTEXT_INPUTS_LENGTH = 41;
Expand All @@ -216,15 +219,20 @@ library Constants {
uint256 internal constant NUM_PUBLIC_VALIDATION_REQUEST_ARRAYS = 5;
uint256 internal constant PUBLIC_VALIDATION_REQUESTS_LENGTH = 834;
uint256 internal constant PUBLIC_DATA_UPDATE_REQUEST_LENGTH = 3;
uint256 internal constant COMBINED_ACCUMULATED_DATA_LENGTH = 610;
uint256 internal constant COMBINED_ACCUMULATED_DATA_LENGTH = 547;
uint256 internal constant TX_CONSTANT_DATA_LENGTH = 35;
uint256 internal constant COMBINED_CONSTANT_DATA_LENGTH = 44;
uint256 internal constant PRIVATE_ACCUMULATED_DATA_LENGTH = 1064;
uint256 internal constant PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 1888;
uint256 internal constant PUBLIC_ACCUMULATED_DATA_LENGTH = 1055;
uint256 internal constant PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 1878;
uint256 internal constant PUBLIC_ACCUMULATED_DATA_LENGTH = 1023;
uint256 internal constant NUM_PUBLIC_ACCUMULATED_DATA_ARRAYS = 8;
uint256 internal constant PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2997;
uint256 internal constant VM_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2374;
uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 664;
uint256 internal constant PRIVATE_TO_PUBLIC_ACCUMULATED_DATA_LENGTH = 578;
uint256 internal constant PRIVATE_TO_AVM_ACCUMULATED_DATA_LENGTH = 160;
uint256 internal constant NUM_PRIVATE_TO_AVM_ACCUMULATED_DATA_ARRAYS = 3;
uint256 internal constant PRIVATE_TO_PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 1199;
uint256 internal constant PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2932;
uint256 internal constant VM_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2341;
uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 601;
uint256 internal constant CONSTANT_ROLLUP_DATA_LENGTH = 13;
uint256 internal constant BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH = 30;
uint256 internal constant BLOCK_ROOT_OR_BLOCK_MERGE_PUBLIC_INPUTS_LENGTH = 60;
Expand Down
28 changes: 15 additions & 13 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ use dep::protocol_types::{
private_circuit_public_inputs::PrivateCircuitPublicInputs,
public_call_request::PublicCallRequest,
read_request::ReadRequest,
side_effect::Counted,
validation_requests::{KeyValidationRequest, KeyValidationRequestAndGenerator},
},
address::{AztecAddress, EthAddress},
constants::{
MAX_ENCRYPTED_LOGS_PER_CALL, MAX_KEY_VALIDATION_REQUESTS_PER_CALL,
MAX_L2_TO_L1_MSGS_PER_CALL, MAX_NOTE_ENCRYPTED_LOGS_PER_CALL,
MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, MAX_NOTE_HASHES_PER_CALL,
MAX_NULLIFIER_READ_REQUESTS_PER_CALL, MAX_NULLIFIERS_PER_CALL,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_UNENCRYPTED_LOGS_PER_CALL, PUBLIC_DISPATCH_SELECTOR,
MAX_ENCRYPTED_LOGS_PER_CALL, MAX_ENQUEUED_CALLS_PER_CALL,
MAX_KEY_VALIDATION_REQUESTS_PER_CALL, MAX_L2_TO_L1_MSGS_PER_CALL,
MAX_NOTE_ENCRYPTED_LOGS_PER_CALL, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
MAX_NOTE_HASHES_PER_CALL, MAX_NULLIFIER_READ_REQUESTS_PER_CALL, MAX_NULLIFIERS_PER_CALL,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, MAX_UNENCRYPTED_LOGS_PER_CALL,
PUBLIC_DISPATCH_SELECTOR,
},
header::Header,
messaging::l2_to_l1_message::L2ToL1Message,
Expand Down Expand Up @@ -66,7 +67,7 @@ pub struct PrivateContext {
nullifiers: BoundedVec<Nullifier, MAX_NULLIFIERS_PER_CALL>,

private_call_requests: BoundedVec<PrivateCallRequest, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL>,
public_call_requests: BoundedVec<PublicCallRequest, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL>,
public_call_requests: BoundedVec<Counted<PublicCallRequest>, MAX_ENQUEUED_CALLS_PER_CALL>,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove counter from PublicCallRequest and use Counted to add counter to any struct.

Will do the same for other side effects in another PR.

public_teardown_call_request: PublicCallRequest,
l2_to_l1_msgs: BoundedVec<L2ToL1Message, MAX_L2_TO_L1_MSGS_PER_CALL>,
// docs:end:private-context
Expand Down Expand Up @@ -503,15 +504,16 @@ impl PrivateContext {

// Public calls are rerouted through the dispatch function.
let function_selector = comptime { FunctionSelector::from_field(PUBLIC_DISPATCH_SELECTOR) };
let call_context = CallContext {

let call_request = PublicCallRequest {
msg_sender: self.this_address(),
contract_address,
function_selector,
is_static_call,
args_hash,
};

let call_request = PublicCallRequest { call_context, args_hash, counter };
self.public_call_requests.push(call_request);
self.public_call_requests.push(Counted::new(call_request, counter));
}

pub fn set_public_teardown_function<let ARGS_COUNT: u32>(
Expand Down Expand Up @@ -555,14 +557,14 @@ impl PrivateContext {
);

let function_selector = comptime { FunctionSelector::from_field(PUBLIC_DISPATCH_SELECTOR) };
let call_context = CallContext {

self.public_teardown_call_request = PublicCallRequest {
msg_sender: self.this_address(),
contract_address,
function_selector,
is_static_call,
args_hash,
};

self.public_teardown_call_request = PublicCallRequest { call_context, args_hash, counter };
}

fn next_counter(&mut self) -> u32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use dep::types::{
private_call_request::PrivateCallRequest,
private_circuit_public_inputs::PrivateCircuitPublicInputsArrayLengths,
private_kernel::private_call_data::PrivateCallData,
public_call_request::PublicCallRequest,
side_effect::{Ordered, RangeOrdered},
},
address::AztecAddress,
Expand All @@ -23,17 +24,6 @@ use dep::types::{
utils::arrays::find_index_hint,
};

fn validate_call_context(target_context: CallContext, this_context: CallContext) {
assert_eq(
target_context.msg_sender,
this_context.contract_address,
"incorrect msg_sender for call request",
);
if !target_context.is_static_call {
assert(this_context.is_static_call == false, "static call cannot make non-static calls");
}
}

fn validate_incrementing_counters_within_range<T, let N: u32>(
counter_start: u32,
counter_end: u32,
Expand Down Expand Up @@ -111,7 +101,6 @@ impl PrivateCallDataValidator {
self.validate_call();
self.validate_private_call_requests();
self.validate_public_call_requests();
self.validate_teardown_call_request();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into validate_public_call_requests.

self.validate_counters();
self.validate_note_logs(accumulated_note_hashes);
}
Expand Down Expand Up @@ -244,7 +233,18 @@ impl PrivateCallDataValidator {
for i in 0..call_requests.len() {
should_check &= i != num_requests;
if should_check {
validate_call_context(call_requests[i].call_context, public_inputs.call_context);
let call_request = call_requests[i];
assert_eq(
call_request.call_context.msg_sender,
public_inputs.call_context.contract_address,
"incorrect msg_sender for call request",
);
if !call_request.call_context.is_static_call {
assert(
public_inputs.call_context.is_static_call == false,
"static call cannot make non-static calls",
);
}
}
}

Expand Down Expand Up @@ -274,22 +274,34 @@ impl PrivateCallDataValidator {

fn validate_public_call_requests(self) {
let public_inputs = self.data.public_inputs;

let call_requests = public_inputs.public_call_requests;
let num_requests = self.array_lengths.public_call_requests;
let mut should_check = true;
for i in 0..call_requests.len() {
should_check &= i != num_requests;
if should_check {
validate_call_context(call_requests[i].call_context, public_inputs.call_context);
self.validate_public_call_request(call_requests[i].inner);
}
}

if !public_inputs.public_teardown_call_request.contract_address.is_zero() {
self.validate_public_call_request(public_inputs.public_teardown_call_request);
}
}

fn validate_teardown_call_request(self) {
let public_inputs = self.data.public_inputs;
let request = public_inputs.public_teardown_call_request;
if request.counter != 0 {
validate_call_context(request.call_context, public_inputs.call_context);
fn validate_public_call_request(self, call_request: PublicCallRequest) {
let this_context = self.data.public_inputs.call_context;
assert_eq(
call_request.msg_sender,
this_context.contract_address,
"incorrect msg_sender for call request",
);
if !call_request.is_static_call {
assert(
this_context.is_static_call == false,
"static call cannot make non-static calls",
);
}
}

Expand Down Expand Up @@ -356,20 +368,7 @@ impl PrivateCallDataValidator {
public_inputs.public_call_requests,
self.array_lengths.public_call_requests,
);

let teardown_call_request_count = if public_inputs.public_teardown_call_request.counter == 0
{
0
} else {
1
};
validate_incrementing_counters_within_range(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to check it as teardown_call_request does not have a counter anymore.

counter_start,
counter_end,
[public_inputs.public_teardown_call_request],
teardown_call_request_count,
);
}
}

fn validate_note_logs<let N: u32>(self, accumulated_note_hashes: [ScopedNoteHash; N]) {
let note_logs = self.data.public_inputs.note_encrypted_logs_hashes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ impl PrivateKernelCircuitOutputValidator {
private_call.historical_header,
"mismatch historical_header",
);
assert(
is_empty(self.output.constants.global_variables),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this check as a new struct TxConstantData is now used in private land, which includes all fields in CombinedConstantData except for global_variables.

"constants.global_variables must be empty",
);
assert_eq(self.output.constants.vk_tree_root, vk_tree_root, "mismatch vk_tree_root");
assert_eq(
self.output.constants.protocol_contract_tree_root,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use dep::types::{
abis::{
combined_constant_data::CombinedConstantData,
kernel_circuit_public_inputs::{
PrivateKernelCircuitPublicInputs, PrivateKernelCircuitPublicInputsBuilder,
},
max_block_number::MaxBlockNumber,
nullifier::{Nullifier, ScopedNullifier},
private_circuit_public_inputs::PrivateCircuitPublicInputs,
tx_constant_data::TxConstantData,
},
address::AztecAddress,
traits::is_empty,
Expand All @@ -31,12 +31,12 @@ impl PrivateKernelCircuitPublicInputsComposer {
) -> Self {
let mut public_inputs = PrivateKernelCircuitPublicInputsBuilder::empty();

public_inputs.constants = CombinedConstantData::private(
private_call_public_inputs.historical_header,
tx_request.tx_context,
public_inputs.constants = TxConstantData {
historical_header: private_call_public_inputs.historical_header,
tx_context: tx_request.tx_context,
vk_tree_root,
protocol_contract_tree_root,
);
};

// Since it's the first iteration, we need to push the tx hash nullifier into the `nullifiers` array
public_inputs.end.nullifiers.push(create_first_nullifier(tx_request));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::components::{
use dep::types::{
abis::{
accumulated_data::combined_accumulated_data::CombinedAccumulatedData,
combined_constant_data::CombinedConstantData,
global_variables::GlobalVariables,
kernel_circuit_public_inputs::{KernelCircuitPublicInputs, PrivateKernelCircuitPublicInputs},
log_hash::{NoteLogHash, ScopedEncryptedLogHash, ScopedLogHash},
note_hash::ScopedNoteHash,
Expand All @@ -33,7 +35,8 @@ impl TailOutputComposer {
let mut output = KernelCircuitPublicInputs::empty();
output.rollup_validation_requests = source.validation_requests.for_rollup;
output.end = self.build_combined_accumulated_data();
output.constants = source.constants;
output.constants =
CombinedConstantData::combine(source.constants, GlobalVariables::empty());
output.fee_payer = source.fee_payer;
output
}
Expand Down Expand Up @@ -62,10 +65,7 @@ impl TailOutputComposer {
0,
|len, l: ScopedLogHash| len + l.log_hash.length,
);
data.gas_used = meter_gas_used(
data,
self.output_composer.public_inputs.constants.tx_context.gas_settings,
);
data.gas_used = meter_gas_used(data);
data
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use dep::types::{
abis::{
accumulated_data::combined_accumulated_data::CombinedAccumulatedData, gas::Gas,
gas_settings::GasSettings,
},
abis::{accumulated_data::combined_accumulated_data::CombinedAccumulatedData, gas::Gas},
constants::{
DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE, L2_GAS_PER_LOG_BYTE, L2_GAS_PER_NOTE_HASH,
L2_GAS_PER_NULLIFIER,
},
utils::arrays::array_length,
};

pub fn meter_gas_used(data: CombinedAccumulatedData, gas_settings: GasSettings) -> Gas {
pub fn meter_gas_used(data: CombinedAccumulatedData) -> Gas {
let mut metered_da_bytes = 0;
let mut metered_l2_gas = 0;

Expand All @@ -34,6 +31,5 @@ pub fn meter_gas_used(data: CombinedAccumulatedData, gas_settings: GasSettings)
metered_da_bytes += data.unencrypted_log_preimages_length as u32;
metered_l2_gas += data.unencrypted_log_preimages_length as u32 * L2_GAS_PER_LOG_BYTE;

let teardown_gas = gas_settings.teardown_gas_limits;
Gas::new(metered_da_bytes * DA_GAS_PER_BYTE, metered_l2_gas) + Gas::tx_overhead() + teardown_gas
Copy link
Collaborator Author

@LeilaWang LeilaWang Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't include teardown_gas for private-only tx.

Before this, the users would have to set it to empty in GasSettings to avoid paying extra fees, which was quite annoying.

Gas::new(metered_da_bytes * DA_GAS_PER_BYTE, metered_l2_gas) + Gas::tx_overhead()
}
Loading
Loading