Skip to content

Commit

Permalink
separating combined accumulated data
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 21, 2024
1 parent a82c2ba commit 3f5cc12
Show file tree
Hide file tree
Showing 16 changed files with 687 additions and 544 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use dep::std;
use dep::types::{
abis::{
call_request::CallRequest, combined_accumulated_data::CombinedAccumulatedData,
function_data::FunctionData, kernel_circuit_public_inputs::PrivateKernelCircuitPublicInputsBuilder,
call_request::CallRequest, accumulated_data::CombinedAccumulatedData, function_data::FunctionData,
kernel_circuit_public_inputs::PrivateKernelCircuitPublicInputsBuilder,
membership_witness::ReadRequestMembershipWitness, new_contract_data::NewContractData,
nullifier_key_validation_request::NullifierKeyValidationRequestContext,
private_circuit_public_inputs::PrivateCircuitPublicInputs,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dep::types::{
abis::{
call_request::CallRequest, public_call_stack_item::PublicCallStackItem,
combined_accumulated_data::{CombinedAccumulatedData, CombinedAccumulatedDataBuilder},
accumulated_data::{CombinedAccumulatedData, CombinedAccumulatedDataBuilder},
kernel_circuit_public_inputs::PublicKernelCircuitPublicInputsBuilder,
new_contract_data::NewContractData, kernel_data::{PrivateKernelTailData, PublicKernelData},
public_call_data::PublicCallData, public_circuit_public_inputs::PublicCircuitPublicInputs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use dep::types::{
},
nullifier_leaf_preimage::NullifierLeafPreimage, public_data_update_request::PublicDataUpdateRequest,
public_data_read::PublicDataRead, kernel_data::PublicKernelData,
side_effect::{SideEffect, SideEffectLinkedToNoteHash},
combined_accumulated_data::CombinedAccumulatedData
side_effect::{SideEffect, SideEffectLinkedToNoteHash}, accumulated_data::CombinedAccumulatedData
},
constants::{
NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH, NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH,
Expand Down Expand Up @@ -569,7 +568,7 @@ mod tests {
new_contract_data::NewContractData, nullifier_leaf_preimage::NullifierLeafPreimage,
public_data_read::PublicDataRead, public_data_update_request::PublicDataUpdateRequest,
kernel_data::PublicKernelData, side_effect::SideEffect,
combined_accumulated_data::CombinedAccumulatedData
accumulated_data::CombinedAccumulatedData
},
address::{AztecAddress, EthAddress},
constants::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod nullifier_key_validation_request;
mod public_data_read;
mod public_data_update_request;

mod combined_accumulated_data;
mod accumulated_data;

mod private_kernel;
mod kernel_circuit_public_inputs;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mod accumulated_non_revertible_data_builder;
mod accumulated_revertible_data_builder;
mod combined_accumulated_data;
mod combined_accumulated_data_builder;
mod private_accumulated_non_revertible_data;
mod private_accumulated_revertible_data;
mod public_accumulated_non_revertible_data;
mod public_accumulated_revertible_data;

use crate::abis::accumulated_data::{
combined_accumulated_data::CombinedAccumulatedData,
private_accumulated_revertible_data::PrivateAccumulatedRevertibleData,
private_accumulated_non_revertible_data::PrivateAccumulatedNonRevertibleData,
combined_accumulated_data_builder::CombinedAccumulatedDataBuilder,
public_accumulated_non_revertible_data::PublicAccumulatedNonRevertibleData,
public_accumulated_revertible_data::PublicAccumulatedRevertibleData,
accumulated_non_revertible_data_builder::AccumulatedNonRevertibleDataBuilder,
accumulated_revertible_data_builder::AccumulatedRevertibleDataBuilder
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::{
abis::{
accumulated_data::{
private_accumulated_non_revertible_data::PrivateAccumulatedNonRevertibleData,
public_accumulated_non_revertible_data::PublicAccumulatedNonRevertibleData
},
call_request::CallRequest, public_data_read::PublicDataRead,
public_data_update_request::PublicDataUpdateRequest,
side_effect::{SideEffect, SideEffectLinkedToNoteHash}
}
};
use crate::constants::{
MAX_NON_REVERTIBLE_COMMITMENTS_PER_TX, MAX_NON_REVERTIBLE_NULLIFIERS_PER_TX,
MAX_NON_REVERTIBLE_PUBLIC_CALL_STACK_LENGTH_PER_TX, MAX_NON_REVERTIBLE_PUBLIC_DATA_READS_PER_TX,
MAX_NON_REVERTIBLE_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
};

struct AccumulatedNonRevertibleDataBuilder {
new_commitments: BoundedVec<SideEffect, MAX_NON_REVERTIBLE_COMMITMENTS_PER_TX>,
new_nullifiers: BoundedVec<SideEffectLinkedToNoteHash, MAX_NON_REVERTIBLE_NULLIFIERS_PER_TX>,
public_call_stack: BoundedVec<CallRequest, MAX_NON_REVERTIBLE_PUBLIC_CALL_STACK_LENGTH_PER_TX>,
public_data_update_requests: BoundedVec<PublicDataUpdateRequest, MAX_NON_REVERTIBLE_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX>,
public_data_reads: BoundedVec<PublicDataRead, MAX_NON_REVERTIBLE_PUBLIC_DATA_READS_PER_TX>,
}

impl AccumulatedNonRevertibleDataBuilder {
pub fn to_private(self) -> PrivateAccumulatedNonRevertibleData {
PrivateAccumulatedNonRevertibleData {
new_commitments: self.new_commitments.storage,
new_nullifiers: self.new_nullifiers.storage,
public_call_stack: self.public_call_stack.storage
}
}
pub fn to_public(self) -> PublicAccumulatedNonRevertibleData {
PublicAccumulatedNonRevertibleData {
new_commitments: self.new_commitments.storage,
new_nullifiers: self.new_nullifiers.storage,
public_call_stack: self.public_call_stack.storage,
public_data_update_requests: self.public_data_update_requests.storage,
public_data_reads: self.public_data_reads.storage
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::{
abis::{
accumulated_data::{
private_accumulated_revertible_data::PrivateAccumulatedRevertibleData,
public_accumulated_revertible_data::PublicAccumulatedRevertibleData
},
call_request::CallRequest, new_contract_data::NewContractData,
nullifier_key_validation_request::NullifierKeyValidationRequestContext,
public_data_read::PublicDataRead, public_data_update_request::PublicDataUpdateRequest,
side_effect::{SideEffect, SideEffectLinkedToNoteHash}
}
};
use crate::constants::{
MAX_READ_REQUESTS_PER_TX, MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX, MAX_NEW_L2_TO_L1_MSGS_PER_TX, MAX_NEW_CONTRACTS_PER_TX,
NUM_FIELDS_PER_SHA256, MAX_REVERTIBLE_COMMITMENTS_PER_TX, MAX_REVERTIBLE_NULLIFIERS_PER_TX,
MAX_REVERTIBLE_PUBLIC_CALL_STACK_LENGTH_PER_TX, MAX_REVERTIBLE_PUBLIC_DATA_READS_PER_TX,
MAX_REVERTIBLE_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
};

struct AccumulatedRevertibleDataBuilder {
read_requests: BoundedVec<SideEffect, MAX_READ_REQUESTS_PER_TX>,
nullifier_key_validation_requests: BoundedVec<NullifierKeyValidationRequestContext, MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX>,

new_commitments: BoundedVec<SideEffect, MAX_REVERTIBLE_COMMITMENTS_PER_TX>,
new_nullifiers: BoundedVec<SideEffectLinkedToNoteHash, MAX_REVERTIBLE_NULLIFIERS_PER_TX>,

private_call_stack: BoundedVec<CallRequest, MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX>,
public_call_stack: BoundedVec<CallRequest, MAX_REVERTIBLE_PUBLIC_CALL_STACK_LENGTH_PER_TX>,
new_l2_to_l1_msgs: BoundedVec<Field, MAX_NEW_L2_TO_L1_MSGS_PER_TX>,

encrypted_logs_hash: [Field; NUM_FIELDS_PER_SHA256],
unencrypted_logs_hash: [Field; NUM_FIELDS_PER_SHA256],

// Here so that the gas cost of this request can be measured by circuits, without actually needing to feed in the
// variable-length data.
encrypted_log_preimages_length: Field,
unencrypted_log_preimages_length: Field,

new_contracts: BoundedVec<NewContractData, MAX_NEW_CONTRACTS_PER_TX>,

public_data_update_requests: BoundedVec<PublicDataUpdateRequest, MAX_REVERTIBLE_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX>,
public_data_reads: BoundedVec<PublicDataRead, MAX_REVERTIBLE_PUBLIC_DATA_READS_PER_TX>,
}

impl AccumulatedRevertibleDataBuilder {
pub fn to_private(self) -> PrivateAccumulatedRevertibleData {
PrivateAccumulatedRevertibleData {
new_commitments: self.new_commitments.storage,
new_nullifiers: self.new_nullifiers.storage,
private_call_stack: self.private_call_stack.storage,
public_call_stack: self.public_call_stack.storage,
new_l2_to_l1_msgs: self.new_l2_to_l1_msgs.storage,
encrypted_logs_hash: self.encrypted_logs_hash,
unencrypted_logs_hash: self.unencrypted_logs_hash,
encrypted_log_preimages_length: self.encrypted_log_preimages_length,
unencrypted_log_preimages_length: self.unencrypted_log_preimages_length,
new_contracts: self.new_contracts.storage
}
}

pub fn to_public(self) -> PublicAccumulatedRevertibleData {
PublicAccumulatedRevertibleData {
read_requests: self.read_requests.storage,
new_commitments: self.new_commitments.storage,
nullifier_key_validation_requests: self.nullifier_key_validation_requests.storage,
new_nullifiers: self.new_nullifiers.storage,
private_call_stack: self.private_call_stack.storage,
public_call_stack: self.public_call_stack.storage,
new_l2_to_l1_msgs: self.new_l2_to_l1_msgs.storage,
encrypted_logs_hash: self.encrypted_logs_hash,
unencrypted_logs_hash: self.unencrypted_logs_hash,
encrypted_log_preimages_length: self.encrypted_log_preimages_length,
unencrypted_log_preimages_length: self.unencrypted_log_preimages_length,
new_contracts: self.new_contracts.storage,
public_data_update_requests: self.public_data_update_requests.storage,
public_data_reads: self.public_data_reads.storage
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
use crate::{
abis::{
accumulated_data::{
public_accumulated_non_revertible_data::PublicAccumulatedNonRevertibleData,
public_accumulated_revertible_data::PublicAccumulatedRevertibleData
},
call_request::CallRequest, new_contract_data::NewContractData,
nullifier_key_validation_request::NullifierKeyValidationRequestContext,
public_data_read::PublicDataRead, public_data_update_request::PublicDataUpdateRequest,
side_effect::{SideEffect, SideEffectLinkedToNoteHash}
}
};
use crate::constants::{
MAX_READ_REQUESTS_PER_TX, MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX, MAX_NEW_COMMITMENTS_PER_TX,
MAX_NEW_NULLIFIERS_PER_TX, MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, MAX_NEW_L2_TO_L1_MSGS_PER_TX, MAX_NEW_CONTRACTS_PER_TX,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, MAX_PUBLIC_DATA_READS_PER_TX, NUM_FIELDS_PER_SHA256,
MAX_NON_REVERTIBLE_COMMITMENTS_PER_TX, MAX_NON_REVERTIBLE_NULLIFIERS_PER_TX,
MAX_NON_REVERTIBLE_PUBLIC_CALL_STACK_LENGTH_PER_TX, MAX_REVERTIBLE_COMMITMENTS_PER_TX,
MAX_REVERTIBLE_NULLIFIERS_PER_TX, MAX_REVERTIBLE_PUBLIC_CALL_STACK_LENGTH_PER_TX,
MAX_REVERTIBLE_PUBLIC_DATA_READS_PER_TX, MAX_REVERTIBLE_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
MAX_NON_REVERTIBLE_PUBLIC_DATA_READS_PER_TX, MAX_NON_REVERTIBLE_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
};

use dep::std::unsafe;
use crate::traits::is_empty;

use crate::utils::arrays::{array_cp, array_concat, array_to_bounded_vec};

struct CombinedAccumulatedData {
read_requests: [SideEffect; MAX_READ_REQUESTS_PER_TX],
nullifier_key_validation_requests: [NullifierKeyValidationRequestContext; MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX],

new_commitments: [SideEffect; MAX_NEW_COMMITMENTS_PER_TX],
new_nullifiers: [SideEffectLinkedToNoteHash; MAX_NEW_NULLIFIERS_PER_TX],

private_call_stack: [CallRequest; MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX],
public_call_stack: [CallRequest; MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX],
new_l2_to_l1_msgs: [Field; MAX_NEW_L2_TO_L1_MSGS_PER_TX],

encrypted_logs_hash: [Field; NUM_FIELDS_PER_SHA256],
unencrypted_logs_hash: [Field; NUM_FIELDS_PER_SHA256],

// Here so that the gas cost of this request can be measured by circuits, without actually needing to feed in the
// variable-length data.
encrypted_log_preimages_length: Field,
unencrypted_log_preimages_length: Field,

new_contracts: [NewContractData; MAX_NEW_CONTRACTS_PER_TX],

public_data_update_requests: [PublicDataUpdateRequest; MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX],

public_data_reads: [PublicDataRead; MAX_PUBLIC_DATA_READS_PER_TX],
}

impl CombinedAccumulatedData {
pub fn needs_app_logic(self) -> bool {
// if we have any enqueued revertible public calls, we need to run the public app logic circuit.
!self.public_call_stack[0].is_empty()
}

pub fn recombine(
non_revertible: PublicAccumulatedNonRevertibleData,
revertible: PublicAccumulatedRevertibleData
) -> CombinedAccumulatedData {
CombinedAccumulatedData {
read_requests: revertible.read_requests,
nullifier_key_validation_requests: revertible.nullifier_key_validation_requests,
new_commitments: array_concat(non_revertible.new_commitments, revertible.new_commitments),
new_nullifiers: array_concat(non_revertible.new_nullifiers, revertible.new_nullifiers),
private_call_stack: revertible.private_call_stack,
public_call_stack: array_concat(
non_revertible.public_call_stack,
revertible.public_call_stack
),
new_l2_to_l1_msgs: revertible.new_l2_to_l1_msgs,
encrypted_logs_hash: revertible.encrypted_logs_hash,
unencrypted_logs_hash: revertible.unencrypted_logs_hash,
encrypted_log_preimages_length: revertible.encrypted_log_preimages_length,
unencrypted_log_preimages_length: revertible.unencrypted_log_preimages_length,
new_contracts: revertible.new_contracts,
public_data_update_requests: array_concat(
non_revertible.public_data_update_requests,
revertible.public_data_update_requests
),
public_data_reads: array_concat(
non_revertible.public_data_reads,
revertible.public_data_reads
)
}
}
}

mod tests {
use crate::abis::{
accumulated_data::combined_accumulated_data_builder::CombinedAccumulatedDataBuilder,
call_request::{CallRequest, CallerContext}, new_contract_data::NewContractData,
nullifier_key_validation_request::NullifierKeyValidationRequestContext,
public_data_read::PublicDataRead, public_data_update_request::PublicDataUpdateRequest,
side_effect::{SideEffect, SideEffectLinkedToNoteHash}
};
use crate::address::AztecAddress;
use crate::utils::arrays::array_eq;
use dep::std::unsafe;

#[test]
unconstrained fn splits_revertible_and_non_revertible() {
let mut builder: CombinedAccumulatedDataBuilder = unsafe::zeroed();

let non_revertible_commitments = [
SideEffect { value: 1, counter: 1 },
SideEffect { value: 2, counter: 3 }
];

let non_revertible_nullifiers = [
SideEffectLinkedToNoteHash { value: 10, note_hash: 1, counter: 2 },
SideEffectLinkedToNoteHash { value: 20, note_hash: 2, counter: 4 }
];

let non_revertible_public_stack = [
CallRequest {
hash: 1,
caller_contract_address: AztecAddress::from_field(1),
caller_context: CallerContext::empty(),
start_side_effect_counter: 5,
end_side_effect_counter: 0
},
CallRequest {
hash: 2,
caller_contract_address: AztecAddress::from_field(1),
caller_context: CallerContext::empty(),
start_side_effect_counter: 6,
end_side_effect_counter: 0
}
];

let revertible_commitments = [
SideEffect { value: 3, counter: 7 },
SideEffect { value: 4, counter: 10 }
];

let revertible_nullifiers = [
SideEffectLinkedToNoteHash { value: 30, note_hash: 3, counter: 8 },
SideEffectLinkedToNoteHash { value: 40, note_hash: 4, counter: 11 }
];

let revertible_public_call_stack = [
CallRequest {
hash: 3,
caller_contract_address: AztecAddress::from_field(3),
caller_context: CallerContext::empty(),
start_side_effect_counter: 9,
end_side_effect_counter: 0
}
];

builder.new_commitments.extend_from_array(non_revertible_commitments);
builder.new_commitments.extend_from_array(revertible_commitments);

builder.new_nullifiers.extend_from_array(non_revertible_nullifiers);
builder.new_nullifiers.extend_from_array(revertible_nullifiers);

builder.public_call_stack.extend_from_array(non_revertible_public_stack);
builder.public_call_stack.extend_from_array(revertible_public_call_stack);

let (non_revertible, revertible) = builder.split(7);

assert(array_eq(non_revertible.new_commitments, non_revertible_commitments));
assert(array_eq(non_revertible.new_nullifiers, non_revertible_nullifiers));
assert(array_eq(non_revertible.public_call_stack, non_revertible_public_stack));

assert(array_eq(revertible.new_commitments, revertible_commitments));
assert(array_eq(revertible.new_nullifiers, revertible_nullifiers));
assert(array_eq(revertible.public_call_stack, revertible_public_call_stack));
}
}
Loading

0 comments on commit 3f5cc12

Please sign in to comment.