Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nullifying key to Token Note #6130

Merged
merged 142 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 140 commits
Commits
Show all changes
142 commits
Select commit Hold shift + click to select a range
3acc686
Initial
sklppy88 Apr 22, 2024
1f53bd5
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 Apr 22, 2024
177445f
fix
sklppy88 Apr 22, 2024
fdbb60a
good
sklppy88 Apr 22, 2024
2d3a95f
asdf
sklppy88 Apr 22, 2024
58f9650
working tests
sklppy88 Apr 23, 2024
42a9c35
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 Apr 23, 2024
f82d1c8
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 Apr 23, 2024
ea93350
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 Apr 24, 2024
781bbe3
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 Apr 25, 2024
3201fd4
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 Apr 26, 2024
1b13804
cleanup
sklppy88 Apr 26, 2024
211a060
yarn format
sklppy88 Apr 26, 2024
29bfb80
fix
sklppy88 Apr 26, 2024
7d1620d
change some stuff
sklppy88 Apr 26, 2024
f0ece69
fix
sklppy88 Apr 26, 2024
0461006
fix
sklppy88 Apr 26, 2024
e4f6d15
asdf
sklppy88 Apr 26, 2024
f9a844b
fix
sklppy88 Apr 26, 2024
0e0b63e
fix
sklppy88 Apr 26, 2024
7572be2
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 Apr 26, 2024
9051c8e
format
sklppy88 Apr 26, 2024
6e3509a
test
sklppy88 Apr 26, 2024
6fbd1d9
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 Apr 26, 2024
711afac
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 Apr 29, 2024
7f5dbfc
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 Apr 29, 2024
afdd9df
initial
sklppy88 Apr 29, 2024
48b0626
initial
sklppy88 Apr 29, 2024
0ca8578
Addressing comments
sklppy88 May 1, 2024
c852740
Apply suggestions from code review
sklppy88 May 1, 2024
3b9e7ba
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 May 1, 2024
b76ab1a
fix
sklppy88 May 1, 2024
7eb2366
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 May 1, 2024
e2a479a
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 May 1, 2024
ede83f4
Merge branch 'ek/feat/constrain-keys-are-fresh-lib' into ek/feat/add-…
sklppy88 May 1, 2024
59a4c18
format
sklppy88 May 1, 2024
dd9ae57
test
sklppy88 May 1, 2024
32598c9
remove
sklppy88 May 1, 2024
0f0df81
another
sklppy88 May 1, 2024
7534a16
Merge remote-tracking branch 'origin/ek/feat/add-nullifying-public-ke…
sklppy88 May 1, 2024
cf27fc9
Add support for both
sklppy88 May 1, 2024
194df19
Address comments
sklppy88 May 1, 2024
938761a
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 May 1, 2024
0baf281
Merge branch 'master' into ek/feat/constrain-keys-are-fresh-lib
sklppy88 May 1, 2024
a85f513
fix
sklppy88 May 1, 2024
209fe0c
Merge branch 'ek/feat/constrain-keys-are-fresh-lib' into ek/feat/add-…
sklppy88 May 1, 2024
94db9f5
Okay
sklppy88 May 1, 2024
0c7dc94
Merge branch 'ek/feat/constrain-keys-are-fresh-lib' into ek/feat/add-…
sklppy88 May 1, 2024
6f0d457
fix
sklppy88 May 2, 2024
1cae18c
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 2, 2024
2f19996
fix
sklppy88 May 2, 2024
fffee53
fmt
sklppy88 May 2, 2024
167603b
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 2, 2024
4416ccb
fix
sklppy88 May 2, 2024
5d1b800
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 2, 2024
c4a745f
Fix
sklppy88 May 2, 2024
3c89a06
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 2, 2024
d69e112
fix
sklppy88 May 2, 2024
b954ecc
format
sklppy88 May 2, 2024
577b69c
one
sklppy88 May 2, 2024
11b4848
fix
sklppy88 May 2, 2024
435c9fd
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 2, 2024
a74311b
Fix
sklppy88 May 2, 2024
1d9964a
comments
sklppy88 May 3, 2024
5fa644d
format
sklppy88 May 3, 2024
227378c
testkeystore
sklppy88 May 3, 2024
ccbb5c9
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 3, 2024
f5659f5
format
sklppy88 May 3, 2024
c8901ff
Adding comments
sklppy88 May 3, 2024
ec75986
comments
sklppy88 May 3, 2024
4bfaefa
why
sklppy88 May 3, 2024
4604a29
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 3, 2024
53a443b
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 4, 2024
d9704ab
fix
sklppy88 May 4, 2024
7fd8b1d
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 6, 2024
b9733a0
Fix
sklppy88 May 6, 2024
9a3829c
fix
sklppy88 May 6, 2024
40ce5c6
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 8, 2024
b432ad2
Fix naming
sklppy88 May 8, 2024
f734d3f
address comments
sklppy88 May 8, 2024
bd7db1e
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 8, 2024
04f058f
fix
sklppy88 May 8, 2024
76850bd
fix
sklppy88 May 8, 2024
4f86276
fix
sklppy88 May 8, 2024
e47580f
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 8, 2024
b85388d
fix formatting
sklppy88 May 8, 2024
b4d24e1
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 8, 2024
29f4225
nargo fmt
sklppy88 May 8, 2024
af0ffd7
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 8, 2024
05f8fef
Fix
sklppy88 May 8, 2024
f8f27a7
fix
sklppy88 May 8, 2024
ded1ee3
format
sklppy88 May 8, 2024
2f76913
migration notes
sklppy88 May 9, 2024
bfe3f87
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 9, 2024
1ce4d63
Update migration_notes.md
sklppy88 May 9, 2024
3e6ec6f
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 9, 2024
2bd798e
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 9, 2024
ba7aa67
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 9, 2024
906e105
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 9, 2024
113b1e9
fmt
sklppy88 May 9, 2024
9fae196
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 10, 2024
e9f2326
Adapting
sklppy88 May 10, 2024
97a6c5a
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 10, 2024
fe73adb
fix
sklppy88 May 10, 2024
eb53c44
fix
sklppy88 May 10, 2024
b6b6969
Fixes
sklppy88 May 10, 2024
8ecec72
fix
sklppy88 May 10, 2024
730c842
Changes
sklppy88 May 10, 2024
1deda94
asfd
sklppy88 May 10, 2024
851c53e
asdf
sklppy88 May 10, 2024
5cee1ce
Fixes
sklppy88 May 10, 2024
a2eed7c
fix
sklppy88 May 10, 2024
1d52a38
format
sklppy88 May 10, 2024
6798917
fix
sklppy88 May 10, 2024
0a523d4
fix
sklppy88 May 10, 2024
06c4cc2
fix
sklppy88 May 10, 2024
a765ec7
asdf
sklppy88 May 10, 2024
5c63480
fix
sklppy88 May 10, 2024
993e33d
fix
sklppy88 May 10, 2024
31ec469
Refactors
sklppy88 May 10, 2024
a1f3f9b
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 10, 2024
f418d3e
fix comments
sklppy88 May 10, 2024
4511907
asdf
sklppy88 May 10, 2024
e9a1007
fmt
sklppy88 May 10, 2024
2478e6e
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 10, 2024
5ca32e2
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 11, 2024
34b8aa1
bump timeouts
sklppy88 May 11, 2024
44f8bf4
test
sklppy88 May 11, 2024
5a41082
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 13, 2024
3287585
fixing
sklppy88 May 13, 2024
dc1476a
format
sklppy88 May 13, 2024
f014dde
Addressing comments
sklppy88 May 14, 2024
84e53c3
Addressing comments
sklppy88 May 14, 2024
fee5a59
Apply suggestions from Jan
sklppy88 May 14, 2024
9563d94
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 14, 2024
071e7c4
format
sklppy88 May 14, 2024
3420a2f
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 14, 2024
79c13a3
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 14, 2024
34eb010
bump timeout
sklppy88 May 14, 2024
d93ce6b
Merge branch 'master' into ek/feat/add-nullifying-public-key-to-token…
sklppy88 May 14, 2024
7890520
merging
benesjan May 15, 2024
880fb54
more concise wording
benesjan May 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/docs/misc/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ Aztec is in full-speed development. Literally every version breaks compatibility

## TBD

### [Aztec.nr] Keys: Token note now stores an owner master nullifying public key hash instead of an owner address.

i.e.

struct TokenNote {
amount: U128,
```diff
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As Lasse pointed out, docs should change in several places here (such as token tutorial where we add address or anywhere where we talk about notes! This can also be done in your other PR where you are changing all other notes

Copy link
Contributor Author

@sklppy88 sklppy88 May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense to handle docs in the final task in the arc, after all the notes are changed, so it's a bit easier to find / replace the entire block, also I wouldn't have to worry about caveats (some have been replaced and some haven't). Although I do not mind doing it in this PR. Wdyt ?

- owner: AztecAddress,
+ npk_m_hash: Field,
```
randomness: Field,
}

Computing the nullifier similarly changes to use this master nullifying public key hash.
LHerskind marked this conversation as resolved.
Show resolved Hide resolved

### [Aztec.nr] Debug logging

The function `debug_log_array_with_prefix` has been removed. Use `debug_log_format` with `{}` instead. The special sequence `{}` will be replaced with the whole array. You can also use `{0}`, `{1}`, ... as usual with `debug_log_format`.
Expand Down
38 changes: 31 additions & 7 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ use crate::{
messaging::process_l1_to_l2_message,
hash::{hash_args_array, ArgsHasher, compute_encrypted_log_hash, compute_unencrypted_log_hash},
oracle::{
arguments, returns, call_private_function::call_private_function_internal,
arguments, returns, call_private_function::call_private_function_internal, header::get_header_at,
logs::emit_encrypted_log, logs_traits::{LensForEncryptedLog, ToBytesForUnencryptedLog},
nullifier_key::{get_nullifier_keys, get_nullifier_keys_with_npk_m_hash, NullifierKeys},
enqueue_public_function_call::{
enqueue_public_function_call_internal, set_public_teardown_function_call_internal,
parse_public_call_stack_item_from_oracle
},
header::get_header_at, logs::emit_encrypted_log,
logs_traits::{LensForEncryptedLog, ToBytesForUnencryptedLog},
nullifier_key::{get_nullifier_keys, NullifierKeys}
}
}
};
use dep::protocol_types::{
Expand All @@ -29,8 +28,10 @@ use dep::protocol_types::{
MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL, MAX_ENCRYPTED_LOGS_PER_CALL,
MAX_UNENCRYPTED_LOGS_PER_CALL
},
grumpkin_point::GrumpkinPoint, header::Header, messaging::l2_to_l1_message::L2ToL1Message,
traits::{is_empty, Deserialize, Empty}
contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest},
grumpkin_private_key::GrumpkinPrivateKey, grumpkin_point::GrumpkinPoint, header::Header,
messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader,
traits::{is_empty, Deserialize, Empty}, hash::poseidon2_hash
};

// When finished, one can call .finish() to convert back to the abi
Expand Down Expand Up @@ -227,6 +228,29 @@ impl PrivateContext {
keys.app_nullifier_secret_key
}

// TODO(#5630) Replace request_app_nullifier_secret_key above with this once we no longer get app nullifier secret key with address
pub fn request_nsk_app_with_npk_m_hash(&mut self, npk_m_hash: Field) -> Field {
let keys = if self.nullifier_key.is_none() {
sklppy88 marked this conversation as resolved.
Show resolved Hide resolved
LHerskind marked this conversation as resolved.
Show resolved Hide resolved
let keys = get_nullifier_keys_with_npk_m_hash(npk_m_hash);
let request = NullifierKeyValidationRequest {
master_nullifier_public_key: keys.master_nullifier_public_key,
app_nullifier_secret_key: keys.app_nullifier_secret_key
};
self.nullifier_key_validation_requests.push(request);
self.nullifier_key = Option::some(keys);
keys
} else {
let keys = self.nullifier_key.unwrap_unchecked();
// If MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL is larger than 1, need to update the way the key pair is cached.
assert(MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL == 1);
keys
};

// We have to check if the key that was requested or cached corresponds to the one we request for
assert_eq(poseidon2_hash(keys.master_nullifier_public_key.serialize()), npk_m_hash);
keys.app_nullifier_secret_key
}

// docs:start:context_message_portal
pub fn message_portal(&mut self, recipient: EthAddress, content: Field) {
// docs:end:context_message_portal
Expand Down
17 changes: 15 additions & 2 deletions noir-projects/aztec-nr/aztec/src/keys/getters.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use dep::protocol_types::{address::AztecAddress, constants::CANONICAL_KEY_REGISTRY_ADDRESS, grumpkin_point::GrumpkinPoint};
use dep::protocol_types::{
address::AztecAddress, constants::CANONICAL_KEY_REGISTRY_ADDRESS, grumpkin_point::GrumpkinPoint,
hash::poseidon2_hash
};
use crate::{
context::PrivateContext, oracle::keys::get_public_keys_and_partial_address,
context::PrivateContext,
oracle::keys::{get_public_keys_and_partial_address, get_public_keys_and_partial_address_with_npk_m_hash},
keys::public_keys::{PublicKeys, NULLIFIER_INDEX, INCOMING_INDEX},
state_vars::{
map::derive_storage_slot_in_map,
Expand All @@ -14,6 +18,10 @@ pub fn get_npk_m(context: &mut PrivateContext, address: AztecAddress) -> Grumpki
get_master_key(context, address, NULLIFIER_INDEX)
}

pub fn get_npk_m_hash(context: &mut PrivateContext, address: AztecAddress) -> Field {
poseidon2_hash(get_master_key(context, address, NULLIFIER_INDEX).serialize())
}

pub fn get_ivpk_m(context: &mut PrivateContext, address: AztecAddress) -> GrumpkinPoint {
get_master_key(context, address, INCOMING_INDEX)
}
Expand Down Expand Up @@ -80,3 +88,8 @@ fn fetch_and_constrain_keys(address: AztecAddress) -> PublicKeys {

public_keys
}

pub fn get_ivpk_m_with_npk_m_hash(npk_m_hash: Field) -> GrumpkinPoint {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this functin should exist, as we whould never really need to make this lookup in the intended flow with the changes required on broadcasting etc. But I can follow it for not making this pr huge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, should have added a comment, this is removed downstream in a stack.

let result = get_public_keys_and_partial_address_with_npk_m_hash(npk_m_hash);
result.0.ivpk_m
}
22 changes: 22 additions & 0 deletions noir-projects/aztec-nr/aztec/src/oracle/keys.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,25 @@ fn get_public_keys_and_partial_address(address: AztecAddress) -> (PublicKeys, Pa

(keys, partial_address)
}

#[oracle(getPublicKeysAndPartialAddressWithNpkMHash)]
fn get_public_keys_and_partial_address_with_npk_m_hash_oracle(_npk_m_hash: Field) -> [Field; 9] {}

unconstrained fn get_public_keys_and_partial_address_with_npk_m_hash_oracle_wrapper(npk_m_hash: Field) -> [Field; 9] {
get_public_keys_and_partial_address_with_npk_m_hash_oracle(npk_m_hash)
}

fn get_public_keys_and_partial_address_with_npk_m_hash(npk_m_hash: Field) -> (PublicKeys, PartialAddress) {
let result = get_public_keys_and_partial_address_with_npk_m_hash_oracle_wrapper(npk_m_hash);

let keys = PublicKeys {
npk_m: GrumpkinPoint::new(result[0], result[1]),
ivpk_m: GrumpkinPoint::new(result[2], result[3]),
ovpk_m: GrumpkinPoint::new(result[4], result[5]),
tpk_m: GrumpkinPoint::new(result[6], result[7])
};

let partial_address = PartialAddress::from_field(result[8]);

(keys, partial_address)
}
24 changes: 24 additions & 0 deletions noir-projects/aztec-nr/aztec/src/oracle/nullifier_key.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use dep::protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint,

// Nullifier keys pertaining to a specific account
struct NullifierKeys {
// TODO(#5630): Replace get_nullifier_keys above with this once we no longer get nullifier keys with address
account: AztecAddress,
master_nullifier_public_key: GrumpkinPoint,
app_nullifier_secret_key: Field,
Expand All @@ -26,3 +27,26 @@ pub fn get_nullifier_keys(account: AztecAddress) -> NullifierKeys {
pub fn get_app_nullifier_secret_key(account: AztecAddress) -> Field {
get_nullifier_keys_internal(account).app_nullifier_secret_key
}

// TODO(#5630): Replace get_nullifier_keys above with this once we no longer get nullifier keys with address
#[oracle(getNullifierKeysWithNpkMHash)]
fn get_nullifier_keys_with_npk_m_hash_oracle(_npk_m_hash: Field) -> [Field; 3] {}

unconstrained fn get_nullifier_keys_with_npk_m_hash_internal(npk_m_hash: Field) -> NullifierKeys {
let result = get_nullifier_keys_with_npk_m_hash_oracle(npk_m_hash);
NullifierKeys {
account: AztecAddress::zero(),
LHerskind marked this conversation as resolved.
Show resolved Hide resolved
master_nullifier_public_key: GrumpkinPoint { x: result[0], y: result[1] },
app_nullifier_secret_key: result[2]
}
}

// We get the full struct Nullifier Keys here
pub fn get_nullifier_keys_with_npk_m_hash(npk_m_hash: Field) -> NullifierKeys {
get_nullifier_keys_with_npk_m_hash_internal(npk_m_hash)
}

// We are only getting the app_nullifier_secret_key here
pub fn get_nsk_app_with_npk_m_hash(npk_m_hash: Field) -> Field {
get_nullifier_keys_with_npk_m_hash_internal(npk_m_hash).app_nullifier_secret_key
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use dep::aztec::prelude::{
};
use dep::aztec::{
context::{PublicContext, Context}, hash::pedersen_hash,
protocol_types::constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
note::{note_getter::view_notes, note_getter_options::SortOrder}
protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, hash::poseidon2_hash},
note::{note_getter::view_notes, note_getter_options::SortOrder}, keys::getters::get_npk_m_hash
};
use crate::types::token_note::{TokenNote, OwnedNote};

Expand Down Expand Up @@ -60,7 +60,10 @@ impl<T> BalancesMap<T> {
owner: AztecAddress,
addend: U128
) where T: NoteInterface<T_SERIALIZED_LEN> + OwnedNote {
let mut addend_note = T::new(addend, owner);
// We fetch the nullifier public key hash in the registry / from our PXE
let owner_npk_m_hash = get_npk_m_hash(self.map.context.private.unwrap(), owner);

let mut addend_note = T::new(addend, owner_npk_m_hash);

// docs:start:insert
self.map.at(owner).insert(&mut addend_note, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
use dep::aztec::{
keys::getters::get_ivpk_m, prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext},
keys::getters::get_ivpk_m_with_npk_m_hash,
prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext},
protocol_types::constants::GENERATOR_INDEX__NOTE_NULLIFIER,
note::utils::compute_note_hash_for_consumption, hash::poseidon2_hash,
oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_app_nullifier_secret_key}
oracle::{unsafe_rand::unsafe_rand, nullifier_key::get_nsk_app_with_npk_m_hash}
};

trait OwnedNote {
fn new(amount: U128, owner: AztecAddress) -> Self;
fn new(amount: U128, owner_npk_m_hash: Field) -> Self;
fn get_amount(self) -> U128;
fn get_owner(self) -> AztecAddress;
fn get_owner_npk_m_hash(self) -> Field;
}

global TOKEN_NOTE_LEN: Field = 3; // 3 plus a header.

#[aztec(note)]
struct TokenNote {
// the amount of tokens in the note
// The amount of tokens in the note
amount: U128,
// the provider of secrets for the nullifier. The owner (recipient) to ensure that the note
// can be privately spent. When nullifier secret and encryption private key is same
// we can simply use the owner for this one.
owner: AztecAddress,
// randomness of the note to hide contents.
// The nullifying public key hash of the person who owns the note.
// This is used with the app_nullifier_secret_key to ensure that the note can be privately spent.
npk_m_hash: Field,
// Randomness of the note to hide its contents
randomness: Field,
}

impl NoteInterface<TOKEN_NOTE_LEN> for TokenNote {
// docs:start:nullifier
fn compute_nullifier(self, context: &mut PrivateContext) -> Field {
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = context.request_app_nullifier_secret_key(self.owner);
let secret = context.request_nsk_app_with_npk_m_hash(self.npk_m_hash);
poseidon2_hash([
note_hash_for_nullify,
secret,
Expand All @@ -40,7 +40,7 @@ impl NoteInterface<TOKEN_NOTE_LEN> for TokenNote {

fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = get_app_nullifier_secret_key(self.owner);
let secret = get_nsk_app_with_npk_m_hash(self.npk_m_hash);
poseidon2_hash([
note_hash_for_nullify,
secret,
Expand All @@ -51,8 +51,9 @@ impl NoteInterface<TOKEN_NOTE_LEN> for TokenNote {
// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field) {
// We only bother inserting the note if non-empty to save funds on gas.
// TODO: (#5901) This will be changed a lot, as it should use the updated encrypted log format
if !(self.amount == U128::from_integer(0)) {
let ivpk_m = get_ivpk_m(context, self.owner);
let ivpk_m = get_ivpk_m_with_npk_m_hash(self.npk_m_hash);
context.emit_encrypted_log(
(*context).this_address(),
slot,
Expand All @@ -65,10 +66,10 @@ impl NoteInterface<TOKEN_NOTE_LEN> for TokenNote {
}

impl OwnedNote for TokenNote {
fn new(amount: U128, owner: AztecAddress) -> Self {
fn new(amount: U128, owner_npk_m_hash: Field) -> Self {
Self {
amount,
owner,
npk_m_hash: owner_npk_m_hash,
randomness: unsafe_rand(),
header: NoteHeader::empty(),
}
Expand All @@ -78,7 +79,7 @@ impl OwnedNote for TokenNote {
self.amount
}

fn get_owner(self) -> AztecAddress {
self.owner
fn get_owner_npk_m_hash(self) -> Field {
self.npk_m_hash
}
}
12 changes: 6 additions & 6 deletions yarn-project/circuit-types/src/keys/key_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export interface KeyStore {
getAccounts(): Promise<AztecAddress[]>;

/**
* Gets the master nullifier public key for a given account.
* Gets the master nullifier public key for a given account or master nullifier public key hash.
* @throws If the account does not exist in the key store.
* @param account - The account address for which to retrieve the master nullifier public key.
* @param accountOrNpkMHash - account - the address or npkMHash - the master nullifier public key hash
* @returns The master nullifier public key for the account.
*/
getMasterNullifierPublicKey(account: AztecAddress): Promise<PublicKey>;
getMasterNullifierPublicKey(accountOrNpkMHash: AztecAddress | Fr): Promise<PublicKey>;

/**
* Gets the master incoming viewing public key for a given account.
Expand All @@ -64,13 +64,13 @@ export interface KeyStore {
getMasterTaggingPublicKey(account: AztecAddress): Promise<PublicKey>;

/**
* Retrieves application nullifier secret key.
* Derives and returns the application nullifier secret key for a given account or master nullifier public key hash.
* @throws If the account does not exist in the key store.
* @param account - The account to retrieve the application nullifier secret key for.
* @param accountOrNpkMHash - account - the address or npkMHash - the master nullifier public key hash
* @param app - The application address to retrieve the nullifier secret key for.
* @returns A Promise that resolves to the application nullifier secret key.
*/
getAppNullifierSecretKey(account: AztecAddress, app: AztecAddress): Promise<Fr>;
getAppNullifierSecretKey(accountOrNpkMHash: AztecAddress | Fr, app: AztecAddress): Promise<Fr>;

/**
* Retrieves application incoming viewing secret key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,5 @@ describe('benchmarks/proving', () => {

const receipts = await Promise.all(txs.map(tx => tx.wait({ timeout: txTimeoutSec })));
expect(receipts.every(r => r.status === TxStatus.MINED)).toBe(true);
});
}, 1_200_000);
});
1 change: 1 addition & 0 deletions yarn-project/end-to-end/src/e2e_2_pxes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ describe('e2e_2_pxes', () => {
await sharedAccountOnB.register();
const sharedWalletOnB = await sharedAccountOnB.getWallet();

// Register wallet B in the pxe of wallet A
await pxeA.registerRecipient(walletB.getCompleteAddress());

// deploy the contract on PXE A
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('guides/writing_an_account_contract', () => {
logger.info(`Deployed account contract at ${address}`);

// docs:start:account-contract-works
const token = await TokenContract.deploy(wallet, { address }, 'TokenName', 'TokenSymbol', 18).send().deployed();
const token = await TokenContract.deploy(wallet, address, 'TokenName', 'TokenSymbol', 18).send().deployed();
logger.info(`Deployed token contract at ${token.address}`);

const secret = Fr.random();
Expand All @@ -84,9 +84,9 @@ describe('guides/writing_an_account_contract', () => {
);
await pxe.addNote(extendedNote);

await token.methods.redeem_shield({ address }, mintAmount, secret).send().wait();
await token.methods.redeem_shield(address, mintAmount, secret).send().wait();

const balance = await token.methods.balance_of_private({ address }).simulate();
const balance = await token.methods.balance_of_private(address).simulate();
logger.info(`Balance of wallet is now ${balance}`);
// docs:end:account-contract-works
expect(balance).toEqual(50n);
Expand Down
Loading