Skip to content

Commit

Permalink
chore: modularize bb (#271)
Browse files Browse the repository at this point in the history
* chore: modularize ts

* chore: reformat
  • Loading branch information
ludamad authored and spalladino committed Mar 27, 2023
1 parent 7671192 commit 63b580d
Show file tree
Hide file tree
Showing 11 changed files with 5,416 additions and 3,664 deletions.
7 changes: 6 additions & 1 deletion ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"name": "@aztec/barretenberg.js",
"version": "0.0.0",
"type": "module",
"exports": "./dest/index.js",
"exports": {
"./crs": "./dest/crs/index.js",
"./crypto": "./dest/crypto/index.js",
"./wasm": "./dest/wasm/index.js"
},
"typedoc": {
"entryPoint": "./src/index.ts",
"displayName": "Barretenberg.js",
Expand All @@ -15,6 +19,7 @@
"bundle-deps": "yalc add @aztec/wasm @aztec/log && yalc add -D @aztec/eslint-config",
"dev-deps": "rm -r node_modules/@aztec/wasm ; yarn link ../../../yarn-project/wasm ; cd ../../../yarn-project/wasm ; yarn build:dev",
"formatting": "prettier --check ./src && eslint --max-warnings 0 ./src",
"formatting:fix": "prettier -w ./src",
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --no-cache --passWithNoTests",
"test-debug": "NODE_NO_WARNINGS=1 node --inspect-brk --experimental-vm-modules $(yarn bin jest) --no-cache --passWithNoTests --runInBand"
},
Expand Down
47 changes: 19 additions & 28 deletions ts/src/crs/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { readFile } from "fs/promises";
import { existsSync } from "fs";
import { readFile } from 'fs/promises';
import { existsSync } from 'fs';

import { dirname } from "path";
import { fileURLToPath } from "url";
import { dirname } from 'path';
import { fileURLToPath } from 'url';

/**
* The path to our SRS object, assuming that we are in barretenberg/ts folder.
*/
export const SRS_DEV_PATH =
dirname(fileURLToPath(import.meta.url)) +
"/../../../cpp/srs_db/ignition/monomial/transcript00.dat";
dirname(fileURLToPath(import.meta.url)) + '/../../../cpp/srs_db/ignition/monomial/transcript00.dat';
/**
* Downloader for CRS from the web or local.
*/
Expand All @@ -21,7 +20,7 @@ export class NetCrs {
/**
* The number of circuit gates.
*/
public readonly numPoints: number
public readonly numPoints: number,
) {}

/**
Expand All @@ -34,14 +33,11 @@ export class NetCrs {
const g1End = g1Start + this.numPoints * 64 - 1;

// Download required range of data.
const response = await fetch(
"https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/sealed/transcript00.dat",
{
headers: {
Range: `bytes=${g1Start}-${g1End}`,
},
}
);
const response = await fetch('https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/sealed/transcript00.dat', {
headers: {
Range: `bytes=${g1Start}-${g1End}`,
},
});

this.data = new Uint8Array(await response.arrayBuffer());

Expand All @@ -55,14 +51,11 @@ export class NetCrs {
const g2Start = 28 + 5040000 * 64;
const g2End = g2Start + 128 - 1;

const response2 = await fetch(
"https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/sealed/transcript00.dat",
{
headers: {
Range: `bytes=${g2Start}-${g2End}`,
},
}
);
const response2 = await fetch('https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/sealed/transcript00.dat', {
headers: {
Range: `bytes=${g2Start}-${g2End}`,
},
});

this.g2Data = new Uint8Array(await response2.arrayBuffer());
}
Expand Down Expand Up @@ -96,7 +89,7 @@ export class FileCrs {
* The number of circuit gates.
*/
public readonly numPoints: number,
private path: string
private path: string,
) {}

/**
Expand Down Expand Up @@ -143,11 +136,9 @@ export class Crs {
/**
* The number of circuit gates.
*/
public readonly numPoints: number
public readonly numPoints: number,
) {
this.crs = existsSync(SRS_DEV_PATH)
? new FileCrs(numPoints, SRS_DEV_PATH)
: new NetCrs(numPoints);
this.crs = existsSync(SRS_DEV_PATH) ? new FileCrs(numPoints, SRS_DEV_PATH) : new NetCrs(numPoints);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ts/src/crypto/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./pedersen/index.js";
export * from './pedersen/index.js';
2 changes: 1 addition & 1 deletion ts/src/crypto/pedersen/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./pedersen.js";
export * from './pedersen.js';
8 changes: 4 additions & 4 deletions ts/src/crypto/pedersen/pedersen.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BarretenbergWasm } from "../../wasm/index.js";
import { pedersenGetHashTree } from "./pedersen.js";
import { BarretenbergWasm } from '../../wasm/barretenberg_wasm.js';
import { pedersenGetHashTree } from './pedersen.js';

describe("pedersen", () => {
describe('pedersen', () => {
let barretenbergWasm!: BarretenbergWasm;
const values: Buffer[] = [];

Expand All @@ -16,7 +16,7 @@ describe("pedersen", () => {
}
});

it("hasher_consistency_and_benchmark", () => {
it('hasher_consistency_and_benchmark', () => {
const start1 = new Date().getTime();
const result = pedersenGetHashTree(barretenbergWasm, values);
const end1 = new Date().getTime() - start1;
Expand Down
65 changes: 23 additions & 42 deletions ts/src/crypto/pedersen/pedersen.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { BarretenbergWasm } from "../../wasm/index.js";
import {
deserializeArrayFromVector,
deserializeField,
serializeBufferArrayToVector,
} from "../../wasm/serialize.js";
import { Buffer } from "buffer";
import { BarretenbergWasm } from '../../wasm/index.js';
import { deserializeArrayFromVector, deserializeField, serializeBufferArrayToVector } from '../../wasm/serialize.js';
import { Buffer } from 'buffer';

/**
* Combines two 32-byte hashes.
Expand All @@ -13,17 +9,13 @@ import { Buffer } from "buffer";
* @param rhs - The second hash.
* @returns The new 32-byte hash.
*/
export function pedersenCompress(
wasm: BarretenbergWasm,
lhs: Uint8Array,
rhs: Uint8Array
): Buffer {
export function pedersenCompress(wasm: BarretenbergWasm, lhs: Uint8Array, rhs: Uint8Array): Buffer {
// If not done already, precompute constants.
wasm.call("pedersen__init");
wasm.call('pedersen__init');
// TODO check if lhs and rhs are <= 32 bytes?
wasm.writeMemory(0, lhs);
wasm.writeMemory(32, rhs);
wasm.call("pedersen__compress_fields", 0, 32, 64);
wasm.call('pedersen__compress_fields', 0, 32, 64);
return Buffer.from(wasm.getMemorySlice(64, 96));
}

Expand All @@ -34,15 +26,12 @@ export function pedersenCompress(
* @param rhs - The second hash.
* @returns The new 32-byte hash.
*/
export function pedersenCompressInputs(
wasm: BarretenbergWasm,
inputs: Buffer[]
): Buffer {
export function pedersenCompressInputs(wasm: BarretenbergWasm, inputs: Buffer[]): Buffer {
// If not done already, precompute constants.
wasm.call("pedersen__init");
wasm.call('pedersen__init');
const inputVectors = serializeBufferArrayToVector(inputs);
wasm.writeMemory(0, inputVectors);
wasm.call("pedersen__compress", 0, 0);
wasm.call('pedersen__compress', 0, 0);
return Buffer.from(wasm.getMemorySlice(0, 32));
}

Expand All @@ -53,16 +42,12 @@ export function pedersenCompressInputs(
* @param rhs - The second hash.
* @returns The new 32-byte hash.
*/
export function pedersenCompressWithHashIndex(
wasm: BarretenbergWasm,
inputs: Buffer[],
hashIndex: number
): Buffer {
export function pedersenCompressWithHashIndex(wasm: BarretenbergWasm, inputs: Buffer[], hashIndex: number): Buffer {
// If not done already, precompute constants.
wasm.call("pedersen__init");
wasm.call('pedersen__init');
const inputVectors = serializeBufferArrayToVector(inputs);
wasm.writeMemory(0, inputVectors);
wasm.call("pedersen__compress_with_hash_index", 0, 0, hashIndex);
wasm.call('pedersen__compress_with_hash_index', 0, 0, hashIndex);
return Buffer.from(wasm.getMemorySlice(0, 32));
}

Expand All @@ -74,11 +59,11 @@ export function pedersenCompressWithHashIndex(
*/
export function pedersenGetHash(wasm: BarretenbergWasm, data: Buffer): Buffer {
// If not done already, precompute constants.
wasm.call("pedersen__init");
const mem = wasm.call("bbmalloc", data.length);
wasm.call('pedersen__init');
const mem = wasm.call('bbmalloc', data.length);
wasm.writeMemory(mem, data);
wasm.call("pedersen__buffer_to_field", mem, data.length, 0);
wasm.call("bbfree", mem);
wasm.call('pedersen__buffer_to_field', mem, data.length, 0);
wasm.call('bbfree', mem);
return Buffer.from(wasm.getMemorySlice(0, 32));
}

Expand All @@ -93,20 +78,16 @@ export function pedersenGetHash(wasm: BarretenbergWasm, data: Buffer): Buffer {
*/
export function pedersenGetHashTree(wasm: BarretenbergWasm, values: Buffer[]) {
// If not done already, precompute constants.
wasm.call("pedersen__init");
wasm.call('pedersen__init');
const data = serializeBufferArrayToVector(values);
const inputPtr = wasm.call("bbmalloc", data.length);
const inputPtr = wasm.call('bbmalloc', data.length);
wasm.writeMemory(inputPtr, data);

const resultPtr = wasm.call("pedersen__hash_to_tree", inputPtr);
const resultNumFields = Buffer.from(
wasm.getMemorySlice(resultPtr, resultPtr + 4)
).readUInt32BE(0);
const resultData = Buffer.from(
wasm.getMemorySlice(resultPtr, resultPtr + 4 + resultNumFields * 32)
);
wasm.call("bbfree", inputPtr);
wasm.call("bbfree", resultPtr);
const resultPtr = wasm.call('pedersen__hash_to_tree', inputPtr);
const resultNumFields = Buffer.from(wasm.getMemorySlice(resultPtr, resultPtr + 4)).readUInt32BE(0);
const resultData = Buffer.from(wasm.getMemorySlice(resultPtr, resultPtr + 4 + resultNumFields * 32));
wasm.call('bbfree', inputPtr);
wasm.call('bbfree', resultPtr);

return deserializeArrayFromVector(deserializeField, resultData).elem;
}
41 changes: 17 additions & 24 deletions ts/src/wasm/barretenberg_wasm.test.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,46 @@
import { FileCrs, SRS_DEV_PATH } from "../crs/index.js";
import { BarretenbergWasm } from "./barretenberg_wasm.js";
import { FileCrs, SRS_DEV_PATH } from '../crs/index.js';
import { BarretenbergWasm } from './barretenberg_wasm.js';

describe("basic barretenberg smoke test", () => {
describe('basic barretenberg smoke test', () => {
const wasm: BarretenbergWasm = new BarretenbergWasm();

beforeAll(async () => {
await wasm.init();
});

it("should new malloc, transfer and slice mem", () => {
it('should new malloc, transfer and slice mem', () => {
const length = 1024;
const ptr = wasm.call("bbmalloc", length);
const ptr = wasm.call('bbmalloc', length);
const buf = Buffer.alloc(length, 128);
wasm.writeMemory(ptr, buf);
wasm.call("bbfree", ptr);
wasm.call('bbfree', ptr);
const result = Buffer.from(wasm.getMemorySlice(ptr, ptr + length));
expect(result).toStrictEqual(buf);
});

it("should use asyncify to do an async callback into js", async () => {
const addr1 = await wasm.asyncCall("test_async_func", 1024 * 1024, 1);
const addr2 = await wasm.asyncCall("test_async_func", 1024 * 1024 * 2, 2);
expect(
wasm.getMemorySlice(addr1, addr1 + 1024 * 1024).every((v) => v === 1)
).toBe(true);
expect(
wasm.getMemorySlice(addr2, addr2 + 1024 * 1024 * 2).every((v) => v === 2)
).toBe(true);
it('should use asyncify to do an async callback into js', async () => {
const addr1 = await wasm.asyncCall('test_async_func', 1024 * 1024, 1);
const addr2 = await wasm.asyncCall('test_async_func', 1024 * 1024 * 2, 2);
expect(wasm.getMemorySlice(addr1, addr1 + 1024 * 1024).every(v => v === 1)).toBe(true);
expect(wasm.getMemorySlice(addr2, addr2 + 1024 * 1024 * 2).every(v => v === 2)).toBe(true);
});

it("should correctly pass CRS data through env_load_verifier_crs", async () => {
it('should correctly pass CRS data through env_load_verifier_crs', async () => {
const crs = new FileCrs(0, SRS_DEV_PATH);
await crs.init();
const g2DataPtr = await wasm.asyncCall("test_env_load_verifier_crs");
const g2DataPtr = await wasm.asyncCall('test_env_load_verifier_crs');
const g2Data = wasm.getMemorySlice(g2DataPtr, g2DataPtr + 128);
expect(Buffer.from(g2Data)).toStrictEqual(crs.getG2Data());
wasm.call("bbfree", g2DataPtr);
wasm.call('bbfree', g2DataPtr);
});

it("should correctly pass CRS data through env_load_prover_crs", async () => {
it('should correctly pass CRS data through env_load_prover_crs', async () => {
const numPoints = 1024;
const crs = new FileCrs(numPoints, SRS_DEV_PATH);
await crs.init();
const g1DataPtr = await wasm.asyncCall(
"test_env_load_prover_crs",
numPoints
);
const g1DataPtr = await wasm.asyncCall('test_env_load_prover_crs', numPoints);
const g1Data = wasm.getMemorySlice(g1DataPtr, g1DataPtr + numPoints * 64);
expect(Buffer.from(g1Data)).toStrictEqual(crs.getG1Data());
wasm.call("bbfree", g1DataPtr);
wasm.call('bbfree', g1DataPtr);
});
});
Loading

0 comments on commit 63b580d

Please sign in to comment.