Skip to content

Commit

Permalink
simpler approach of computing initialization slot
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 26, 2024
1 parent 165027d commit 5ed77a4
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 10 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 @@ -69,10 +69,10 @@ library Constants {
uint256 internal constant L1_TO_L2_MSG_SUBTREE_HEIGHT = 4;
uint256 internal constant L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH = 12;
uint256 internal constant FUNCTION_SELECTOR_NUM_BYTES = 4;
uint256 internal constant MAPPING_SLOT_PEDERSEN_SEPARATOR = 4;
uint256 internal constant NUM_FIELDS_PER_SHA256 = 2;
uint256 internal constant ARGS_HASH_CHUNK_LENGTH = 32;
uint256 internal constant ARGS_HASH_CHUNK_COUNT = 32;
uint256 internal constant INITIALIZATION_SLOT_SEPARATOR = 1000;
uint256 internal constant MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS = 1000;
uint256 internal constant MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS = 500;
uint256 internal constant MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS = 500;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
context::Context, history::public_value_inclusion::prove_public_value_inclusion,
oracle::{storage::{storage_read, storage_write}}, state_vars::storage::Storage
};
use dep::protocol_types::traits::{Deserialize, Serialize};
use dep::protocol_types::{constants::INITIALIZATION_SLOT_SEPARATOR, traits::{Deserialize, Serialize}};

struct SharedImmutable<T>{
context: Context,
Expand Down Expand Up @@ -34,7 +34,7 @@ impl<T> SharedImmutable<T> {
// );

// We check that the struct is not yet initialized by checking if the initialization slot is 0
let initialization_slot = self.storage_slot - 1;
let initialization_slot = INITIALIZATION_SLOT_SEPARATOR + self.storage_slot;
let fields_read: [Field; 1] = storage_read(initialization_slot);
assert(fields_read[0] == 0, "SharedImmutable already initialized");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ global L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH: Field = 12;

// MISC CONSTANTS
global FUNCTION_SELECTOR_NUM_BYTES: Field = 4;
global MAPPING_SLOT_PEDERSEN_SEPARATOR: Field = 4;
// sha256 hash is stored in two fields to accommodate all 256-bits of the hash
global NUM_FIELDS_PER_SHA256: Field = 2;
global ARGS_HASH_CHUNK_LENGTH: u32 = 32;
global ARGS_HASH_CHUNK_COUNT: u32 = 32;
global INITIALIZATION_SLOT_SEPARATOR: Field = 1000;

// CONTRACT CLASS CONSTANTS
// This should be around 8192 (assuming 2**15 instructions packed at 8 bytes each),
Expand Down
7 changes: 2 additions & 5 deletions noir/aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,8 +932,7 @@ fn assign_storage_slots(
)),
}?;

// We start from 2 because 0 storage slot is buggy and 1 is reserved for the initialization slot
let mut storage_slot: u64 = 2;
let mut storage_slot: u64 = 1;
for (index, (_, expr_id)) in storage_constructor_expression.fields.iter().enumerate() {
let fields = r#struct.borrow().get_fields(&[]);
let (_, field_type) = fields.get(index).unwrap();
Expand Down Expand Up @@ -980,9 +979,7 @@ fn assign_storage_slots(
));
});

// We add 1 on the next line because some of the types use value in initialization storage slot
// (set as "type storage slot - 1") to determine whether the value in the type was already initialized.
storage_slot += type_serialized_len + 1;
storage_slot += type_serialized_len;
}
}
}
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 @@ -55,10 +55,10 @@ export const PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH = 35;
export const L1_TO_L2_MSG_SUBTREE_HEIGHT = 4;
export const L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH = 12;
export const FUNCTION_SELECTOR_NUM_BYTES = 4;
export const MAPPING_SLOT_PEDERSEN_SEPARATOR = 4;
export const NUM_FIELDS_PER_SHA256 = 2;
export const ARGS_HASH_CHUNK_LENGTH = 32;
export const ARGS_HASH_CHUNK_COUNT = 32;
export const INITIALIZATION_SLOT_SEPARATOR = 1000;
export const MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS = 1000;
export const MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS = 500;
export const MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS = 500;
Expand Down

0 comments on commit 5ed77a4

Please sign in to comment.