Skip to content

Commit

Permalink
refactor: CompleteAddress cleanup (#6300)
Browse files Browse the repository at this point in the history
Fixes #5834
  • Loading branch information
benesjan authored May 10, 2024
1 parent 6374a32 commit 9c30759
Show file tree
Hide file tree
Showing 49 changed files with 395 additions and 500 deletions.
24 changes: 11 additions & 13 deletions noir-projects/aztec-nr/aztec/src/keys/getters.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use dep::protocol_types::{address::AztecAddress, constants::CANONICAL_KEY_REGISTRY_ADDRESS, grumpkin_point::GrumpkinPoint};
use dep::protocol_types::{
address::{AztecAddress, PublicKeysHash}, constants::CANONICAL_KEY_REGISTRY_ADDRESS,
grumpkin_point::GrumpkinPoint
};
use crate::{
context::PrivateContext, oracle::keys::get_public_keys_and_partial_address,
state_vars::{
Expand Down Expand Up @@ -80,20 +83,15 @@ fn fetch_key_from_registry(
fn fetch_and_constrain_keys(address: AztecAddress) -> [GrumpkinPoint; 4] {
let (public_keys, partial_address) = get_public_keys_and_partial_address(address);

let nullifier_pub_key = public_keys[0];
let incoming_pub_key = public_keys[1];
let outgoing_pub_key = public_keys[2];
let tagging_pub_key = public_keys[3];
let npk_m = public_keys[0];
let ivpk_m = public_keys[1];
let ovpk_m = public_keys[2];
let tpk_m = public_keys[3];

let computed_address = AztecAddress::compute_from_public_keys_and_partial_address(
nullifier_pub_key,
incoming_pub_key,
outgoing_pub_key,
tagging_pub_key,
partial_address
);
let public_keys_hash = PublicKeysHash::compute(npk_m, ivpk_m, ovpk_m, tpk_m);
let computed_address = AztecAddress::compute(public_keys_hash, partial_address);

assert(computed_address.eq(address));

[nullifier_pub_key, incoming_pub_key, outgoing_pub_key, tagging_pub_key]
[npk_m, ivpk_m, ovpk_m, tpk_m]
}
22 changes: 5 additions & 17 deletions noir-projects/aztec-nr/aztec/src/oracle/get_public_key.nr
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
use dep::protocol_types::{address::{AztecAddress, PartialAddress, PublicKeysHash}, grumpkin_point::GrumpkinPoint};

#[oracle(getPublicKeyAndPartialAddress)]
fn get_public_key_and_partial_address_oracle(_address: AztecAddress) -> [Field; 3] {}

unconstrained fn get_public_key_and_partial_address_internal(address: AztecAddress) -> [Field; 3] {
get_public_key_and_partial_address_oracle(address)
}
use dep::protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint};
use crate::oracle::keys::get_public_keys_and_partial_address;

// To be nuked in my next PR: https://github.com/AztecProtocol/aztec-packages/pull/6219
pub fn get_public_key(address: AztecAddress) -> GrumpkinPoint {
let result = get_public_key_and_partial_address_internal(address);
let pub_key = GrumpkinPoint::new(result[0], result[1]);
let partial_address = PartialAddress::from_field(result[2]);

// TODO(#5830): disabling the following constraint until we update the oracle according to the new key scheme
// let calculated_address = AztecAddress::compute(PublicKeysHash::compute(pub_key), partial_address);
// assert(calculated_address.eq(address));

pub_key
let result = get_public_keys_and_partial_address(address);
result.0[1]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ contract KeyRegistry {

use dep::aztec::{
state_vars::{SharedMutable, Map},
protocol_types::{grumpkin_point::GrumpkinPoint, address::{AztecAddress, PartialAddress}}
protocol_types::{grumpkin_point::GrumpkinPoint, address::{AztecAddress, PartialAddress, PublicKeysHash}}
};

global KEY_ROTATION_DELAY = 5;
Expand All @@ -27,11 +27,7 @@ contract KeyRegistry {
}

#[aztec(public)]
fn rotate_nullifier_public_key(
address: AztecAddress,
new_nullifier_public_key: GrumpkinPoint,
nonce: Field
) {
fn rotate_npk_m(address: AztecAddress, new_npk_m: GrumpkinPoint, nonce: Field) {
// TODO: (#6137)
if (!address.eq(context.msg_sender())) {
assert_current_call_valid_authwit_public(&mut context, address);
Expand All @@ -41,26 +37,21 @@ contract KeyRegistry {

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_nullifier_public_key.x);
npk_m_y_registry.schedule_value_change(new_nullifier_public_key.y);
npk_m_x_registry.schedule_value_change(new_npk_m.x);
npk_m_y_registry.schedule_value_change(new_npk_m.y);
}

#[aztec(public)]
fn register(
address: AztecAddress,
partial_address: PartialAddress,
nullifier_public_key: GrumpkinPoint,
incoming_public_key: GrumpkinPoint,
outgoing_public_key: GrumpkinPoint,
tagging_public_key: GrumpkinPoint
npk_m: GrumpkinPoint,
ivpk_m: GrumpkinPoint,
ovpk_m: GrumpkinPoint,
tpk_m: GrumpkinPoint
) {
let computed_address = AztecAddress::compute_from_public_keys_and_partial_address(
nullifier_public_key,
incoming_public_key,
outgoing_public_key,
tagging_public_key,
partial_address
);
let public_keys_hash = PublicKeysHash::compute(npk_m, ivpk_m, ovpk_m, tpk_m);
let computed_address = AztecAddress::compute(public_keys_hash, partial_address);

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

Expand All @@ -73,14 +64,14 @@ contract KeyRegistry {
// let tpk_m_x_registry = storage.tpk_m_x_registry.at(address);
// let tpk_m_y_registry = storage.tpk_m_y_registry.at(address);

npk_m_x_registry.schedule_value_change(nullifier_public_key.x);
npk_m_y_registry.schedule_value_change(nullifier_public_key.y);
ivpk_m_x_registry.schedule_value_change(incoming_public_key.x);
ivpk_m_y_registry.schedule_value_change(incoming_public_key.y);
npk_m_x_registry.schedule_value_change(npk_m.x);
npk_m_y_registry.schedule_value_change(npk_m.y);
ivpk_m_x_registry.schedule_value_change(ivpk_m.x);
ivpk_m_y_registry.schedule_value_change(ivpk_m.y);
// Commented out as we hit the max enqueued public calls limit when not done so
// ovpk_m_x_registry.schedule_value_change(outgoing_public_key.x);
// ovpk_m_y_registry.schedule_value_change(outgoing_public_key.y);
// tpk_m_x_registry.schedule_value_change(tagging_public_key.x);
// tpk_m_y_registry.schedule_value_change(tagging_public_key.y);
// ovpk_m_x_registry.schedule_value_change(ovpk_m.x);
// ovpk_m_y_registry.schedule_value_change(ovpk_m.y);
// tpk_m_x_registry.schedule_value_change(tpk_m.x);
// tpk_m_y_registry.schedule_value_change(tpk_m.y);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ contract SchnorrSingleKeyAccount {

use dep::authwit::{entrypoint::{app::AppPayload, fee::FeePayload}, account::AccountActions};

use crate::{util::recover_address, auth_oracle::get_auth_witness};
// use crate::{util::recover_address, auth_oracle::get_auth_witness};
use crate::auth_oracle::get_auth_witness;

global ACCOUNT_ACTIONS_STORAGE_SLOT = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ use dep::aztec::protocol_types::address::PublicKeysHash;
use dep::std::{schnorr::verify_signature_slice};
use crate::auth_oracle::AuthWitness;

pub fn recover_address(message_hash: Field, witness: AuthWitness) -> AztecAddress {
let message_bytes = message_hash.to_be_bytes(32);
let verification = verify_signature_slice(
witness.owner.x,
witness.owner.y,
witness.signature,
message_bytes
);
assert(verification == true);
// TODO(#5830): the following is currently broken because we are no longer able to compute public keys hash
// pub fn recover_address(message_hash: Field, witness: AuthWitness) -> AztecAddress {
// let message_bytes = message_hash.to_be_bytes(32);
// let verification = verify_signature_slice(
// witness.owner.x,
// witness.owner.y,
// witness.signature,
// message_bytes
// );
// assert(verification == true);

AztecAddress::compute(
PublicKeysHash::compute(witness.owner),
witness.partial_address
)
}
// AztecAddress::compute(
// PublicKeysHash::compute(witness.owner),
// witness.partial_address
// )
// }
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,6 @@ impl AztecAddress {
)
}

pub fn compute_from_public_keys_and_partial_address(
nullifier_public_key: GrumpkinPoint,
incoming_public_key: GrumpkinPoint,
outgoing_public_key: GrumpkinPoint,
tagging_public_key: GrumpkinPoint,
partial_address: PartialAddress
) -> AztecAddress {
let public_keys_hash = PublicKeysHash::compute_new(
nullifier_public_key,
incoming_public_key,
outgoing_public_key,
tagging_public_key
);

let computed_address = AztecAddress::compute(public_keys_hash, partial_address);

computed_address
}

pub fn is_zero(self) -> bool {
self.inner == 0
}
Expand All @@ -93,7 +74,7 @@ impl AztecAddress {
}

#[test]
fn compute_address_from_partial_and_pubkey() {
fn compute_address_from_partial_and_pub_keys_hash() {
let pub_keys_hash = PublicKeysHash::from_field(1);
let partial_address = PartialAddress::from_field(2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,18 @@ impl PublicKeysHash {
Self { inner: field }
}

// TODO(#5830): When we do this refactor, rename compute_new -> compute
pub fn compute(public_key: GrumpkinPoint) -> Self {
PublicKeysHash::from_field(
pedersen_hash(
[
public_key.x,
public_key.y
],
GENERATOR_INDEX__PARTIAL_ADDRESS
)
)
}

// TODO(#5830): When we do this refactor, rename compute_new -> compute
pub fn compute_new(
nullifier_public_key: GrumpkinPoint,
incoming_public_key: GrumpkinPoint,
outgoing_public_key: GrumpkinPoint,
tagging_public_key: GrumpkinPoint
) -> Self {
pub fn compute(npk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint, ovpk_m: GrumpkinPoint, tpk_m: GrumpkinPoint) -> Self {
PublicKeysHash::from_field(
poseidon2_hash(
[
nullifier_public_key.x,
nullifier_public_key.y,
incoming_public_key.x,
incoming_public_key.y,
outgoing_public_key.x,
outgoing_public_key.y,
tagging_public_key.x,
tagging_public_key.y,
npk_m.x,
npk_m.y,
ivpk_m.x,
ivpk_m.y,
ovpk_m.x,
ovpk_m.y,
tpk_m.x,
tpk_m.y,
GENERATOR_INDEX__PUBLIC_KEYS_HASH
]
)
Expand All @@ -84,11 +65,14 @@ impl PublicKeysHash {
}
}

// TODO(#5830): re-enable this test once the compute function is updated
// #[test]
// fn compute_public_keys_hash() {
// let point = GrumpkinPoint { x: 1, y: 2 };
// let actual = PublicKeysHash::compute(point);
// let expected_public_keys_hash = 0x22d83a089d7650514c2de24cd30185a414d943eaa19817c67bffe2c3183006a3;
// assert(actual.to_field() == expected_public_keys_hash);
// }
#[test]
fn compute_public_keys_hash() {
let npk_m = GrumpkinPoint { x: 1, y: 2 };
let ivpk_m = GrumpkinPoint { x: 3, y: 4 };
let ovpk_m = GrumpkinPoint { x: 5, y: 6 };
let tpk_m = GrumpkinPoint { x: 7, y: 8 };

let actual = PublicKeysHash::compute(npk_m, ivpk_m, ovpk_m, tpk_m);
let expected_public_keys_hash = 0x1936abe4f6a920d16a9f6917f10a679507687e2cd935dd1f1cdcb1e908c027f3;
assert(actual.to_field() == expected_public_keys_hash);
}
5 changes: 2 additions & 3 deletions yarn-project/accounts/src/defaults/account_contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type AccountContract, type AccountInterface, type AuthWitnessProvider } from '@aztec/aztec.js/account';
import { type CompleteAddress } from '@aztec/circuit-types';
import { type Fr } from '@aztec/circuits.js';
import { type ContractArtifact } from '@aztec/foundation/abi';
import { type NodeInfo } from '@aztec/types/interfaces';

Expand All @@ -20,7 +19,7 @@ export abstract class DefaultAccountContract implements AccountContract {
return this.artifact;
}

getInterface(address: CompleteAddress, publicKeysHash: Fr, nodeInfo: NodeInfo): AccountInterface {
return new DefaultAccountInterface(this.getAuthWitnessProvider(address), address, publicKeysHash, nodeInfo);
getInterface(address: CompleteAddress, nodeInfo: NodeInfo): AccountInterface {
return new DefaultAccountInterface(this.getAuthWitnessProvider(address), address, nodeInfo);
}
}
5 changes: 0 additions & 5 deletions yarn-project/accounts/src/defaults/account_interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class DefaultAccountInterface implements AccountInterface {
constructor(
private authWitnessProvider: AuthWitnessProvider,
private address: CompleteAddress,
private publicKeysHash: Fr,
nodeInfo: Pick<NodeInfo, 'chainId' | 'protocolVersion'>,
) {
this.entrypoint = new DefaultAccountEntrypoint(
Expand All @@ -38,10 +37,6 @@ export class DefaultAccountInterface implements AccountInterface {
return this.authWitnessProvider.createAuthWit(messageHash);
}

getPublicKeysHash(): Fr {
return this.publicKeysHash;
}

getCompleteAddress(): CompleteAddress {
return this.address;
}
Expand Down
4 changes: 3 additions & 1 deletion yarn-project/accounts/src/testing/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export async function getDeployedTestAccountsWallets(pxe: PXE): Promise<AccountW
INITIAL_TEST_SECRET_KEYS.filter(initialSecretKey => {
const initialEncryptionKey = sha512ToGrumpkinScalar([initialSecretKey, GeneratorIndex.IVSK_M]);
const publicKey = generatePublicKey(initialEncryptionKey);
return registeredAccounts.find(registered => registered.publicKey.equals(publicKey)) != undefined;
return (
registeredAccounts.find(registered => registered.masterIncomingViewingPublicKey.equals(publicKey)) != undefined
);
}).map(secretKey => {
const signingKey = sha512ToGrumpkinScalar([secretKey, GeneratorIndex.IVSK_M]);
// TODO(#5726): use actual salt here instead of hardcoding Fr.ZERO
Expand Down
4 changes: 1 addition & 3 deletions yarn-project/aztec.js/src/account/contract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { type CompleteAddress } from '@aztec/circuit-types';
import { type Fr } from '@aztec/circuits.js';
import { type ContractArtifact } from '@aztec/foundation/abi';
import { type NodeInfo } from '@aztec/types/interfaces';

Expand All @@ -26,11 +25,10 @@ export interface AccountContract {
* The account interface is responsible for assembling tx requests given requested function calls, and
* for creating signed auth witnesses given action identifiers (message hashes).
* @param address - Address where this account contract is deployed.
* @param publicKeysHash - Hash of the public keys used to authorize actions.
* @param nodeInfo - Info on the chain where it is deployed.
* @returns An account interface instance for creating tx requests and authorizing actions.
*/
getInterface(address: CompleteAddress, publicKeysHash: Fr, nodeInfo: NodeInfo): AccountInterface;
getInterface(address: CompleteAddress, nodeInfo: NodeInfo): AccountInterface;

/**
* Returns the auth witness provider for the given address.
Expand Down
3 changes: 0 additions & 3 deletions yarn-project/aztec.js/src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ export interface AccountInterface extends AuthWitnessProvider, EntrypointInterfa
/** Returns the complete address for this account. */
getCompleteAddress(): CompleteAddress;

/** Returns the public keys hash for this account. */
getPublicKeysHash(): Fr;

/** Returns the address for this account. */
getAddress(): AztecAddress;

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/account_manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class AccountManager {
public async getAccount(): Promise<AccountInterface> {
const nodeInfo = await this.pxe.getNodeInfo();
const completeAddress = this.getCompleteAddress();
return this.accountContract.getInterface(completeAddress, this.getPublicKeysHash(), nodeInfo);
return this.accountContract.getInterface(completeAddress, nodeInfo);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function waitForAccountSynch(
address: CompleteAddress,
{ interval, timeout }: WaitOpts = DefaultWaitOpts,
): Promise<void> {
const publicKey = address.publicKey.toString();
const publicKey = address.masterIncomingViewingPublicKey.toString();
await retryUntil(
async () => {
const status = await pxe.getSyncStatus();
Expand Down
4 changes: 0 additions & 4 deletions yarn-project/aztec.js/src/wallet/account_wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ export class AccountWallet extends BaseWallet {
super(pxe);
}

getPublicKeysHash(): Fr {
return this.account.getPublicKeysHash();
}

createTxExecutionRequest(exec: ExecutionRequestInit): Promise<TxExecutionRequest> {
return this.account.createTxExecutionRequest(exec);
}
Expand Down
Loading

0 comments on commit 9c30759

Please sign in to comment.