Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Oct 17, 2024
1 parent 6fef111 commit e009360
Show file tree
Hide file tree
Showing 47 changed files with 259 additions and 129 deletions.
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 @@ -152,7 +152,7 @@ library Constants {
uint256 internal constant GAS_SETTINGS_LENGTH = 7;
uint256 internal constant CALL_CONTEXT_LENGTH = 5;
uint256 internal constant CONTENT_COMMITMENT_LENGTH = 4;
uint256 internal constant CONTRACT_INSTANCE_LENGTH = 5;
uint256 internal constant CONTRACT_INSTANCE_LENGTH = 16;
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 3;
uint256 internal constant CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
uint256 internal constant ETH_ADDRESS_LENGTH = 1;
Expand Down
22 changes: 19 additions & 3 deletions noir-projects/aztec-nr/aztec/src/deploy.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,29 @@ pub fn deploy_contract(context: &mut PrivateContext, target: AztecAddress) {
serialized_args[0] = instance.salt;
serialized_args[1] = instance.contract_class_id.to_field();
serialized_args[2] = instance.initialization_hash;
serialized_args[3] = instance.public_keys_hash.to_field();
serialized_args[4] = universal_deploy as Field;

let serialized_public_keys = instance.public_keys.serialize();
serialized_args[3] = serialized_public_keys[0];
serialized_args[4] = serialized_public_keys[1];
serialized_args[5] = serialized_public_keys[2];
serialized_args[6] = serialized_public_keys[3];
serialized_args[7] = serialized_public_keys[4];
serialized_args[8] = serialized_public_keys[5];
serialized_args[9] = serialized_public_keys[6];
serialized_args[10] = serialized_public_keys[7];
serialized_args[11] = serialized_public_keys[8];
serialized_args[12] = serialized_public_keys[9];
serialized_args[13] = serialized_public_keys[10];
serialized_args[14] = serialized_public_keys[11];

serialized_args[15] = universal_deploy as Field;

let _call_result = context.call_private_function(
DEPLOYER_CONTRACT_ADDRESS,
comptime {
FunctionSelector::from_signature("deploy(Field,(Field),Field,(Field),bool)")
FunctionSelector::from_signature(
"deploy(Field,(Field),Field,(Field,Field,Field,Field,Field,Field,Field,Field,Field,Field,Field,Field),bool)"
)
},
serialized_args
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use dep::aztec::macros::aztec;
#[aztec]
contract ContractInstanceDeployer {
use dep::aztec::protocol_types::{
address::{AztecAddress, PublicKeysHash, PartialAddress}, contract_class_id::ContractClassId,
constants::DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE, abis::log_hash::LogHash,
traits::Serialize
address::{AztecAddress, PublicKeysHash, PublicKeys, PartialAddress},
contract_class_id::ContractClassId, constants::DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE,
abis::log_hash::LogHash, traits::Serialize
};
use dep::aztec::{
hash::compute_unencrypted_log_hash, oracle::logs::emit_unencrypted_log_private,
Expand All @@ -22,7 +22,7 @@ contract ContractInstanceDeployer {
salt: Field,
contract_class_id: ContractClassId,
initialization_hash: Field,
public_keys_hash: PublicKeysHash,
public_keys: PublicKeys,
deployer: AztecAddress,
}

Expand All @@ -32,7 +32,7 @@ contract ContractInstanceDeployer {
salt: Field,
contract_class_id: ContractClassId,
initialization_hash: Field,
public_keys_hash: PublicKeysHash,
public_keys: PublicKeys,
universal_deploy: bool
) {
// TODO(@spalladino): assert nullifier_exists silo(contract_class_id, ContractClassRegisterer)
Expand All @@ -45,7 +45,7 @@ contract ContractInstanceDeployer {

let partial_address = PartialAddress::compute(contract_class_id, salt, initialization_hash, deployer);

let address = AztecAddress::compute(public_keys_hash, partial_address);
let address = AztecAddress::compute(public_keys.hash(), partial_address);

// Emit the address as a nullifier to be able to prove that this instance has been (not) deployed
context.push_nullifier(address.to_field());
Expand All @@ -55,7 +55,7 @@ contract ContractInstanceDeployer {
DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE,
contract_class_id,
address,
public_keys_hash,
public_keys,
initialization_hash,
salt,
deployer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,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 = 16;
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
@@ -1,5 +1,8 @@
use crate::{
address::{aztec_address::AztecAddress, partial_address::PartialAddress, public_keys_hash::PublicKeysHash},
address::{
aztec_address::AztecAddress, partial_address::PartialAddress, public_keys_hash::PublicKeysHash,
public_keys::PublicKeys
},
contract_class_id::ContractClassId, constants::CONTRACT_INSTANCE_LENGTH,
traits::{Deserialize, Hash, Serialize}
};
Expand All @@ -9,12 +12,12 @@ pub struct ContractInstance {
deployer: AztecAddress,
contract_class_id : ContractClassId,
initialization_hash : Field,
public_keys_hash : PublicKeysHash,
public_keys : PublicKeys,
}

impl Eq for ContractInstance {
fn eq(self, other: Self) -> bool {
self.public_keys_hash.eq(other.public_keys_hash)
self.public_keys.eq(other.public_keys)
& self.initialization_hash.eq(other.initialization_hash)
& self.contract_class_id.eq(other.contract_class_id)
& self.salt.eq(other.salt)
Expand All @@ -23,12 +26,24 @@ impl Eq for ContractInstance {

impl Serialize<CONTRACT_INSTANCE_LENGTH> for ContractInstance {
fn serialize(self) -> [Field; CONTRACT_INSTANCE_LENGTH] {
let public_keys_serialized = self.public_keys.serialize();
[
self.salt,
self.deployer.to_field(),
self.contract_class_id.to_field(),
self.initialization_hash,
self.public_keys_hash.to_field()
public_keys_serialized[0],
public_keys_serialized[1],
public_keys_serialized[2],
public_keys_serialized[3],
public_keys_serialized[4],
public_keys_serialized[5],
public_keys_serialized[6],
public_keys_serialized[7],
public_keys_serialized[8],
public_keys_serialized[9],
public_keys_serialized[10],
public_keys_serialized[11]
]
}
}
Expand All @@ -40,7 +55,22 @@ 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: PublicKeys::deserialize(
[
serialized[4],
serialized[5],
serialized[6],
serialized[7],
serialized[8],
serialized[9],
serialized[10],
serialized[11],
serialized[12],
serialized[13],
serialized[14],
serialized[15]
]
)
}
}
}
Expand All @@ -54,7 +84,7 @@ impl Hash for ContractInstance {
impl ContractInstance {
fn to_address(self) -> AztecAddress {
AztecAddress::compute(
self.public_keys_hash,
self.public_keys.hash(),
PartialAddress::compute(
self.contract_class_id,
self.salt,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Fr } from '@aztec/circuits.js';
import { type PublicKeys } from '@aztec/circuits.js';
import {
type ContractArtifact,
type FunctionArtifact,
Expand All @@ -23,15 +23,15 @@ export class DeployAccountMethod extends DeployMethod {

constructor(
authWitnessProvider: AuthWitnessProvider,
publicKeysHash: Fr,
publicKeys: PublicKeys,
wallet: Wallet,
artifact: ContractArtifact,
args: any[] = [],
constructorNameOrArtifact?: string | FunctionArtifact,
feePaymentNameOrArtifact?: string | FunctionArtifact,
) {
super(
publicKeysHash,
publicKeys,
wallet,
artifact,
(address, wallet) => Contract.at(address, artifact, wallet),
Expand Down
26 changes: 19 additions & 7 deletions yarn-project/aztec.js/src/account_manager/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { CompleteAddress, type PXE } from '@aztec/circuit-types';
import { type ContractInstanceWithAddress, deriveKeys, getContractInstanceFromDeployParams } from '@aztec/circuits.js';
import {
type ContractInstanceWithAddress,
type PublicKeys,
deriveKeys,
getContractInstanceFromDeployParams,
} from '@aztec/circuits.js';
import { Fr } from '@aztec/foundation/fields';

import { type AccountContract } from '../account/contract.js';
Expand Down Expand Up @@ -32,17 +37,24 @@ export class AccountManager {
// TODO(@spalladino): Does it make sense to have both completeAddress and instance?
private completeAddress?: CompleteAddress;
private instance?: ContractInstanceWithAddress;
private publicKeysHash?: Fr;
private publicKeys?: PublicKeys;

constructor(private pxe: PXE, private secretKey: Fr, private accountContract: AccountContract, salt?: Salt) {
this.salt = salt !== undefined ? new Fr(salt) : Fr.random();
}

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

protected getPublicKeys() {
if (!this.publicKeys) {
this.publicKeys = deriveKeys(this.secretKey).publicKeys;
}
return this.publicKeys;
}

/**
Expand Down Expand Up @@ -87,7 +99,7 @@ export class AccountManager {
this.instance = getContractInstanceFromDeployParams(this.accountContract.getContractArtifact(), {
constructorArgs: this.accountContract.getDeploymentArgs(),
salt: this.salt,
publicKeysHash: this.getPublicKeysHash(),
publicKeys: this.getPublicKeys(),
});
}
return this.instance;
Expand Down Expand Up @@ -146,7 +158,7 @@ export class AccountManager {
const args = this.accountContract.getDeploymentArgs() ?? [];
return new DeployAccountMethod(
this.accountContract.getAuthWitnessProvider(this.getCompleteAddress()),
this.getPublicKeysHash(),
this.getPublicKeys(),
deployWallet,
this.accountContract.getContractArtifact(),
args,
Expand Down
11 changes: 6 additions & 5 deletions yarn-project/aztec.js/src/contract/contract.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PublicKeys } from '@aztec/circuits.js';
import { type ContractArtifact } from '@aztec/foundation/abi';
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';
Expand Down Expand Up @@ -38,25 +39,25 @@ export class Contract extends ContractBase {
*/
public static deploy(wallet: Wallet, artifact: ContractArtifact, args: any[], constructorName?: string) {
const postDeployCtor = (address: AztecAddress, wallet: Wallet) => Contract.at(address, artifact, wallet);
return new DeployMethod(Fr.ZERO, wallet, artifact, postDeployCtor, args, constructorName);
return new DeployMethod(PublicKeys.empty(), wallet, artifact, postDeployCtor, args, constructorName);
}

/**
* Creates a tx to deploy a new instance of a contract using the specified public keys hash to derive the address.
* @param publicKeysHash - Hash of public keys to use for deriving the address.
* @param publicKeys - Hash of public keys to use for deriving the address.
* @param wallet - The wallet for executing the deployment.
* @param artifact - Build artifact of the contract.
* @param args - Arguments for the constructor.
* @param constructorName - The name of the constructor function to call.
*/
public static deployWithPublicKeysHash(
publicKeysHash: Fr,
public static deployWithPublicKeys(
publicKeys: PublicKeys,
wallet: Wallet,
artifact: ContractArtifact,
args: any[],
constructorName?: string,
) {
const postDeployCtor = (address: AztecAddress, wallet: Wallet) => Contract.at(address, artifact, wallet);
return new DeployMethod(publicKeysHash, wallet, artifact, postDeployCtor, args, constructorName);
return new DeployMethod(publicKeys, wallet, artifact, postDeployCtor, args, constructorName);
}
}
5 changes: 3 additions & 2 deletions yarn-project/aztec.js/src/contract/deploy_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type FunctionCall, type TxExecutionRequest } from '@aztec/circuit-types
import {
AztecAddress,
type ContractInstanceWithAddress,
PublicKeys,
computePartialAddress,
getContractClassFromArtifact,
getContractInstanceFromDeployParams,
Expand Down Expand Up @@ -52,7 +53,7 @@ export class DeployMethod<TContract extends ContractBase = Contract> extends Bas
private constructorArtifact: FunctionArtifact | undefined;

constructor(
private publicKeysHash: Fr,
private publicKeys: PublicKeys,
wallet: Wallet,
private artifact: ContractArtifact,
private postDeployCtor: (address: AztecAddress, wallet: Wallet) => Promise<TContract>,
Expand Down Expand Up @@ -221,7 +222,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.publicKeys,
constructorArtifact: this.constructorArtifact,
deployer: options.universalDeploy ? AztecAddress.ZERO : this.wallet.getAddress(),
});
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/aztec.js/src/deployment/contract_deployer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AztecAddress } from '@aztec/circuits.js';
import { type AztecAddress, PublicKeys } from '@aztec/circuits.js';
import { type ContractArtifact } from '@aztec/foundation/abi';
import { Fr } from '@aztec/foundation/fields';

Expand All @@ -14,7 +14,7 @@ export class ContractDeployer {
constructor(
private artifact: ContractArtifact,
private wallet: Wallet,
private publicKeysHash?: Fr,
private publicKeys?: PublicKeys,
private constructorName?: string,
) {}

Expand All @@ -30,7 +30,7 @@ export class ContractDeployer {
public deploy(...args: any[]) {
const postDeployCtor = (address: AztecAddress, wallet: Wallet) => Contract.at(address, this.artifact, wallet);
return new DeployMethod(
this.publicKeysHash ?? Fr.ZERO,
this.publicKeys ?? PublicKeys.empty(),
this.wallet,
this.artifact,
postDeployCtor,
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec.js/src/deployment/deploy_instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getDeployerContract } from './protocol_contracts.js';
*/
export function deployInstance(wallet: Wallet, instance: ContractInstanceWithAddress): ContractFunctionInteraction {
const deployerContract = getDeployerContract(wallet);
const { salt, contractClassId, publicKeysHash, deployer } = instance;
const { salt, contractClassId, publicKeys, deployer } = instance;
const isUniversalDeploy = deployer.isZero();
if (!isUniversalDeploy && !wallet.getAddress().equals(deployer)) {
throw new Error(
Expand All @@ -22,7 +22,7 @@ export function deployInstance(wallet: Wallet, instance: ContractInstanceWithAdd
salt,
contractClassId,
instance.initializationHash,
publicKeysHash,
publicKeys,
isUniversalDeploy,
);
}
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export { AccountWallet, AccountWalletWithSecretKey, SignerlessWallet, type Walle
export {
AztecAddress,
EthAddress,
PublicKeys,
Fq,
Fr,
GlobalVariables,
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/aztec/src/cli/cmds/start_pxe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type ContractArtifact,
type ContractInstanceWithAddress,
Fr,
PublicKeys,
getContractClassFromArtifact,
} from '@aztec/aztec.js';
import { type AztecNode, createAztecNodeClient } from '@aztec/circuit-types';
Expand Down Expand Up @@ -104,7 +105,7 @@ export async function addPXE(
address,
deployer: AztecAddress.ZERO,
contractClassId: getContractClassFromArtifact(artifact!).id,
publicKeysHash: Fr.ZERO,
publicKeys: PublicKeys.empty(),
};
userLog(`Registering ${name} at ${address.toString()}`);
await pxe.registerContract({ artifact, instance });
Expand Down
Loading

0 comments on commit e009360

Please sign in to comment.