Skip to content

Commit

Permalink
fix: trying to get non-simd to run during unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil committed Dec 19, 2024
1 parent 2844ada commit cfc722f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 76 deletions.
37 changes: 26 additions & 11 deletions packages/as-sha256/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,39 @@ import {HashObject, byteArrayIntoHashObject, byteArrayToHashObject, hashObjectTo
import SHA256 from "./sha256.js";
export {HashObject, byteArrayToHashObject, hashObjectToByteArray, byteArrayIntoHashObject, SHA256};

let hasSimd = await simd();
if (process.env.DISABLE_SIMD === "true") {
hasSimd = false;
}
const hasSimd = await simd();

const ctx = newInstance(hasSimd);
const wasmInputValue = ctx.input.value;
const wasmOutputValue = ctx.output.value;
const inputUint8Array = new Uint8Array(ctx.memory.buffer, wasmInputValue, ctx.INPUT_LENGTH);
const outputUint8Array = new Uint8Array(ctx.memory.buffer, wasmOutputValue, ctx.PARALLEL_FACTOR * 32);
let ctx: WasmSimdContext;
let wasmInputValue: number;
let wasmOutputValue: number;
let inputUint8Array: Uint8Array;
let outputUint8Array: Uint8Array;
/** output uint8array, length 32, used to easily copy output data */
const outputUint8Array32 = new Uint8Array(ctx.memory.buffer, wasmOutputValue, 32);
const inputUint32Array = new Uint32Array(ctx.memory.buffer, wasmInputValue, ctx.INPUT_LENGTH);
let outputUint8Array32: Uint8Array;
let inputUint32Array: Uint32Array;

function initializeInstance(useSimd: boolean): void {
ctx = newInstance(useSimd) as WasmSimdContext;
wasmInputValue = ctx.input.value;
wasmOutputValue = ctx.output.value;
inputUint8Array = new Uint8Array(ctx.memory.buffer, wasmInputValue, ctx.INPUT_LENGTH);
outputUint8Array = new Uint8Array(ctx.memory.buffer, wasmOutputValue, ctx.PARALLEL_FACTOR * 32);
/** output uint8array, length 32, used to easily copy output data */
outputUint8Array32 = new Uint8Array(ctx.memory.buffer, wasmOutputValue, 32);
inputUint32Array = new Uint32Array(ctx.memory.buffer, wasmInputValue, ctx.INPUT_LENGTH);
}

initializeInstance(hasSimd);

export function simdEnabled(): boolean {
return Boolean(ctx.HAS_SIMD.valueOf());
}

export function reinitializeInstance(useSimd: boolean): boolean {
initializeInstance(useSimd);
return simdEnabled();
}

export function digest(data: Uint8Array): Uint8Array {
if (data.length === 64) {
return digest64(data);
Expand Down
75 changes: 75 additions & 0 deletions packages/as-sha256/test/unit/getSimdTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import {expect} from "chai";
import crypto from "crypto";
import {byteArrayToHashObject, hashObjectToByteArray} from "../../src/hashObject.js";

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function getSimdTests(sha256: any, hasSimd: boolean): void {
describe(`Test as-sha256 ${hasSimd ? "with SIMD" : "without SIMD"}`, () => {
before(function () {
expect(sha256.simdEnabled()).to.equal(hasSimd);
});
it("testHash4UintArray64s", () => {
const input1 = "gajindergajindergajindergajinder";
const input2 = "gajindergajindergajindergajinder";
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,
]);
for (let i = 0; i < 4; i++) {
expect(outputs[i]).to.be.deep.equal(expectedOutput, "incorrect batchHash4UintArray64s result " + i);
}
});

it("testHash4UintArray64s 1000 times", () => {
for (let i = 0; i < 1000; i++) {
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);
}
});

it("testHash4HashObjectInputs", () => {
const input1 = "gajindergajindergajindergajinder";
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,
]);
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);
}
});

const numHashes = [4, 5, 6, 7];
for (const numHash of numHashes) {
it(`hashInto ${numHash} hashes`, () => {
const inputs = Array.from({length: numHash}, () => crypto.randomBytes(64));
const input = new Uint8Array(numHash * 64);
for (let i = 0; i < numHash; i++) {
input.set(inputs[i], i * 64);
}
const output = new Uint8Array(numHash * 32);

sha256.hashInto(input, output);

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]);
}
});
}
});
}
2 changes: 2 additions & 0 deletions packages/as-sha256/test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Buffer} from "buffer";
import {expect} from "chai";
import * as sha256 from "../../src/index.js";

sha256.reinitializeInstance(true);

describe("as-sha256", function () {
describe("digest()", function () {
const digestTestCases = [
Expand Down
6 changes: 6 additions & 0 deletions packages/as-sha256/test/unit/noSimd.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as sha256 from "../../src/index.js";
import {getSimdTests} from "./getSimdTests.js";

const useSimd = false;
sha256.reinitializeInstance(useSimd);
getSimdTests(sha256, useSimd);
70 changes: 5 additions & 65 deletions packages/as-sha256/test/unit/simd.test.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,6 @@
import {expect} from "chai";
import crypto from "crypto";
import {byteArrayToHashObject, hashObjectToByteArray} from "../../src/hashObject";
import * as sha256 from "../../src";
import * as sha256 from "../../src/index.js";
import {getSimdTests} from "./getSimdTests.js";

describe("Test SIMD implementation of as-sha256", () => {
it("testHash4UintArray64s", () => {
const input1 = "gajindergajindergajindergajinder";
const input2 = "gajindergajindergajindergajinder";
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,
]);
for (let i = 0; i < 4; i++) {
expect(outputs[i]).to.be.deep.equal(expectedOutput, "incorrect batchHash4UintArray64s result " + i);
}
});

it("testHash4UintArray64s 1000 times", () => {
for (let i = 0; i < 1000; i++) {
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);
}
});

it("testHash4HashObjectInputs", () => {
const input1 = "gajindergajindergajindergajinder";
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,
]);
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);
}
});

const numHashes = [4, 5, 6, 7];
for (const numHash of numHashes) {
it(`hashInto ${numHash} hashes`, () => {
const inputs = Array.from({length: numHash}, () => crypto.randomBytes(64));
const input = new Uint8Array(numHash * 64);
for (let i = 0; i < numHash; i++) {
input.set(inputs[i], i * 64);
}
const output = new Uint8Array(numHash * 32);

sha256.hashInto(input, output);

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]);
}
});
}
});
const useSimd = true;
sha256.reinitializeInstance(useSimd);
getSimdTests(sha256, useSimd);

0 comments on commit cfc722f

Please sign in to comment.