Skip to content

Commit

Permalink
chore: rename apis
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Apr 23, 2024
1 parent d7d8b6a commit ac4c0a1
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 42 deletions.
6 changes: 3 additions & 3 deletions packages/as-sha256/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export function digest64(inPtr: usize, outPtr: usize): void {
* remaining 48 items are computed inside hashBlocksV128 loop.
* @param outPtr
*/
export function hash4Input64s(outPtr: usize): void {
export function hash4UintArray64s(outPtr: usize): void {
for (i = 0; i < 16; i++) {
store32(wPtr, PARALLEL_FACTOR * i, load32be(inputPtr, i));
store32(wPtr, PARALLEL_FACTOR * i + 1, load32be(inputPtr, i + 16));
Expand All @@ -333,7 +333,7 @@ export function hash4Input64s(outPtr: usize): void {
}

/*
* Hash 8 hash objects which are 4 inputs of 64 bytes each similar to hash4Input64s
* Hash 4 HashObject inputs, 64 bytes each similar to hash4UintArray64s
*
* Input pointer is 64 u32 (256 bytes) as below:
* input 0 input 1 input 2 input 3
Expand All @@ -352,7 +352,7 @@ export function hash4Input64s(outPtr: usize): void {
* remaining 48 items are computed inside hashBlocksV128 loop.
*
*/
export function hash8HashObjects(outPtr: usize): void {
export function hash4HashObjectInputs(outPtr: usize): void {
for (i = 0; i < 16 * PARALLEL_FACTOR; i++) {
store32(wPtr, i, load32be(inputPtr, i));
}
Expand Down
Binary file modified packages/as-sha256/build/optimized.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions packages/as-sha256/build/optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@
(export "final" (func $assembly/index/final))
(export "digest" (func $assembly/index/digest))
(export "digest64" (func $assembly/index/digest64))
(export "hash4Input64s" (func $assembly/index/hash4Input64s))
(export "hash8HashObjects" (func $assembly/index/hash8HashObjects))
(export "hash4UintArray64s" (func $assembly/index/hash4UintArray64s))
(export "hash4HashObjectInputs" (func $assembly/index/hash4HashObjectInputs))
(export "memory" (memory $0))
(start $~start)
(func $~lib/rt/tlsf/Root#set:flMap (param $0 i32) (param $1 i32)
Expand Down Expand Up @@ -5751,7 +5751,7 @@
call $~lib/polyfills/bswap<i32>
i32.store offset=124
)
(func $assembly/index/hash4Input64s (param $0 i32)
(func $assembly/index/hash4UintArray64s (param $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
Expand Down Expand Up @@ -5948,7 +5948,7 @@
local.get $0
call $assembly/simd/digest64V128
)
(func $assembly/index/hash8HashObjects (param $0 i32)
(func $assembly/index/hash4HashObjectInputs (param $0 i32)
(local $1 i32)
(local $2 i32)
i32.const 0
Expand Down
Binary file modified packages/as-sha256/build/untouched.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions packages/as-sha256/build/untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
(export "final" (func $assembly/index/final))
(export "digest" (func $assembly/index/digest))
(export "digest64" (func $assembly/index/digest64))
(export "hash4Input64s" (func $assembly/index/hash4Input64s))
(export "hash8HashObjects" (func $assembly/index/hash8HashObjects))
(export "hash4UintArray64s" (func $assembly/index/hash4UintArray64s))
(export "hash4HashObjectInputs" (func $assembly/index/hash4HashObjectInputs))
(export "memory" (memory $0))
(start $~start)
(func $~lib/rt/tlsf/Root#set:flMap (param $0 i32) (param $1 i32)
Expand Down Expand Up @@ -10635,7 +10635,7 @@
local.get $180
i32.store
)
(func $assembly/index/hash4Input64s (param $0 i32)
(func $assembly/index/hash4UintArray64s (param $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
Expand Down Expand Up @@ -11106,7 +11106,7 @@
local.get $0
call $assembly/simd/digest64V128
)
(func $assembly/index/hash8HashObjects (param $0 i32)
(func $assembly/index/hash4HashObjectInputs (param $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
Expand Down
22 changes: 12 additions & 10 deletions packages/as-sha256/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ export function digest64HashObjects(obj1: HashObject, obj2: HashObject): HashObj

/**
* Hash 4 Uint8Array objects in parallel, each 64 length as below
* Inputs: 0 1 2 3 4 5 6 7
* \ / \ / \ / \ /
* Outputs: 0 1 2 3
*
* Inputs: i0 i1 i2 i3 i4 i5 i6 i7
* \ / \ / \ / \ /
* Outputs: o0 o1 o2 o3
*/
export function hash4Input64s(inputs: Uint8Array[]): Uint8Array[] {
export function hash4UintArray64s(inputs: Uint8Array[]): Uint8Array[] {
if (inputs.length !== 4) {
throw new Error("Input length must be 4");
}
Expand All @@ -102,7 +103,7 @@ export function hash4Input64s(inputs: Uint8Array[]): Uint8Array[] {
inputUint8Array.set(inputs[2], 128);
inputUint8Array.set(inputs[3], 192);

ctx.hash4Input64s(wasmOutputValue);
ctx.hash4UintArray64s(wasmOutputValue);

const output0 = outputUint8Array.slice(0, 32);
const output1 = outputUint8Array.slice(32, 64);
Expand All @@ -113,14 +114,15 @@ export function hash4Input64s(inputs: Uint8Array[]): Uint8Array[] {
}

/**
* Hash 8 HashObjects in parallel:
* - input${i} has h0 to h7, each 4 bytes which make it 32 bytes
* Hash 4 HashObject inputs in parallel
* - Each input (inputs{i}) is 4 bytes which make it 32 bytes
* - Each HashObject input contains 2 HashObjects which is 64 bytes similar to hash4UintArray64s
*
* Inputs h0 h1 h2 h3 h4 h5 h6 h7
* Inputs i0 i1 i2 i3 i4 i5 i6 i7
* \ / \ / \ / \ /
* Outputs o0 o1 o2 o3
*/
export function hash8HashObjects(inputs: HashObject[]): HashObject[] {
export function hash4HashObjectInputs(inputs: HashObject[]): HashObject[] {
if (inputs.length !== 8) {
throw new Error("Input length must be 8");
}
Expand Down Expand Up @@ -223,7 +225,7 @@ export function hash8HashObjects(inputs: HashObject[]): HashObject[] {
inputUint32Array[62] = inputs[5].h7;
inputUint32Array[63] = inputs[7].h7;

ctx.hash8HashObjects(wasmOutputValue);
ctx.hash4HashObjectInputs(wasmOutputValue);

const output0 = byteArrayToHashObject(outputUint8Array.subarray(0, 32));
const output1 = byteArrayToHashObject(outputUint8Array.subarray(32, 64));
Expand Down
4 changes: 2 additions & 2 deletions packages/as-sha256/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export interface WasmContext {

digest(length: number): void;
digest64(inPtr: number, outPtr: number): void;
hash4Input64s(outPtr: number): void;
hash8HashObjects(outPtr: number): void;
hash4UintArray64s(outPtr: number): void;
hash4HashObjectInputs(outPtr: number): void;
}

const importObj = {
Expand Down
2 changes: 1 addition & 1 deletion packages/as-sha256/src/wasmCode.ts

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions packages/as-sha256/test/perf/simd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {byteArrayToHashObject} from "../../src/hashObject";
/**
* Mac M1 Apr 2024
*
digest64 vs hash4Input64s vs hash8HashObjects
digest64 vs hash4UintArray64s vs hash4HashObjectInputs
✓ digest64 200092 times 6.816078 ops/s 146.7119 ms/op - 66 runs 10.3 s
✓ hash 200092 times using hash4Input64s 8.093460 ops/s 123.5566 ms/op - 78 runs 10.2 s
✓ hash 200092 times using hash8HashObjects 8.141334 ops/s 122.8300 ms/op - 78 runs 10.2 s
✓ hash 200092 times using hash4UintArray64s 8.093460 ops/s 123.5566 ms/op - 78 runs 10.2 s
✓ hash 200092 times using hash4HashObjectInputs 8.141334 ops/s 122.8300 ms/op - 78 runs 10.2 s
*/
describe("digest64 vs hash4Input64s vs hash8HashObjects", function () {
describe("digest64 vs hash4UintArray64s vs hash4HashObjectInputs", function () {
this.timeout(0);

setBenchOpts({
Expand All @@ -24,19 +24,19 @@ describe("digest64 vs hash4Input64s vs hash8HashObjects", function () {
for (let j = 0; j < iterations * 4; j++) sha256.digest64(input);
});

// hash4Input64s do 4 sha256 in parallel
itBench(`hash ${iterations * 4} times using hash4Input64s`, () => {
// hash4UintArray64s do 4 sha256 in parallel
itBench(`hash ${iterations * 4} times using hash4UintArray64s`, () => {
for (let j = 0; j < iterations; j++) {
sha256.hash4Input64s([input, input, input, input]);
sha256.hash4UintArray64s([input, input, input, input]);
}
});

const hashObject = byteArrayToHashObject(Buffer.from("gajindergajindergajindergajinder", "utf8"));
const hashInputs = Array.from({length: 8}, () => hashObject);
// hash8HashObjects do 4 sha256 in parallel
itBench(`hash ${iterations * 4} times using hash8HashObjects`, () => {
// hash4HashObjectInputs do 4 sha256 in parallel
itBench(`hash ${iterations * 4} times using hash4HashObjectInputs`, () => {
for (let j = 0; j < iterations; j++) {
sha256.hash8HashObjects(hashInputs);
sha256.hash4HashObjectInputs(hashInputs);
}
});
});
16 changes: 8 additions & 8 deletions packages/as-sha256/test/unit/simd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import {byteArrayToHashObject, hashObjectToByteArray} from "../../src/hashObject
import * as sha256 from "../../src";

describe("Test SIMD implementation of as-sha256", () => {
it("testHash4Input64s", () => {
it("testHash4UintArray64s", () => {
const input1 = "gajindergajindergajindergajinder";
const input2 = "gajindergajindergajindergajinder";
const input = Buffer.from(input1 + input2, "utf8");
const outputs = sha256.hash4Input64s([input, input, input, input]);
const outputs = sha256.hash4UintArray64s([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 hash4Input64s result " + i);
expect(outputs[i]).to.be.deep.equal(expectedOutput, "incorrect hash4UintArray64s result " + i);
}
});

it("testHash4Input64s 1000 times", () => {
it("testHash4UintArray64s 1000 times", () => {
for (let i = 0; i < 1000; i++) {
const input = crypto.randomBytes(64);
const outputs = sha256.hash4Input64s([input, input, input, input]);
const outputs = sha256.hash4UintArray64s([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);
Expand All @@ -30,18 +30,18 @@ describe("Test SIMD implementation of as-sha256", () => {
}
});

it("testHash4HashObjects", () => {
it("testHash4HashObjectInputs", () => {
const input1 = "gajindergajindergajindergajinder";
const inputHashObject = byteArrayToHashObject(Buffer.from(input1, "utf8"));
const outputs = sha256.hash8HashObjects(Array.from({length: 8}, () => inputHashObject));
const outputs = sha256.hash4HashObjectInputs(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 hash4Input64s result " + i);
expect(output).to.be.deep.equal(expectedOutput, "incorrect hash4UintArray64s result " + i);
}
});
});

0 comments on commit ac4c0a1

Please sign in to comment.