Skip to content

Commit

Permalink
Merge 6b301d4 into e58781f
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain authored Nov 11, 2024
2 parents e58781f + 6b301d4 commit d786006
Show file tree
Hide file tree
Showing 98 changed files with 3,047 additions and 1,488 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ jobs:
run: yarn check-types
- name: Lint
run: yarn lint
- name: Tests
run: yarn test
- name: Unit Tests
run: yarn test:unit
- name: Browsers Tests
run: yarn test:browsers

# Download spec tests with cache
- name: Restore spec tests cache
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"lint": "lerna run lint",
"check-types": "lerna run check-types",
"coverage": "lerna run coverage",
"test:unit": "lerna run test:unit",
"test:browsers": "lerna run test:browsers",
"test": "lerna run test",
"benchmark:files": "NODE_OPTIONS=--max_old_space_size=4096 benchmark --config .benchrc.yaml",
"benchmark": "yarn benchmark:files 'packages/*/test/perf/**/*.test.ts'",
Expand All @@ -31,27 +33,24 @@
"@types/node": "^14.14.17",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"@vitest/browser": "^2.1.4",
"@vitest/coverage-v8": "^2.1.4",
"babel-loader": "^8.2.2",
"chai": "^4.3.4",
"eslint": "^7.30.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-no-only-tests": "^2.4.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.3.1",
"karma": "^6.3.16",
"karma-babel-preprocessor": "^8.0.1",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^3.1.0",
"karma-firefox-launcher": "^1.2.0",
"karma-mocha": "^1.3.0",
"karma-webpack": "^5.0.0",
"lerna": "^8.1.3",
"mocha": "^8.3.2",
"nyc": "^15.0.0",
"prettier": "^2.2.1",
"ts-loader": "^9.2.8",
"ts-node": "^9.1.1",
"typescript": "~4.2.3",
"vite-plugin-node-polyfills": "^0.22.0",
"vitest": "^2.1.4",
"webdriverio": "^9.2.11",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^3.11.2"
Expand Down
46 changes: 0 additions & 46 deletions packages/as-sha256/karma.config.js

This file was deleted.

7 changes: 4 additions & 3 deletions packages/as-sha256/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
"build:lib": "tsc -p tsconfig.build.json",
"build:web": "webpack --mode production --entry ./index.js --output ./dist/as-sha256.min.js",
"test": "yarn run test:unit",
"test:unit": "yarn run test:unit:node && yarn run test:unit:browser",
"test:unit:node": "mocha -r ts-node/register test/unit/*.test.ts",
"test:unit:browser": "karma start --single-run --browsers ChromeHeadless,FirefoxHeadless karma.config.js",
"test:unit": "vitest run --dir test/unit/",
"test:browsers": "yarn test:browsers:chrome && yarn test:browsers:firefox",
"test:browsers:chrome": "vitest run --browser chrome --config ./vitest.browser.config.ts --dir test/unit",
"test:browsers:firefox": "vitest run --browser firefox --config ./vitest.browser.config.ts --dir test/unit",
"benchmark": "node -r ts-node/register ./node_modules/.bin/benchmark 'test/perf/*.test.ts'",
"benchmark:local": "yarn benchmark --local",
"test:ci": "yarn test:as-ci"
Expand Down
3 changes: 2 additions & 1 deletion packages/as-sha256/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {allocUnsafe} from "./alloc";
import {newInstance} from "./wasm";
import {HashObject, byteArrayIntoHashObject, byteArrayToHashObject, hashObjectToByteArray} from "./hashObject";
import type {HashObject} from "./hashObject";
import {byteArrayIntoHashObject, byteArrayToHashObject, hashObjectToByteArray} from "./hashObject";
import SHA256 from "./sha256";
export {HashObject, byteArrayToHashObject, hashObjectToByteArray, byteArrayIntoHashObject, SHA256};

Expand Down
20 changes: 10 additions & 10 deletions packages/as-sha256/test/unit/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import {describe, it, expect} from "vitest";
import {Buffer} from "buffer";
import * as sha256 from "../../src";
import {expect} from "chai";

describe("sha256", function () {
describe("digest function", function () {
it("abc", function () {
const input = Buffer.from("abc", "utf8");
expect(Buffer.from(sha256.digest(input)).toString("hex")).to.be.equal(
expect(Buffer.from(sha256.digest(input)).toString("hex")).toEqual(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
});

it("empty string", function () {
const input = Buffer.from("", "utf8");
expect(Buffer.from(sha256.digest(input)).toString("hex")).to.be.equal(
expect(Buffer.from(sha256.digest(input)).toString("hex")).toEqual(
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
);
});

it("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", function () {
const input = Buffer.from("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "utf8");
expect(Buffer.from(sha256.digest(input)).toString("hex")).to.be.equal(
expect(Buffer.from(sha256.digest(input)).toString("hex")).toEqual(
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
);
});
Expand All @@ -30,7 +30,7 @@ describe("sha256", function () {
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
"utf8"
);
expect(Buffer.from(sha256.digest(input)).toString("hex")).to.be.equal(
expect(Buffer.from(sha256.digest(input)).toString("hex")).toEqual(
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
);
});
Expand All @@ -45,7 +45,7 @@ describe("sha256", function () {
131, 72, 178, 215, 235, 20, 207, 110,
]);
expect(output).to.be.deep.equal(expectedOutput, "incorrect digest64 result");
expect(Buffer.from(output).toString("hex")).to.be.equal(
expect(Buffer.from(output).toString("hex")).toEqual(
"be39380ff1d0261e6f37dafe4278b662ef611f1cb2f7c0a18348b2d7eb14cf6e"
);
// digestTwoHashObjects should be the same to digest64
Expand All @@ -57,7 +57,7 @@ describe("sha256", function () {
const output2 = new Uint8Array(32);
sha256.hashObjectToByteArray(obj, output2, 0);
for (let i = 0; i < 32; i++) {
expect(output2[i]).to.be.equal(output[i], "failed at index" + i);
expect(output2[i]).toEqual(output[i], "failed at index" + i);
}
expect(output2).to.be.deep.equal(expectedOutput, "incorrect digestTwoHashObjects result");

Expand All @@ -69,16 +69,16 @@ describe("sha256", function () {
const input = Buffer.from("harkamalharkamalharkamalharkamalharkamalharkamalharkamalharkamal", "utf8");
const output = Buffer.from(sha256.digest(input)).toString("hex");
const output64 = Buffer.from(sha256.digest64(input)).toString("hex");
expect(output).to.be.equal(output64);
expect(output).toEqual(output64);
});

it("1024 digest test", function () {
const inputStr = "12345678".repeat(128);
const input = Buffer.from(inputStr, "utf8");
expect(input.length).to.be.equal(1024);
expect(input.length).toEqual(1024);

const output = Buffer.from(sha256.digest(input)).toString("hex");
expect(output).to.be.equal("54c7cb8a82d68145fd5f5da4103f5a66f422dbea23d9fc9f40f59b1dcf5403a9");
expect(output).toEqual("54c7cb8a82d68145fd5f5da4103f5a66f422dbea23d9fc9f40f59b1dcf5403a9");
});
});
});
Expand Down
24 changes: 12 additions & 12 deletions packages/as-sha256/test/unit/simd.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect} from "chai";
import {describe, it, expect} from "vitest";
import crypto from "crypto";
import {byteArrayToHashObject, hashObjectToByteArray} from "../../src/hashObject";
import * as sha256 from "../../src";
Expand All @@ -10,11 +10,11 @@ describe("Test SIMD implementation of as-sha256", () => {
const input = Buffer.from(input1 + input2, "utf8");
const outputs = sha256.batchHash4UintArray64s([input, input, input, input]);
const expectedOutput = new Uint8Array([
190, 57, 56, 15, 241, 208, 38, 30, 111, 55, 218, 254, 66, 120, 182, 98, 239, 97, 31, 28, 178, 247, 192, 161,
131, 72, 178, 215, 235, 20, 207, 110,
190, 57, 56, 15, 241, 208, 38, 30, 111, 55, 218, 254, 66, 120, 182, 98, 239, 97, 31, 28, 178, 247, 192, 161, 131,
72, 178, 215, 235, 20, 207, 110,
]);
for (let i = 0; i < 4; i++) {
expect(outputs[i]).to.be.deep.equal(expectedOutput, "incorrect batchHash4UintArray64s result " + i);
expect(outputs[i]).toEqualWithMessage(expectedOutput, "incorrect batchHash4UintArray64s result " + i);
}
});

Expand All @@ -23,10 +23,10 @@ describe("Test SIMD implementation of as-sha256", () => {
const input = crypto.randomBytes(64);
const outputs = sha256.batchHash4UintArray64s([input, input, input, input]);
const expectedOutput = sha256.digest64(input);
expect(outputs[0]).to.be.deep.equal(expectedOutput);
expect(outputs[1]).to.be.deep.equal(expectedOutput);
expect(outputs[2]).to.be.deep.equal(expectedOutput);
expect(outputs[3]).to.be.deep.equal(expectedOutput);
expect(outputs[0]).toEqual(expectedOutput);
expect(outputs[1]).toEqual(expectedOutput);
expect(outputs[2]).toEqual(expectedOutput);
expect(outputs[3]).toEqual(expectedOutput);
}
});

Expand All @@ -35,13 +35,13 @@ describe("Test SIMD implementation of as-sha256", () => {
const inputHashObject = byteArrayToHashObject(Buffer.from(input1, "utf8"), 0);
const outputs = sha256.batchHash4HashObjectInputs(Array.from({length: 8}, () => inputHashObject));
const expectedOutput = new Uint8Array([
190, 57, 56, 15, 241, 208, 38, 30, 111, 55, 218, 254, 66, 120, 182, 98, 239, 97, 31, 28, 178, 247, 192, 161,
131, 72, 178, 215, 235, 20, 207, 110,
190, 57, 56, 15, 241, 208, 38, 30, 111, 55, 218, 254, 66, 120, 182, 98, 239, 97, 31, 28, 178, 247, 192, 161, 131,
72, 178, 215, 235, 20, 207, 110,
]);
for (let i = 0; i < 4; i++) {
const output = new Uint8Array(32);
hashObjectToByteArray(outputs[i], output, 0);
expect(output).to.be.deep.equal(expectedOutput, "incorrect batchHash4UintArray64s result " + i);
expect(output).toEqualWithMessage(expectedOutput, "incorrect batchHash4UintArray64s result " + i);
}
});

Expand All @@ -59,7 +59,7 @@ describe("Test SIMD implementation of as-sha256", () => {

const expectedOutputs = Array.from({length: numHash}, (_, i) => sha256.digest64(inputs[i]));
for (let i = 0; i < numHash; i++) {
expect(output.subarray(i * 32, (i + 1) * 32)).to.be.deep.equal(expectedOutputs[i]);
expect(output.subarray(i * 32, (i + 1) * 32)).toEqual(expectedOutputs[i]);
}
});
}
Expand Down
4 changes: 4 additions & 0 deletions packages/as-sha256/vitest.browser.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {defineConfig, mergeConfig} from "vitest/config";
import vitestConfig from "../../vitest.base.browser.config";

export default mergeConfig(vitestConfig, defineConfig({}));
4 changes: 4 additions & 0 deletions packages/as-sha256/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {defineConfig, mergeConfig} from "vitest/config";
import baseConfig from "../../vitest.base.unit.config";

export default mergeConfig(baseConfig, defineConfig({}));
2 changes: 1 addition & 1 deletion packages/persistent-merkle-tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"benchmark:files": "node --max-old-space-size=4096 --expose-gc -r ts-node/register ../../node_modules/.bin/benchmark",
"benchmark": "yarn benchmark:files 'test/perf/*.test.ts'",
"benchmark:local": "yarn benchmark --local",
"test": "mocha -r ts-node/register 'test/unit/**/*.test.ts'"
"test": "vitest run --dir test/unit"
},
"pre-push": [
"lint"
Expand Down
7 changes: 3 additions & 4 deletions packages/persistent-merkle-tree/test/unit/backing.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {assert} from "chai";
import {describe, it} from "mocha";
import {describe, it, expect} from "vitest";
import {Tree, LeafNode, subtreeFillToDepth} from "../../src";

describe("get", () => {
it("should return the right node", () => {
const n = subtreeFillToDepth(LeafNode.fromRoot(Buffer.alloc(32, 1)), 3);
const backing = new Tree(n);
assert.deepEqual(backing.getRoot(8n), Buffer.alloc(32, 1));
expect(backing.getRoot(8n)).toEqual(Uint8Array.from(Buffer.alloc(32, 1)));
});
});

Expand All @@ -16,6 +15,6 @@ describe("set", () => {
const n2 = LeafNode.fromRoot(Buffer.alloc(32, 2));
const backing = new Tree(n);
backing.setNode(15n, n2);
assert.deepEqual(backing.getRoot(15n), Buffer.alloc(32, 2));
expect(backing.getRoot(15n)).toEqual(Uint8Array.from(Buffer.alloc(32, 2)));
});
});
13 changes: 6 additions & 7 deletions packages/persistent-merkle-tree/test/unit/gindex.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {assert, expect} from "chai";
import {describe, it} from "mocha";
import {describe, it, expect} from "vitest";
import {
countToDepth,
bitIndexBigInt,
Expand Down Expand Up @@ -28,7 +27,7 @@ describe("countToDepth", () => {
for (const [count, depth] of testCases) {
it(`should correctly get depth for ${count} elements`, () => {
const actual = countToDepth(BigInt(count));
assert.equal(actual, depth);
expect(actual).toEqual(depth);
});
}
});
Expand All @@ -49,7 +48,7 @@ describe("bigIndexBigInt", () => {
for (const [gindex, depth] of testCases) {
it(`should correctly get depth for gindex ${gindex}`, () => {
const actual = bitIndexBigInt(BigInt(gindex));
assert.equal(actual, depth);
expect(actual).toEqual(depth);
});
}
});
Expand All @@ -73,7 +72,7 @@ describe("iterateAtDepth", () => {
const actual = Array.from(iterateAtDepth(input[0], input[1], input[2]));
expect(actual).to.deep.equal(expected);
const gindicesActual = getGindicesAtDepth(input[0], Number(input[1]), Number(input[2]));
expect(gindicesActual).to.deep.equal(expected);
expect(gindicesActual).toEqual(expected);
});
}
});
Expand All @@ -91,7 +90,7 @@ describe("concatGindices", () => {
for (const {input, expected} of testCases) {
it("should correctly concatenate gindices", () => {
const actual = concatGindices(input);
expect(actual).to.equal(expected);
expect(actual).toEqual(expected);
});
}
});
Expand All @@ -111,7 +110,7 @@ describe("gindexIterator", () => {
const actual = Array.from(gindexIterator(input));
expect(actual).to.deep.equal(expected);
const arrActual = getGindexBits(input);
expect(arrActual).to.deep.equal(expected);
expect(arrActual).toEqual(expected);
});
}
});
Loading

0 comments on commit d786006

Please sign in to comment.