Skip to content

Commit

Permalink
refactor: nuking Curve interface
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Aug 1, 2023
1 parent 78f6f36 commit cf695e6
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 69 deletions.
6 changes: 3 additions & 3 deletions yarn-project/acir-simulator/src/client/private_execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
PrivateCallStackItem,
PublicCallRequest,
} from '@aztec/circuits.js';
import { Curve } from '@aztec/circuits.js/barretenberg';
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
import { FunctionAbi, decodeReturnValues } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
Expand Down Expand Up @@ -39,7 +39,7 @@ export class PrivateFunctionExecution {
private functionData: FunctionData,
private argsHash: Fr,
private callContext: CallContext,
private curve: Curve,
private curve: Grumpkin,

private log = createDebugLogger('aztec:simulator:secret_execution'),
) {}
Expand Down Expand Up @@ -263,7 +263,7 @@ export class PrivateFunctionExecution {
targetFunctionSelector: Buffer,
targetArgsHash: Fr,
callerContext: CallContext,
curve: Curve,
curve: Grumpkin,
) {
const targetAbi = await this.context.db.getFunctionABI(targetContractAddress, targetFunctionSelector);
const targetFunctionData = new FunctionData(targetFunctionSelector, targetAbi.isInternal, true, false);
Expand Down
33 changes: 0 additions & 33 deletions yarn-project/circuits.js/src/barretenberg/crypto/curve/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { IWasmModule } from '@aztec/foundation/wasm';

import { CircuitsWasm, Fr, Point, PrivateKey } from '../../../index.js';
import { Curve } from '../index.js';

/**
* Grumpkin elliptic curve operations.
*/
export class Grumpkin implements Curve {
export class Grumpkin {
/**
* Creates a new Grumpkin instance.
* @returns New Grumpkin instance.
Expand Down
1 change: 0 additions & 1 deletion yarn-project/circuits.js/src/barretenberg/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ export * from './pedersen/index.js';
export * from './ecdsa/index.js';
export * from './secp256k1/index.js';
export * from './schnorr/index.js';
export * from './curve/index.js';
export * from './signature/index.js';
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('secp256k1', () => {

it('should correctly compute public key', () => {
const privateKey = PrivateKey.random();
const lhs = secp256k1.mul(Secp256k1.generator, privateKey);
const lhs = secp256k1.mul(Secp256k1.generator, privateKey.value);
const rhs = ecdsa.computePublicKey(privateKey);
expect(lhs).toEqual(rhs);
});
Expand Down
33 changes: 16 additions & 17 deletions yarn-project/circuits.js/src/barretenberg/crypto/secp256k1/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { IWasmModule } from '@aztec/foundation/wasm';

import { CircuitsWasm, Fr, Point, PrivateKey } from '../../../index.js';
import { Curve } from '../curve/index.js';
import { CircuitsWasm } from '../../../index.js';

/**
* Secp256k1 elliptic curve operations.
*/
export class Secp256k1 implements Curve {
export class Secp256k1 {
/**
* Creates a new Secp256k1 instance.
* @returns New Secp256k1 instance.
Expand All @@ -18,51 +17,51 @@ export class Secp256k1 implements Curve {
constructor(private wasm: IWasmModule) {}

// prettier-ignore
static generator = Point.fromBuffer(Buffer.from([
static generator = Buffer.from([
0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07,
0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98,
0x48, 0x3a, 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4, 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8,
0xfd, 0x17, 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47, 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8,
]));
]);

/**
* Point generator
* @returns The generator for the curve.
*/
public generator(): Point {
public generator(): Buffer {
return Secp256k1.generator;
}

/**
* Multiplies a point by a private key (adds the point `privateKey` amount of time).
* Multiplies a point by a scalar (adds the point `scalar` amount of time).
* @param point - Point to multiply.
* @param privateKey - Private key to multiply by.
* @param scalar - Scalar to multiply by.
* @returns Result of the multiplication.
*/
public mul(point: Point, privateKey: PrivateKey): Point {
this.wasm.writeMemory(0, point.toBuffer());
this.wasm.writeMemory(64, privateKey.value);
public mul(point: Uint8Array, scalar: Uint8Array) {
this.wasm.writeMemory(0, point);
this.wasm.writeMemory(64, scalar);
this.wasm.call('ecc_secp256k1__mul', 0, 64, 96);
return Point.fromBuffer(Buffer.from(this.wasm.getMemorySlice(96, 160)));
return Buffer.from(this.wasm.getMemorySlice(96, 160));
}

/**
* Gets a random field element.
* @returns Random field element.
*/
public getRandomFr(): Fr {
public getRandomFr() {
this.wasm.call('ecc_secp256k1__get_random_scalar_mod_circuit_modulus', 0);
return Fr.fromBuffer(Buffer.from(this.wasm.getMemorySlice(0, 32)));
return Buffer.from(this.wasm.getMemorySlice(0, 32));
}

/**
* Converts a 512 bits long buffer to a field.
* @param uint512Buf - The buffer to convert.
* @returns A field element.
* @returns Buffer representation of the field element.
*/
public reduce512BufferToFr(uint512Buf: Buffer): Fr {
public reduce512BufferToFr(uint512Buf: Buffer) {
this.wasm.writeMemory(0, uint512Buf);
this.wasm.call('ecc_secp256k1__reduce512_buffer_mod_circuit_modulus', 0, 64);
return Fr.fromBuffer(Buffer.from(this.wasm.getMemorySlice(64, 96)));
return Buffer.from(this.wasm.getMemorySlice(64, 96));
}
}
6 changes: 3 additions & 3 deletions yarn-project/key-store/src/key_pair.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PrivateKey } from '@aztec/circuits.js';
import { Curve } from '@aztec/circuits.js/barretenberg';
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
import { KeyPair, PublicKey } from '@aztec/types';

/**
Expand All @@ -15,7 +15,7 @@ export class ConstantKeyPair implements KeyPair {
* @param curve - The curve used for elliptic curve cryptography operations.
* @returns A randomly generated ConstantKeyPair instance.
*/
public static random(curve: Curve) {
public static random(curve: Grumpkin) {
const privateKey = PrivateKey.random();
const publicKey = curve.mul(curve.generator(), privateKey);
return new ConstantKeyPair(publicKey, privateKey);
Expand All @@ -28,7 +28,7 @@ export class ConstantKeyPair implements KeyPair {
* @param privateKey - The private key.
* @returns A new instance.
*/
public static fromPrivateKey(curve: Curve, privateKey: PrivateKey) {
public static fromPrivateKey(curve: Grumpkin, privateKey: PrivateKey) {
const publicKey = curve.mul(curve.generator(), privateKey);
return new ConstantKeyPair(publicKey, privateKey);
}
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/key-store/src/test_key_store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PrivateKey } from '@aztec/circuits.js';
import { Curve } from '@aztec/circuits.js/barretenberg';
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
import { KeyPair, KeyStore, PublicKey } from '@aztec/types';

import { ConstantKeyPair } from './key_pair.js';
Expand All @@ -10,7 +10,7 @@ import { ConstantKeyPair } from './key_pair.js';
*/
export class TestKeyStore implements KeyStore {
private accounts: KeyPair[] = [];
constructor(private curve: Curve) {}
constructor(private curve: Grumpkin) {}

/**
* Adds an account to the key store from the provided private key.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PrivateKey, PublicKey } from '@aztec/circuits.js';
import { Curve } from '@aztec/circuits.js/barretenberg';
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
import { sha256 } from '@aztec/foundation/crypto';
import { Point } from '@aztec/foundation/fields';
import { numToUInt8 } from '@aztec/foundation/serialize';
Expand All @@ -17,7 +17,7 @@ import { createCipheriv, createDecipheriv } from 'browserify-cipher';
* @param grumpkin - The curve to use for curve operations.
* @returns A Buffer containing the derived AES secret key.
*/
export function deriveAESSecret(ecdhPubKey: PublicKey, ecdhPrivKey: PrivateKey, curve: Curve): Buffer {
export function deriveAESSecret(ecdhPubKey: PublicKey, ecdhPrivKey: PrivateKey, curve: Grumpkin): Buffer {
const sharedSecret = curve.mul(ecdhPubKey, ecdhPrivKey);
const secretBuffer = Buffer.concat([sharedSecret.toBuffer(), numToUInt8(1)]);
const hash = sha256(secretBuffer);
Expand All @@ -36,7 +36,7 @@ export function deriveAESSecret(ecdhPubKey: PublicKey, ecdhPrivKey: PrivateKey,
* @param curve - The curve instance used for elliptic curve operations.
* @returns A Buffer containing the encrypted data and the ephemeral public key.
*/
export function encryptBuffer(data: Buffer, ownerPubKey: PublicKey, ephPrivKey: PrivateKey, curve: Curve): Buffer {
export function encryptBuffer(data: Buffer, ownerPubKey: PublicKey, ephPrivKey: PrivateKey, curve: Grumpkin): Buffer {
const aesSecret = deriveAESSecret(ownerPubKey, ephPrivKey, curve);
const aesKey = aesSecret.subarray(0, 16);
const iv = aesSecret.subarray(16, 32);
Expand All @@ -57,7 +57,7 @@ export function encryptBuffer(data: Buffer, ownerPubKey: PublicKey, ephPrivKey:
* @param curve - The curve object used in the decryption process.
* @returns The decrypted plaintext as a Buffer or undefined if decryption fails.
*/
export function decryptBuffer(data: Buffer, ownerPrivKey: PrivateKey, curve: Curve): Buffer | undefined {
export function decryptBuffer(data: Buffer, ownerPrivKey: PrivateKey, curve: Grumpkin): Buffer | undefined {
const ephPubKey = Point.fromBuffer(data.subarray(-64));
const aesSecret = deriveAESSecret(ephPubKey, ownerPrivKey, curve);
const aesKey = aesSecret.subarray(0, 16);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AztecAddress, PrivateKey, PublicKey } from '@aztec/circuits.js';
import { Curve } from '@aztec/circuits.js/barretenberg';
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
import { serializeToBuffer } from '@aztec/circuits.js/utils';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader } from '@aztec/foundation/serialize';
Expand Down Expand Up @@ -60,7 +60,7 @@ export class NoteSpendingInfo {
* @param curve - The curve instance to use.
* @returns The encrypted NoteSpendingInfo object.
*/
public toEncryptedBuffer(ownerPubKey: PublicKey, curve: Curve): Buffer {
public toEncryptedBuffer(ownerPubKey: PublicKey, curve: Grumpkin): Buffer {
const ephPrivKey = PrivateKey.random();
return encryptBuffer(this.toBuffer(), ownerPubKey, ephPrivKey, curve);
}
Expand All @@ -72,7 +72,7 @@ export class NoteSpendingInfo {
* @param curve - The curve instance to use.
* @returns Instance of NoteSpendingInfo if the decryption was successful, undefined otherwise.
*/
static fromEncryptedBuffer(data: Buffer, ownerPrivKey: PrivateKey, curve: Curve): NoteSpendingInfo | undefined {
static fromEncryptedBuffer(data: Buffer, ownerPrivKey: PrivateKey, curve: Grumpkin): NoteSpendingInfo | undefined {
const buf = decryptBuffer(data, ownerPrivKey, curve);
if (!buf) {
return;
Expand Down

0 comments on commit cf695e6

Please sign in to comment.