Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Jul 15, 2024
1 parent f70bcaf commit 6898030
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 14 deletions.
16 changes: 12 additions & 4 deletions noir-projects/aztec-nr/aztec/src/context/unconstrained_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ impl UnconstrainedContext {
self.chain_id
}

unconstrained fn raw_storage_read<N>(self: Self, storage_slot: Field) -> [Field; N] {
storage_read(self.this_address(), storage_slot, self.block_number())
unconstrained fn raw_storage_read<N>(
self: Self,
address: AztecAddress,
storage_slot: Field
) -> [Field; N] {
storage_read(address, storage_slot, self.block_number())
}

unconstrained fn storage_read<T, N>(self, storage_slot: Field) -> T where T: Deserialize<N> {
T::deserialize(self.raw_storage_read(storage_slot))
unconstrained fn storage_read<T, N>(
self,
address: AztecAddress,
storage_slot: Field
) -> T where T: Deserialize<N> {
T::deserialize(self.raw_storage_read(address, storage_slot))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ impl <T, T_SERIALIZED_LEN> PublicImmutable<T, &mut PublicContext> where T: Seria

impl<T, T_SERIALIZED_LEN> PublicImmutable<T, UnconstrainedContext>where T: Deserialize<T_SERIALIZED_LEN> {
unconstrained pub fn read(self) -> T {
self.context.storage_read(self.storage_slot)
self.context.storage_read(self.context.this_address(), self.storage_slot)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ impl<T, T_SERIALIZED_LEN> PublicMutable<T, &mut PublicContext> where T: Serializ

impl<T, T_SERIALIZED_LEN> PublicMutable<T, UnconstrainedContext> where T: Deserialize<T_SERIALIZED_LEN> {
unconstrained pub fn read(self) -> T {
self.context.storage_read(self.storage_slot)
self.context.storage_read(self.context.this_address(), self.storage_slot)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<T, T_SERIALIZED_LEN> SharedImmutable<T, &mut PublicContext> where T: Serial

impl<T, T_SERIALIZED_LEN> SharedImmutable<T, UnconstrainedContext> where T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN> {
unconstrained pub fn read_public(self) -> T {
self.context.storage_read(self.storage_slot)
self.context.storage_read(self.context.this_address(), self.storage_slot)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ impl<T, INITIAL_DELAY> SharedMutable<T, INITIAL_DELAY, &mut PublicContext> where
self.read_delay_change().get_scheduled()
}

// The AVM currently does not support reading from other contract addresses
// pub fn get_current_value_in_public_other(context: &mut PublicContext, address: AztecAddress, storage_slot: Field) -> T {
// let block_number = context.block_number() as u32;

// let dummy = SharedMutable::new(context, storage_slot);

// dummy.read_value_change().get_current_at(block_number)
// }

// pub fn get_current_delay_in_public_other(context: &mut PublicContext, address: AztecAddress, storage_slot: Field) -> u32 {
// let block_number = context.block_number() as u32;
// self.read_delay_change().get_current(block_number)
// }

// pub fn get_scheduled_value_in_public_other(context: &mut PublicContext, address: AztecAddress, storage_slot: Field) -> (T, u32) {
// self.read_value_change().get_scheduled()
// }

// pub fn get_scheduled_delay_in_public_other(context: &mut PublicContext, address: AztecAddress, storage_slot: Field) -> (u32, u32) {
// self.read_delay_change().get_scheduled()
// }

fn read_value_change(self) -> ScheduledValueChange<T> {
self.context.storage_read(self.get_value_change_storage_slot())
}
Expand Down Expand Up @@ -199,14 +221,23 @@ impl<T, INITIAL_DELAY> SharedMutable<T, INITIAL_DELAY, &mut PublicContext> where
}
}

impl<T, INITIAL_DELAY> SharedMutable<T, INITIAL_DELAY, &mut PrivateContext> where T: ToField + FromField + Eq {
pub fn get_current_value_in_private(self) -> T {
impl<T, INITIAL_DELAY> SharedMutable<T, INITIAL_DELAY, &mut PrivateContext> {
pub fn get_current_value_in_private(self) -> T where T: ToField + FromField + Eq {
SharedMutable::get_current_value_in_private_other::<T, INITIAL_DELAY>(self.context, self.context.this_address(), self.storage_slot)
}

pub fn get_current_value_in_private_other<T_OTHER, INITIAL_DELAY_OTHER>(
context: &mut PrivateContext,
address: AztecAddress,
storage_slot: Field
) -> T_OTHER where T_OTHER: FromField + ToField + Eq {
// When reading the current value in private we construct a historical state proof for the public value.
// However, since this value might change, we must constrain the maximum transaction block number as this proof
// will only be valid for however many blocks we can ensure the value will not change, which will depend on the
// current delay and any scheduled delay changes.
let dummy: SharedMutable<T_OTHER, INITIAL_DELAY_OTHER, ()> = SharedMutable::new((), storage_slot);

let (value_change, delay_change, historical_block_number) = self.historical_read_from_public_storage(self.context.get_header(), self.context.this_address());
let (value_change, delay_change, historical_block_number): (ScheduledValueChange<T_OTHER>, ScheduledDelayChange<INITIAL_DELAY_OTHER>, u32) = dummy.historical_read_from_public_storage(context.get_header(), address);

// We use the effective minimum delay as opposed to the current delay at the historical block as this one also
// takes into consideration any scheduled delay changes.
Expand All @@ -220,15 +251,30 @@ impl<T, INITIAL_DELAY> SharedMutable<T, INITIAL_DELAY, &mut PrivateContext> wher

// We prevent this transaction from being included in any block after the block horizon, ensuring that the
// historical public value matches the current one, since it can only change after the horizon.
self.context.set_tx_max_block_number(block_horizon);
context.set_tx_max_block_number(block_horizon);
value_change.get_current_at(historical_block_number)
}
}

impl<T, INITIAL_DELAY> SharedMutable<T, INITIAL_DELAY, UnconstrainedContext> where T: ToField + FromField + Eq {
unconstrained pub fn get_current_value_in_unconstrained(self) -> T {
let block_number = self.context.block_number() as u32;
self.read_value_change().get_current_at(block_number)
SharedMutable::get_current_value_in_unconstrained_other(self.context, self.context.this_address(), self.storage_slot)
}

unconstrained pub fn get_current_value_in_unconstrained_other(
context: UnconstrainedContext,
address: AztecAddress,
storage_slot: Field
) -> T {
let block_number = context.block_number() as u32;

let dummy = SharedMutable::new(context, storage_slot);

dummy.read_value_change(address).get_current_at(block_number)
}

fn read_value_change(self, address: AztecAddress) -> ScheduledValueChange<T> {
self.context.storage_read(address, self.get_value_change_storage_slot())
}
}

Expand Down
18 changes: 17 additions & 1 deletion noir-projects/noir-contracts/contracts/test_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract Test {

use dep::aztec::note::constants::MAX_NOTES_PER_PAGE;

use dep::aztec::state_vars::{shared_mutable::SharedMutablePrivateGetter};
use dep::aztec::state_vars::{shared_mutable::{SharedMutable, SharedMutablePrivateGetter}};

use dep::aztec::{
context::inputs::private_context_inputs::PrivateContextInputs,
Expand Down Expand Up @@ -482,6 +482,22 @@ contract Test {
constant.value
}

// This function is used in the e2e_state_vars to test the SharedMutablePrivateGetter in isolation
#[aztec(private)]
fn test_shared_mutable_getter(
contract_address_to_read: AztecAddress,
storage_slot_of_shared_mutable: Field
) -> Field {
// It's a bit wonky because we need to know the delay for get_current_value_in_private to work correctly
let test = SharedMutable::get_current_value_in_private_other::<AztecAddress, 5>(
&mut context,
contract_address_to_read,
storage_slot_of_shared_mutable
);

test.to_field()
}

// This function is used in the e2e_state_vars to test the SharedMutablePrivateGetter in isolation
#[aztec(private)]
fn test_shared_mutable_private_getter(
Expand Down

0 comments on commit 6898030

Please sign in to comment.