Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Sep 5, 2023
1 parent a522724 commit 0b1e955
Show file tree
Hide file tree
Showing 67 changed files with 362 additions and 303 deletions.
6 changes: 3 additions & 3 deletions yarn-project/acir-simulator/src/client/db_oracle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CompleteAddress, HistoricBlockData, PrivateKey, PublicKey } from '@aztec/circuits.js';
import { CompleteAddress, GrumpkinPrivateKey, HistoricBlockData, PublicKey } from '@aztec/circuits.js';
import { FunctionAbi, FunctionDebugMetadata, FunctionSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
Expand Down Expand Up @@ -95,10 +95,10 @@ export interface DBOracle extends CommitmentsDB {
*
* @param contractAddress - The contract address. Ignored here. But we might want to return different keys for different contracts.
* @param pubKey - The public key of an account.
* @returns A Promise that resolves to the secret key as a Buffer.
* @returns A Promise that resolves to the secret key.
* @throws An Error if the input address does not match the public key address of the key pair.
*/
getSecretKey(contractAddress: AztecAddress, pubKey: PublicKey): Promise<PrivateKey>;
getSecretKey(contractAddress: AztecAddress, pubKey: PublicKey): Promise<GrumpkinPrivateKey>;

/**
* Retrieves a set of notes stored in the database for a given contract address and storage slot.
Expand Down
15 changes: 7 additions & 8 deletions yarn-project/acir-simulator/src/client/private_execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
L1_TO_L2_MSG_TREE_HEIGHT,
MAX_NEW_COMMITMENTS_PER_CALL,
PRIVATE_DATA_TREE_HEIGHT,
PrivateKey,
PublicCallRequest,
TxContext,
} from '@aztec/circuits.js';
Expand All @@ -28,7 +27,7 @@ import { asyncMap } from '@aztec/foundation/async-map';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { toBufferBE } from '@aztec/foundation/bigint-buffer';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { Fr, GrumpkinScalar } from '@aztec/foundation/fields';
import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
import { AppendOnlyTree, Pedersen, StandardTree, newTree } from '@aztec/merkle-tree';
import {
Expand Down Expand Up @@ -66,7 +65,7 @@ describe('Private Execution test suite', () => {
let logger: DebugLogger;

const defaultContractAddress = AztecAddress.random();
const ownerPk = PrivateKey.fromString('5e30a2f886b4b6a11aea03bf4910fbd5b24e61aa27ea4d05c393b3ab592a8d33');
const ownerPk = GrumpkinScalar.fromString('2dcc5485a58316776299be08c78fa3788a1a7961ae30dc747fb1be17692a8d32');

const treeHeights: { [name: string]: number } = {
privateData: PRIVATE_DATA_TREE_HEIGHT,
Expand Down Expand Up @@ -168,7 +167,7 @@ describe('Private Execution test suite', () => {

describe('private token airdrop contract', () => {
const contractAddress = defaultContractAddress;
const recipientPk = PrivateKey.fromString('0c9ed344548e8f9ba8aa3c9f8651eaa2853130f6c1e9c050ccf198f7ea18a7ec');
const recipientPk = GrumpkinScalar.fromString('0c9ed344548e8f9ba8aa3c9f8651eaa2853130f6c1e9c050ccf198f7ea18a7ec');
const mockFirstNullifier = new Fr(1111);
let owner: AztecAddress;
let recipient: AztecAddress;
Expand Down Expand Up @@ -233,7 +232,7 @@ describe('Private Execution test suite', () => {
const siloedNoteHash = siloCommitment(circuitsWasm, contractAddress, innerNoteHash);
const uniqueSiloedNoteHash = computeUniqueCommitment(circuitsWasm, note.nonce, siloedNoteHash);
const innerNullifier = Fr.fromBuffer(
pedersenPlookupCommitInputs(circuitsWasm, [uniqueSiloedNoteHash.toBuffer(), ownerPk.value]),
pedersenPlookupCommitInputs(circuitsWasm, [uniqueSiloedNoteHash.toBuffer(), ownerPk.toBuffer()]),
);

const result = await acirSimulator.computeNoteHashAndNullifier(
Expand Down Expand Up @@ -400,7 +399,7 @@ describe('Private Execution test suite', () => {

describe('private token contract', () => {
const contractAddress = defaultContractAddress;
const recipientPk = PrivateKey.fromString('0c9ed344548e8f9ba8aa3c9f8651eaa2853130f6c1e9c050ccf198f7ea18a7ec');
const recipientPk = GrumpkinScalar.fromString('0c9ed344548e8f9ba8aa3c9f8651eaa2853130f6c1e9c050ccf198f7ea18a7ec');
const mockFirstNullifier = new Fr(1111);
let owner: AztecAddress;
let recipient: AztecAddress;
Expand Down Expand Up @@ -465,7 +464,7 @@ describe('Private Execution test suite', () => {
const siloedNoteHash = siloCommitment(circuitsWasm, contractAddress, innerNoteHash);
const uniqueSiloedNoteHash = computeUniqueCommitment(circuitsWasm, note.nonce, siloedNoteHash);
const innerNullifier = Fr.fromBuffer(
pedersenPlookupCommitInputs(circuitsWasm, [uniqueSiloedNoteHash.toBuffer(), ownerPk.value]),
pedersenPlookupCommitInputs(circuitsWasm, [uniqueSiloedNoteHash.toBuffer(), ownerPk.toBuffer()]),
);

const result = await acirSimulator.computeNoteHashAndNullifier(
Expand Down Expand Up @@ -680,7 +679,7 @@ describe('Private Execution test suite', () => {

describe('consuming messages', () => {
const contractAddress = defaultContractAddress;
const recipientPk = PrivateKey.fromString('0c9ed344548e8f9ba8aa3c9f8651eaa2853130f6c1e9c050ccf198f7ea18a7ec');
const recipientPk = GrumpkinScalar.fromString('0c9ed344548e8f9ba8aa3c9f8651eaa2853130f6c1e9c050ccf198f7ea18a7ec');

let recipient: AztecAddress;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CompleteAddress, FunctionData, HistoricBlockData, PrivateKey } from '@aztec/circuits.js';
import { CompleteAddress, FunctionData, HistoricBlockData } from '@aztec/circuits.js';
import { FunctionSelector, encodeArguments } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { Fr, GrumpkinScalar } from '@aztec/foundation/fields';
import { PrivateTokenContractAbi } from '@aztec/noir-contracts/artifacts';
import { FunctionCall } from '@aztec/types';

Expand All @@ -21,7 +21,7 @@ describe('Unconstrained Execution test suite', () => {
});

describe('private token contract', () => {
const ownerPk = PrivateKey.fromString('5e30a2f886b4b6a11aea03bf4910fbd5b24e61aa27ea4d05c393b3ab592a8d33');
const ownerPk = GrumpkinScalar.fromString('2dcc5485a58316776299be08c78fa3788a1a7961ae30dc747fb1be17692a8d32');

let owner: AztecAddress;

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/acir-simulator/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CircuitsWasm, PrivateKey } from '@aztec/circuits.js';
import { CircuitsWasm, GrumpkinPrivateKey } from '@aztec/circuits.js';
import { Grumpkin, pedersenPlookupCommitInputs } from '@aztec/circuits.js/barretenberg';
import { Fr } from '@aztec/foundation/fields';

Expand Down Expand Up @@ -37,7 +37,7 @@ export function computeSlotForMapping(mappingSlot: Fr, owner: NoirPoint | Fr, bb
* @param grumpkin - The grumpkin instance.
* @returns The public key.
*/
export function toPublicKey(privateKey: PrivateKey, grumpkin: Grumpkin): NoirPoint {
export function toPublicKey(privateKey: GrumpkinPrivateKey, grumpkin: Grumpkin): NoirPoint {
const point = grumpkin.mul(Grumpkin.generator, privateKey);
return {
x: point.x.value,
Expand Down
21 changes: 12 additions & 9 deletions yarn-project/aztec-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Contract,
ContractDeployer,
Fr,
GrumpkinScalar,
Point,
generatePublicKey,
getAccountWallets,
Expand All @@ -15,7 +16,7 @@ import { DebugLogger, LogFn } from '@aztec/foundation/log';
import { fileURLToPath } from '@aztec/foundation/url';
import { compileContract } from '@aztec/noir-compiler/cli';
import { SchnorrAccountContractAbi } from '@aztec/noir-contracts/artifacts';
import { CompleteAddress, ContractData, L2BlockL2Logs, PrivateKey, TxHash } from '@aztec/types';
import { CompleteAddress, ContractData, L2BlockL2Logs, TxHash } from '@aztec/types';

import { Command } from 'commander';
import { readFileSync } from 'fs';
Expand Down Expand Up @@ -96,7 +97,7 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {

program
.command('generate-private-key')
.description('Generates a 32-byte private key.')
.description('Generates a private key which fits into the field used by Grumpkin curve.')
.option(
'-m, --mnemonic',
'An optional mnemonic string used for the private key generation. If not provided, random private key will be generated.',
Expand All @@ -106,11 +107,13 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
let publicKey;
if (options.mnemonic) {
const acc = mnemonicToAccount(options.mnemonic);
const key = Buffer.from(acc.getHdKey().privateKey!);
privKey = key.toString('hex');
publicKey = await generatePublicKey(new PrivateKey(key));
// Given that the mnemonicToAccount is a function from viem/accounts, it works with Ethereum private keys.
// Ethereum private keys are 256 bits and for this reason we need to fit it to Fr as that is the canonical
// representation of a private key used with Grumpkin curve.
const key = GrumpkinScalar.fromBufferWithWrapping(Buffer.from(acc.getHdKey().privateKey!));
publicKey = await generatePublicKey(key);
} else {
const key = PrivateKey.random();
const key = GrumpkinScalar.random();
privKey = key.toString();
publicKey = await generatePublicKey(key);
}
Expand All @@ -132,8 +135,8 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
.action(async options => {
const client = await createCompatibleClient(options.rpcUrl, debugLogger);
const privateKey = options.privateKey
? PrivateKey.fromString(stripLeadingHex(options.privateKey))
: PrivateKey.random();
? GrumpkinScalar.fromString(stripLeadingHex(options.privateKey))
: GrumpkinScalar.random();

const account = getSchnorrAccount(client, privateKey, privateKey, accountCreationSalt);
const wallet = await account.waitDeploy();
Expand Down Expand Up @@ -382,7 +385,7 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
);
}

const privateKey = PrivateKey.fromString(stripLeadingHex(options.privateKey));
const privateKey = GrumpkinScalar.fromString(stripLeadingHex(options.privateKey));

const client = await createCompatibleClient(options.rpcUrl, debugLogger);
const wallet = await getAccountWallets(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { Fr, GrumpkinScalar, Point } from '@aztec/foundation/fields';
import { JsonRpcServer } from '@aztec/foundation/json-rpc/server';
import {
AztecRPC,
CompleteAddress,
ContractData,
ExtendedContractData,
L2BlockL2Logs,
PrivateKey,
Tx,
TxExecutionRequest,
TxHash,
Expand Down Expand Up @@ -36,8 +35,8 @@ export function getHttpRpcServer(aztecRpcServer: AztecRPC): JsonRpcServer {
TxHash,
EthAddress,
Point,
PrivateKey,
Fr,
GrumpkinScalar,
},
{ Tx, TxReceipt, L2BlockL2Logs },
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
CompleteAddress,
EthAddress,
FunctionData,
GrumpkinPrivateKey,
KernelCircuitPublicInputsFinal,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX,
PartialAddress,
PrivateKey,
PublicCallRequest,
} from '@aztec/circuits.js';
import { encodeArguments } from '@aztec/foundation/abi';
Expand Down Expand Up @@ -99,7 +99,7 @@ export class AztecRPCServer implements AztecRPC {
this.log.info('Stopped');
}

public async registerAccount(privKey: PrivateKey, partialAddress: PartialAddress) {
public async registerAccount(privKey: GrumpkinPrivateKey, partialAddress: PartialAddress) {
const completeAddress = await CompleteAddress.fromPrivateKeyAndPartialAddress(privKey, partialAddress);
const wasAdded = await this.db.addCompleteAddress(completeAddress);
if (wasAdded) {
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec-rpc/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
EthAddress,
Fr,
FunctionSelector,
GrumpkinPrivateKey,
HistoricBlockData,
PrivateKey,
PublicKey,
} from '@aztec/circuits.js';
import { siloCommitment } from '@aztec/circuits.js/abis';
Expand All @@ -33,7 +33,7 @@ export class SimulatorOracle implements DBOracle {
private dataTreeProvider: DataCommitmentProvider,
) {}

getSecretKey(_contractAddress: AztecAddress, pubKey: PublicKey): Promise<PrivateKey> {
getSecretKey(_contractAddress: AztecAddress, pubKey: PublicKey): Promise<GrumpkinPrivateKey> {
return this.keyStore.getAccountPrivateKey(pubKey);
}

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec-rpc/src/synchroniser/synchroniser.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CompleteAddress, Fr, HistoricBlockData, PrivateKey } from '@aztec/circuits.js';
import { CompleteAddress, Fr, GrumpkinScalar, HistoricBlockData } from '@aztec/circuits.js';
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
import { TestKeyStore } from '@aztec/key-store';
import { AztecNode, L2Block, MerkleTreeId } from '@aztec/types';
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('Synchroniser', () => {

// Manually adding account to database so that we can call synchroniser.isAccountStateSynchronised
const keyStore = new TestKeyStore(await Grumpkin.new());
const privateKey = PrivateKey.random();
const privateKey = GrumpkinScalar.random();
keyStore.addAccount(privateKey);
const completeAddress = await CompleteAddress.fromPrivateKeyAndPartialAddress(privateKey, Fr.random());
await database.addCompleteAddress(completeAddress);
Expand Down
12 changes: 10 additions & 2 deletions yarn-project/aztec-sandbox/src/examples/private_token_contract.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { AztecAddress, Contract, Fr, PrivateKey, Wallet, createAccounts, createAztecRpcClient } from '@aztec/aztec.js';
import {
AztecAddress,
Contract,
Fr,
GrumpkinScalar,
Wallet,
createAccounts,
createAztecRpcClient,
} from '@aztec/aztec.js';
import { createDebugLogger } from '@aztec/foundation/log';
import { SchnorrSingleKeyAccountContractAbi } from '@aztec/noir-contracts/artifacts';
import { PrivateTokenContract } from '@aztec/noir-contracts/types';

const logger = createDebugLogger('aztec:http-rpc-client');

export const privateKey = PrivateKey.fromString('ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80');
export const privateKey = GrumpkinScalar.fromString('ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80');

const url = 'http://localhost:8080';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
createAztecRpcClient,
getL1ContractAddresses,
} from '@aztec/aztec.js';
import { PrivateKey } from '@aztec/circuits.js';
import { GrumpkinScalar } from '@aztec/circuits.js';
import { createDebugLogger } from '@aztec/foundation/log';
import { UniswapPortalAbi, UniswapPortalBytecode } from '@aztec/l1-artifacts';
import { SchnorrSingleKeyAccountContractAbi } from '@aztec/noir-contracts/artifacts';
Expand Down Expand Up @@ -37,7 +37,7 @@ const aztecRpcUrl = 'http://localhost:8080';
const ethRpcUrl = 'http://localhost:8545';

const hdAccount = mnemonicToAccount(MNEMONIC);
const privateKey = new PrivateKey(Buffer.from(hdAccount.getHdKey().privateKey!));
const privateKey = GrumpkinScalar.fromBuffer(Buffer.from(hdAccount.getHdKey().privateKey!));

const walletClient = createWalletClient({
account: hdAccount,
Expand Down
3 changes: 1 addition & 2 deletions yarn-project/aztec-sandbox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { AztecNodeConfig, AztecNodeService, getConfigEnvVars } from '@aztec/aztec-node';
import { createAztecRPCServer, getConfigEnvVars as getRpcConfigEnvVars } from '@aztec/aztec-rpc';
import { deployInitialSandboxAccounts } from '@aztec/aztec.js';
import { PrivateKey } from '@aztec/circuits.js';
import { deployL1Contracts } from '@aztec/ethereum';
import { createDebugLogger } from '@aztec/foundation/log';
import { retryUntil } from '@aztec/foundation/retry';
Expand Down Expand Up @@ -72,7 +71,7 @@ async function main() {
logger.info(`Setting up Aztec Sandbox v${version}, please stand by...`);
logger.info('Deploying rollup contracts to L1...');
const deployedL1Contracts = await waitThenDeploy(aztecNodeConfig.rpcUrl, hdAccount);
aztecNodeConfig.publisherPrivateKey = new PrivateKey(Buffer.from(privKey!));
aztecNodeConfig.publisherPrivateKey = `0x${Buffer.from(privKey!).toString('hex')}`;
aztecNodeConfig.rollupContract = deployedL1Contracts.rollupAddress;
aztecNodeConfig.contractDeploymentEmitterContract = deployedL1Contracts.contractDeploymentEmitterAddress;
aztecNodeConfig.inboxContract = deployedL1Contracts.inboxAddress;
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec.js/src/account/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fr, PublicKey, getContractDeploymentInfo } from '@aztec/circuits.js';
import { AztecRPC, CompleteAddress, PrivateKey } from '@aztec/types';
import { AztecRPC, CompleteAddress, GrumpkinPrivateKey } from '@aztec/types';

import { AccountWallet, ContractDeployer, DeployMethod, WaitOpts, generatePublicKey } from '../index.js';
import { DeployAccountSentTx } from './deploy_account_sent_tx.js';
Expand All @@ -19,7 +19,7 @@ export class Account {

constructor(
private rpc: AztecRPC,
private encryptionPrivateKey: PrivateKey,
private encryptionPrivateKey: GrumpkinPrivateKey,
private accountContract: AccountContract,
saltOrAddress?: Salt | CompleteAddress,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { Ecdsa } from '@aztec/circuits.js/barretenberg';
import { ContractAbi } from '@aztec/foundation/abi';
import { CompleteAddress, NodeInfo, PrivateKey } from '@aztec/types';
import { CompleteAddress, NodeInfo } from '@aztec/types';

import EcdsaAccountContractAbi from '../../abis/ecdsa_account_contract.json' assert { type: 'json' };
import { StoredKeyAccountEntrypoint } from '../entrypoint/stored_key_account_entrypoint.js';
import { EcdsaStoredKeyAccountEntrypoint } from '../index.js';
import { AccountContract } from './index.js';

/**
* Account contract that authenticates transactions using ECDSA signatures
* verified against a secp256k1 public key stored in an immutable encrypted note.
*/ export class EcdsaAccountContract implements AccountContract {
constructor(private signingPrivateKey: PrivateKey) {}
constructor(private signingPrivateKey: Buffer) {}

public async getDeploymentArgs() {
const signingPublicKey = await Ecdsa.new().then(e => e.computePublicKey(this.signingPrivateKey));
return [signingPublicKey.subarray(0, 32), signingPublicKey.subarray(32, 64)];
}

public async getEntrypoint({ address }: CompleteAddress, { chainId, version }: NodeInfo) {
return new StoredKeyAccountEntrypoint(address, this.signingPrivateKey, await Ecdsa.new(), chainId, version);
return new EcdsaStoredKeyAccountEntrypoint(address, this.signingPrivateKey, await Ecdsa.new(), chainId, version);
}

public getContractAbi(): ContractAbi {
Expand Down
Loading

0 comments on commit 0b1e955

Please sign in to comment.