Skip to content

Commit

Permalink
chore: Add more serialisation traits to protocol circuits (#6385)
Browse files Browse the repository at this point in the history
This PR simply adds more serialisation traits and tests to protocol
circuit structs.
  • Loading branch information
PhilWindle authored May 14, 2024
1 parent aca81ae commit 97d5422
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ const size_t PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH =
VALIDATION_REQUESTS_LENGTH + PUBLIC_ACCUMULATED_DATA_LENGTH + PUBLIC_ACCUMULATED_DATA_LENGTH +
COMBINED_CONSTANT_DATA_LENGTH + 1 + (MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX * CALL_REQUEST_LENGTH) +
AZTEC_ADDRESS_LENGTH;
const size_t KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = ROLLUP_VALIDATION_REQUESTS_LENGTH +
COMBINED_ACCUMULATED_DATA_LENGTH + COMBINED_CONSTANT_DATA_LENGTH +
PARTIAL_STATE_REFERENCE_LENGTH + 1 + AZTEC_ADDRESS_LENGTH;
const size_t CONSTANT_ROLLUP_DATA_LENGTH = APPEND_ONLY_TREE_SNAPSHOT_LENGTH + 4 + GLOBAL_VARIABLES_LENGTH;
const size_t BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH =
CONSTANT_ROLLUP_DATA_LENGTH + PARTIAL_STATE_REFERENCE_LENGTH + PARTIAL_STATE_REFERENCE_LENGTH + 4;
const size_t ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 2 + FUNCTION_DATA_LENGTH + CALL_CONTEXT_LENGTH;
const size_t GET_NOTES_ORACLE_RETURN_LENGTH = 674;
const size_t NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP = 2048;
Expand Down
7 changes: 7 additions & 0 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ library Constants {
uint256 internal constant PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = VALIDATION_REQUESTS_LENGTH
+ PUBLIC_ACCUMULATED_DATA_LENGTH + PUBLIC_ACCUMULATED_DATA_LENGTH + COMBINED_CONSTANT_DATA_LENGTH
+ 1 + (MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX * CALL_REQUEST_LENGTH) + AZTEC_ADDRESS_LENGTH;
uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = ROLLUP_VALIDATION_REQUESTS_LENGTH
+ COMBINED_ACCUMULATED_DATA_LENGTH + COMBINED_CONSTANT_DATA_LENGTH
+ PARTIAL_STATE_REFERENCE_LENGTH + 1 + AZTEC_ADDRESS_LENGTH;
uint256 internal constant CONSTANT_ROLLUP_DATA_LENGTH =
APPEND_ONLY_TREE_SNAPSHOT_LENGTH + 4 + GLOBAL_VARIABLES_LENGTH;
uint256 internal constant BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH = CONSTANT_ROLLUP_DATA_LENGTH
+ PARTIAL_STATE_REFERENCE_LENGTH + PARTIAL_STATE_REFERENCE_LENGTH + 4;
uint256 internal constant ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH =
2 + FUNCTION_DATA_LENGTH + CALL_CONTEXT_LENGTH;
uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use dep::types::{
abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot,
partial_state_reference::PartialStateReference, mocked::AggregationObject, traits::Empty
constants::BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH, partial_state_reference::PartialStateReference,
mocked::AggregationObject, traits::{Empty, Serialize, Deserialize}, utils::reader::Reader
};
use crate::abis::constant_rollup_data::ConstantRollupData;

Expand Down Expand Up @@ -43,3 +44,60 @@ impl Empty for BaseOrMergeRollupPublicInputs {
}
}
}

impl Eq for BaseOrMergeRollupPublicInputs {
fn eq(self, other: Self) -> bool {
(self.rollup_type == other.rollup_type) &
(self.height_in_block_tree == other.height_in_block_tree) &
(self.constants.eq(other.constants)) &
(self.start.eq(other.start)) &
(self.end.eq(other.end)) &
(self.txs_effects_hash == other.txs_effects_hash) &
(self.out_hash == other.out_hash)
}
}

impl Serialize<BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH> for BaseOrMergeRollupPublicInputs {
fn serialize(self) -> [Field; BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH] {
let mut fields: BoundedVec<Field, BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH> = BoundedVec::new();

fields.push(self.rollup_type as Field);
fields.push(self.height_in_block_tree as Field);
fields.extend_from_array(self.constants.serialize());
fields.extend_from_array(self.start.serialize());
fields.extend_from_array(self.end.serialize());
fields.push(self.txs_effects_hash as Field);
fields.push(self.out_hash as Field);

assert_eq(fields.len(), BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH);

fields.storage
}
}

impl Deserialize<BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH> for BaseOrMergeRollupPublicInputs {
fn deserialize(fields: [Field; BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH]) -> BaseOrMergeRollupPublicInputs {
let mut reader = Reader::new(fields);
let item = Self {
rollup_type: reader.read() as u32,
height_in_block_tree: reader.read(),
aggregation_object : AggregationObject::empty(),
constants: reader.read_struct(ConstantRollupData::deserialize),
start: reader.read_struct(PartialStateReference::deserialize),
end: reader.read_struct(PartialStateReference::deserialize),
txs_effects_hash: reader.read(),
out_hash: reader.read(),
};

reader.finish();
item
}
}

#[test]
fn serialization_of_empty() {
let item = BaseOrMergeRollupPublicInputs::empty();
let serialized = item.serialize();
let deserialized = BaseOrMergeRollupPublicInputs::deserialize(serialized);
assert(item.eq(deserialized));
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use dep::std::cmp::Eq;
use dep::types::{
abis::{global_variables::GlobalVariables, append_only_tree_snapshot::AppendOnlyTreeSnapshot},
traits::Empty
traits::{Empty, Serialize, Deserialize}, constants::CONSTANT_ROLLUP_DATA_LENGTH,
utils::reader::Reader
};

struct ConstantRollupData {
Expand Down Expand Up @@ -41,3 +42,45 @@ impl Empty for ConstantRollupData {
}
}
}

impl Serialize<CONSTANT_ROLLUP_DATA_LENGTH> for ConstantRollupData {
fn serialize(self) -> [Field; CONSTANT_ROLLUP_DATA_LENGTH] {
let mut fields: BoundedVec<Field, CONSTANT_ROLLUP_DATA_LENGTH> = BoundedVec::new();

fields.extend_from_array(self.last_archive.serialize());
fields.push(self.private_kernel_vk_tree_root as Field);
fields.push(self.public_kernel_vk_tree_root as Field);
fields.push(self.base_rollup_vk_hash as Field);
fields.push(self.merge_rollup_vk_hash as Field);
fields.extend_from_array(self.global_variables.serialize());

assert_eq(fields.len(), CONSTANT_ROLLUP_DATA_LENGTH);

fields.storage
}
}

impl Deserialize<CONSTANT_ROLLUP_DATA_LENGTH> for ConstantRollupData {
fn deserialize(fields: [Field; CONSTANT_ROLLUP_DATA_LENGTH]) -> ConstantRollupData {
let mut reader = Reader::new(fields);
let item = Self {
last_archive: reader.read_struct(AppendOnlyTreeSnapshot::deserialize),
private_kernel_vk_tree_root: reader.read(),
public_kernel_vk_tree_root: reader.read(),
base_rollup_vk_hash: reader.read(),
merge_rollup_vk_hash: reader.read(),
global_variables: reader.read_struct(GlobalVariables::deserialize),
};

reader.finish();
item
}
}

#[test]
fn serialization_of_empty() {
let item = ConstantRollupData::empty();
let serialized = item.serialize();
let deserialized = ConstantRollupData::deserialize(serialized);
assert(item.eq(deserialized));
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::{
accumulated_data::CombinedAccumulatedData, combined_constant_data::CombinedConstantData,
validation_requests::RollupValidationRequests, gas::Gas
},
address::AztecAddress, partial_state_reference::PartialStateReference, traits::Empty
address::AztecAddress, partial_state_reference::PartialStateReference,
traits::{Empty, Eq, Serialize, Deserialize}, constants::KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH,
utils::reader::Reader
};
use crate::mocked::AggregationObject;

Expand Down Expand Up @@ -38,6 +40,51 @@ impl Empty for KernelCircuitPublicInputs {
}
}

impl Eq for KernelCircuitPublicInputs {
fn eq(self, other: Self) -> bool {
(self.rollup_validation_requests.eq(other.rollup_validation_requests)) &
(self.end.eq(other.end)) &
(self.constants.eq(other.constants)) &
(self.start_state.eq(other.start_state)) &
(self.revert_code == other.revert_code) &
(self.fee_payer.eq(other.fee_payer))
}
}

impl Serialize<KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH> for KernelCircuitPublicInputs {
fn serialize(self) -> [Field; KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH] {
let mut fields: BoundedVec<Field, KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH> = BoundedVec::new();

fields.extend_from_array(self.rollup_validation_requests.serialize());
fields.extend_from_array(self.end.serialize());
fields.extend_from_array(self.constants.serialize());
fields.extend_from_array(self.start_state.serialize());
fields.push(self.revert_code as Field);
fields.extend_from_array(self.fee_payer.serialize());

assert_eq(fields.len(), KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH);

fields.storage
}
}

impl Deserialize<KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH> for KernelCircuitPublicInputs {
fn deserialize(fields: [Field; KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH]) -> KernelCircuitPublicInputs {
let mut reader = Reader::new(fields);
let item = Self {
rollup_validation_requests: reader.read_struct(RollupValidationRequests::deserialize),
end: reader.read_struct(CombinedAccumulatedData::deserialize),
constants: reader.read_struct(CombinedConstantData::deserialize),
start_state: reader.read_struct(PartialStateReference::deserialize),
revert_code: reader.read() as u8,
fee_payer: reader.read_struct(AztecAddress::deserialize),
};

reader.finish();
item
}
}

mod tests {
use crate::abis::{
kernel_circuit_public_inputs::kernel_circuit_public_inputs::KernelCircuitPublicInputs,
Expand Down Expand Up @@ -80,4 +127,12 @@ mod tests {
let transaction_fee = inputs.compute_transaction_fee();
assert_eq(transaction_fee, 42 + 2 * 10 + 3 * 20);
}

#[test]
fn serialization_of_empty() {
let item = KernelCircuitPublicInputs::empty();
let serialized = item.serialize();
let deserialized = KernelCircuitPublicInputs::deserialize(serialized);
assert(item.eq(deserialized));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Empty for ValidationRequests {

impl Eq for ValidationRequests {
fn eq(self, other: Self) -> bool {
self.for_rollup.eq(other.for_rollup) &
(self.for_rollup.eq(other.for_rollup)) &
(self.note_hash_read_requests == other.note_hash_read_requests) &
(self.nullifier_read_requests == other.nullifier_read_requests) &
(self.nullifier_non_existent_read_requests == other.nullifier_non_existent_read_requests) &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ global PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 1 + VALIDATION_REQUESTS_LEN
global PUBLIC_ACCUMULATED_DATA_LENGTH = (MAX_NEW_NOTE_HASHES_PER_TX * NOTE_HASH_LENGTH) + (MAX_NEW_NULLIFIERS_PER_TX * NULLIFIER_LENGTH) + (MAX_NEW_L2_TO_L1_MSGS_PER_TX * 1) + (MAX_ENCRYPTED_LOGS_PER_TX * SIDE_EFFECT_LENGTH) + (MAX_UNENCRYPTED_LOGS_PER_TX * SIDE_EFFECT_LENGTH) + 2 + (MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX * PUBLIC_DATA_UPDATE_REQUEST_LENGTH) + (MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX * CALL_REQUEST_LENGTH) + GAS_LENGTH;
global PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = VALIDATION_REQUESTS_LENGTH + PUBLIC_ACCUMULATED_DATA_LENGTH + PUBLIC_ACCUMULATED_DATA_LENGTH + COMBINED_CONSTANT_DATA_LENGTH + 1 + (MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX * CALL_REQUEST_LENGTH) + AZTEC_ADDRESS_LENGTH;

global KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = ROLLUP_VALIDATION_REQUESTS_LENGTH + COMBINED_ACCUMULATED_DATA_LENGTH + COMBINED_CONSTANT_DATA_LENGTH + PARTIAL_STATE_REFERENCE_LENGTH + 1 + AZTEC_ADDRESS_LENGTH;

global CONSTANT_ROLLUP_DATA_LENGTH = APPEND_ONLY_TREE_SNAPSHOT_LENGTH + 4 + GLOBAL_VARIABLES_LENGTH;
global BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH = CONSTANT_ROLLUP_DATA_LENGTH + PARTIAL_STATE_REFERENCE_LENGTH + PARTIAL_STATE_REFERENCE_LENGTH + 4;

global ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH: u64 = 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;
Expand Down
10 changes: 10 additions & 0 deletions yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ export const PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH =
1 +
MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX * CALL_REQUEST_LENGTH +
AZTEC_ADDRESS_LENGTH;
export const KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH =
ROLLUP_VALIDATION_REQUESTS_LENGTH +
COMBINED_ACCUMULATED_DATA_LENGTH +
COMBINED_CONSTANT_DATA_LENGTH +
PARTIAL_STATE_REFERENCE_LENGTH +
1 +
AZTEC_ADDRESS_LENGTH;
export const CONSTANT_ROLLUP_DATA_LENGTH = APPEND_ONLY_TREE_SNAPSHOT_LENGTH + 4 + GLOBAL_VARIABLES_LENGTH;
export const BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH =
CONSTANT_ROLLUP_DATA_LENGTH + PARTIAL_STATE_REFERENCE_LENGTH + PARTIAL_STATE_REFERENCE_LENGTH + 4;
export const ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 2 + FUNCTION_DATA_LENGTH + CALL_CONTEXT_LENGTH;
export const GET_NOTES_ORACLE_RETURN_LENGTH = 674;
export const NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP = 2048;
Expand Down

0 comments on commit 97d5422

Please sign in to comment.