Wasm-compat for keccak512 & sha256 (#1216)

* Wasm-compa for keccak512 & sha256

* wasm/non-wasm tests

* linting

* Bump wasm

* formatting
This commit is contained in:
Jaco
2021-11-19 14:59:59 +02:00
committed by GitHub
parent 88580b98a6
commit 085862ce79
9 changed files with 144 additions and 37 deletions
+1 -1
View File
@@ -28,7 +28,7 @@
"@babel/runtime": "^7.16.3",
"@polkadot/networks": "7.8.3-36",
"@polkadot/util": "7.8.3-36",
"@polkadot/wasm-crypto": "^4.2.1",
"@polkadot/wasm-crypto": "^4.2.2-17",
"@polkadot/x-randomvalues": "7.8.3-36",
"blakejs": "^1.1.1",
"bn.js": "^4.12.0",
@@ -58,4 +58,26 @@ describe('blake2AsU8a', (): void => {
expect(a).toEqual(b);
});
it.skip.each([false, true])('performance: onlyJs=%p', (onlyJs): void => {
const label = `blake2AsU8a:onlyJs=${onlyJs.toString()}`;
const test = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8
]);
console.time(label);
for (let i = 0; i < 32768; i++) {
blake2AsU8a(test);
}
console.timeEnd(label);
});
});
@@ -42,4 +42,26 @@ describe('keccakAsU8a', (): void => {
});
});
});
it.skip.each([false, true])('performance: onlyJs=%p', (onlyJs): void => {
const label = `keccakAsU8a:onlyJs=${onlyJs.toString()}`;
const test = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8
]);
console.time(label);
for (let i = 0; i < 128000; i++) {
keccakAsU8a(test, undefined, onlyJs);
}
console.timeEnd(label);
});
});
+7 -4
View File
@@ -6,7 +6,7 @@ import type { HexString } from '@polkadot/util/types';
import js from 'js-sha3';
import { u8aToU8a } from '@polkadot/util';
import { isReady, keccak256 } from '@polkadot/wasm-crypto';
import { isReady, keccak256, keccak512 } from '@polkadot/wasm-crypto';
/**
* @name keccakAsU8a
@@ -24,10 +24,13 @@ import { isReady, keccak256 } from '@polkadot/wasm-crypto';
*/
export function keccakAsU8a (value: HexString | Buffer | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs = false): Uint8Array {
const is256 = bitLength === 256;
const u8a = u8aToU8a(value);
return isReady() && is256 && !onlyJs
? keccak256(u8aToU8a(value))
return isReady() && !onlyJs
? is256
? keccak256(u8a)
: keccak512(u8a)
: new Uint8Array(
(is256 ? js.keccak256 : js.keccak512).update(u8aToU8a(value)).arrayBuffer()
(is256 ? js.keccak256 : js.keccak512).update(u8a).arrayBuffer()
);
}
+33 -4
View File
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import { hexToU8a } from '@polkadot/util';
import { waitReady } from '@polkadot/wasm-crypto';
import { sha256AsU8a } from '.';
@@ -29,9 +30,37 @@ const TESTS = [
];
describe('sha256AsU8a', (): void => {
it.each(TESTS)('creates known sha-256 hash', ({ input, output }): void => {
expect(
sha256AsU8a(hexToU8a(input))
).toEqual(hexToU8a(output));
beforeEach(async (): Promise<void> => {
await waitReady();
});
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
it.each(TESTS)('creates known sha-256 hash', ({ input, output }): void => {
expect(
sha256AsU8a(hexToU8a(input), onlyJs)
).toEqual(hexToU8a(output));
});
});
it.skip.each([false, true])('performance: onlyJs=%p', (onlyJs): void => {
const label = `sha256AsU8a:onlyJs=${onlyJs.toString()}`;
const test = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8
]);
console.time(label);
for (let i = 0; i < 128000; i++) {
sha256AsU8a(test, onlyJs);
}
console.timeEnd(label);
});
});
+11 -7
View File
@@ -3,15 +3,19 @@
import hash from 'hash.js';
import { isReady, sha256 } from '@polkadot/wasm-crypto';
/**
* @name sha256AsU8a
* @summary Creates sha-256 hash of the input.
*/
export function sha256AsU8a (data: Uint8Array): Uint8Array {
return new Uint8Array(
hash
.sha256()
.update(data)
.digest()
);
export function sha256AsU8a (data: Uint8Array, onlyJs = false): Uint8Array {
return isReady() && !onlyJs
? sha256(data)
: new Uint8Array(
hash
.sha256()
.update(data)
.digest()
);
}
@@ -21,4 +21,26 @@ describe('sha512AsU8a', (): void => {
);
});
});
it.skip.each([false, true])('performance: onlyJs=%p', (onlyJs): void => {
const label = `sha512AsU8a:onlyJs=${onlyJs.toString()}`;
const test = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8
]);
console.time(label);
for (let i = 0; i < 128000; i++) {
sha512AsU8a(test, onlyJs);
}
console.timeEnd(label);
});
});
+7 -2
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import nacl from 'tweetnacl';
import hash from 'hash.js';
import { isReady, sha512 } from '@polkadot/wasm-crypto';
@@ -22,5 +22,10 @@ import { isReady, sha512 } from '@polkadot/wasm-crypto';
export function sha512AsU8a (data: Uint8Array, onlyJs = false): Uint8Array {
return isReady() && !onlyJs
? sha512(data)
: nacl.hash(data);
: new Uint8Array(
hash
.sha512()
.update(data)
.digest()
);
}