Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement bb.js browser tests (web-test-runner) #661

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ts/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"timeout": 5000
}
34 changes: 20 additions & 14 deletions ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aztec/bb.js",
"version": "0.3.6",
"version": "0.4.0",
"homepage": "https://github.com/AztecProtocol/aztec-packages/tree/master/circuits/cpp/barretenberg/ts",
"license": "MIT",
"type": "module",
Expand All @@ -9,8 +9,10 @@
"displayName": "bb.js",
"tsconfig": "./tsconfig.json"
},
"main": "./dest/index.js",
"bin": "./dest/main.js",
"main": "./dest/node/index.js",
"bin": {
"bb": "dest/node/main.js"
},
"files": [
"src/",
"dest/",
Expand All @@ -28,45 +30,49 @@
"serve": "webpack serve",
"formatting": "prettier --check ./src && eslint --max-warnings 0 ./src",
"formatting:fix": "prettier -w ./src",
"test": "yarn build:ts:browser && yarn build:ts:node && yarn test:jest && yarn test:bin",
"test:jest": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --no-cache --passWithNoTests",
"test": "yarn build && yarn test:browser && yarn test:node && yarn test:bin",
"test:browser": "web-test-runner",
"test:node": "NODE_NO_WARNINGS=1 mocha 'dest/node/**/*.test.js'",
"test:bin": "cd ./bin-test && ./bin-test.sh",
"test-debug": "NODE_NO_WARNINGS=1 node --inspect-brk=0.0.0.0 --experimental-vm-modules $(yarn bin jest) --no-cache --passWithNoTests --runInBand",
"test-debug": "NODE_NO_WARNINGS=1 node --inspect-brk=0.0.0.0 'dest/node/**/*.test.js'",
"prepack": "yarn build",
"deploy": "npm publish --access public"
},
"jest": {
"testRegex": "./dest/node/.*\\.test\\.js$",
"rootDir": "./dest"
},
"dependencies": {
"comlink": "^4.4.1",
"commander": "^10.0.1",
"debug": "^4.3.4",
"idb-keyval": "^6.2.1"
},
"devDependencies": {
"@jest/globals": "^29.4.3",
"@esm-bundle/chai": "^4.3.4-fix.0",
"@rollup/plugin-commonjs": "^25.0.4",
"@types/debug": "^4.1.7",
"@types/detect-node": "^2.0.0",
"@types/jest": "^29.4.0",
"@types/node": "^18.7.23",
"@types/source-map-support": "^0.5.6",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.54.1",
"@web/dev-server-esbuild": "^0.4.1",
"@web/dev-server-rollup": "^0.5.2",
"@web/test-runner": "^0.15.3",
"@web/test-runner-playwright": "^0.10.0",
"buffer": "^6.0.3",
"chai": "^4.3.7",
"copy-webpack-plugin": "^11.0.0",
"eslint": "^8.35.0",
"eslint-config-prettier": "^8.8.0",
"expect": "^29.6.3",
"html-webpack-plugin": "^5.5.1",
"jest": "^29.5.0",
"mocha": "^10.2.0",
"prettier": "^2.8.4",
"replace-in-file": "^7.0.1",
"resolve-typescript-plugin": "^2.0.1",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.2",
"tsconfig-paths-webpack-plugin": "^4.0.1",
"tslib": "^2.6.2",
"typescript": "^5.0.4",
"uvu": "^0.5.6",
"webpack": "^5.82.1",
"webpack-cli": "^5.1.1",
"webpack-dev-server": "^4.15.0"
Expand Down
40 changes: 40 additions & 0 deletions ts/src/barretenberg_api/blake2s.browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Fr } from '../types/index.js';
import { newBarretenbergApiSync } from '../factory/index.js';
import { BarretenbergApiSync } from './index.js';
import * as assert from 'uvu/assert';

describe('blake2s', () => {
let api: BarretenbergApiSync;

before(async () => {
api = await newBarretenbergApiSync();
});

after(async () => {
await api.destroy();
});

it('blake2s', () => {
const encoder = new TextEncoder();
const input = encoder.encode('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
const expected = new Uint8Array([
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c, 0x2a,
0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
]);
const result = api.blake2s(input);
assert.is(result, expected);
});

it('blake2sToField', () => {
const encoder = new TextEncoder();
const input = encoder.encode('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
const expected = Fr.fromBufferReduce(
new Uint8Array([
0x44, 0xdd, 0xdb, 0x39, 0xbd, 0xb2, 0xaf, 0x80, 0xc1, 0x47, 0x89, 0x4c, 0x1d, 0x75, 0x6a, 0xda, 0x3d, 0x1c,
0x2a, 0xc2, 0xb1, 0x00, 0x54, 0x1e, 0x04, 0xfe, 0x87, 0xb4, 0xa5, 0x9e, 0x12, 0x43,
]),
);
const result = api.blake2sToField(input);
assert.is(result, expected);
});
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { expect } from 'chai';
import { newBarretenbergApiSync } from '../factory/index.js';
import { Buffer32, Fr } from '../types/index.js';
import { BarretenbergApiSync } from './index.js';

describe('blake2s', () => {
let api: BarretenbergApiSync;

beforeAll(async () => {
before(async () => {
api = await newBarretenbergApiSync();
});

afterAll(async () => {
after(async () => {
await api.destroy();
});

Expand All @@ -22,7 +23,7 @@ describe('blake2s', () => {
]),
);
const result = api.blake2s(input);
expect(result).toEqual(expected);
expect(result).to.deep.equal(expected);
});

it('blake2sToField', () => {
Expand All @@ -34,6 +35,6 @@ describe('blake2s', () => {
]),
);
const result = api.blake2sToField(input);
expect(result).toEqual(expected);
expect(result).to.deep.equal(expected);
});
});
23 changes: 23 additions & 0 deletions ts/src/barretenberg_api/common.browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as assert from 'uvu/assert';
import { BarretenbergApiAsync, newBarretenbergApiAsync } from '../factory/index.js';

describe('env', () => {
let api: BarretenbergApiAsync;

before(async function () {
this.timeout(15000);
api = await newBarretenbergApiAsync(3);
});

after(async () => {
await api.destroy();
});

it('thread test', async () => {
// Main thread doesn't do anything in this test, so -1.
const threads = (await api.getNumThreads()) - 1;
const iterations = 100000;
const result = await api.testThreads(threads, iterations);
assert.is(result, iterations);
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { expect } from 'chai';
import { BarretenbergApiAsync, newBarretenbergApiAsync } from '../factory/index.js';

describe('env', () => {
let api: BarretenbergApiAsync;

beforeAll(async () => {
before(async function () {
this.timeout(15000);
api = await newBarretenbergApiAsync(3);
}, 15000);
});

afterAll(async () => {
after(async () => {
await api.destroy();
});

Expand All @@ -16,6 +18,6 @@ describe('env', () => {
const threads = (await api.getNumThreads()) - 1;
const iterations = 100000;
const result = await api.testThreads(threads, iterations);
expect(result).toBe(iterations);
expect(result).to.equal(iterations);
});
});
Original file line number Diff line number Diff line change
@@ -1,77 +1,92 @@
import { expect } from 'chai';
import { newBarretenbergApiSync } from '../factory/index.js';
import { Fr } from '../types/index.js';
import { BarretenbergApiSync } from './index.js';

describe('pedersen', () => {
let api: BarretenbergApiSync;

beforeAll(async () => {
before(async () => {
api = await newBarretenbergApiSync();
api.pedersenHashInit();
});

afterAll(async () => {
after(async () => {
await api.destroy();
});

it('pedersenCompressFields', () => {
const result = api.pedersenCompressFields(new Fr(4n), new Fr(8n));
expect(result).toEqual(new Fr(16672613430297770667465722499387909817686322516130512258122141976728892914370n));
expect(result).to.deep.equal(
new Fr(16672613430297770667465722499387909817686322516130512258122141976728892914370n),
);
});

it('pedersenPlookupCompressFields', () => {
const result = api.pedersenPlookupCompressFields(new Fr(4n), new Fr(8n));
expect(result).toEqual(new Fr(21568810706345846819294487214368613840251909831689369685420108292337497444070n));
expect(result).to.deep.equal(
new Fr(21568810706345846819294487214368613840251909831689369685420108292337497444070n),
);
});

it('pedersenCompress', () => {
const result = api.pedersenCompress([new Fr(4n), new Fr(8n), new Fr(12n)]);
expect(result).toEqual(new Fr(20749503715308760529311051818180468653739005441229560405092292242074298877245n));
expect(result).to.deep.equal(
new Fr(20749503715308760529311051818180468653739005441229560405092292242074298877245n),
);
});

it('pedersenPlookupCompress', () => {
const result = api.pedersenPlookupCompress([new Fr(4n), new Fr(8n), new Fr(12n)]);
expect(result).toEqual(new Fr(4213911891650716450883144878301329379460622830501147795631256054071351353887n));
expect(result).to.deep.equal(new Fr(4213911891650716450883144878301329379460622830501147795631256054071351353887n));
});

it('pedersenCompressWithHashIndex', () => {
const result = api.pedersenCompressWithHashIndex([new Fr(4n), new Fr(8n)], 7);
expect(result).toEqual(new Fr(11068631634751286805527305272746775861010877976108429785597565355072506728435n));
expect(result).to.deep.equal(
new Fr(11068631634751286805527305272746775861010877976108429785597565355072506728435n),
);
});

it('pedersenCommit', () => {
const result = api.pedersenCommit([new Fr(4n), new Fr(8n), new Fr(12n)]);
expect(result).toEqual(new Fr(20749503715308760529311051818180468653739005441229560405092292242074298877245n));
expect(result).to.deep.equal(
new Fr(20749503715308760529311051818180468653739005441229560405092292242074298877245n),
);
});

it('pedersenPlookupCommit', () => {
const result = api.pedersenPlookupCommit([new Fr(4n), new Fr(8n)]);
expect(result).toEqual(new Fr(21568810706345846819294487214368613840251909831689369685420108292337497444070n));
expect(result).to.deep.equal(
new Fr(21568810706345846819294487214368613840251909831689369685420108292337497444070n),
);
});

it('pedersenBufferToField', () => {
const result = api.pedersenBufferToField(Buffer.from('Hello world! I am a buffer to be converted to a field!'));
expect(result).toEqual(new Fr(4923399520610513632896240312051201308554838580477778325691012985962614653619n));
expect(result).to.deep.equal(new Fr(4923399520610513632896240312051201308554838580477778325691012985962614653619n));
});

it('pedersenHashPair', () => {
const result = api.pedersenHashPair(new Fr(4n), new Fr(8n));
expect(result).toEqual(new Fr(7508407170365331152493586290597472346478280823936748458450026785528968221772n));
expect(result).to.deep.equal(new Fr(7508407170365331152493586290597472346478280823936748458450026785528968221772n));
});

it('pedersenHashMultiple', () => {
const result = api.pedersenHashMultiple([new Fr(4n), new Fr(8n), new Fr(12n)]);
expect(result).toEqual(new Fr(641613987782189905475142047603559162464012327378197326488471789040703504911n));
expect(result).to.deep.equal(new Fr(641613987782189905475142047603559162464012327378197326488471789040703504911n));
});

it('pedersenHashMultipleWithHashIndex', () => {
const result = api.pedersenHashMultipleWithHashIndex([new Fr(4n), new Fr(8n)], 7);
expect(result).toEqual(new Fr(14181105996307540196932058280391669339364159586581375348016341320932872505408n));
expect(result).to.deep.equal(
new Fr(14181105996307540196932058280391669339364159586581375348016341320932872505408n),
);
});

it('pedersenHashToTree', () => {
const result = api.pedersenHashToTree([new Fr(4n), new Fr(8n), new Fr(12n), new Fr(16n)]);
expect(result).toEqual([
expect(result).to.deep.equal([
new Fr(4n),
new Fr(8n),
new Fr(12n),
Expand Down
Loading