Skip to content

Commit

Permalink
chore: fix trait constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Oct 25, 2024
1 parent 05d0ea1 commit 055316b
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 21 deletions.
5 changes: 3 additions & 2 deletions noir-projects/aztec-nr/aztec/src/event/event_interface.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use dep::protocol_types::abis::event_selector::EventSelector;
use dep::protocol_types::traits::{Deserialize, Serialize};

pub trait EventInterface<let N: u32> {
pub trait EventInterface<let N: u32>: Serialize<N> {
fn private_to_be_bytes(self, randomness: Field) -> [u8; N * 32 + 64];
fn to_be_bytes(self) -> [u8; N * 32 + 32];
fn get_event_type_id() -> EventSelector;
fn emit<Env>(self, _emit: fn[Env](Self) -> ());
fn emit<Env>(self, emit: fn[Env](Self) -> ());
}
7 changes: 6 additions & 1 deletion noir-projects/aztec-nr/aztec/src/note/note_interface.nr
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use crate::context::PrivateContext;
use crate::note::note_header::NoteHeader;
use dep::protocol_types::traits::{Deserialize, Empty, Serialize};

pub trait NoteProperties<T> {
fn properties() -> T;
}

pub trait PartialNote<S, F> {
pub trait PartialNote<S, F>
where
S: crate::protocol_types::traits::Empty,
F: crate::protocol_types::traits::Empty,
{
fn setup_payload() -> S;

fn finalization_payload() -> F;
Expand Down
10 changes: 8 additions & 2 deletions noir-projects/aztec-nr/aztec/src/state_vars/map.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::state_vars::storage::Storage;
use dep::protocol_types::{storage::map::derive_storage_slot_in_map, traits::ToField};
use dep::protocol_types::{
storage::map::derive_storage_slot_in_map,
traits::{Deserialize, Serialize, ToField},
};

// docs:start:map
pub struct Map<K, V, Context> {
Expand All @@ -9,7 +12,10 @@ pub struct Map<K, V, Context> {
}
// docs:end:map

impl<K, T, Context> Storage<T> for Map<K, T, Context> {}
impl<K, T, Context, let N: u32> Storage<T, N> for Map<K, T, Context>
where
T: Serialize<N> + Deserialize<N>,
{}

impl<K, V, Context> Map<K, V, Context> {
// docs:start:new
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use dep::protocol_types::{
constants::GENERATOR_INDEX__INITIALIZATION_NULLIFIER, hash::poseidon2_hash_with_separator,
constants::GENERATOR_INDEX__INITIALIZATION_NULLIFIER,
hash::poseidon2_hash_with_separator,
traits::{Deserialize, Serialize},
};

use crate::context::{PrivateContext, UnconstrainedContext};
Expand All @@ -20,7 +22,10 @@ pub struct PrivateImmutable<Note, Context> {
}
// docs:end:struct

impl<T, Context> Storage<T> for PrivateImmutable<T, Context> {}
impl<T, Context, let N: u32> Storage<T, N> for PrivateImmutable<T, Context>
where
T: Serialize<N> + Deserialize<N>,
{}

impl<Note, Context> PrivateImmutable<Note, Context> {
// docs:start:new
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use dep::protocol_types::{
constants::GENERATOR_INDEX__INITIALIZATION_NULLIFIER, hash::poseidon2_hash_with_separator,
constants::GENERATOR_INDEX__INITIALIZATION_NULLIFIER,
hash::poseidon2_hash_with_separator,
traits::{Deserialize, Serialize},
};

use crate::context::{PrivateContext, UnconstrainedContext};
Expand All @@ -22,7 +24,10 @@ pub struct PrivateMutable<Note, Context> {

mod test;

impl<T, Context> Storage<T> for PrivateMutable<T, Context> {}
impl<T, Context, let N: u32> Storage<T, N> for PrivateMutable<T, Context>
where
T: Serialize<N> + Deserialize<N>,
{}

impl<Note, Context> PrivateMutable<Note, Context> {
// docs:start:new
Expand Down
9 changes: 7 additions & 2 deletions noir-projects/aztec-nr/aztec/src/state_vars/private_set.nr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use crate::note::{
};
use crate::state_vars::storage::Storage;
use dep::protocol_types::{
abis::read_request::ReadRequest, constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
abis::read_request::ReadRequest,
constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
traits::{Deserialize, Serialize},
};

// docs:start:struct
Expand All @@ -21,7 +23,10 @@ pub struct PrivateSet<Note, Context> {
}
// docs:end:struct

impl<T, Context> Storage<T> for PrivateSet<T, Context> {}
impl<T, Context, let N: u32> Storage<T, N> for PrivateSet<T, Context>
where
T: Serialize<N> + Deserialize<N>,
{}

impl<Note, Context> PrivateSet<Note, Context> {
// docs:start:new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ pub struct PublicImmutable<T, Context> {
}
// docs:end:public_immutable_struct

impl<T, Context> Storage<T> for PublicImmutable<T, Context> {}
impl<T, Context, let N: u32> Storage<T, N> for PublicImmutable<T, Context>
where
T: Serialize<N> + Deserialize<N>,
{}

impl<T, Context> PublicImmutable<T, Context> {
// docs:start:public_immutable_struct_new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ pub struct PublicMutable<T, Context> {
}
// docs:end:public_mutable_struct

impl<T, Context> Storage<T> for PublicMutable<T, Context> {}
impl<T, Context, let N: u32> Storage<T, N> for PublicMutable<T, Context>
where
T: Serialize<N> + Deserialize<N>,
{}

impl<T, Context> PublicMutable<T, Context> {
// docs:start:public_mutable_struct_new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ pub struct SharedImmutable<T, Context> {
storage_slot: Field,
}

impl<T, Context> Storage<T> for SharedImmutable<T, Context> {}
impl<T, Context, let N: u32> Storage<T, N> for SharedImmutable<T, Context>
where
T: Serialize<N> + Deserialize<N>,
{}

impl<T, Context> SharedImmutable<T, Context> {
pub fn new(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dep::protocol_types::{
address::AztecAddress,
hash::{poseidon2_hash, poseidon2_hash_with_separator},
traits::{FromField, ToField},
traits::{Deserialize, FromField, Serialize, ToField},
utils::arrays::array_concat,
};

Expand Down Expand Up @@ -34,7 +34,10 @@ global HASH_SEPARATOR: u32 = 2;
//
// TODO https://github.com/AztecProtocol/aztec-packages/issues/5736: change the storage allocation scheme so that we
// can actually use it here
impl<T, let INITIAL_DELAY: u32, Context> Storage<T> for SharedMutable<T, INITIAL_DELAY, Context> {}
impl<T, let INITIAL_DELAY: u32, Context, let N: u32> Storage<T, N> for SharedMutable<T, INITIAL_DELAY, Context>
where
T: Serialize<N> + Deserialize<N>,
{}

// SharedMutable<T> stores a value of type T that is:
// - publicly known (i.e. unencrypted)
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/state_vars/storage.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::protocol_types::traits::{Deserialize, Serialize};

pub trait Storage<T>
pub trait Storage<T, let N: u32>
where
T: Serialize<N> + Deserialize<N>,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::{context::PublicContext, event::event_interface::EventInterface};
use dep::protocol_types::traits::Serialize;

fn emit<Event, let N: u32, let M: u32>(context: &mut PublicContext, event: Event)
where
Event: EventInterface<N>,
Event: Serialize<N>,
[Field; N]: LensForEventSelector<N, M>,
{
let selector = Event::get_event_type_id();
Expand All @@ -27,7 +25,6 @@ pub fn encode_event<Event, let N: u32, let M: u32>(
) -> fn[(&mut PublicContext,)](Event) -> ()
where
Event: EventInterface<N>,
Event: Serialize<N>,
[Field; N]: LensForEventSelector<N, M>,
{
|e: Event| { emit(context, e); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ contract ContractInstanceDeployer {
use std::meta::derive;

#[event]
#[derive(Serialize)]
struct ContractInstanceDeployed {
DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE: Field,
address: AztecAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use dep::aztec::{
context::PrivateContext,
macros::notes::note,
note::{note_header::NoteHeader, note_interface::NullifiableNote},
protocol_types::{address::AztecAddress, traits::{Deserialize, Serialize}},
};

// A note which stores a field and is expected to be passed around using the `addNote` function.
// WARNING: This Note is not private as it does not contain randomness and hence it can be easy to perform
// serialized_note attack on it. This note has been developed purely for testing purposes so that it can easily be
// manually added to PXE. Do not use for real applications.
#[note]
#[derive(Serialize, Deserialize)]
pub struct TestNote {
value: Field,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ contract Token {
// an overall small circuit regardless.
global RECURSIVE_TRANSFER_CALL_MAX_NOTES: u32 = 8;

#[event]
#[derive(Serialize)]
#[event]
struct Transfer {
from: AztecAddress,
to: AztecAddress,
Expand Down

0 comments on commit 055316b

Please sign in to comment.