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: completing MockNote #8059

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ library Constants {
uint256 internal constant L2_GAS_PER_NOTE_HASH = 32;
uint256 internal constant L2_GAS_PER_NULLIFIER = 64;
uint256 internal constant CANONICAL_KEY_REGISTRY_ADDRESS =
21209182303070804160941065409360795406831433542792830301721453026531461944353;
9694109890306420370616891858093188542026876097103155811681068343994212062621;
uint256 internal constant CANONICAL_AUTH_REGISTRY_ADDRESS =
16522644890256297179255458951626875692461008240031142745359776058397274208468;
uint256 internal constant DEPLOYER_CONTRACT_ADDRESS =
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ mod test {
// The following value was generated by `tagged_log.test.ts`
// --> Run the test with AZTEC_GENERATE_TEST_DATA=1 flag to update test data.
let encrypted_note_log_from_typescript = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141, 70, 12, 14, 67, 77, 132, 110, 193, 234, 40, 110, 64, 144, 235, 86, 55, 111, 242, 123, 221, 193, 170, 202, 225, 216, 86, 84, 159, 112, 31, 167, 126, 79, 51, 186, 47, 71, 253, 172, 99, 112, 241, 59, 197, 241, 107, 186, 232, 87, 187, 230, 171, 62, 228, 234, 42, 51, 145, 146, 238, 242, 42, 71, 206, 13, 244, 66, 111, 195, 20, 203, 98, 148, 204, 242, 145, 183, 156, 29, 141, 54, 44, 220, 194, 35, 229, 16, 32, 204, 211, 49, 142, 112, 82, 202, 116, 241, 254, 146, 42, 217, 20, 189, 70, 228, 182, 171, 205, 104, 27, 99, 171, 28, 91, 244, 21, 30, 130, 240, 5, 72, 174, 124, 97, 197, 157, 248, 193, 23, 193, 76, 46, 141, 144, 70, 211, 45, 67, 167, 218, 129, 140, 104, 190, 41, 110, 249, 209, 68, 106, 135, 164, 80, 235, 63, 101, 80, 32, 13, 38, 99, 145, 91, 11, 173, 151, 231, 247, 65, 153, 117, 229, 167, 64, 239, 182, 126, 235, 83, 4, 169, 8, 8, 160, 4, 235, 252, 21, 96, 84, 161, 69, 145, 145, 215, 254, 161, 117, 246, 198, 65, 89, 179, 194, 90, 19, 121, 12, 202, 114, 80, 195, 14, 60, 128, 105, 142, 100, 86, 90, 108, 157, 219, 22, 172, 20, 121, 195, 25, 159, 236, 2, 70, 75, 42, 37, 34, 2, 17, 149, 20, 176, 32, 18, 204, 56, 117, 121, 34, 15, 3, 88, 123, 64, 68, 74, 233, 63, 59, 131, 222, 194, 192, 167, 110, 217, 10, 128, 73, 129, 172, 205, 103, 212, 60, 151, 141, 10, 151, 222, 151, 180, 43, 91, 148, 201, 110, 165, 10, 238, 32, 134, 235, 99, 216, 200, 182, 31, 22, 156, 18, 209, 222, 172, 239, 193, 212, 86, 99, 62, 70, 182, 45, 175, 241, 91, 202, 179, 225, 99, 1, 150, 232, 2, 252, 20, 83, 49, 132, 162, 93, 116, 212, 87, 71, 211, 58, 159, 163, 40, 253, 31, 3, 192, 48, 14, 201, 80, 24, 135, 154, 207, 58, 140, 128, 29, 101, 207, 189, 182, 191, 71, 210, 64, 172, 131, 83, 46, 232, 19, 216, 183, 108, 234, 17, 104, 60, 113, 231, 145, 195, 157, 24
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141, 70, 12, 14, 67, 77, 132, 110, 193, 234, 40, 110, 64, 144, 235, 86, 55, 111, 242, 123, 221, 193, 170, 202, 225, 216, 86, 84, 159, 112, 31, 167, 126, 79, 51, 186, 47, 71, 253, 172, 99, 112, 241, 59, 197, 241, 107, 186, 232, 87, 187, 230, 171, 62, 228, 234, 42, 51, 145, 146, 238, 242, 42, 71, 206, 13, 244, 66, 111, 195, 20, 203, 98, 148, 204, 242, 145, 183, 156, 29, 141, 54, 44, 220, 194, 35, 229, 16, 32, 204, 211, 49, 142, 112, 82, 202, 116, 241, 254, 146, 42, 217, 20, 189, 70, 228, 182, 171, 205, 104, 27, 99, 171, 28, 91, 244, 21, 30, 130, 240, 5, 72, 174, 124, 97, 197, 157, 248, 193, 23, 193, 76, 46, 141, 144, 70, 211, 45, 67, 167, 218, 129, 140, 104, 190, 41, 110, 249, 209, 68, 106, 135, 164, 80, 235, 63, 101, 80, 32, 13, 38, 99, 145, 91, 11, 173, 151, 231, 247, 65, 153, 117, 229, 167, 64, 239, 182, 126, 235, 83, 4, 169, 8, 8, 160, 4, 235, 252, 21, 96, 84, 161, 69, 145, 145, 215, 254, 161, 117, 246, 198, 65, 89, 179, 194, 90, 19, 121, 12, 202, 114, 80, 195, 14, 60, 128, 105, 142, 100, 86, 90, 108, 157, 219, 22, 172, 20, 121, 195, 25, 159, 236, 2, 70, 75, 42, 37, 34, 2, 17, 149, 20, 176, 32, 18, 204, 56, 117, 121, 34, 15, 3, 88, 123, 64, 68, 74, 233, 63, 59, 131, 222, 194, 192, 167, 110, 217, 10, 128, 73, 129, 172, 205, 103, 212, 60, 151, 141, 10, 151, 222, 151, 180, 43, 91, 148, 201, 110, 165, 10, 238, 32, 134, 235, 99, 216, 200, 182, 31, 22, 156, 18, 209, 222, 172, 239, 193, 212, 86, 99, 62, 70, 182, 45, 175, 241, 91, 202, 179, 225, 236, 95, 71, 66, 151, 225, 203, 53, 216, 85, 102, 130, 6, 8, 25, 180, 86, 58, 140, 198, 105, 102, 177, 42, 94, 115, 247, 145, 147, 24, 231, 39, 73, 27, 10, 219, 130, 115, 188, 74, 114, 5, 177, 199, 83, 183, 106, 87, 204, 238, 231, 72, 45, 240, 39, 174, 25, 98, 53, 187, 156, 159, 244, 38
];
for i in 0..encrypted_note_log_from_typescript.len() {
assert_eq(log[i], encrypted_note_log_from_typescript[i]);
Expand Down
8 changes: 2 additions & 6 deletions noir-projects/aztec-nr/aztec/src/note/utils.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use crate::{
context::PrivateContext, generators::G_slot,
note::{note_header::NoteHeader, note_interface::NoteInterface}
};
use crate::{context::PrivateContext, note::{note_header::NoteHeader, note_interface::NoteInterface}};

use dep::protocol_types::{
hash::{
compute_unique_note_hash, compute_siloed_note_hash as compute_siloed_note_hash,
compute_siloed_nullifier as compute_siloed_nullifier_from_preimage
},
point::Point, utils::arr_copy_slice
utils::arr_copy_slice
};
use dep::std::{embedded_curve_ops::multi_scalar_mul, hash::from_field_unsafe};

pub fn compute_siloed_nullifier<Note, let N: u32, let M: u32>(
note_with_header: Note,
Expand Down
32 changes: 22 additions & 10 deletions noir-projects/aztec-nr/aztec/src/test/mocks/mock_note.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::{context::PrivateContext, note::{note_header::NoteHeader, note_interface::NoteInterface}};
use crate::{
context::PrivateContext, generators::Ga1 as G_val, hash::poseidon2_hash_with_separator,
note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_nullify}
};

use dep::protocol_types::{address::AztecAddress, point::Point, traits::Eq};
use dep::protocol_types::{address::AztecAddress, constants::GENERATOR_INDEX__NOTE_NULLIFIER, point::Point, traits::Eq};
use dep::std::{embedded_curve_ops::multi_scalar_mul, hash::from_field_unsafe};

global MOCK_NOTE_LENGTH = 1;
// MOCK_NOTE_LENGTH * 32 + 32(storage_slot as bytes) + 32(note_type_id as bytes)
Expand All @@ -23,9 +27,11 @@ impl NoteInterface<MOCK_NOTE_LENGTH, MOCK_NOTE_BYTES_LENGTH> for MockNote {
}
}

fn compute_note_hiding_point(_self: Self) -> Point {
// TODO(#7636): Properly implement these functions.
crate::generators::Ga1
fn compute_note_hiding_point(self: Self) -> Point {
assert(self.header.storage_slot != 0, "Storage slot must be set before computing note hiding point");
// We use the unsafe version because the multi_scalar_mul will constrain the scalars.
let value_scalar = from_field_unsafe(self.value);
multi_scalar_mul([G_val], [value_scalar])
}

fn get_header(self) -> NoteHeader {
Expand All @@ -37,15 +43,21 @@ impl NoteInterface<MOCK_NOTE_LENGTH, MOCK_NOTE_BYTES_LENGTH> for MockNote {
}

fn get_note_type_id() -> Field {
0
// randomly chose note type id
4135
}

fn compute_nullifier(_self: Self, _context: &mut PrivateContext, _note_hash_for_nullify: Field) -> Field {
0
fn compute_nullifier(self, context: &mut PrivateContext, note_hash_for_nullify: Field) -> Field {
// We don't use any kind of secret here since this is only a mock note and having it here would make tests
// more cumbersome
poseidon2_hash_with_separator([note_hash_for_nullify], GENERATOR_INDEX__NOTE_NULLIFIER as Field)
}

fn compute_nullifier_without_context(_self: Self) -> Field {
0
fn compute_nullifier_without_context(self) -> Field {
// We don't use any kind of secret here since this is only a mock note and having it here would make tests
// more cumbersome
let note_hash_for_nullify = compute_note_hash_for_nullify(self);
poseidon2_hash_with_separator([note_hash_for_nullify], GENERATOR_INDEX__NOTE_NULLIFIER as Field)
}

fn to_be_bytes(self, storage_slot: Field) -> [u8; MOCK_NOTE_BYTES_LENGTH] {
Expand Down
1 change: 0 additions & 1 deletion noir-projects/noir-contracts/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ members = [
"contracts/fee_juice_contract",
"contracts/import_test_contract",
"contracts/key_registry_contract",
"contracts/new_key_registry_contract",
"contracts/inclusion_proofs_contract",
"contracts/lending_contract",
"contracts/parent_contract",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,62 @@ contract KeyRegistry {
use dep::authwit::auth::assert_current_call_valid_authwit_public;

use dep::aztec::{
keys::PublicKeys, state_vars::{SharedMutable, Map},
keys::{PublicKeys, stored_keys::StoredKeys}, state_vars::{PublicMutable, Map},
protocol_types::{point::Point, address::{AztecAddress, PartialAddress}}
};

global KEY_ROTATION_DELAY = 5;

#[aztec(storage)]
struct Storage {
// The following stores a hash of individual master public keys
// If you change slots of vars below, you must update the slots in `SharedMutablePrivateGetter` in aztec-nr/keys.
// We store x and y coordinates in individual shared mutables as shared mutable currently supports only 1 field
npk_m_x_registry: Map<AztecAddress, SharedMutable<Field, KEY_ROTATION_DELAY>>,
npk_m_y_registry: Map<AztecAddress, SharedMutable<Field, KEY_ROTATION_DELAY>>,

ivpk_m_x_registry: Map<AztecAddress, SharedMutable<Field, KEY_ROTATION_DELAY>>,
ivpk_m_y_registry: Map<AztecAddress, SharedMutable<Field, KEY_ROTATION_DELAY>>,

ovpk_m_x_registry: Map<AztecAddress, SharedMutable<Field, KEY_ROTATION_DELAY>>,
ovpk_m_y_registry: Map<AztecAddress, SharedMutable<Field, KEY_ROTATION_DELAY>>,

tpk_m_x_registry: Map<AztecAddress, SharedMutable<Field, KEY_ROTATION_DELAY>>,
tpk_m_y_registry: Map<AztecAddress, SharedMutable<Field, KEY_ROTATION_DELAY>>,
}
struct Storage {
current_keys: Map<AztecAddress, PublicMutable<StoredKeys>>,
}

#[aztec(public)]
fn rotate_npk_m(address: AztecAddress, new_npk_m: Point, nonce: Field) {
// TODO: (#6137)
if (!address.eq(context.msg_sender())) {
assert_current_call_valid_authwit_public(&mut context, address);
} else {
assert(nonce == 0, "invalid nonce");
impl<Context> Storage<Context> {
// The init function is typically automatically generated by the macros - here we implement it manually in order
// to have control over which storage slot is assigned to the current_keys state variable.
fn init(context: Context) -> Self {
Storage {
// Ideally we'd do KEY_REGISTRY_STORAGE_SLOT instead of hardcoding the 1 here, but that is currently
// causing compilation errors.
// TODO(#7829): fix this
current_keys: Map::new(
context,
1,
|context, slot| { PublicMutable::new(context, slot) }
)
}
}

let npk_m_x_registry = storage.npk_m_x_registry.at(address);
let npk_m_y_registry = storage.npk_m_y_registry.at(address);
npk_m_x_registry.schedule_value_change(new_npk_m.x);
npk_m_y_registry.schedule_value_change(new_npk_m.y);
}

// We need to have two separate register functions because a single one would produce too many storage writes, since
// each SharedMutable.schedule_value_change call results in 5 writes (pre, post, block_of_change, delay and hash),
// totaling 40 writes, while the kernels only accept up to 32 writes.
// Once SharedMutable accepts multi-field values, we can have a single state variable hold all keys, and that way
// also have a single block of change, hash, and delay.
// TODO (#5491): make this be a single function with a single schedule call.
unconstrained fn get_current_keys(account: AztecAddress) -> pub PublicKeys {
// If #7524 were to be implemented, this function could be called by an oracle from an unconstrained function
// in order to produce the preimage of the stored hash, and hence prove the correctness of the keys.
storage.current_keys.at(account).read().public_keys
}

#[aztec(public)]
fn register_npk_and_ivpk(address: AztecAddress, partial_address: PartialAddress, keys: PublicKeys) {
fn register_initial_keys(account: AztecAddress, partial_address: PartialAddress, keys: PublicKeys) {
let computed_address = AztecAddress::compute(keys.hash(), partial_address);
assert(computed_address.eq(account), "Computed address does not match supplied address");

assert(computed_address.eq(address), "Computed address does not match supplied address");

let npk_m_x_registry = storage.npk_m_x_registry.at(address);
let npk_m_y_registry = storage.npk_m_y_registry.at(address);
let ivpk_m_x_registry = storage.ivpk_m_x_registry.at(address);
let ivpk_m_y_registry = storage.ivpk_m_y_registry.at(address);

npk_m_x_registry.schedule_value_change(keys.npk_m.x);
npk_m_y_registry.schedule_value_change(keys.npk_m.y);
ivpk_m_x_registry.schedule_value_change(keys.ivpk_m.x);
ivpk_m_y_registry.schedule_value_change(keys.ivpk_m.y);
storage.current_keys.at(account).write(StoredKeys::new(keys));
}

#[aztec(public)]
fn register_ovpk_and_tpk(address: AztecAddress, partial_address: PartialAddress, keys: PublicKeys) {
let computed_address = AztecAddress::compute(keys.hash(), partial_address);
fn rotate_npk_m(account: AztecAddress, new_npk_m: Point, nonce: Field) {
if (!account.eq(context.msg_sender())) {
assert_current_call_valid_authwit_public(&mut context, account);
} else {
assert(nonce == 0, "invalid nonce");
}

assert(computed_address.eq(address), "Computed address does not match supplied address");
let account_key_storage = storage.current_keys.at(account);

let ovpk_m_x_registry = storage.ovpk_m_x_registry.at(address);
let ovpk_m_y_registry = storage.ovpk_m_y_registry.at(address);
let tpk_m_x_registry = storage.tpk_m_x_registry.at(address);
let tpk_m_y_registry = storage.tpk_m_y_registry.at(address);
// We read all other current keys so that we can compute the new hash - we can't update just the npk. This means
// updating all keys at once costs the same as updating just one (unless setting public storage to its current
// value is cheaper than changing it, e.g. EIP-2200).
let mut current_keys = account_key_storage.read().public_keys;
current_keys.npk_m = new_npk_m;

ovpk_m_x_registry.schedule_value_change(keys.ovpk_m.x);
ovpk_m_y_registry.schedule_value_change(keys.ovpk_m.y);
tpk_m_x_registry.schedule_value_change(keys.tpk_m.x);
tpk_m_y_registry.schedule_value_change(keys.tpk_m.y);
account_key_storage.write(StoredKeys::new(current_keys));
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ global L2_GAS_PER_NOTE_HASH: u32 = 32;
global L2_GAS_PER_NULLIFIER: u32 = 64;

// CANONICAL CONTRACT ADDRESSES
global CANONICAL_KEY_REGISTRY_ADDRESS = AztecAddress::from_field(0x2ee3f8c67efa88f9e6fb44242f1e9dcc0f9a6752ded07af0d9fac3875a61d421);
global CANONICAL_KEY_REGISTRY_ADDRESS = AztecAddress::from_field(0x156eabf84e3ea50d40e3330224f2d2e81648fff8f1f7ec1bc6d2873cca6e959d);
global CANONICAL_AUTH_REGISTRY_ADDRESS = AztecAddress::from_field(0x24877c50868f86712240eb535d90d1c97403d074805dd3758c3aecb02958f8d4);
global DEPLOYER_CONTRACT_ADDRESS = AztecAddress::from_field(0x2ab1a2bd6d07d8d61ea56d85861446349e52c6b7c0612b702cb1e6db6ad0b089);
global REGISTERER_CONTRACT_ADDRESS = AztecAddress::from_field(0x05d15342d76e46e5be07d3cda0d753158431cdc5e39d29ce4e8fe1f5c070564a);
Expand Down
Loading
Loading