-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: trying to get non-simd to run during unit tests
- Loading branch information
1 parent
2844ada
commit cfc722f
Showing
5 changed files
with
114 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
}); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |