From cfc722f33d5225d6c44d96bf2a8ac77747310230 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 20 Dec 2024 06:14:55 +0700 Subject: [PATCH] fix: trying to get non-simd to run during unit tests --- packages/as-sha256/src/index.ts | 37 +++++++--- packages/as-sha256/test/unit/getSimdTests.ts | 75 ++++++++++++++++++++ packages/as-sha256/test/unit/index.test.ts | 2 + packages/as-sha256/test/unit/noSimd.test.ts | 6 ++ packages/as-sha256/test/unit/simd.test.ts | 70 ++---------------- 5 files changed, 114 insertions(+), 76 deletions(-) create mode 100644 packages/as-sha256/test/unit/getSimdTests.ts create mode 100644 packages/as-sha256/test/unit/noSimd.test.ts diff --git a/packages/as-sha256/src/index.ts b/packages/as-sha256/src/index.ts index bdaafdea..f2374734 100644 --- a/packages/as-sha256/src/index.ts +++ b/packages/as-sha256/src/index.ts @@ -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); diff --git a/packages/as-sha256/test/unit/getSimdTests.ts b/packages/as-sha256/test/unit/getSimdTests.ts new file mode 100644 index 00000000..8ed6f229 --- /dev/null +++ b/packages/as-sha256/test/unit/getSimdTests.ts @@ -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]); + } + }); + } + }); +} diff --git a/packages/as-sha256/test/unit/index.test.ts b/packages/as-sha256/test/unit/index.test.ts index 895cfecb..241f7d90 100644 --- a/packages/as-sha256/test/unit/index.test.ts +++ b/packages/as-sha256/test/unit/index.test.ts @@ -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 = [ diff --git a/packages/as-sha256/test/unit/noSimd.test.ts b/packages/as-sha256/test/unit/noSimd.test.ts new file mode 100644 index 00000000..9b38ee99 --- /dev/null +++ b/packages/as-sha256/test/unit/noSimd.test.ts @@ -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); diff --git a/packages/as-sha256/test/unit/simd.test.ts b/packages/as-sha256/test/unit/simd.test.ts index a17726b1..acce45b3 100644 --- a/packages/as-sha256/test/unit/simd.test.ts +++ b/packages/as-sha256/test/unit/simd.test.ts @@ -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);