Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jan 17, 2025
1 parent 4c0cb6b commit 67fafc2
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use dep::protocol_types::{
PRIVATE_LOG_SIZE_IN_FIELDS, PUBLIC_DISPATCH_SELECTOR,
},
messaging::l2_to_l1_message::L2ToL1Message,
traits::Empty,
traits::{Empty, FromField, Hash},
};

// When finished, one can call .finish() to convert back to the abi
Expand Down
10 changes: 5 additions & 5 deletions noir-projects/aztec-nr/aztec/src/context/public_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::hash::{
use dep::protocol_types::abis::function_selector::FunctionSelector;
use dep::protocol_types::address::{AztecAddress, EthAddress};
use dep::protocol_types::constants::MAX_FIELD_VALUE;
use dep::protocol_types::traits::{Deserialize, Empty, Serialize};
use dep::protocol_types::traits::{Empty, Packable, Serialize};

pub struct PublicContext {
pub args_hash: Option<Field>,
Expand Down Expand Up @@ -219,9 +219,9 @@ impl PublicContext {

pub fn storage_read<T, let N: u32>(self, storage_slot: Field) -> T
where
T: Deserialize<N>,
T: Packable<N>,
{
T::deserialize(self.raw_storage_read(storage_slot))
T::unpack(self.raw_storage_read(storage_slot))
}

pub fn raw_storage_write<let N: u32>(_self: Self, storage_slot: Field, values: [Field; N]) {
Expand All @@ -233,9 +233,9 @@ impl PublicContext {

pub fn storage_write<T, let N: u32>(self, storage_slot: Field, value: T)
where
T: Serialize<N>,
T: Packable<N>,
{
self.raw_storage_write(storage_slot, value.serialize());
self.raw_storage_write(storage_slot, value.pack());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::oracle::{
execution::{get_block_number, get_chain_id, get_contract_address, get_version},
storage::storage_read,
};
use dep::protocol_types::{address::AztecAddress, traits::Deserialize};
use dep::protocol_types::{address::AztecAddress, traits::Packable};

pub struct UnconstrainedContext {
block_number: u32,
Expand Down Expand Up @@ -62,8 +62,8 @@ impl UnconstrainedContext {

pub unconstrained fn storage_read<T, let N: u32>(self, storage_slot: Field) -> T
where
T: Deserialize<N>,
T: Packable<N>,
{
T::deserialize(self.raw_storage_read(storage_slot))
T::unpack(self.raw_storage_read(storage_slot))
}
}
12 changes: 6 additions & 6 deletions noir-projects/aztec-nr/aztec/src/oracle/storage.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::protocol_types::{address::AztecAddress, traits::Deserialize};
use dep::protocol_types::{address::AztecAddress, traits::{Packable, ToField}};

#[oracle(storageRead)]
unconstrained fn storage_read_oracle<let N: u32>(
Expand Down Expand Up @@ -27,14 +27,14 @@ pub unconstrained fn storage_read<T, let N: u32>(
block_number: u32,
) -> T
where
T: Deserialize<N>,
T: Packable<N>,
{
T::deserialize(raw_storage_read(address, storage_slot, block_number))
T::unpack(raw_storage_read(address, storage_slot, block_number))
}

mod tests {
use crate::oracle::storage::{raw_storage_read, storage_read};
use dep::protocol_types::address::AztecAddress;
use dep::protocol_types::{address::AztecAddress, traits::{FromField, Packable}};

use crate::test::mocks::mock_struct::MockStruct;
use std::test::OracleMock;
Expand All @@ -47,7 +47,7 @@ mod tests {
unconstrained fn test_raw_storage_read() {
let written = MockStruct { a: 13, b: 42 };

let _ = OracleMock::mock("storageRead").returns(written.serialize());
let _ = OracleMock::mock("storageRead").returns(written.pack());

let read: [Field; 2] = raw_storage_read(address, slot, block_number);
assert_eq(read[0], 13);
Expand All @@ -58,7 +58,7 @@ mod tests {
unconstrained fn test_storage_read() {
let written = MockStruct { a: 13, b: 42 };

let _ = OracleMock::mock("storageRead").returns(written.serialize());
let _ = OracleMock::mock("storageRead").returns(written.pack());

let read: MockStruct = storage_read(address, slot, block_number);
assert_eq(read.a, 13);
Expand Down
24 changes: 11 additions & 13 deletions noir-projects/aztec-nr/aztec/src/state_vars/public_immutable.nr
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::{
context::{PrivateContext, PublicContext, UnconstrainedContext},
history::public_storage::PublicStorageHistoricalRead,
state_vars::storage::Storage,
};
use dep::protocol_types::{
constants::INITIALIZATION_SLOT_SEPARATOR,
traits::{Deserialize, Serialize},
};
use dep::protocol_types::{constants::INITIALIZATION_SLOT_SEPARATOR, traits::Packable};

/// Stores an immutable value in public state which can be read from public, private and unconstrained execution
/// contexts.
Expand All @@ -18,7 +16,7 @@ pub struct PublicImmutable<T, Context> {

impl<T, Context, let N: u32> Storage<T, N> for PublicImmutable<T, Context>
where
T: Serialize<N> + Deserialize<N>,
T: Packable<N>,
{
fn get_storage_slot(self) -> Field {
self.storage_slot
Expand All @@ -38,9 +36,9 @@ impl<T, Context> PublicImmutable<T, Context> {
// docs:end:public_immutable_struct_new
}

impl<T, let T_SERIALIZED_LEN: u32> PublicImmutable<T, &mut PublicContext>
impl<T, let T_PACKED_LEN: u32> PublicImmutable<T, &mut PublicContext>
where
T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN>,
T: Packable<T_PACKED_LEN>,
{
// docs:start:public_immutable_struct_write
pub fn initialize(self, value: T) {
Expand All @@ -63,29 +61,29 @@ where
// docs:end:public_immutable_struct_read
}

impl<T, let T_SERIALIZED_LEN: u32> PublicImmutable<T, UnconstrainedContext>
impl<T, let T_PACKED_LEN: u32> PublicImmutable<T, UnconstrainedContext>
where
T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN>,
T: Packable<T_PACKED_LEN>,
{
pub unconstrained fn read(self) -> T {
self.context.storage_read(self.storage_slot)
}
}

impl<T, let T_SERIALIZED_LEN: u32> PublicImmutable<T, &mut PrivateContext>
impl<T, let T_PACKED_LEN: u32> PublicImmutable<T, &mut PrivateContext>
where
T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN>,
T: Packable<T_PACKED_LEN>,
{
pub fn read(self) -> T {
let header = self.context.get_block_header();
let mut fields = [0; T_SERIALIZED_LEN];
let mut fields = [0; T_PACKED_LEN];

for i in 0..fields.len() {
fields[i] = header.public_storage_historical_read(
self.storage_slot + i as Field,
(*self.context).this_address(),
);
}
T::deserialize(fields)
T::unpack(fields)
}
}
12 changes: 6 additions & 6 deletions noir-projects/aztec-nr/aztec/src/state_vars/public_mutable.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::context::{PublicContext, UnconstrainedContext};
use crate::state_vars::storage::Storage;
use dep::protocol_types::traits::{Deserialize, Serialize};
use dep::protocol_types::traits::Packable;

// docs:start:public_mutable_struct
pub struct PublicMutable<T, Context> {
Expand All @@ -11,7 +11,7 @@ pub struct PublicMutable<T, Context> {

impl<T, Context, let N: u32> Storage<T, N> for PublicMutable<T, Context>
where
T: Serialize<N> + Deserialize<N>,
T: Packable<N>,
{
fn get_storage_slot(self) -> Field {
self.storage_slot
Expand All @@ -31,9 +31,9 @@ impl<T, Context> PublicMutable<T, Context> {
// docs:end:public_mutable_struct_new
}

impl<T, let T_SERIALIZED_LEN: u32> PublicMutable<T, &mut PublicContext>
impl<T, let T_PACKED_LEN: u32> PublicMutable<T, &mut PublicContext>
where
T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN>,
T: Packable<T_PACKED_LEN>,
{
// docs:start:public_mutable_struct_read
pub fn read(self) -> T {
Expand All @@ -48,9 +48,9 @@ where
// docs:end:public_mutable_struct_write
}

impl<T, let T_SERIALIZED_LEN: u32> PublicMutable<T, UnconstrainedContext>
impl<T, let T_PACKED_LEN: u32> PublicMutable<T, UnconstrainedContext>
where
T: Deserialize<T_SERIALIZED_LEN>,
T: Packable<T_PACKED_LEN>,
{
pub unconstrained fn read(self) -> T {
self.context.storage_read(self.storage_slot)
Expand Down
7 changes: 3 additions & 4 deletions noir-projects/aztec-nr/aztec/src/state_vars/shared_mutable.nr
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ where
// scheduled. Therefore, the hints must then correspond to uninitialized scheduled changes.
assert_eq(
value_change_hint,
ScheduledValueChange::deserialize(zeroed()),
ScheduledValueChange::unpack(zeroed()),
"Non-zero value change for zero hash",
);
assert_eq(
delay_change_hint,
ScheduledDelayChange::deserialize(zeroed()),
ScheduledDelayChange::unpack(zeroed()),
"Non-zero delay change for zero hash",
);
};
Expand All @@ -242,8 +242,7 @@ where
value_change: ScheduledValueChange<T>,
delay_change: ScheduledDelayChange<INITIAL_DELAY>,
) -> Field {
let concatenated: [Field; 4] =
array_concat(value_change.serialize(), delay_change.serialize());
let concatenated: [Field; 4] = array_concat(value_change.pack(), delay_change.pack());
poseidon2_hash(concatenated)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::protocol_types::traits::{Deserialize, Serialize};
use dep::protocol_types::traits::Packable;
use std::cmp::min;

mod test;
Expand Down Expand Up @@ -125,8 +125,8 @@ impl<let INITIAL_DELAY: u32> ScheduledDelayChange<INITIAL_DELAY> {
}
}

impl<let INITIAL_DELAY: u32> Serialize<1> for ScheduledDelayChange<INITIAL_DELAY> {
fn serialize(self) -> [Field; 1] {
impl<let INITIAL_DELAY: u32> Packable<1> for ScheduledDelayChange<INITIAL_DELAY> {
fn pack(self) -> [Field; 1] {
// We pack all three u32 values into a single U128, which is made up of two u64 limbs.
// Low limb: [ pre_inner: u32 | post_inner: u32 ]
// High limb: [ empty | pre_is_some: u8 | post_is_some: u8 | block_of_change: u32 ]
Expand All @@ -141,10 +141,8 @@ impl<let INITIAL_DELAY: u32> Serialize<1> for ScheduledDelayChange<INITIAL_DELAY

[packed.to_integer()]
}
}

impl<let INITIAL_DELAY: u32> Deserialize<1> for ScheduledDelayChange<INITIAL_DELAY> {
fn deserialize(input: [Field; 1]) -> Self {
fn unpack(input: [Field; 1]) -> Self {
let packed = U128::from_integer(input[0]);

// We use division and modulo to clear the bits that correspond to other values when unpacking.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ unconstrained fn assert_equal_after_conversion(original: ScheduledDelayChange<TE
// We have to do explicit type annotations because Noir lacks turbofish support.
// TODO: improve syntax once https://github.com/noir-lang/noir/issues/4710 is implemented.
let converted: ScheduledDelayChange<TEST_INITIAL_DELAY> =
ScheduledDelayChange::deserialize((original).serialize());
ScheduledDelayChange::unpack(original.pack());

assert_eq(original, converted); // This also tests the Eq impl
assert_eq(original.pre, converted.pre);
Expand Down Expand Up @@ -81,7 +81,7 @@ unconstrained fn get_non_initial_delay_change(
}

unconstrained fn get_initial_delay_change() -> ScheduledDelayChange<TEST_INITIAL_DELAY> {
ScheduledDelayChange::deserialize([0])
ScheduledDelayChange::unpack([0])
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::protocol_types::traits::{Deserialize, FromField, Serialize, ToField};
use dep::protocol_types::traits::{FromField, Packable, ToField};
use std::cmp::min;

mod test;
Expand Down Expand Up @@ -133,20 +133,15 @@ impl<T> ScheduledValueChange<T> {
}
}

impl<T> Serialize<3> for ScheduledValueChange<T>
impl<T> Packable<3> for ScheduledValueChange<T>
where
T: ToField,
T: ToField + FromField,
{
fn serialize(self) -> [Field; 3] {
fn pack(self) -> [Field; 3] {
[self.pre.to_field(), self.post.to_field(), self.block_of_change.to_field()]
}
}

impl<T> Deserialize<3> for ScheduledValueChange<T>
where
T: FromField,
{
fn deserialize(input: [Field; 3]) -> Self {
fn unpack(input: [Field; 3]) -> Self {
Self {
pre: FromField::from_field(input[0]),
post: FromField::from_field(input[1]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use crate::state_vars::shared_mutable::scheduled_value_change::ScheduledValueCha
global TEST_DELAY: u32 = 200;

#[test]
unconstrained fn test_serde() {
unconstrained fn test_packable() {
let pre = 1;
let post = 2;
let block_of_change = 50;

let original = ScheduledValueChange::new(pre, post, block_of_change);
let converted = ScheduledValueChange::deserialize((original).serialize());
let converted = ScheduledValueChange::unpack((original).pack());

assert_eq(original, converted); // This also tests the Eq impl
assert_eq(original.pre, converted.pre);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ unconstrained fn test_get_current_value_in_private_bad_value_hints() {
env.contract_address().to_field(), private_state_var.get_value_change_storage_slot(),
schedule_block_number, 3,
))
.returns(mocked.serialize())
.returns(mocked.pack())
.times(1);

let _ = private_state_var.get_current_value();
Expand All @@ -319,7 +319,7 @@ unconstrained fn test_get_current_value_in_private_bad_delay_hints() {
env.contract_address().to_field(), private_state_var.get_delay_change_storage_slot(),
schedule_block_number, 1,
))
.returns(mocked.serialize())
.returns(mocked.pack())
.times(1);

let _ = private_state_var.get_current_value();
Expand All @@ -338,7 +338,7 @@ unconstrained fn test_get_current_value_in_private_bad_zero_hash_value_hints() {
env.contract_address().to_field(), state_var.get_value_change_storage_slot(),
historical_block_number, 3,
))
.returns(mocked.serialize())
.returns(mocked.pack())
.times(1);

let _ = state_var.get_current_value();
Expand All @@ -358,7 +358,7 @@ unconstrained fn test_get_current_value_in_private_bad_zero_hash_delay_hints() {
env.contract_address().to_field(), state_var.get_delay_change_storage_slot(),
historical_block_number, 1,
))
.returns(mocked.serialize())
.returns(mocked.pack())
.times(1);

let _ = state_var.get_current_value();
Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/state_vars/storage.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use dep::protocol_types::traits::{Deserialize, Serialize};
use dep::protocol_types::traits::Packable;

pub trait Storage<T, let N: u32>
where
T: Serialize<N> + Deserialize<N>,
T: Packable<N>,
{
fn get_storage_slot(self) -> Field;
}
Expand Down
Loading

0 comments on commit 67fafc2

Please sign in to comment.