Skip to content

Commit

Permalink
efficiently assigning initialization slot
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 26, 2024
1 parent e43fade commit 165027d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
22 changes: 10 additions & 12 deletions noir-projects/aztec-nr/aztec/src/state_vars/shared_immutable.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{
context::{Context}, hash::pedersen_hash,
history::public_value_inclusion::prove_public_value_inclusion,
context::Context, history::public_value_inclusion::prove_public_value_inclusion,
oracle::{storage::{storage_read, storage_write}}, state_vars::storage::Storage
};
use dep::protocol_types::{constants::GENERATOR_INDEX__IMMUTABLE_INITIALIZE_PLACEHOLDER, traits::{Deserialize, Serialize}};
use dep::protocol_types::traits::{Deserialize, Serialize};

struct SharedImmutable<T>{
context: Context,
Expand All @@ -18,7 +17,9 @@ impl<T> SharedImmutable<T> {
context: Context,
storage_slot: Field
) -> Self {
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1.");
assert(
(storage_slot != 0) & (storage_slot != 1), "Storage slots 0 and 1 not allowed. Storage slots must start from 2."
);
Self { context, storage_slot }
}

Expand All @@ -32,16 +33,13 @@ impl<T> SharedImmutable<T> {
// self.context.public.unwrap_unchecked().is_deployment(), "SharedImmutable can only be initialized during contract deployment"
// );

// We check that the struct is not yet initialized by checking if the placeholder slot is 0
let placeholder_storage_slot = pedersen_hash(
[self.storage_slot],
GENERATOR_INDEX__IMMUTABLE_INITIALIZE_PLACEHOLDER
);
let fields_read: [Field; 1] = storage_read(placeholder_storage_slot);
// 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 fields_read: [Field; 1] = storage_read(initialization_slot);
assert(fields_read[0] == 0, "SharedImmutable already initialized");

// We populate the placeholder slot with a non-zero value to indicate that the struct is initialized
storage_write(placeholder_storage_slot, [0xdead]);
// We populate the initialization slot with a non-zero value to indicate that the struct is initialized
storage_write(initialization_slot, [0xdead]);

let fields_write = T::serialize(value);
storage_write(self.storage_slot, fields_write);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,3 @@ global GENERATOR_INDEX__VK = 41;
global GENERATOR_INDEX__PRIVATE_CIRCUIT_PUBLIC_INPUTS = 42;
global GENERATOR_INDEX__PUBLIC_CIRCUIT_PUBLIC_INPUTS = 43;
global GENERATOR_INDEX__FUNCTION_ARGS = 44;
global GENERATOR_INDEX__IMMUTABLE_INITIALIZE_PLACEHOLDER = 45;
7 changes: 5 additions & 2 deletions noir/aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,8 @@ fn assign_storage_slots(
)),
}?;

let mut storage_slot: u64 = 1;
// We start from 2 because 0 storage slot is buggy and 1 is reserved for the initialization slot
let mut storage_slot: u64 = 2;
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 @@ -979,7 +980,9 @@ fn assign_storage_slots(
));
});

storage_slot += type_serialized_len;
// 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;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,4 @@ export enum GeneratorIndex {
PRIVATE_CIRCUIT_PUBLIC_INPUTS = 42,
PUBLIC_CIRCUIT_PUBLIC_INPUTS = 43,
FUNCTION_ARGS = 44,
IMMUTABLE_INITIALIZE_PLACEHOLDER = 45,
}

0 comments on commit 165027d

Please sign in to comment.