Skip to content

Commit

Permalink
refactor private call data
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Oct 8, 2024
1 parent 33799a7 commit 1e3d24f
Show file tree
Hide file tree
Showing 38 changed files with 175 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
context::PrivateContext, event::event_interface::EventInterface,
encrypted_logs::payload::compute_encrypted_log,
keys::getters::get_ovsk_app, oracle::random::random
encrypted_logs::payload::compute_encrypted_log, keys::getters::get_ovsk_app, oracle::random::random
};
use dep::protocol_types::{address::{AztecAddress, public_keys::{OvpkM, IvpkM}}, hash::sha256_to_field};

Expand Down
4 changes: 1 addition & 3 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ pub fn compute_encrypted_log<let P: u32, let M: u32>(
let header = EncryptedLogHeader::new(contract_address);

let address_point = recipient.to_point();
let address_ivpk = IvpkM {
inner: address_point,
};
let address_ivpk = IvpkM { inner: address_point };

let incoming_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, address_ivpk);
let outgoing_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, ovpk);
Expand Down
7 changes: 4 additions & 3 deletions noir-projects/aztec-nr/aztec/src/keys/getters/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ pub fn get_public_keys(account: AztecAddress) -> PublicKeys {
let (hinted_canonical_public_keys, partial_address) = unsafe {
get_public_keys_and_partial_address(account)
};
// assert_eq(
// account, AztecAddress::compute(hinted_canonical_public_keys.hash(), partial_address), "Invalid public keys hint for address"
// );

assert_eq(
account, AztecAddress::compute_from_public_keys(hinted_canonical_public_keys, partial_address), "Invalid public keys hint for address"
);

hinted_canonical_public_keys
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn get_contract_instance(address: AztecAddress) -> ContractInstance {
};
// The to_address function combines all values in the instance object to produce an address, so by checking that we
// get the expected address we validate the entire struct.
// assert_eq(instance.to_address(), address);
assert_eq(instance.to_address(), address);

instance
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ impl PrivateCallDataValidator {
);
// println(f"computed_partial_address={computed_partial_address}");

let computed_address = AztecAddress::compute(self.data.public_keys_hash, computed_partial_address);
let computed_address = AztecAddress::compute_from_public_keys(self.data.public_keys, computed_partial_address);
// println(f"computed_address={computed_address}");

// assert(
// !computed_address.eq(contract_address), "computed contract address does not match expected one"
// );
assert(
computed_address.eq(contract_address), "computed contract address does not match expected one"
);
}

fn validate_call(self) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn validate_contract_address_incorrect_partial_address_preimage_fails() {
fn validate_contract_address_incorrect_address_preimage_fails() {
let mut builder = PrivateCallDataValidatorBuilder::new();

builder.private_call.public_keys_hash.inner = builder.private_call.public_keys_hash.inner + 1;
builder.private_call.public_keys.ivpk_m.inner.x = builder.private_call.public_keys.ivpk_m.inner.x + 1;

builder.validate();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
abis::{private_call_stack_item::PrivateCallStackItem},
address::{SaltedInitializationHash, PublicKeysHash}, constants::FUNCTION_TREE_HEIGHT,
address::{SaltedInitializationHash, PublicKeysHash, PublicKeys}, constants::FUNCTION_TREE_HEIGHT,
merkle_tree::membership::MembershipWitness, recursion::{verification_key::VerificationKey}
};

Expand All @@ -10,7 +10,7 @@ pub struct PrivateCallData {
vk: VerificationKey,

salted_initialization_hash: SaltedInitializationHash,
public_keys_hash: PublicKeysHash,
public_keys: PublicKeys,
contract_class_artifact_hash: Field,
contract_class_public_bytecode_commitment: Field,
function_leaf_membership_witness: MembershipWitness<FUNCTION_TREE_HEIGHT>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
address::{partial_address::PartialAddress, public_keys_hash::PublicKeysHash},
address::{partial_address::PartialAddress, public_keys::PublicKeys, public_keys_hash::PublicKeysHash},
constants::{AZTEC_ADDRESS_LENGTH, GENERATOR_INDEX__CONTRACT_ADDRESS_V1},
hash::poseidon2_hash_with_separator, traits::{Empty, FromField, ToField, Serialize, Deserialize},
utils
Expand All @@ -10,8 +10,11 @@ global BN254_FR_MODULUS_DIV_2: Field = 10944121435919637611123202872628637544274
// We do below because `use crate::point::Point;` does not work
use dep::std::embedded_curve_ops::EmbeddedCurvePoint as Point;

use std::ec::{sqrt, pow};
use crate::debug_log::debug_log_format;
use std::{
ec::{sqrt, pow},
embedded_curve_ops::{fixed_base_scalar_mul as derive_public_key, EmbeddedCurveScalar}
};
use crate::constants::GENERATOR_INDEX__PUBLIC_KEYS_HASH;

// Aztec address
pub struct AztecAddress {
Expand Down Expand Up @@ -68,6 +71,32 @@ impl AztecAddress {
)
}

pub fn compute_from_public_keys(public_keys: PublicKeys, partial_address: PartialAddress) -> AztecAddress {
let public_keys_hash = public_keys.hash();

let pre_address = poseidon2_hash_with_separator(
[public_keys_hash.to_field(), partial_address.to_field()],
GENERATOR_INDEX__CONTRACT_ADDRESS_V1
);

let address_point = derive_public_key(EmbeddedCurveScalar::from_field(pre_address)).add(public_keys.ivpk_m.to_point());
AztecAddress::from_field(address_point.x)
}

pub fn compute_new(
public_keys_hash: PublicKeysHash,
partial_address: PartialAddress,
ivpk_m: Point
) -> AztecAddress {
let pre_address = poseidon2_hash_with_separator(
[public_keys_hash.to_field(), partial_address.to_field()],
GENERATOR_INDEX__CONTRACT_ADDRESS_V1
);

let address_point = derive_public_key(EmbeddedCurveScalar::from_field(pre_address)).add(ivpk_m);
AztecAddress::from_field(address_point.x)
}

pub fn is_zero(self) -> bool {
self.inner == 0
}
Expand Down Expand Up @@ -100,11 +129,7 @@ impl AztecAddress {
y = (BN254_FR_MODULUS_DIV_2 + BN254_FR_MODULUS_DIV_2 + 1) - y;
}

Point {
x: self.inner,
y,
is_infinite: false
}
Point { x: self.inner, y, is_infinite: false }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ global GAS_LENGTH: u32 = 2;
global GAS_SETTINGS_LENGTH: u32 = GAS_LENGTH * 2 + GAS_FEES_LENGTH + /* inclusion_fee */ 1;
global CALL_CONTEXT_LENGTH: u32 = 5;
global CONTENT_COMMITMENT_LENGTH: u32 = 4;
global CONTRACT_INSTANCE_LENGTH: u32 = 5;
global CONTRACT_INSTANCE_LENGTH: u32 = 8;
global CONTRACT_STORAGE_READ_LENGTH: u32 = 3;
global CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH: u32 = 3;
global ETH_ADDRESS_LENGTH: u32 = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ use crate::{
traits::{Deserialize, Hash, Serialize}
};

use dep::std::embedded_curve_ops::EmbeddedCurvePoint as Point;

pub struct ContractInstance {
salt : Field,
deployer: AztecAddress,
contract_class_id : ContractClassId,
initialization_hash : Field,
public_keys_hash : PublicKeysHash,
// Refactor this to use the correct type
ivpk_m: Point,
}

impl Eq for ContractInstance {
Expand All @@ -18,6 +22,7 @@ impl Eq for ContractInstance {
& self.initialization_hash.eq(other.initialization_hash)
& self.contract_class_id.eq(other.contract_class_id)
& self.salt.eq(other.salt)
& self.ivpk_m.eq(other.ivpk_m)
}
}

Expand All @@ -28,7 +33,10 @@ impl Serialize<CONTRACT_INSTANCE_LENGTH> for ContractInstance {
self.deployer.to_field(),
self.contract_class_id.to_field(),
self.initialization_hash,
self.public_keys_hash.to_field()
self.public_keys_hash.to_field(),
self.ivpk_m.x,
self.ivpk_m.y,
self.ivpk_m.is_infinite as Field
]
}
}
Expand All @@ -40,7 +48,8 @@ impl Deserialize<CONTRACT_INSTANCE_LENGTH> for ContractInstance {
deployer: AztecAddress::from_field(serialized[1]),
contract_class_id: ContractClassId::from_field(serialized[2]),
initialization_hash: serialized[3],
public_keys_hash: PublicKeysHash::from_field(serialized[4])
public_keys_hash: PublicKeysHash::from_field(serialized[4]),
ivpk_m: Point { x: serialized[5], y: serialized[6], is_infinite: serialized[7] as bool }
}
}
}
Expand All @@ -53,14 +62,15 @@ impl Hash for ContractInstance {

impl ContractInstance {
fn to_address(self) -> AztecAddress {
AztecAddress::compute(
AztecAddress::compute_new(
self.public_keys_hash,
PartialAddress::compute(
self.contract_class_id,
self.salt,
self.initialization_hash,
self.deployer
)
),
self.ivpk_m
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
ScopedKeyValidationRequestAndGenerator
}
},
address::{AztecAddress, EthAddress, SaltedInitializationHash, PublicKeysHash},
address::{AztecAddress, EthAddress, SaltedInitializationHash, PublicKeysHash, PublicKeys},
constants::{
FUNCTION_TREE_HEIGHT, MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX,
MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_TX, MAX_L2_TO_L1_MSGS_PER_TX, MAX_PUBLIC_DATA_READS_PER_CALL,
Expand Down Expand Up @@ -134,7 +134,7 @@ pub struct FixtureBuilder {

// Private call.
salted_initialization_hash: SaltedInitializationHash,
public_keys_hash: PublicKeysHash,
public_keys: PublicKeys,
contract_class_artifact_hash: Field,
contract_class_public_bytecode_commitment: Field,
function_leaf_membership_witness: MembershipWitness<FUNCTION_TREE_HEIGHT>,
Expand Down Expand Up @@ -182,7 +182,7 @@ impl FixtureBuilder {
builder.function_data = contract_function.data;
builder.function_leaf_membership_witness = contract_function.membership_witness;
builder.salted_initialization_hash = contract_data.salted_initialization_hash;
builder.public_keys_hash = contract_data.public_keys_hash;
builder.public_keys = contract_data.public_keys;
builder.contract_class_artifact_hash = contract_data.artifact_hash;
builder.contract_class_public_bytecode_commitment = contract_data.public_bytecode_commitment;
builder.acir_hash = contract_function.acir_hash;
Expand Down Expand Up @@ -319,7 +319,7 @@ impl FixtureBuilder {
vk: self.vk,
function_leaf_membership_witness: self.function_leaf_membership_witness,
salted_initialization_hash: self.salted_initialization_hash,
public_keys_hash: self.public_keys_hash,
public_keys: self.public_keys,
contract_class_artifact_hash: self.contract_class_artifact_hash,
contract_class_public_bytecode_commitment: self.contract_class_public_bytecode_commitment,
acir_hash: self.acir_hash
Expand Down Expand Up @@ -1220,7 +1220,7 @@ impl Empty for FixtureBuilder {
returns_hash: 0,
function_leaf_membership_witness: MembershipWitness::empty(),
salted_initialization_hash: SaltedInitializationHash::from_field(0),
public_keys_hash: PublicKeysHash::from_field(0),
public_keys: PublicKeys::empty(),
contract_class_artifact_hash: 0,
contract_class_public_bytecode_commitment: 0,
acir_hash: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
address::{AztecAddress, PublicKeysHash, SaltedInitializationHash, PartialAddress},
address::{AztecAddress, PublicKeysHash, SaltedInitializationHash, PartialAddress, PublicKeys},
contract_class_id::ContractClassId
};

Expand All @@ -10,7 +10,7 @@ pub struct ContractData {
contract_class_id: ContractClassId,
private_functions_root: Field,
public_bytecode_commitment: Field,
public_keys_hash: PublicKeysHash,
public_keys: PublicKeys,
salted_initialization_hash: SaltedInitializationHash,
partial_address: PartialAddress,
deployer: AztecAddress,
Expand All @@ -25,7 +25,7 @@ global default_contract = ContractData {
address: AztecAddress { inner: 0x0e66d7cd9692428c550b93c9ef5f49ca9f02c03e98cb3c922d8c773f78f79fed },
partial_address: PartialAddress { inner: 0x0cf203c94c91bed28440b00ecd888d88cce1f86ddf2aa8d33acbb9b6fc06d382 },
contract_class_id: ContractClassId { inner: 0x28e91aaf764bc6083e2796ff884079ad895d4b948d6ce8f37f01b29d0bc95a21 },
public_keys_hash: PublicKeysHash { inner: 0x000000000000000000000000000000000000000000000000000000000000b26e },
public_keys: PublicKeys::empty(),
salted_initialization_hash: SaltedInitializationHash { inner: 0x13a939daa511233e5446905ed2cadbee14948fa75df183b53b5c14b612bffe88 },
deployer: AztecAddress { inner: 0x0000000000000000000000000000000000000000000000000000000000000000 }
};
Expand All @@ -39,7 +39,7 @@ global parent_contract = ContractData {
address: AztecAddress { inner: 0x24415b2e716d6c7099580ab8e383fd5b16dc9fb441aa308571d8e24a2257da24 },
partial_address: PartialAddress { inner: 0x245df9f519d616473880260dd64b19a838081bb44dc17cd6ea5d870a63d2bf57 },
contract_class_id: ContractClassId { inner: 0x00236b0dc6c537d5106543053c5b85c4cbe95b0474f8238b094bae63f1cbcfee },
public_keys_hash: PublicKeysHash { inner: 0x00000000000000000000000000000000000000000000000000000000000011c1 },
public_keys: PublicKeys::empty(),
salted_initialization_hash: SaltedInitializationHash { inner: 0x24bd6ac7a182e2cf25e437c72f53544ef81dfd97d9afee23abb07a638e7be749 },
deployer: AztecAddress { inner: 0x0000000000000000000000000000000000000000000000000000000000000000 }
};
12 changes: 1 addition & 11 deletions yarn-project/aztec.js/src/account_manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export class AccountManager {
// TODO(@spalladino): Does it make sense to have both completeAddress and instance?
private completeAddress: CompleteAddress;
private instance: ContractInstanceWithAddress;
private publicKeysHash?: Fr;

constructor(private pxe: PXE, private secretKey: Fr, private accountContract: AccountContract, salt?: Salt) {
this.salt = salt !== undefined ? new Fr(salt) : Fr.random();
Expand All @@ -47,15 +46,10 @@ export class AccountManager {
});

this.completeAddress = CompleteAddress.fromSecretKeyAndInstance(this.secretKey, this.instance);

this.instance.address = this.completeAddress.address;
}

protected getPublicKeysHash() {
if (!this.publicKeysHash) {
this.publicKeysHash = deriveKeys(this.secretKey).publicKeys.hash();
}
return this.publicKeysHash;
return deriveKeys(this.secretKey).publicKeys.hash();
}

/**
Expand All @@ -74,10 +68,6 @@ export class AccountManager {
* @returns The address, partial address, and encryption public key.
*/
public getCompleteAddress(): CompleteAddress {
if (!this.completeAddress) {
const instance = this.getInstance();
this.completeAddress = CompleteAddress.fromSecretKeyAndInstance(this.secretKey, instance);
}
return this.completeAddress;
}

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/contract/deploy_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export class DeployMethod<TContract extends ContractBase = Contract> extends Bas
this.instance = getContractInstanceFromDeployParams(this.artifact, {
constructorArgs: this.args,
salt: options.contractAddressSalt,
publicKeysHash: this.publicKeysHash,
publicKeys: this.completeAddress?.publicKeys,
constructorArtifact: this.constructorArtifact,
deployer: options.universalDeploy ? AztecAddress.ZERO : this.wallet.getAddress(),
});
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/aztec/src/cli/cmds/start_pxe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { L2BasicContractsMap, Network } from '@aztec/types/network';

import { extractRelevantOptions } from '../util.js';
import { Point, PublicKeys } from '@aztec/circuits.js';

const contractAddressesUrl = 'http://static.aztec.network';

Expand Down Expand Up @@ -105,6 +106,7 @@ export async function addPXE(
deployer: AztecAddress.ZERO,
contractClassId: getContractClassFromArtifact(artifact!).id,
publicKeysHash: Fr.ZERO,
ivpkM: Point.ZERO,
};
userLog(`Registering ${name} at ${address.toString()}`);
await pxe.registerContract({ artifact, instance });
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/bb-prover/src/avm_proving.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AvmCircuitInputs, AvmVerificationKeyData, FunctionSelector, Gas, GlobalVariables } from '@aztec/circuits.js';
import { Fr } from '@aztec/foundation/fields';
import { Fr, Point } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
import { AvmSimulator, PublicSideEffectTrace, type WorldStateDB } from '@aztec/simulator';
import {
Expand Down Expand Up @@ -65,6 +65,7 @@ const proveAndVerifyAvmTestContract = async (
contractClassId: new Fr(0x789),
initializationHash: new Fr(0x101112),
publicKeysHash: new Fr(0x161718),
ivpkM: new Point(new Fr(123), new Fr(456), false),
}).withAddress(environment.address);
worldStateDB.getContractInstance.mockResolvedValue(Promise.resolve(contractInstance));

Expand Down
Loading

0 comments on commit 1e3d24f

Please sign in to comment.