Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jan 23, 2024
1 parent 6901fa1 commit 7a5881c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 46 deletions.
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 @@ -68,7 +68,7 @@ library Constants {
uint256 internal constant MAX_NOTES_PER_PAGE = 10;
uint256 internal constant VIEW_NOTE_ORACLE_RETURN_LENGTH = 212;
uint256 internal constant CALL_CONTEXT_LENGTH = 8;
uint256 internal constant BLOCK_HEADER_LENGTH = 7;
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 = 189;
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 @@ -54,7 +54,7 @@ export const GET_NOTE_ORACLE_RETURN_LENGTH = 23;
export const MAX_NOTES_PER_PAGE = 10;
export const VIEW_NOTE_ORACLE_RETURN_LENGTH = 212;
export const CALL_CONTEXT_LENGTH = 8;
export const BLOCK_HEADER_LENGTH = 7;
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 = 189;
Expand Down
62 changes: 42 additions & 20 deletions yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
use crate::{
abis::{
append_only_tree_snapshot::AppendOnlyTreeSnapshot,
global_variables::GlobalVariables,
append_only_tree_snapshot::{
AppendOnlyTreeSnapshot,
APPEND_ONLY_TREE_SNAPSHOT_LENGTH,
},
global_variables::{
GlobalVariables,
GLOBAL_VARIABLES_LENGTH,
},
},
constants::{
GENERATOR_INDEX__BLOCK_HASH,
HEADER_LENGTH,
NUM_FIELDS_PER_SHA256,
},
hash::pedersen_hash,
state_reference::StateReference,
state_reference::{
StateReference,
STATE_REFERENCE_LENGTH,
},
utils::{
arr_copy_slice,
bounded_vec::BoundedVec,
},
};

struct Header {
Expand All @@ -22,26 +35,35 @@ struct Header {
impl Header {

pub fn serialize(self) -> [Field; HEADER_LENGTH] {
[
self.note_hash_tree_root,
self.nullifier_tree_root,
self.contract_tree_root,
self.l1_to_l2_message_tree_root,
self.archive_root,
self.public_data_tree_root,
self.global_variables_hash
]
let mut fields: BoundedVec<Field, HEADER_LENGTH> = BoundedVec::new(0);

fields.push_array(self.last_archive.serialize());
fields.push_array(self.body_hash);
fields.push_array(self.state.serialize());
fields.push_array(self.global_variables.serialize());

fields.storage
}

pub fn deserialize(deserialized: [Field; HEADER_LENGTH]) -> Self {
pub fn deserialize(serialized: [Field; HEADER_LENGTH]) -> Self {
let mut offset = 0;

let last_archive_fields = arr_copy_slice(serialized, [0; APPEND_ONLY_TREE_SNAPSHOT_LENGTH], offset);
offset = offset + APPEND_ONLY_TREE_SNAPSHOT_LENGTH;

let body_hash = arr_copy_slice(serialized, [0; NUM_FIELDS_PER_SHA256], offset);
offset = offset + NUM_FIELDS_PER_SHA256;

let state_fields = arr_copy_slice(serialized, [0; STATE_REFERENCE_LENGTH], offset);
offset = offset + STATE_REFERENCE_LENGTH;

let global_variables_fields = arr_copy_slice(serialized, [0; GLOBAL_VARIABLES_LENGTH], offset);

Header {
note_hash_tree_root: deserialized[0],
nullifier_tree_root: deserialized[1],
contract_tree_root: deserialized[2],
l1_to_l2_message_tree_root: deserialized[3],
archive_root: deserialized[4],
public_data_tree_root: deserialized[5],
global_variables_hash: deserialized[6],
last_archive: AppendOnlyTreeSnapshot::deserialize(last_archive_fields),
body_hash,
state: StateReference::deserialize(state_fields),
global_variables: GlobalVariables::deserialize(global_variables_fields),
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
use crate::abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot;
use crate::partial_state_reference::PartialStateReference;
use crate::{
abis::append_only_tree_snapshot::{
AppendOnlyTreeSnapshot,
APPEND_ONLY_TREE_SNAPSHOT_LENGTH,
},
partial_state_reference::{
PartialStateReference,
PARTIAL_STATE_REFERENCE_LENGTH,
},
utils::{
arr_copy_slice,
bounded_vec::BoundedVec,
},
};

struct StateReference {
l1_to_l2_message_tree: AppendOnlyTreeSnapshot,
Expand All @@ -15,32 +27,25 @@ impl StateReference {
}

fn serialize(self) -> [Field; STATE_REFERENCE_LENGTH] {
let serialized_l1_to_l2_message_tree = self.l1_to_l2_message_tree.serialize();
let serialized_partial = self.partial.serialize();

[
serialized_l1_to_l2_message_tree[0],
serialized_l1_to_l2_message_tree[1],
serialized_partial[0],
serialized_partial[1],
serialized_partial[2],
serialized_partial[3],
serialized_partial[4],
serialized_partial[5],
serialized_partial[6],
serialized_partial[7],
]
let mut fields: BoundedVec<Field, STATE_REFERENCE_LENGTH> = BoundedVec::new(0);

fields.push_array(self.l1_to_l2_message_tree.serialize());
fields.push_array(self.partial.serialize());

fields.storage
}

fn deserialize(serialized: [Field; STATE_REFERENCE_LENGTH]) -> StateReference {
let mut offset = 0;

let l1_to_l2_message_tree_fields = arr_copy_slice(serialized, [0; APPEND_ONLY_TREE_SNAPSHOT_LENGTH], offset);
offset = offset + APPEND_ONLY_TREE_SNAPSHOT_LENGTH;

let partial_fields = arr_copy_slice(serialized, [0; PARTIAL_STATE_REFERENCE_LENGTH], offset);

StateReference {
l1_to_l2_message_tree: AppendOnlyTreeSnapshot::deserialize(
[serialized[0], serialized[1]]
),
partial: PartialStateReference::deserialize(
[serialized[2], serialized[3], serialized[4], serialized[5],
serialized[6], serialized[7], serialized[8], serialized[9]]
),
l1_to_l2_message_tree: AppendOnlyTreeSnapshot::deserialize(l1_to_l2_message_tree_fields),
partial: PartialStateReference::deserialize(partial_fields),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ mod uint256;
pub fn conditional_assign(predicate: bool, lhs: Field, rhs: Field) -> Field {
if predicate { lhs } else { rhs }
}

// Copied over from "yarn-project/aztec-nr/aztec/src/utils.nr"
pub fn arr_copy_slice<T, N, M>(src: [T; N], mut dst: [T; M], offset: Field) -> [T; M] {
for i in 0..dst.len() {
dst[i] = src[i + offset];
}
dst
}

0 comments on commit 7a5881c

Please sign in to comment.