Skip to content

Commit

Permalink
Merge b43e091 into b1cb502
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunkar authored Jan 22, 2025
2 parents b1cb502 + b43e091 commit 150f560
Show file tree
Hide file tree
Showing 67 changed files with 443 additions and 322 deletions.
52 changes: 52 additions & 0 deletions barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ WASM_EXPORT void ecdsa__construct_signature(uint8_t const* message,
write(output_sig_v, sig.v);
}

WASM_EXPORT void ecdsa__construct_signature_(uint8_t const* message_buf,
uint8_t const* private_key,
uint8_t* output_sig_r,
uint8_t* output_sig_s,
uint8_t* output_sig_v)
{
using serialize::write;
auto priv_key = from_buffer<secp256k1::fr>(private_key);
secp256k1::g1::affine_element pub_key = secp256k1::g1::one * priv_key;
ecdsa_key_pair<secp256k1::fr, secp256k1::g1> key_pair = { priv_key, pub_key };

auto message = from_buffer<std::string>(message_buf);

auto sig = ecdsa_construct_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(message, key_pair);
write(output_sig_r, sig.r);
write(output_sig_s, sig.s);
write(output_sig_v, sig.v);
}

WASM_EXPORT void ecdsa__recover_public_key_from_signature(uint8_t const* message,
size_t msg_len,
uint8_t const* sig_r,
Expand All @@ -48,6 +67,21 @@ WASM_EXPORT void ecdsa__recover_public_key_from_signature(uint8_t const* message
write(output_pub_key, recovered_pub_key);
}

WASM_EXPORT void ecdsa__recover_public_key_from_signature_(
uint8_t const* message_buf, uint8_t const* sig_r, uint8_t const* sig_s, uint8_t* sig_v, uint8_t* output_pub_key)
{
std::array<uint8_t, 32> r, s;
std::copy(sig_r, sig_r + 32, r.begin());
std::copy(sig_s, sig_s + 32, s.begin());
const uint8_t v = *sig_v;

auto message = from_buffer<std::string>(message_buf);
ecdsa_signature sig = { r, s, v };
auto recovered_pub_key =
ecdsa_recover_public_key<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(message, sig);
write(output_pub_key, recovered_pub_key);
}

WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message,
size_t msg_len,
uint8_t const* pub_key,
Expand All @@ -65,3 +99,21 @@ WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message,
return ecdsa_verify_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
std::string((char*)message, msg_len), pubk, sig);
}

WASM_EXPORT void ecdsa__verify_signature_(uint8_t const* message_buf,
uint8_t const* pub_key,
uint8_t const* sig_r,
uint8_t const* sig_s,
uint8_t const* sig_v,
bool* result)
{
auto pubk = from_buffer<secp256k1::g1::affine_element>(pub_key);
std::array<uint8_t, 32> r, s;
std::copy(sig_r, sig_r + 32, r.begin());
std::copy(sig_s, sig_s + 32, s.begin());
const uint8_t v = *sig_v;

auto message = from_buffer<std::string>(message_buf);
ecdsa_signature sig = { r, s, v };
*result = ecdsa_verify_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(message, pubk, sig);
}
14 changes: 13 additions & 1 deletion barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <ecc/curves/secp256k1/secp256k1.hpp>
#include "barretenberg/common/wasm_export.hpp"
#include <ecc/curves/secp256k1/secp256k1.hpp>

WASM_EXPORT void ecdsa__compute_public_key(uint8_t const* private_key, uint8_t* public_key_buf);

Expand All @@ -10,16 +10,28 @@ WASM_EXPORT void ecdsa__construct_signature(uint8_t const* message,
uint8_t* output_sig_s,
uint8_t* output_sig_v);

WASM_EXPORT void ecdsa__construct_signature_(uint8_t const* message_buf,
uint8_t const* private_key,
uint8_t* output_sig_r,
uint8_t* output_sig_s,
uint8_t* output_sig_v);

WASM_EXPORT void ecdsa__recover_public_key_from_signature(uint8_t const* message,
size_t msg_len,
uint8_t const* sig_r,
uint8_t const* sig_s,
uint8_t* sig_v,
uint8_t* output_pub_key);

WASM_EXPORT void ecdsa__recover_public_key_from_signature_(
uint8_t const* message_buf, uint8_t const* sig_r, uint8_t const* sig_s, uint8_t* sig_v, uint8_t* output_pub_key);

WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message,
size_t msg_len,
uint8_t const* pub_key,
uint8_t const* sig_r,
uint8_t const* sig_s,
uint8_t const* sig_v);

WASM_EXPORT bool ecdsa__verify_signature_(
uint8_t const* message, uint8_t const* pub_key, uint8_t const* sig_r, uint8_t const* sig_s, uint8_t const* sig_v);
3 changes: 1 addition & 2 deletions boxes/boxes/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"main": "./dist/index.js",
"scripts": {
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile --silence-warnings",
"codegen": "${AZTEC_BUILDER:-aztec-builder} codegen src/contracts/target -o artifacts",
"codegen": "${AZTEC_BUILDER:-aztec} codegen src/contracts/target -o artifacts",
"clean": "rm -rf ./dist .tsbuildinfo ./artifacts ./src/contracts/target",
"prep": "yarn clean && yarn compile && yarn codegen",
"dev": "yarn prep && webpack serve --mode development",
Expand Down Expand Up @@ -38,7 +38,6 @@
"dependencies": {
"@aztec/accounts": "latest",
"@aztec/aztec.js": "latest",
"@aztec/builder": "latest",
"classnames": "^2.3.2",
"formik": "^2.4.3",
"react": "^18.2.0",
Expand Down
21 changes: 9 additions & 12 deletions boxes/boxes/react/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@ import { SingleKeyAccountContract } from '@aztec/accounts/single_key';
const SECRET_KEY = Fr.random();

export class PrivateEnv {
pxe;
accountContract;
account: AccountManager;
private constructor(private accountManager: AccountManager) {}

constructor(
private secretKey: Fr,
private pxeURL: string,
) {
this.pxe = createPXEClient(this.pxeURL);
static async create(secretKey: Fr, pxeURL: string) {
const pxe = createPXEClient(pxeURL);
const encryptionPrivateKey = deriveMasterIncomingViewingSecretKey(secretKey);
this.accountContract = new SingleKeyAccountContract(encryptionPrivateKey);
this.account = new AccountManager(this.pxe, this.secretKey, this.accountContract);
const accountContract = new SingleKeyAccountContract(encryptionPrivateKey);
const accountManager = await AccountManager.create(pxe, secretKey, accountContract);

return new PrivateEnv(accountManager);
}

async getWallet() {
// taking advantage that register is no-op if already registered
return await this.account.register();
return await this.accountManager.register();
}
}

export const deployerEnv = new PrivateEnv(SECRET_KEY, process.env.PXE_URL || 'http://localhost:8080');
export const deployerEnv = await PrivateEnv.create(SECRET_KEY, process.env.PXE_URL || 'http://localhost:8080');

const IGNORE_FUNCTIONS = ['constructor', 'compute_note_hash_and_optionally_a_nullifier'];
export const filteredInterface = BoxReactContractArtifact.functions.filter(f => !IGNORE_FUNCTIONS.includes(f.name));
5 changes: 2 additions & 3 deletions boxes/boxes/vanilla/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile --silence-warnings",
"codegen": "${AZTEC_BUILDER:-aztec-builder} codegen src/contracts/target -o artifacts",
"codegen": "${AZTEC_BUILDER:-aztec} codegen src/contracts/target -o artifacts",
"clean": "rm -rf ./dest .tsbuildinfo ./artifacts ./src/contracts/target",
"prep": "yarn clean && yarn compile && yarn codegen && tsc -b",
"dev": "yarn prep && webpack serve --mode development",
Expand All @@ -18,8 +18,7 @@
},
"dependencies": {
"@aztec/accounts": "latest",
"@aztec/aztec.js": "latest",
"@aztec/builder": "latest"
"@aztec/aztec.js": "latest"
},
"devDependencies": {
"@playwright/test": "^1.49.0",
Expand Down
16 changes: 3 additions & 13 deletions boxes/boxes/vanilla/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const secretKey = Fr.random();
const pxe = createPXEClient(process.env.PXE_URL || 'http://localhost:8080');

const encryptionPrivateKey = deriveMasterIncomingViewingSecretKey(secretKey);
const account = new AccountManager(pxe, secretKey, new SingleKeyAccountContract(encryptionPrivateKey));
const account = await AccountManager.create(pxe, secretKey, new SingleKeyAccountContract(encryptionPrivateKey));
let contract: any = null;
let wallet: Wallet | null = null;

Expand All @@ -21,11 +21,7 @@ document.querySelector('#deploy').addEventListener('click', async ({ target }: a
setWait(true);
wallet = await account.register();

contract = await VanillaContract.deploy(
wallet,
Fr.random(),
wallet.getCompleteAddress().address
)
contract = await VanillaContract.deploy(wallet, Fr.random(), wallet.getCompleteAddress().address)
.send({ contractAddressSalt: Fr.random() })
.deployed();
alert(`Contract deployed at ${contract.address}`);
Expand All @@ -41,13 +37,7 @@ document.querySelector('#set').addEventListener('submit', async (e: Event) => {

const { value } = document.querySelector('#number') as HTMLInputElement;
const { address: owner } = wallet.getCompleteAddress();
await contract.methods
.setNumber(
parseInt(value),
owner,
)
.send()
.wait();
await contract.methods.setNumber(parseInt(value), owner).send().wait();

setWait(false);
alert('Number set!');
Expand Down
8 changes: 4 additions & 4 deletions boxes/boxes/vite/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const SECRET_KEY = Fr.random();
export class PrivateEnv {
pxe;
accountContract;
account: AccountManager;
accountManager: AccountManager;

constructor(
private secretKey: Fr,
Expand Down Expand Up @@ -74,16 +74,16 @@ export class PrivateEnv {
this.secretKey,
);
this.accountContract = new SchnorrAccountContract(encryptionPrivateKey);
this.account = new AccountManager(
this.accountManager = await AccountManager.create(
this.pxe,
this.secretKey,
this.accountContract,
);
await this.account.deploy().wait();
await this.accountManager.deploy().wait();
}

async getWallet() {
return await this.account.register();
return await this.accountManager.register();
}
}

Expand Down
21 changes: 0 additions & 21 deletions boxes/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ __metadata:
languageName: node
linkType: soft

"@aztec/builder@npm:latest":
version: 0.52.0
resolution: "@aztec/builder@npm:0.52.0"
dependencies:
"@aztec/foundation": "npm:0.52.0"
"@aztec/types": "npm:0.52.0"
commander: "npm:^12.1.0"
bin:
aztec-builder: dest/bin/cli.js
checksum: 10c0/2207259255fc3e2ffbbd08829f2a4adc9070befaf09e0541213beaf378632a501c29104e447f310aebbf65a21e3cb77b99259a4122e9253640ee232ce4413675
languageName: node
linkType: hard

"@aztec/circuit-types@link:../yarn-project/circuit-types::locator=aztec-app%40workspace%3A.":
version: 0.0.0-use.local
resolution: "@aztec/circuit-types@link:../yarn-project/circuit-types::locator=aztec-app%40workspace%3A."
Expand Down Expand Up @@ -94,7 +81,6 @@ __metadata:
dependencies:
"@aztec/accounts": "npm:latest"
"@aztec/aztec.js": "npm:latest"
"@aztec/builder": "npm:latest"
"@playwright/test": "npm:1.49.0"
"@types/jest": "npm:^29.5.0"
"@types/node": "npm:^20.5.9"
Expand Down Expand Up @@ -145,19 +131,12 @@ __metadata:
languageName: node
linkType: soft

"@aztec/types@link:../yarn-project/types::locator=aztec-app%40workspace%3A.":
version: 0.0.0-use.local
resolution: "@aztec/types@link:../yarn-project/types::locator=aztec-app%40workspace%3A."
languageName: node
linkType: soft

"@aztec/vanilla@workspace:boxes/vanilla":
version: 0.0.0-use.local
resolution: "@aztec/vanilla@workspace:boxes/vanilla"
dependencies:
"@aztec/accounts": "npm:latest"
"@aztec/aztec.js": "npm:latest"
"@aztec/builder": "npm:latest"
"@playwright/test": "npm:^1.49.0"
"@types/node": "npm:^20.11.17"
assert: "npm:^2.1.0"
Expand Down
11 changes: 11 additions & 0 deletions docs/docs/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ keywords: [sandbox, aztec, notes, migration, updating, upgrading]
Aztec is in full-speed development. Literally every version breaks compatibility with the previous ones. This page attempts to target errors and difficulties you might encounter when upgrading, and how to resolve them.

## 0.72.0
### Some functions in `aztec.js` and `@aztec/accounts` are now async
In our efforts to make libraries more browser-friendly and providing with more bundling options for `bb.js` (like a non top-level-await version), some functions are being made async, in particular those that access our cryptographic functions.

```diff
- AztecAddress.random();
+ await AztecAddress.random();

- getSchnorrAccount();
+ await getSchnorrAccount();
```

### Public logs replace unencrypted logs
Any log emitted from public is now known as a public log, rather than an unencrypted log. This means methods relating to these logs have been renamed e.g. in the pxe, archiver, txe:
```diff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function CreateAccountDialog({
const createAccount = async () => {
setDeployingAccount(true);
const salt = Fr.random();
const account = getSchnorrAccount(
const account = await getSchnorrAccount(
pxe,
secretKey,
deriveSigningKey(secretKey),
Expand Down
2 changes: 1 addition & 1 deletion gaztec/src/components/sidebar/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export function SidebarComponent() {
}
const accountAddress = AztecAddress.fromString(event.target.value);
const accountData = await walletDB.retrieveAccount(accountAddress);
const account = getSchnorrAccount(
const account = await getSchnorrAccount(
pxe,
accountData.secretKey,
deriveSigningKey(accountData.secretKey),
Expand Down
7 changes: 0 additions & 7 deletions gaztec/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ __metadata:
languageName: node
linkType: soft

"@aztec/circuit-types@link:../yarn-project/circuit-types::locator=vite%40workspace%3A.":
version: 0.0.0-use.local
resolution: "@aztec/circuit-types@link:../yarn-project/circuit-types::locator=vite%40workspace%3A."
languageName: node
linkType: soft

"@aztec/circuits.js@link:../yarn-project/circuits.js::locator=vite%40workspace%3A.":
version: 0.0.0-use.local
resolution: "@aztec/circuits.js@link:../yarn-project/circuits.js::locator=vite%40workspace%3A."
Expand Down Expand Up @@ -4683,7 +4677,6 @@ __metadata:
"@aztec/accounts": "link:../yarn-project/accounts"
"@aztec/aztec.js": "link:../yarn-project/aztec.js"
"@aztec/bb-prover": "link:../yarn-project/bb-prover"
"@aztec/circuit-types": "link:../yarn-project/circuit-types"
"@aztec/circuits.js": "link:../yarn-project/circuits.js"
"@aztec/foundation": "link:../yarn-project/foundation"
"@aztec/key-store": "link:../yarn-project/key-store"
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/accounts/src/defaults/account_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DefaultAccountInterface } from '../defaults/account_interface.js';
*/
export abstract class DefaultAccountContract implements AccountContract {
abstract getAuthWitnessProvider(address: CompleteAddress): AuthWitnessProvider;
abstract getDeploymentArgs(): any[] | undefined;
abstract getDeploymentArgs(): Promise<any[] | undefined>;

constructor(private artifact: ContractArtifact) {}

Expand Down
8 changes: 4 additions & 4 deletions yarn-project/accounts/src/ecdsa/ecdsa_k/account_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export class EcdsaKAccountContract extends DefaultAccountContract {
super(EcdsaKAccountContractArtifact as ContractArtifact);
}

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

Expand All @@ -30,9 +30,9 @@ export class EcdsaKAccountContract extends DefaultAccountContract {
class EcdsaKAuthWitnessProvider implements AuthWitnessProvider {
constructor(private signingPrivateKey: Buffer) {}

createAuthWit(messageHash: Fr): Promise<AuthWitness> {
async createAuthWit(messageHash: Fr): Promise<AuthWitness> {
const ecdsa = new Ecdsa();
const signature = ecdsa.constructSignature(messageHash.toBuffer(), this.signingPrivateKey);
const signature = await ecdsa.constructSignature(messageHash.toBuffer(), this.signingPrivateKey);
return Promise.resolve(new AuthWitness(messageHash, [...signature.r, ...signature.s]));
}
}
9 changes: 7 additions & 2 deletions yarn-project/accounts/src/ecdsa/ecdsa_k/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ export { EcdsaKAccountContract };
* @param signingPrivateKey - Secp256k1 key used for signing transactions.
* @param salt - Deployment salt.
*/
export function getEcdsaKAccount(pxe: PXE, secretKey: Fr, signingPrivateKey: Buffer, salt?: Salt): AccountManager {
return new AccountManager(pxe, secretKey, new EcdsaKAccountContract(signingPrivateKey), salt);
export function getEcdsaKAccount(
pxe: PXE,
secretKey: Fr,
signingPrivateKey: Buffer,
salt?: Salt,
): Promise<AccountManager> {
return AccountManager.create(pxe, secretKey, new EcdsaKAccountContract(signingPrivateKey), salt);
}

/**
Expand Down
Loading

0 comments on commit 150f560

Please sign in to comment.