Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Oct 27, 2024
1 parent ac31baa commit 99cc1cf
Show file tree
Hide file tree
Showing 25 changed files with 148 additions and 155 deletions.
6 changes: 2 additions & 4 deletions boxes/boxes/react/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,21 @@ contract BoxReact {
fn constructor(
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: OvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
let mut new_number = ValueNote::new(number, owner);
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
}

#[private]
fn setNumber(
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: OvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
let mut new_number = ValueNote::new(number, owner);
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
}

Expand Down
6 changes: 2 additions & 4 deletions boxes/boxes/vanilla/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,21 @@ contract Vanilla {
fn constructor(
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: OvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
let mut new_number = ValueNote::new(number, owner);
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
}

#[private]
fn setNumber(
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: OvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
let mut new_number = ValueNote::new(number, owner);
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
}

Expand Down
14 changes: 8 additions & 6 deletions noir-projects/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::aztec::{
context::PrivateContext,
keys::getters::get_nsk_app,
keys::getters::{get_nsk_app, get_public_keys},
macros::notes::note,
note::{
note_header::NoteHeader, note_interface::NullifiableNote,
Expand All @@ -20,7 +20,7 @@ use dep::aztec::{
pub struct AddressNote {
address: AztecAddress,
// The nullifying public key hash is used with the nsk_app to ensure that the note can be privately spent.
npk_m_hash: Field,
owner: AztecAddress,
randomness: Field,
}
// docs:end:address_note_struct
Expand All @@ -32,7 +32,8 @@ impl NullifiableNote for AddressNote {
context: &mut PrivateContext,
note_hash_for_nullify: Field,
) -> Field {
let secret = context.request_nsk_app(self.npk_m_hash);
let owner_npk_m_hash = get_public_keys(self.owner).npk_m.hash();
let secret = context.request_nsk_app(owner_npk_m_hash);
poseidon2_hash_with_separator(
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
Expand All @@ -41,7 +42,8 @@ impl NullifiableNote for AddressNote {

unconstrained fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_nullify(self);
let secret = get_nsk_app(self.npk_m_hash);
let owner_npk_m_hash = get_public_keys(self.owner).npk_m.hash();
let secret = get_nsk_app(owner_npk_m_hash);
poseidon2_hash_with_separator(
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
Expand All @@ -50,13 +52,13 @@ impl NullifiableNote for AddressNote {
}

impl AddressNote {
pub fn new(address: AztecAddress, npk_m_hash: Field) -> Self {
pub fn new(address: AztecAddress, owner: AztecAddress) -> Self {
// We use the randomness to preserve the privacy of the note recipient by preventing brute-forcing, so a
// malicious sender could use non-random values to make the note less private. But they already know the full
// note pre-image anyway, and so the recipient already trusts them to not disclose this information. We can
// therefore assume that the sender will cooperate in the random value generation.
let randomness = unsafe { random() };
AddressNote { address, npk_m_hash, randomness, header: NoteHeader::empty() }
AddressNote { address, owner, randomness, header: NoteHeader::empty() }
}
// docs:end:address_note_def
}
19 changes: 10 additions & 9 deletions noir-projects/aztec-nr/uint-note/src/uint_note.nr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use dep::aztec::{
keys::getters::get_nsk_app,
keys::getters::{get_nsk_app, get_public_keys},
macros::notes::partial_note,
note::utils::compute_note_hash_for_nullify,
oracle::random::random,
prelude::{NoteHeader, NullifiableNote, PrivateContext},
protocol_types::{
constants::GENERATOR_INDEX__NOTE_NULLIFIER, hash::poseidon2_hash_with_separator,
address::AztecAddress, constants::GENERATOR_INDEX__NOTE_NULLIFIER, hash::poseidon2_hash_with_separator,
},
};

Expand All @@ -14,8 +14,7 @@ use dep::aztec::{
pub struct UintNote {
// The amount of tokens in the note
value: U128,
// The nullifying public key hash is used with the nsk_app to ensure that the note can be privately spent.
npk_m_hash: Field,
owner: AztecAddress,
// Randomness of the note to hide its contents
randomness: Field,
}
Expand All @@ -28,7 +27,8 @@ impl NullifiableNote for UintNote {
context: &mut PrivateContext,
note_hash_for_nullify: Field,
) -> Field {
let secret = context.request_nsk_app(self.npk_m_hash);
let owner_npk_m_hash = get_public_keys(self.owner).npk_m.hash();
let secret = context.request_nsk_app(owner_npk_m_hash);
poseidon2_hash_with_separator(
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
Expand All @@ -38,7 +38,8 @@ impl NullifiableNote for UintNote {

unconstrained fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_nullify(self);
let secret = get_nsk_app(self.npk_m_hash);
let owner_npk_m_hash = get_public_keys(self.owner).npk_m.hash();
let secret = get_nsk_app(owner_npk_m_hash);
poseidon2_hash_with_separator(
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER,
Expand All @@ -49,19 +50,19 @@ impl NullifiableNote for UintNote {
impl Eq for UintNote {
fn eq(self, other: Self) -> bool {
(self.value == other.value)
& (self.npk_m_hash == other.npk_m_hash)
& (self.owner == other.owner)
& (self.randomness == other.randomness)
}
}

impl UintNote {
pub fn new(value: U128, owner_npk_m_hash: Field) -> Self {
pub fn new(value: U128, owner: AztecAddress) -> Self {
// We use the randomness to preserve the privacy of the note recipient by preventing brute-forcing, so a
// malicious sender could use non-random values to make the note less private. But they already know the full
// note pre-image anyway, and so the recipient already trusts them to not disclose this information. We can
// therefore assume that the sender will cooperate in the random value generation.
let randomness = unsafe { random() };
Self { value, npk_m_hash: owner_npk_m_hash, randomness, header: NoteHeader::empty() }
Self { value, owner, randomness, header: NoteHeader::empty() }
}

pub fn get_value(self) -> U128 {
Expand Down
21 changes: 11 additions & 10 deletions noir-projects/aztec-nr/value-note/src/value_note.nr
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use dep::aztec::{
context::PrivateContext,
keys::getters::get_nsk_app,
keys::getters::{get_nsk_app, get_public_keys},
macros::notes::note,
note::{
note_header::NoteHeader, note_interface::NullifiableNote,
utils::compute_note_hash_for_nullify,
},
oracle::random::random,
protocol_types::{
constants::GENERATOR_INDEX__NOTE_NULLIFIER, hash::poseidon2_hash_with_separator,
traits::Serialize,
address::AztecAddress, constants::GENERATOR_INDEX__NOTE_NULLIFIER,
hash::poseidon2_hash_with_separator, traits::Serialize,
},
};

Expand All @@ -22,8 +22,7 @@ global VALUE_NOTE_LEN: u32 = 3; // 3 plus a header.
#[derive(Serialize)]
pub struct ValueNote {
value: Field,
// The nullifying public key hash is used with the nsk_app to ensure that the note can be privately spent.
npk_m_hash: Field,
owner: AztecAddress,
randomness: Field,
}
// docs:end:value-note-def
Expand All @@ -36,7 +35,8 @@ impl NullifiableNote for ValueNote {
context: &mut PrivateContext,
note_hash_for_nullify: Field,
) -> Field {
let secret = context.request_nsk_app(self.npk_m_hash);
let owner_npk_m_hash: Field = get_public_keys(self.owner).npk_m.hash();
let secret = context.request_nsk_app(owner_npk_m_hash);
poseidon2_hash_with_separator(
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
Expand All @@ -47,7 +47,8 @@ impl NullifiableNote for ValueNote {

unconstrained fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_nullify(self);
let secret = get_nsk_app(self.npk_m_hash);
let owner_npk_m_hash: Field = get_public_keys(self.owner).npk_m.hash();
let secret = get_nsk_app(owner_npk_m_hash);
poseidon2_hash_with_separator(
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
Expand All @@ -56,21 +57,21 @@ impl NullifiableNote for ValueNote {
}

impl ValueNote {
pub fn new(value: Field, npk_m_hash: Field) -> Self {
pub fn new(value: Field, owner: AztecAddress) -> Self {
// We use the randomness to preserve the privacy of the note recipient by preventing brute-forcing, so a
// malicious sender could use non-random values to make the note less private. But they already know the full
// note pre-image anyway, and so the recipient already trusts them to not disclose this information. We can
// therefore assume that the sender will cooperate in the random value generation.
let randomness = unsafe { random() };
let header = NoteHeader::empty();
ValueNote { value, npk_m_hash, randomness, header }
ValueNote { value, owner, randomness, header }
}
}

impl Eq for ValueNote {
fn eq(self, other: Self) -> bool {
(self.value == other.value)
& (self.npk_m_hash == other.npk_m_hash)
& (self.owner == other.owner)
& (self.randomness == other.randomness)
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use dep::aztec::{
hash::poseidon2_hash_with_separator,
keys::getters::get_nsk_app,
keys::getters::{get_nsk_app, get_public_keys},
macros::notes::note,
note::utils::compute_note_hash_for_nullify,
oracle::random::random,
prelude::{NoteHeader, NullifiableNote, PrivateContext},
protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER,
protocol_types::{address::AztecAddress, constants::GENERATOR_INDEX__NOTE_NULLIFIER},
};

#[note]
pub struct SubscriptionNote {
// The nullifying public key hash is used with the nsk_app to ensure that the note can be privately spent.
npk_m_hash: Field,
owner: AztecAddress,
expiry_block_number: Field,
remaining_txs: Field,
// Randomness of the note to hide its contents
Expand All @@ -24,7 +24,8 @@ impl NullifiableNote for SubscriptionNote {
context: &mut PrivateContext,
note_hash_for_nullify: Field,
) -> Field {
let secret = context.request_nsk_app(self.npk_m_hash);
let owner_npk_m_hash: Field = get_public_keys(self.owner).npk_m.hash();
let secret = context.request_nsk_app(owner_npk_m_hash);
poseidon2_hash_with_separator(
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
Expand All @@ -33,7 +34,8 @@ impl NullifiableNote for SubscriptionNote {

unconstrained fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_nullify(self);
let secret = get_nsk_app(self.npk_m_hash);
let owner_npk_m_hash: Field = get_public_keys(self.owner).npk_m.hash();
let secret = get_nsk_app(owner_npk_m_hash);
poseidon2_hash_with_separator(
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
Expand All @@ -42,14 +44,14 @@ impl NullifiableNote for SubscriptionNote {
}

impl SubscriptionNote {
pub fn new(npk_m_hash: Field, expiry_block_number: Field, remaining_txs: Field) -> Self {
pub fn new(owner: AztecAddress, expiry_block_number: Field, remaining_txs: Field) -> Self {
// We use the randomness to preserve the privacy of the note recipient by preventing brute-forcing, so a
// malicious sender could use non-random values to make the note less private. But they already know the full
// note pre-image anyway, and so the recipient already trusts them to not disclose this information. We can
// therefore assume that the sender will cooperate in the random value generation.
let randomness = unsafe { random() };
Self {
npk_m_hash,
owner,
expiry_block_number,
remaining_txs,
randomness,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use dep::aztec::{
keys::getters::get_public_keys,
note::constants::MAX_NOTES_PER_PAGE,
protocol_types::{
address::AztecAddress,
constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
traits::{FromField, Serialize, ToField},
},
Expand Down Expand Up @@ -55,13 +56,13 @@ pub struct CardNote {
}

impl CardNote {
fn new(strength: u32, points: u32, npk_m_hash: Field) -> Self {
fn new(strength: u32, points: u32, owner: AztecAddress) -> Self {
let card = Card { strength, points };
CardNote::from_card(card, npk_m_hash)
CardNote::from_card(card, owner)
}

pub fn from_card(card: Card, npk_m_hash: Field) -> CardNote {
CardNote { card, note: ValueNote::new(card.to_field(), npk_m_hash) }
pub fn from_card(card: Card, owner: AztecAddress) -> CardNote {
CardNote { card, note: ValueNote::new(card.to_field(), owner) }
}

pub fn from_note(note: ValueNote) -> CardNote {
Expand Down Expand Up @@ -108,14 +109,11 @@ impl<Context> Deck<Context> {

impl Deck<&mut PrivateContext> {
pub fn add_cards<let N: u32>(&mut self, cards: [Card; N], owner: AztecAddress) -> [CardNote] {
let owner_keys = get_public_keys(owner);

let owner_npk_m_hash = owner_keys.npk_m.hash();
let msg_sender_ovpk_m = get_public_keys(self.set.context.msg_sender()).ovpk_m;

let mut inserted_cards = &[];
for card in cards {
let mut card_note = CardNote::from_card(card, owner_npk_m_hash);
let mut card_note = CardNote::from_card(card, owner);
self.set.insert(&mut card_note.note).emit(encode_and_encrypt_note(
self.set.context,
msg_sender_ovpk_m,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use crate::types::card_note::{CARD_NOTE_LEN, CardNote};
use dep::aztec::prelude::NoteGetterOptions;

use dep::aztec::{note::note_getter_options::SortOrder, utils::comparison::Comparator};
use dep::aztec::protocol_types::constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL;
use dep::aztec::protocol_types::{address::AztecAddress, constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL};

// Shows how to use NoteGetterOptions and query for notes.

// docs:start:state_vars-NoteGetterOptionsSelectSortOffset
pub fn create_npk_card_getter_options(
account_npk_m_hash: Field,
account: AztecAddress,
offset: u32,
) -> NoteGetterOptions<CardNote, CARD_NOTE_LEN, Field, Field> {
let mut options = NoteGetterOptions::new();
options
.select(CardNote::properties().npk_m_hash, Comparator.EQ, account_npk_m_hash)
.select(CardNote::properties().owner, Comparator.EQ, account)
.sort(CardNote::properties().points, SortOrder.DESC)
.set_offset(offset)
}
Expand All @@ -23,13 +23,13 @@ pub fn create_npk_card_getter_options(
pub fn create_exact_card_getter_options(
points: u8,
secret: Field,
account_npk_m_hash: Field,
account: AztecAddress,
) -> NoteGetterOptions<CardNote, CARD_NOTE_LEN, Field, Field> {
let mut options = NoteGetterOptions::new();
options
.select(CardNote::properties().points, Comparator.EQ, points as Field)
.select(CardNote::properties().randomness, Comparator.EQ, secret)
.select(CardNote::properties().npk_m_hash, Comparator.EQ, account_npk_m_hash)
.select(CardNote::properties().owner, Comparator.EQ, account)
}
// docs:end:state_vars-NoteGetterOptionsMultiSelects

Expand Down
Loading

0 comments on commit 99cc1cf

Please sign in to comment.