Skip to content

Commit

Permalink
chore: rename singletons to leaky because they leak privacy
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Jan 23, 2024
1 parent ee317e6 commit 8aa8299
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 89 deletions.
74 changes: 36 additions & 38 deletions docs/docs/dev_docs/contracts/syntax/storage/main.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions yarn-project/aztec-nr/aztec/src/state_vars.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod immutable_singleton;
mod leaky_immutable_singleton;
mod map;
mod public_state;
mod set;
mod singleton;
mod leaky_singleton;
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ use crate::note::{
use crate::oracle::notes::check_nullifier_exists;

// docs:start:struct
struct ImmutableSingleton<Note, N> {
struct LeakyImmutableSingleton<Note, N> {
context: Option<&mut PrivateContext>,
storage_slot: Field,
note_interface: NoteInterface<Note, N>,
}
// docs:end:struct

impl<Note, N> ImmutableSingleton<Note, N> {
impl<Note, N> LeakyImmutableSingleton<Note, N> {
// docs:start:new
pub fn new(
context: Context,
storage_slot: Field,
note_interface: NoteInterface<Note, N>,
) -> Self {
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1.");
ImmutableSingleton {
Self {
context: context.private,
storage_slot,
note_interface,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ use crate::oracle::{
};

// docs:start:struct
struct Singleton<Note, N> {
struct LeakySingleton<Note, N> {
context: Option<&mut PrivateContext>,
storage_slot: Field,
note_interface: NoteInterface<Note, N>,
}
// docs:end:struct

impl<Note, N> Singleton<Note, N> {
impl<Note, N> LeakySingleton<Note, N> {
// docs:start:new
pub fn new(
context: Context,
storage_slot: Field,
note_interface: NoteInterface<Note, N>,
) -> Self {
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1.");
Singleton {
Self {
context: context.private,
storage_slot,
note_interface,
Expand Down
20 changes: 10 additions & 10 deletions yarn-project/end-to-end/src/e2e_singleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DocsExampleContract } from '@aztec/noir-contracts';

import { setup } from './fixtures/utils.js';

describe('e2e_singleton', () => {
describe('e2e_singletons', () => {
let wallet: Wallet;

let teardown: () => Promise<void>;
Expand All @@ -19,13 +19,13 @@ describe('e2e_singleton', () => {

afterAll(() => teardown());

describe('Singleton', () => {
it('fail to read uninitialized singleton', async () => {
describe('LeakySingleton', () => {
it('fail to read uninitialized leakySingleton', async () => {
expect(await contract.methods.is_legendary_initialized().view()).toEqual(false);
await expect(contract.methods.get_legendary_card().view()).rejects.toThrowError();
});

it('initialize singleton', async () => {
it('initialize leakySingleton', async () => {
expect(await contract.methods.is_legendary_initialized().view()).toEqual(false);
const receipt = await contract.methods.initialize_private(RANDOMNESS, POINTS).send().wait();
expect(receipt.status).toEqual(TxStatus.MINED);
Expand All @@ -43,7 +43,7 @@ describe('e2e_singleton', () => {
expect(await contract.methods.is_legendary_initialized().view()).toEqual(true);
});

it('read initialized singleton', async () => {
it('read initialized leakySingleton', async () => {
expect(await contract.methods.is_legendary_initialized().view()).toEqual(true);
const { points, randomness } = await contract.methods.get_legendary_card().view();
expect(points).toEqual(POINTS);
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('e2e_singleton', () => {
expect(noteBefore.header.nonce).not.toEqual(noteAfter.header.nonce);
});

it('replace singleton with other values', async () => {
it('replace leakySingleton with other values', async () => {
expect(await contract.methods.is_legendary_initialized().view()).toEqual(true);
const receipt = await contract.methods
.update_legendary_card(RANDOMNESS + 2n, POINTS + 1n)
Expand All @@ -90,7 +90,7 @@ describe('e2e_singleton', () => {
expect(randomness).toEqual(RANDOMNESS + 2n);
});

it('replace singleton dependent on prior value', async () => {
it('replace leakySingleton dependent on prior value', async () => {
expect(await contract.methods.is_legendary_initialized().view()).toEqual(true);
const noteBefore = await contract.methods.get_legendary_card().view();
const receipt = await contract.methods.increase_legendary_points().send().wait();
Expand All @@ -107,12 +107,12 @@ describe('e2e_singleton', () => {
});

describe('Immutable Singleton', () => {
it('fail to read uninitialized singleton', async () => {
it('fail to read uninitialized leakySingleton', async () => {
expect(await contract.methods.is_imm_initialized().view()).toEqual(false);
await expect(contract.methods.get_imm_card().view()).rejects.toThrowError();
});

it('initialize singleton', async () => {
it('initialize leakySingleton', async () => {
expect(await contract.methods.is_imm_initialized().view()).toEqual(false);
const receipt = await contract.methods.initialize_immutable_singleton(RANDOMNESS, POINTS).send().wait();
expect(receipt.status).toEqual(TxStatus.MINED);
Expand All @@ -132,7 +132,7 @@ describe('e2e_singleton', () => {
expect(await contract.methods.is_imm_initialized().view()).toEqual(true);
});

it('read initialized singleton', async () => {
it('read initialized leakySingleton', async () => {
expect(await contract.methods.is_imm_initialized().view()).toEqual(true);
const { points, randomness } = await contract.methods.get_imm_card().view();
expect(points).toEqual(POINTS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
mod options;
mod types;

// Following is a very simple game to show case use of singleton in as minimalistic way as possible
// It also serves as an e2e test that you can read and then replace the singleton in the same call
// Following is a very simple game to show case use of LeakySingleton in as minimalistic way as possible
// It also serves as an e2e test that you can read and then replace the LeakySingleton in the same call
// (tests ordering in the circuit)

// you have a card (singleton). Anyone can create a bigger card. Whoever is bigger will be the leader.
// it also has dummy methods and other examples used for documentation e.g.
// You have a card (LeakySingleton). If you get a new card that have more points, you can replace it as the leader.
// It also has dummy methods and other examples used for documentation e.g.
// how to create custom notes, a custom struct for public state, a custom note that may be unencrypted
// also has `options.nr` which shows various ways of using `NoteGetterOptions` to query notes
// it also shows what our macros do behind the scenes!
// It also shows what our macros do behind the scenes!

contract DocsExample {
// how to import dependencies defined in your workspace
Expand All @@ -23,7 +23,12 @@ contract DocsExample {
utils as note_utils,
},
context::{PrivateContext, PublicContext, Context},
state_vars::{map::Map, public_state::PublicState,singleton::Singleton, immutable_singleton::ImmutableSingleton},
state_vars::{
map::Map,
public_state::PublicState,
leaky_singleton::LeakySingleton,
leaky_immutable_singleton::LeakyImmutableSingleton
},
};
// how to import methods from other files/folders within your workspace
use crate::options::create_account_card_getter_options;
Expand All @@ -35,14 +40,14 @@ contract DocsExample {
struct Storage {
// Shows how to create a custom struct in Public
leader: PublicState<Leader, LEADER_SERIALIZED_LEN>,
// docs:start:storage-singleton-declaration
legendary_card: Singleton<CardNote, CARD_NOTE_LEN>,
// docs:end:storage-singleton-declaration
// just used for docs example to show how to create a singleton map.
// docs:start:storage-map-singleton-declaration
profiles: Map<AztecAddress, Singleton<CardNote, CARD_NOTE_LEN>>,
// docs:end:storage-map-singleton-declaration
imm_singleton: ImmutableSingleton<CardNote, CARD_NOTE_LEN>,
// docs:start:storage-leaky-singleton-declaration
legendary_card: LeakySingleton<CardNote, CARD_NOTE_LEN>,
// docs:end:storage-leaky-singleton-declaration
// just used for docs example to show how to create a LeakySingleton map.
// docs:start:storage-map-leaky-singleton-declaration
profiles: Map<AztecAddress, LeakySingleton<CardNote, CARD_NOTE_LEN>>,
// docs:end:storage-map-leaky-singleton-declaration
imm_singleton: LeakyImmutableSingleton<CardNote, CARD_NOTE_LEN>,
}

impl Storage {
Expand All @@ -53,20 +58,20 @@ contract DocsExample {
1,
LeaderSerializationMethods,
),
// docs:start:start_vars_singleton
legendary_card: Singleton::new(context, 2, CardNoteMethods),
// docs:end:start_vars_singleton
// docs:start:state-vars-leaky-singleton
legendary_card: LeakySingleton::new(context, 2, CardNoteMethods),
// docs:end:state-vars-leaky-singleton
// just used for docs example (not for game play):
// docs:start:state_vars-MapSingleton
// docs:start:state-vars-map-leaky-singleton
profiles: Map::new(
context,
3,
|context, slot| {
Singleton::new(context, slot, CardNoteMethods)
LeakySingleton::new(context, slot, CardNoteMethods)
},
),
// docs:end:state_vars-MapSingleton
imm_singleton: ImmutableSingleton::new(context, 4, CardNoteMethods),
// docs:end:state-vars-map-leaky-singleton
imm_singleton: LeakyImmutableSingleton::new(context, 4, CardNoteMethods),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ contract EcdsaAccount {
utils as note_utils,
},
oracle::get_public_key::get_public_key,
state_vars::immutable_singleton::ImmutableSingleton,
state_vars::leaky_immutable_singleton::LeakyImmutableSingleton,
};
use dep::authwit:: {
entrypoint::{ EntrypointPayload, ENTRYPOINT_PAYLOAD_SIZE },
Expand All @@ -27,13 +27,13 @@ contract EcdsaAccount {
};

struct Storage {
public_key: ImmutableSingleton<EcdsaPublicKeyNote, ECDSA_PUBLIC_KEY_NOTE_LEN>,
public_key: LeakyImmutableSingleton<EcdsaPublicKeyNote, ECDSA_PUBLIC_KEY_NOTE_LEN>,
}

impl Storage {
fn init(context: Context) -> Self {
Storage {
public_key: ImmutableSingleton::new(context, 1, EcdsaPublicKeyNoteInterface),
public_key: LeakyImmutableSingleton::new(context, 1, EcdsaPublicKeyNoteInterface),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract SchnorrAccount {
context::{PrivateContext, Context},
note::{ note_header::NoteHeader, utils as note_utils },
oracle::get_public_key::get_public_key,
state_vars::immutable_singleton::ImmutableSingleton,
state_vars::leaky_immutable_singleton::LeakyImmutableSingleton,
};
use dep::authwit:: {
entrypoint::{ EntrypointPayload, ENTRYPOINT_PAYLOAD_SIZE },
Expand All @@ -24,15 +24,15 @@ contract SchnorrAccount {

struct Storage {
// docs:start:storage
signing_public_key: ImmutableSingleton<PublicKeyNote, PUBLIC_KEY_NOTE_LEN>,
signing_public_key: LeakyImmutableSingleton<PublicKeyNote, PUBLIC_KEY_NOTE_LEN>,
// docs:end:storage
}

impl Storage {
fn init(context: Context) -> Self {
Storage {
// docs:start:storage_init
signing_public_key: ImmutableSingleton::new(context, 1, PublicKeyNoteMethods),
signing_public_key: LeakyImmutableSingleton::new(context, 1, PublicKeyNoteMethods),
// docs:end:storage_init
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract Test {
context::get_portal_address,
rand::rand
},
state_vars::immutable_singleton::ImmutableSingleton,
state_vars::leaky_immutable_singleton::LeakyImmutableSingleton,
log::emit_unencrypted_log_from_private,
types::vec::BoundedVec,
};
Expand All @@ -42,13 +42,13 @@ contract Test {
}

struct Storage {
example_constant: ImmutableSingleton<FieldNote, FIELD_NOTE_LEN>,
example_constant: LeakyImmutableSingleton<FieldNote, FIELD_NOTE_LEN>,
}

impl Storage {
fn init(context: Context) -> Self {
Storage {
example_constant: ImmutableSingleton::new(context, 1, FieldNoteMethods),
example_constant: LeakyImmutableSingleton::new(context, 1, FieldNoteMethods),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract TokenBlacklist {
},
context::{PrivateContext, PublicContext, Context},
hash::{compute_secret_hash},
state_vars::{map::Map, public_state::PublicState, set::Set, immutable_singleton::ImmutableSingleton},
state_vars::{map::Map, public_state::PublicState, set::Set, leaky_immutable_singleton::LeakyImmutableSingleton},
types::type_serialization::{
field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN},
bool_serialization::{BoolSerializationMethods, BOOL_SERIALIZED_LEN},
Expand Down Expand Up @@ -63,7 +63,7 @@ contract TokenBlacklist {
total_supply: PublicState<SafeU120, SAFE_U120_SERIALIZED_LEN>,
pending_shields: Set<TransparentNote, TRANSPARENT_NOTE_LEN>,
public_balances: Map<AztecAddress, PublicState<SafeU120, SAFE_U120_SERIALIZED_LEN>>,
slow_update: ImmutableSingleton<FieldNote, FIELD_NOTE_LEN>,
slow_update: LeakyImmutableSingleton<FieldNote, FIELD_NOTE_LEN>,
public_slow_update: PublicState<AztecAddress, AZTEC_ADDRESS_SERIALIZED_LEN>,
}

Expand Down Expand Up @@ -95,7 +95,7 @@ contract TokenBlacklist {
),
// Below is an abomination to have same value in private and public (immutable in solidity).
// docs:start:slow_updates_storage
slow_update: ImmutableSingleton::new(context, 7, FieldNoteMethods),
slow_update: LeakyImmutableSingleton::new(context, 7, FieldNoteMethods),
public_slow_update: PublicState::new(
context,
8,
Expand Down

0 comments on commit 8aa8299

Please sign in to comment.