Skip to content

Commit

Permalink
refactor: another pedantic change to public state naming (#1359)
Browse files Browse the repository at this point in the history
# Description

I was writing docs, and something still felt a bit off when trying to
explain and justify the process of `PublicState` initialisation.
Renaming things helped describe what was happening.

I renamed structs from `MyTypeSerialisationInterface` to
`MyTypeSerialisationMethods`. Only `TypeSerialisationInterface` is an
interface. We then create global instances of methods which adhere to
this interface.

Similarly, I've renamed `MyNoteInterface` structs to be `MyNoteMethods`,
because they're collections of methods which adhere to the
`NoteInterface`. E.g. `ValueNoteMethods` implement the `NoteInterface`.
  • Loading branch information
iAmMichaelConnor authored Aug 2, 2023
1 parent ba9d00b commit cb77440
Show file tree
Hide file tree
Showing 28 changed files with 98 additions and 97 deletions.
2 changes: 1 addition & 1 deletion docs/docs/aztec/developer/wallet-providers/keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ An application in Noir can request a nullifier from the current user for computi

```noir
fn compute_nullifier(self) -> Field {
let siloed_note_hash = compute_siloed_note_hash(ValueNoteInterface, self);
let siloed_note_hash = compute_siloed_note_hash(ValueNoteMethods, self);
let secret = get_secret_key(self.owner);
dep::std::hash::pedersen([siloed_note_hash, secret])[0]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::aztec::state_vars::public_state::PublicState;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FieldSerialisationInterface;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FIELD_SERIALISED_LEN;
use dep::aztec::state_vars::type_serialisation::field_serialisation::FieldSerialisationMethods;
use dep::aztec::state_vars::type_serialisation::field_serialisation::FIELD_SERIALISED_LEN;

struct Storage {
current_value: PublicState<Field, FIELD_SERIALISED_LEN>,
Expand All @@ -9,7 +9,7 @@ struct Storage {
impl Storage {
fn init() -> Self {
Storage {
current_value: PublicState::new(1, FieldSerialisationInterface),
current_value: PublicState::new(1, FieldSerialisationMethods),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ contract EasyZkToken {
use dep::value_note::{
balance_utils,
value_note::{
ValueNoteInterface,
ValueNoteMethods,
VALUE_NOTE_LEN,
},
};
Expand Down Expand Up @@ -105,6 +105,6 @@ contract EasyZkToken {
// Note 2: Having it in all the contracts gives us the ability to compute the note hash and nullifier differently for different kind of notes.
unconstrained fn compute_note_hash_and_nullifier(contract_address: Field, nonce: Field, storage_slot: Field, preimage: [Field; VALUE_NOTE_LEN]) -> [Field; 4] {
let note_header = NoteHeader { contract_address, nonce, storage_slot };
note_utils::compute_note_hash_and_nullifier(ValueNoteInterface, note_header, preimage)
note_utils::compute_note_hash_and_nullifier(ValueNoteMethods, note_header, preimage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl AddressNote {
}

fn compute_nullifier(self) -> Field {
let siloed_note_hash = compute_siloed_note_hash(AddressNoteInterface, self);
let siloed_note_hash = compute_siloed_note_hash(AddressNoteMethods, self);
let owner_nullifying_public_key = get_public_key(self.owner);
let secret = get_secret_key(owner_nullifying_public_key);
dep::std::hash::pedersen([
Expand Down Expand Up @@ -82,7 +82,7 @@ fn set_header(note: &mut AddressNote, header: NoteHeader) {
note.set_header(header);
}

global AddressNoteInterface = NoteInterface {
global AddressNoteMethods = NoteInterface {
deserialise,
serialise,
compute_note_hash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract Escrow {

use crate::address_note::{
AddressNote,
AddressNoteInterface,
AddressNoteMethods,
ADDRESS_NOTE_LEN
};

Expand Down Expand Up @@ -74,6 +74,6 @@ contract Escrow {
unconstrained fn compute_note_hash_and_nullifier(contract_address: Field, nonce: Field, storage_slot: Field, preimage: [Field; ADDRESS_NOTE_LEN]) -> [Field; 4] {
let note_header = NoteHeader { contract_address, nonce, storage_slot };
assert(storage_slot == 1);
note_utils::compute_note_hash_and_nullifier(AddressNoteInterface, note_header, preimage)
note_utils::compute_note_hash_and_nullifier(AddressNoteMethods, note_header, preimage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::aztec::state_vars::{

use crate::address_note::{
AddressNote,
AddressNoteInterface,
AddressNoteMethods,
ADDRESS_NOTE_LEN,
};

Expand All @@ -15,7 +15,7 @@ struct Storage {
impl Storage {
fn init() -> Self {
Storage {
owners: Set::new(1, AddressNoteInterface),
owners: Set::new(1, AddressNoteMethods),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use dep::aztec::state_vars::map::Map;
use dep::aztec::state_vars::public_state::PublicState;
use dep::aztec::state_vars::type_serialisation_interface::TypeSerialisationInterface;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FieldSerialisationInterface;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FIELD_SERIALISED_LEN;
use dep::aztec::state_vars::type_serialisation::TypeSerialisationInterface;
use dep::aztec::state_vars::type_serialisation::field_serialisation::FieldSerialisationMethods;
use dep::aztec::state_vars::type_serialisation::field_serialisation::FIELD_SERIALISED_LEN;
use dep::std::hash::pedersen;

// Utility struct used to easily get a "id" for a private user that sits in the same
Expand Down Expand Up @@ -30,7 +30,6 @@ impl Account {
}


global TOT_SERIALISED_LEN: Field = 2;

// Struct to be used to represent "totals". Generally, there should be one per asset.
// It stores the global values that are shared among all users, such as an accumulator
Expand All @@ -42,6 +41,8 @@ struct Tot {
last_updated_ts: u120,
}

global TOT_SERIALISED_LEN: Field = 2;

fn deserialiseTot(fields: [Field; TOT_SERIALISED_LEN]) -> Tot {
Tot {
interest_accumulator: fields[0] as u120,
Expand All @@ -53,35 +54,35 @@ fn serialiseTot(tot: Tot) -> [Field; TOT_SERIALISED_LEN] {
[tot.interest_accumulator as Field, tot.last_updated_ts as Field]
}

global TotSerialisationInterface = TypeSerialisationInterface {
global TotSerialisationMethods = TypeSerialisationInterface {
deserialise: deserialiseTot,
serialise: serialiseTot,
};


// Struct to be used to represent positions when we have more reads.
global POS_SERIALISED_LEN: Field = 2;
// // Struct to be used to represent positions when we have more reads.
// global POS_SERIALISED_LEN: Field = 2;

struct Pos {
owner: Field,
value: Field,
}
// struct Pos {
// owner: Field,
// value: Field,
// }

fn deserialisePos(fields: [Field; POS_SERIALISED_LEN]) -> Pos {
Pos {
owner: fields[0],
value: fields[1],
}
}
// fn deserialisePos(fields: [Field; POS_SERIALISED_LEN]) -> Pos {
// Pos {
// owner: fields[0],
// value: fields[1],
// }
// }

fn serialisePos(pos: Pos) -> [Field; POS_SERIALISED_LEN] {
[pos.owner, pos.value]
}
// fn serialisePos(pos: Pos) -> [Field; POS_SERIALISED_LEN] {
// [pos.owner, pos.value]
// }

global PosSerialisationInterface = TypeSerialisationInterface {
deserialise: deserialisePos,
serialise: serialisePos,
};
// global PosSerialisationMethods = TypeSerialisationInterface {
// deserialise: deserialisePos,
// serialise: serialisePos,
// };


// Storage structure, containing all storage, and specifying what slots they use.
Expand All @@ -94,9 +95,9 @@ struct Storage {
impl Storage {
fn init() -> Self {
Storage {
assets: Map::new(1, |slot| PublicState::new(slot, TotSerialisationInterface)), // uses 2 storage slots.
collateral: Map::new(2, |slot| PublicState::new(slot, FieldSerialisationInterface)), // uses 1 storage slots.
static_debt: Map::new(3, |slot| PublicState::new(slot, FieldSerialisationInterface)), // uses 1 storage slots.
assets: Map::new(1, |slot| PublicState::new(slot, TotSerialisationMethods)),
collateral: Map::new(2, |slot| PublicState::new(slot, FieldSerialisationMethods)),
static_debt: Map::new(3, |slot| PublicState::new(slot, FieldSerialisationMethods)),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract NonNativeToken {
use dep::value_note::{
balance_utils,
utils::{send_note, spend_notes},
value_note::{VALUE_NOTE_LEN, ValueNoteInterface},
value_note::{VALUE_NOTE_LEN, ValueNoteMethods},
};

use crate::transparent_note::TransparentNote;
Expand Down Expand Up @@ -327,6 +327,6 @@ contract NonNativeToken {
// Note 2: Having it in all the contracts gives us the ability to compute the note hash and nullifier differently for different kind of notes.
unconstrained fn compute_note_hash_and_nullifier(contract_address: Field, nonce: Field, storage_slot: Field, preimage: [Field; VALUE_NOTE_LEN]) -> [Field; 4] {
let note_header = NoteHeader { contract_address, nonce, storage_slot };
note_utils::compute_note_hash_and_nullifier(ValueNoteInterface, note_header, preimage)
note_utils::compute_note_hash_and_nullifier(ValueNoteMethods, note_header, preimage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use dep::aztec::state_vars::{
map::Map,
set::Set,
public_state::PublicState,
type_serialisation_interface::field_serialisation_interface::{
type_serialisation::field_serialisation::{
FIELD_SERIALISED_LEN,
FieldSerialisationInterface,
FieldSerialisationMethods,
},
};

use dep::value_note::value_note::{
ValueNote,
ValueNoteInterface,
ValueNoteMethods,
VALUE_NOTE_LEN,
};

Expand All @@ -23,8 +23,8 @@ struct Storage {
impl Storage {
fn init() -> Self {
Storage {
balances: Map::new(1, |slot| Set::new(slot, ValueNoteInterface)),
public_balances: Map::new(2, |slot| PublicState::new(slot, FieldSerialisationInterface)),
balances: Map::new(1, |slot| Set::new(slot, ValueNoteMethods)),
public_balances: Map::new(2, |slot| PublicState::new(slot, FieldSerialisationMethods)),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract PendingCommitments {
use dep::value_note::{
balance_utils,
filter::get_2_notes,
value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteInterface},
value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteMethods},
};

use crate::storage::Storage;
Expand Down Expand Up @@ -231,6 +231,6 @@ contract PendingCommitments {
// Note 2: Having it in all the contracts gives us the ability to compute the note hash and nullifier differently for different kind of notes.
unconstrained fn compute_note_hash_and_nullifier(contract_address: Field, nonce: Field, storage_slot: Field, preimage: [Field; VALUE_NOTE_LEN]) -> [Field; 4] {
let note_header = NoteHeader { contract_address, nonce, storage_slot };
note_utils::compute_note_hash_and_nullifier(ValueNoteInterface, note_header, preimage)
note_utils::compute_note_hash_and_nullifier(ValueNoteMethods, note_header, preimage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::aztec::state_vars::{
};
use dep::value_note::value_note::{
ValueNote,
ValueNoteInterface,
ValueNoteMethods,
VALUE_NOTE_LEN,
};

Expand All @@ -15,7 +15,7 @@ struct Storage {
impl Storage {
fn init() -> Self {
Storage {
balances: Map::new(1, |s| Set::new(s, ValueNoteInterface)),
balances: Map::new(1, |s| Set::new(s, ValueNoteMethods)),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl AddressNote {
}

fn compute_nullifier(self) -> Field {
let siloed_note_hash = compute_siloed_note_hash(AddressNoteInterface, self);
let siloed_note_hash = compute_siloed_note_hash(AddressNoteMethods, self);
let owner_nullifying_public_key = get_public_key(self.address);
let secret = get_secret_key(owner_nullifying_public_key);
dep::std::hash::pedersen([
Expand Down Expand Up @@ -79,7 +79,7 @@ fn set_header(note: &mut AddressNote, header: NoteHeader) {
note.set_header(header);
}

global AddressNoteInterface = NoteInterface {
global AddressNoteMethods = NoteInterface {
deserialise,
serialise,
compute_note_hash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ contract PokeableToken {
use dep::value_note::{
balance_utils,
utils::{send_note, spend_notes},
value_note::{VALUE_NOTE_LEN, ValueNoteInterface, ValueNote},
value_note::{VALUE_NOTE_LEN, ValueNoteMethods, ValueNote},
filter::get_2_notes,
};
use dep::aztec::abi;
Expand All @@ -22,7 +22,7 @@ contract PokeableToken {
use dep::aztec::log::emit_encrypted_log;

use crate::storage::Storage;
use crate::address_note::{AddressNote, AddressNoteInterface};
use crate::address_note::{AddressNote, AddressNoteMethods};

// Constructs the contract and sets `initial_supply` which is fully owned by `sender`.
fn constructor(
Expand Down Expand Up @@ -135,9 +135,9 @@ contract PokeableToken {
unconstrained fn compute_note_hash_and_nullifier(contract_address: Field, nonce: Field, storage_slot: Field, preimage: [Field; VALUE_NOTE_LEN]) -> [Field; 4] {
let note_header = NoteHeader { contract_address, nonce, storage_slot };
if (storage_slot == 1) | (storage_slot == 2) {
note_utils::compute_note_hash_and_nullifier(AddressNoteInterface, note_header, preimage)
note_utils::compute_note_hash_and_nullifier(AddressNoteMethods, note_header, preimage)
} else {
note_utils::compute_note_hash_and_nullifier(ValueNoteInterface, note_header, preimage)
note_utils::compute_note_hash_and_nullifier(ValueNoteMethods, note_header, preimage)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use dep::aztec::state_vars::{
};
use crate::address_note::{
AddressNote,
AddressNoteInterface,
AddressNoteMethods,
ADDRESS_NOTE_LEN,
};
use dep::value_note::value_note::{
ValueNote,
ValueNoteInterface,
ValueNoteMethods,
VALUE_NOTE_LEN,
};

Expand All @@ -23,9 +23,9 @@ struct Storage {
impl Storage {
fn init() -> Self {
Storage {
sender: ImmutableSingleton::new(1, AddressNoteInterface),
recipient: ImmutableSingleton::new(2, AddressNoteInterface),
balances: Map::new(3, |s| Set::new(s, ValueNoteInterface)),
sender: ImmutableSingleton::new(1, AddressNoteMethods),
recipient: ImmutableSingleton::new(2, AddressNoteMethods),
balances: Map::new(3, |slot| Set::new(slot, ValueNoteMethods)),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dep::aztec::state_vars::map::Map;
use dep::aztec::state_vars::public_state::PublicState;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FieldSerialisationInterface;
use dep::aztec::state_vars::type_serialisation_interface::field_serialisation_interface::FIELD_SERIALISED_LEN;
use dep::aztec::state_vars::type_serialisation::field_serialisation::FieldSerialisationMethods;
use dep::aztec::state_vars::type_serialisation::field_serialisation::FIELD_SERIALISED_LEN;

struct Storage {
balances: Map<PublicState<Field, FIELD_SERIALISED_LEN>>,
Expand All @@ -11,7 +11,7 @@ struct Storage {
impl Storage {
fn init() -> Self {
Storage {
balances: Map::new(1, |slot| PublicState::new(slot, FieldSerialisationInterface)),
balances: Map::new(1, |slot| PublicState::new(slot, FieldSerialisationMethods)),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ contract SchnorrMultiKeyAccount {

use crate::storage::Storage;
use crate::public_key_note::PublicKeyNote;
use crate::public_key_note::PublicKeyNoteInterface;
use crate::public_key_note::PublicKeyNoteMethods;
use crate::public_key_note::PUBLIC_KEY_NOTE_LEN;

fn entrypoint(
Expand Down Expand Up @@ -90,6 +90,6 @@ contract SchnorrMultiKeyAccount {
unconstrained fn compute_note_hash_and_nullifier(contract_address: Field, nonce: Field, storage_slot: Field, preimage: [Field; PUBLIC_KEY_NOTE_LEN]) -> [Field; 4] {
assert(storage_slot == 1);
let note_header = NoteHeader { contract_address, nonce, storage_slot };
note_utils::compute_note_hash_and_nullifier(PublicKeyNoteInterface, note_header, preimage)
note_utils::compute_note_hash_and_nullifier(PublicKeyNoteMethods, note_header, preimage)
}
}
Loading

0 comments on commit cb77440

Please sign in to comment.