Skip to content

Commit 2fb989b

Browse files
authored
Merge 0fd1d31 into 02e6529
2 parents 02e6529 + 0fd1d31 commit 2fb989b

File tree

36 files changed

+490
-142
lines changed

36 files changed

+490
-142
lines changed

docs/docs/migration_notes.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@ keywords: [sandbox, aztec, notes, migration, updating, upgrading]
66

77
Aztec is in full-speed development. Literally every version breaks compatibility with the previous ones. This page attempts to target errors and difficulties you might encounter when upgrading, and how to resolve them.
88

9+
## TBD
10+
11+
### [Aztec.nr] Introduction of `Packable` trait
12+
We have introduced a `Packable` trait that allows types to be serialized and deserialized with a focus on minimizing the size of the resulting Field array.
13+
This is in contrast to the `Serialize` and `Deserialize` traits, which follows Noir's intrinsic serialization format.
14+
This is a breaking change because we now require `Packable` trait implementation for any type that is to be stored in contract storage.
15+
16+
Example implementation of Packable trait for `U128` type from `noir::std`:
17+
18+
```
19+
use crate::traits::{Packable, ToField};
20+
21+
let U128_PACKED_LEN: u32 = 1;
22+
23+
impl Packable<U128_PACKED_LEN> for U128 {
24+
fn pack(self) -> [Field; U128_PACKED_LEN] {
25+
[self.to_field()]
26+
}
27+
28+
fn unpack(fields: [Field; U128_PACKED_LEN]) -> Self {
29+
U128::from_integer(fields[0])
30+
}
31+
}
32+
```
33+
934
## 0.72.0
1035
### Some functions in `aztec.js` and `@aztec/accounts` are now async
1136
In our efforts to make libraries more browser-friendly and providing with more bundling options for `bb.js` (like a non top-level-await version), some functions are being made async, in particular those that access our cryptographic functions.

noir-projects/aztec-nr/aztec/src/context/public_context.nr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::hash::{
55
use dep::protocol_types::abis::function_selector::FunctionSelector;
66
use dep::protocol_types::address::{AztecAddress, EthAddress};
77
use dep::protocol_types::constants::MAX_FIELD_VALUE;
8-
use dep::protocol_types::traits::{Deserialize, Empty, Serialize};
8+
use dep::protocol_types::traits::{Empty, Packable, Serialize};
99

1010
pub struct PublicContext {
1111
pub args_hash: Option<Field>,
@@ -219,9 +219,9 @@ impl PublicContext {
219219

220220
pub fn storage_read<T, let N: u32>(self, storage_slot: Field) -> T
221221
where
222-
T: Deserialize<N>,
222+
T: Packable<N>,
223223
{
224-
T::deserialize(self.raw_storage_read(storage_slot))
224+
T::unpack(self.raw_storage_read(storage_slot))
225225
}
226226

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

234234
pub fn storage_write<T, let N: u32>(self, storage_slot: Field, value: T)
235235
where
236-
T: Serialize<N>,
236+
T: Packable<N>,
237237
{
238-
self.raw_storage_write(storage_slot, value.serialize());
238+
self.raw_storage_write(storage_slot, value.pack());
239239
}
240240
}
241241

noir-projects/aztec-nr/aztec/src/context/unconstrained_context.nr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::oracle::{
22
execution::{get_block_number, get_chain_id, get_contract_address, get_version},
33
storage::storage_read,
44
};
5-
use dep::protocol_types::{address::AztecAddress, traits::Deserialize};
5+
use dep::protocol_types::{address::AztecAddress, traits::Packable};
66

77
pub struct UnconstrainedContext {
88
block_number: u32,
@@ -62,8 +62,8 @@ impl UnconstrainedContext {
6262

6363
pub unconstrained fn storage_read<T, let N: u32>(self, storage_slot: Field) -> T
6464
where
65-
T: Deserialize<N>,
65+
T: Packable<N>,
6666
{
67-
T::deserialize(self.raw_storage_read(storage_slot))
67+
T::unpack(self.raw_storage_read(storage_slot))
6868
}
6969
}

noir-projects/aztec-nr/aztec/src/oracle/pxe_db.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use protocol_types::{address::AztecAddress, traits::{Deserialize, Serialize}};
22

33
/// Stores arbitrary information in a per-contract non-volatile database, which can later be retrieved with `load`. If
4-
/// data was already stored at this slot, it is overwrriten.
4+
/// data was already stored at this slot, it is overwritten.
55
pub unconstrained fn store<T, let N: u32>(contract_address: AztecAddress, slot: Field, value: T)
66
where
77
T: Serialize<N>,

noir-projects/aztec-nr/aztec/src/oracle/storage.nr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use dep::protocol_types::{address::AztecAddress, traits::Deserialize};
1+
use dep::protocol_types::{address::AztecAddress, traits::Packable};
22

33
#[oracle(storageRead)]
44
unconstrained fn storage_read_oracle<let N: u32>(
@@ -27,14 +27,14 @@ pub unconstrained fn storage_read<T, let N: u32>(
2727
block_number: u32,
2828
) -> T
2929
where
30-
T: Deserialize<N>,
30+
T: Packable<N>,
3131
{
32-
T::deserialize(raw_storage_read(address, storage_slot, block_number))
32+
T::unpack(raw_storage_read(address, storage_slot, block_number))
3333
}
3434

3535
mod tests {
3636
use crate::oracle::storage::{raw_storage_read, storage_read};
37-
use dep::protocol_types::address::AztecAddress;
37+
use dep::protocol_types::{address::AztecAddress, traits::{FromField, Packable}};
3838

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

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

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

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

6363
let read: MockStruct = storage_read(address, slot, block_number);
6464
assert_eq(read.a, 13);

noir-projects/aztec-nr/aztec/src/state_vars/map.nr

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use crate::state_vars::storage::Storage;
2-
use dep::protocol_types::{
3-
storage::map::derive_storage_slot_in_map,
4-
traits::{Deserialize, Serialize, ToField},
5-
};
2+
use dep::protocol_types::{storage::map::derive_storage_slot_in_map, traits::{Packable, ToField}};
63

74
// docs:start:map
85
pub struct Map<K, V, Context> {
@@ -14,7 +11,7 @@ pub struct Map<K, V, Context> {
1411

1512
impl<K, T, Context, let N: u32> Storage<T, N> for Map<K, T, Context>
1613
where
17-
T: Serialize<N> + Deserialize<N>,
14+
T: Packable<N>,
1815
{
1916
fn get_storage_slot(self) -> Field {
2017
self.storage_slot

noir-projects/aztec-nr/aztec/src/state_vars/private_immutable.nr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use dep::protocol_types::{
2-
constants::GENERATOR_INDEX__INITIALIZATION_NULLIFIER,
3-
hash::poseidon2_hash_with_separator,
4-
traits::{Deserialize, Serialize},
2+
constants::GENERATOR_INDEX__INITIALIZATION_NULLIFIER, hash::poseidon2_hash_with_separator,
3+
traits::Packable,
54
};
65

76
use crate::context::{PrivateContext, UnconstrainedContext};
@@ -24,7 +23,7 @@ pub struct PrivateImmutable<Note, Context> {
2423

2524
impl<T, Context, let N: u32> Storage<T, N> for PrivateImmutable<T, Context>
2625
where
27-
T: Serialize<N> + Deserialize<N>,
26+
T: Packable<N>,
2827
{
2928
fn get_storage_slot(self) -> Field {
3029
self.storage_slot

noir-projects/aztec-nr/aztec/src/state_vars/private_mutable.nr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use dep::protocol_types::{
2-
constants::GENERATOR_INDEX__INITIALIZATION_NULLIFIER,
3-
hash::poseidon2_hash_with_separator,
4-
traits::{Deserialize, Serialize},
2+
constants::GENERATOR_INDEX__INITIALIZATION_NULLIFIER, hash::poseidon2_hash_with_separator,
3+
traits::Packable,
54
};
65

76
use crate::context::{PrivateContext, UnconstrainedContext};
@@ -26,7 +25,7 @@ mod test;
2625

2726
impl<T, Context, let N: u32> Storage<T, N> for PrivateMutable<T, Context>
2827
where
29-
T: Serialize<N> + Deserialize<N>,
28+
T: Packable<N>,
3029
{
3130
fn get_storage_slot(self) -> Field {
3231
self.storage_slot

noir-projects/aztec-nr/aztec/src/state_vars/private_set.nr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ use crate::note::{
1111
};
1212
use crate::state_vars::storage::Storage;
1313
use dep::protocol_types::{
14-
abis::read_request::ReadRequest,
15-
constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
16-
traits::{Deserialize, Serialize},
14+
abis::read_request::ReadRequest, constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
15+
traits::Packable,
1716
};
1817

1918
// docs:start:struct
@@ -25,7 +24,7 @@ pub struct PrivateSet<Note, Context> {
2524

2625
impl<T, Context, let N: u32> Storage<T, N> for PrivateSet<T, Context>
2726
where
28-
T: Serialize<N> + Deserialize<N>,
27+
T: Packable<N>,
2928
{
3029
fn get_storage_slot(self) -> Field {
3130
self.storage_slot

noir-projects/aztec-nr/aztec/src/state_vars/public_immutable.nr

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use crate::{
22
context::{PrivateContext, PublicContext, UnconstrainedContext},
3+
history::public_storage::PublicStorageHistoricalRead,
34
state_vars::storage::Storage,
45
};
5-
use dep::protocol_types::{
6-
constants::INITIALIZATION_SLOT_SEPARATOR,
7-
traits::{Deserialize, Serialize},
8-
};
6+
use dep::protocol_types::{constants::INITIALIZATION_SLOT_SEPARATOR, traits::Packable};
97

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

1917
impl<T, Context, let N: u32> Storage<T, N> for PublicImmutable<T, Context>
2018
where
21-
T: Serialize<N> + Deserialize<N>,
19+
T: Packable<N>,
2220
{
2321
fn get_storage_slot(self) -> Field {
2422
self.storage_slot
@@ -38,9 +36,9 @@ impl<T, Context> PublicImmutable<T, Context> {
3836
// docs:end:public_immutable_struct_new
3937
}
4038

41-
impl<T, let T_SERIALIZED_LEN: u32> PublicImmutable<T, &mut PublicContext>
39+
impl<T, let T_PACKED_LEN: u32> PublicImmutable<T, &mut PublicContext>
4240
where
43-
T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN>,
41+
T: Packable<T_PACKED_LEN>,
4442
{
4543
// docs:start:public_immutable_struct_write
4644
pub fn initialize(self, value: T) {
@@ -63,29 +61,29 @@ where
6361
// docs:end:public_immutable_struct_read
6462
}
6563

66-
impl<T, let T_SERIALIZED_LEN: u32> PublicImmutable<T, UnconstrainedContext>
64+
impl<T, let T_PACKED_LEN: u32> PublicImmutable<T, UnconstrainedContext>
6765
where
68-
T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN>,
66+
T: Packable<T_PACKED_LEN>,
6967
{
7068
pub unconstrained fn read(self) -> T {
7169
self.context.storage_read(self.storage_slot)
7270
}
7371
}
7472

75-
impl<T, let T_SERIALIZED_LEN: u32> PublicImmutable<T, &mut PrivateContext>
73+
impl<T, let T_PACKED_LEN: u32> PublicImmutable<T, &mut PrivateContext>
7674
where
77-
T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN>,
75+
T: Packable<T_PACKED_LEN>,
7876
{
7977
pub fn read(self) -> T {
8078
let header = self.context.get_block_header();
81-
let mut fields = [0; T_SERIALIZED_LEN];
79+
let mut fields = [0; T_PACKED_LEN];
8280

8381
for i in 0..fields.len() {
8482
fields[i] = header.public_storage_historical_read(
8583
self.storage_slot + i as Field,
8684
(*self.context).this_address(),
8785
);
8886
}
89-
T::deserialize(fields)
87+
T::unpack(fields)
9088
}
9189
}

0 commit comments

Comments
 (0)