From c60e35ee9e81fb61ad774d694d0fd88e4c540601 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 25 Nov 2021 09:59:26 +0100 Subject: [PATCH] Ensure BigInt(...) is called conditionally (#1240) * Ensure BigInt(...) is called consitionally * adjust * comments * adjust --- packages/util-crypto/src/xxhash/xxhash64.ts | 57 ++++++++++++++------- packages/util/src/bi/consts.ts | 24 +++++++-- packages/util/src/bi/index.ts | 1 + packages/util/src/bi/new.ts | 16 ++++++ packages/util/src/bi/sqrt.ts | 11 ++-- packages/util/src/bi/toU8a.ts | 13 +++-- 6 files changed, 89 insertions(+), 33 deletions(-) create mode 100644 packages/util/src/bi/new.ts diff --git a/packages/util-crypto/src/xxhash/xxhash64.ts b/packages/util-crypto/src/xxhash/xxhash64.ts index 87d73129d..ad0cb4e49 100644 --- a/packages/util-crypto/src/xxhash/xxhash64.ts +++ b/packages/util-crypto/src/xxhash/xxhash64.ts @@ -1,6 +1,8 @@ // Copyright 2017-2021 @polkadot/util-crypto authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { newBigInt } from '@polkadot/util'; + // Adapted from https://github.com/pierrec/js-xxhash/blob/0504e76f3d31a21ae8528a7f590c7289c9e431d2/lib/xxhash64.js // // xxHash64 implementation in pure Javascript @@ -24,19 +26,36 @@ interface State { v4: bigint; } -const P64_1 = 11400714785074694791n; -const P64_2 = 14029467366897019727n; -const P64_3 = 1609587929392839161n; -const P64_4 = 9650029242287828579n; -const P64_5 = 2870177450012600261n; +const P64_1 = newBigInt('11400714785074694791'); +const P64_2 = newBigInt('14029467366897019727'); +const P64_3 = newBigInt('1609587929392839161'); +const P64_4 = newBigInt('9650029242287828579'); +const P64_5 = newBigInt('2870177450012600261'); // mask for a u64, all bits set -const U64 = 2n ** 64n - 1n; +const U64 = newBigInt('0xffffffffffffffff'); + +// various constants +const _0n = newBigInt(0); +const _1n = newBigInt(1); +const _7n = newBigInt(7); +const _11n = newBigInt(11); +const _12n = newBigInt(12); +const _16n = newBigInt(16); +const _18n = newBigInt(18); +const _23n = newBigInt(23); +const _27n = newBigInt(27); +const _29n = newBigInt(29); +const _31n = newBigInt(31); +const _32n = newBigInt(32); +const _33n = newBigInt(33); +const _64n = newBigInt(64); +const _256n = newBigInt(256); function rotl (a: bigint, b: bigint): bigint { const c = a & U64; - return ((c << b) | (c >> (64n - b))) & U64; + return ((c << b) | (c >> (_64n - b))) & U64; } function fromU8a (u8a: Uint8Array, p: number, count: 2 | 4): bigint { @@ -47,10 +66,10 @@ function fromU8a (u8a: Uint8Array, p: number, count: 2 | 4): bigint { bigints[i] = BigInt(u8a[p + offset] | (u8a[p + 1 + offset] << 8)); } - let result = 0n; + let result = _0n; for (let i = count - 1; i >= 0; i--) { - result = (result << 16n) + bigints[i]; + result = (result << _16n) + bigints[i]; } return result; @@ -60,9 +79,9 @@ function toU8a (h64: bigint): Uint8Array { const result = new Uint8Array(8); for (let i = 7; i >= 0; i--) { - result[i] = Number(h64 % 256n); + result[i] = Number(h64 % _256n); - h64 = h64 / 256n; + h64 = h64 / _256n; } return result; @@ -95,7 +114,7 @@ function init (state: State, input: Uint8Array): State { if (limit >= 0) { const adjustV = (v: bigint) => - P64_1 * rotl(v + P64_2 * fromU8a(input, p, 4), 31n); + P64_1 * rotl(v + P64_2 * fromU8a(input, p, 4), _31n); do { state.v1 = adjustV(state.v1); p += 8; @@ -118,26 +137,26 @@ export function xxhash64 (input: Uint8Array, initSeed: bigint | number): Uint8Ar let p = 0; let h64 = U64 & (BigInt(input.length) + ( input.length >= 32 - ? (((((((((rotl(v1, 1n) + rotl(v2, 7n) + rotl(v3, 12n) + rotl(v4, 18n)) ^ (P64_1 * rotl(v1 * P64_2, 31n))) * P64_1 + P64_4) ^ (P64_1 * rotl(v2 * P64_2, 31n))) * P64_1 + P64_4) ^ (P64_1 * rotl(v3 * P64_2, 31n))) * P64_1 + P64_4) ^ (P64_1 * rotl(v4 * P64_2, 31n))) * P64_1 + P64_4) + ? (((((((((rotl(v1, _1n) + rotl(v2, _7n) + rotl(v3, _12n) + rotl(v4, _18n)) ^ (P64_1 * rotl(v1 * P64_2, _31n))) * P64_1 + P64_4) ^ (P64_1 * rotl(v2 * P64_2, _31n))) * P64_1 + P64_4) ^ (P64_1 * rotl(v3 * P64_2, _31n))) * P64_1 + P64_4) ^ (P64_1 * rotl(v4 * P64_2, _31n))) * P64_1 + P64_4) : (seed + P64_5) )); while (p <= (u8asize - 8)) { - h64 = U64 & (P64_4 + P64_1 * rotl(h64 ^ (P64_1 * rotl(P64_2 * fromU8a(u8a, p, 4), 31n)), 27n)); + h64 = U64 & (P64_4 + P64_1 * rotl(h64 ^ (P64_1 * rotl(P64_2 * fromU8a(u8a, p, 4), _31n)), _27n)); p += 8; } if ((p + 4) <= u8asize) { - h64 = U64 & (P64_3 + P64_2 * rotl(h64 ^ (P64_1 * fromU8a(u8a, p, 2)), 23n)); + h64 = U64 & (P64_3 + P64_2 * rotl(h64 ^ (P64_1 * fromU8a(u8a, p, 2)), _23n)); p += 4; } while (p < u8asize) { - h64 = U64 & (P64_1 * rotl(h64 ^ (P64_5 * BigInt(u8a[p++])), 11n)); + h64 = U64 & (P64_1 * rotl(h64 ^ (P64_5 * BigInt(u8a[p++])), _11n)); } - h64 = U64 & (P64_2 * (h64 ^ (h64 >> 33n))); - h64 = U64 & (P64_3 * (h64 ^ (h64 >> 29n))); + h64 = U64 & (P64_2 * (h64 ^ (h64 >> _33n))); + h64 = U64 & (P64_3 * (h64 ^ (h64 >> _29n))); - return toU8a(U64 & (h64 ^ (h64 >> 32n))); + return toU8a(U64 & (h64 ^ (h64 >> _32n))); } diff --git a/packages/util/src/bi/consts.ts b/packages/util/src/bi/consts.ts index 8bc7d45a7..01f6988e1 100644 --- a/packages/util/src/bi/consts.ts +++ b/packages/util/src/bi/consts.ts @@ -1,26 +1,42 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { newBigInt } from './new'; + +/** + * @name BI_ZERO + * @summary BigInt constant for 0. + */ +export const BI_ZERO = newBigInt(0); + +/** + * @name BI_ONE + * @summary BigInt constant for 0. + */ +export const BI_ONE = newBigInt(1); + /** * @name BI_MILLION * @summary BigInt constant for 1,000,000. */ -export const BI_MILLION = 1_000_000n; +export const BI_MILLION = newBigInt(1_000_000); /** * @name BI_BILLION * @summary BigInt constant for 1,000,000,000. */ -export const BI_BILLION = 1_000_000_000n; +export const BI_BILLION = newBigInt(1_000_000_000); /** * @name BI_QUINTILL * @summary BigInt constant for 1,000,000,000,000,000,000. */ -export const BI_QUINTILL = BI_BILLION * BI_BILLION; +export const BI_QUINTILL = BI_BILLION + ? BI_BILLION * BI_BILLION + : undefined; /** * @name BI_MAX_INTEGER * @summary BigInt constant for MAX_SAFE_INTEGER */ -export const BI_MAX_INTEGER = BigInt(Number.MAX_SAFE_INTEGER); +export const BI_MAX_INTEGER = newBigInt(Number.MAX_SAFE_INTEGER); diff --git a/packages/util/src/bi/index.ts b/packages/util/src/bi/index.ts index d454f05a2..fd5737bf1 100644 --- a/packages/util/src/bi/index.ts +++ b/packages/util/src/bi/index.ts @@ -7,6 +7,7 @@ export * from './consts'; +export { newBigInt } from './new'; export { biSqrt } from './sqrt'; export { biToBigInt } from './toBigInt'; export { biMax } from './max'; diff --git a/packages/util/src/bi/new.ts b/packages/util/src/bi/new.ts new file mode 100644 index 000000000..d7faeea6b --- /dev/null +++ b/packages/util/src/bi/new.ts @@ -0,0 +1,16 @@ +// Copyright 2017-2021 @polkadot/util authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { hasBigInt } from '../has'; + +/** + * @name newBigInt + * @description Creates a new instance of BigInt in environments that do support it + */ +export function newBigInt (n: bigint | number | string): bigint { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + return hasBigInt + ? BigInt(n) + : undefined; +} diff --git a/packages/util/src/bi/sqrt.ts b/packages/util/src/bi/sqrt.ts index b1671b0c7..1752efc21 100644 --- a/packages/util/src/bi/sqrt.ts +++ b/packages/util/src/bi/sqrt.ts @@ -5,10 +5,11 @@ import type { BN } from '../bn'; import type { ToBigInt, ToBn } from '../types'; import { assert } from '../assert'; -import { BI_MAX_INTEGER } from './consts'; +import { BI_MAX_INTEGER, BI_ONE, BI_ZERO } from './consts'; +import { newBigInt } from './new'; import { biToBigInt } from './toBigInt'; -const SQRT_MAX_SAFE_INTEGER = 94906265n; +const SQRT_MAX_SAFE_INTEGER = newBigInt(94906265); /** * @name biSqrt @@ -17,7 +18,7 @@ const SQRT_MAX_SAFE_INTEGER = 94906265n; export function biSqrt (value: ExtToBn | BN | bigint | string | number | null): bigint { const n = biToBigInt(value); - assert(n >= 0n, 'square root of negative numbers is not supported'); + assert(n >= BI_ZERO, 'square root of negative numbers is not supported'); // https://stackoverflow.com/questions/53683995/javascript-big-integer-square-root/ // shortcut <= 2^53 - 1 to use the JS utils @@ -30,9 +31,9 @@ export function biSqrt (value: ExtToBn | BN | let x0 = SQRT_MAX_SAFE_INTEGER; while (true) { - const x1 = ((n / x0) + x0) >> 1n; + const x1 = ((n / x0) + x0) >> BI_ONE; - if (x0 === x1 || (x0 === (x1 - 1n))) { + if (x0 === x1 || (x0 === (x1 - BI_ONE))) { return x0; } diff --git a/packages/util/src/bi/toU8a.ts b/packages/util/src/bi/toU8a.ts index 01ddad9f3..80bd46ed1 100644 --- a/packages/util/src/bi/toU8a.ts +++ b/packages/util/src/bi/toU8a.ts @@ -5,13 +5,16 @@ import type { BN } from '../bn/bn'; import type { ToBigInt, ToBn, ToBnOptions } from '../types'; import { objectSpread } from '../object/spread'; +import { BI_ONE, BI_ZERO } from './consts'; +import { newBigInt } from './new'; import { biToBigInt } from './toBigInt'; interface Options extends ToBnOptions { bitLength?: number; } -const DIV = 256n; +const DIV = newBigInt(256); +const NEG_MASK = newBigInt(0xff); function createEmpty ({ bitLength = 0 }: Options): Uint8Array { return bitLength === -1 @@ -23,14 +26,14 @@ function toU8a (value: bigint, { isLe, isNegative }: Options): Uint8Array { const arr: number[] = []; if (isNegative) { - value = (value + 1n) * -1n; + value = (value + BI_ONE) * -BI_ONE; } - while (value !== 0n) { + while (value !== BI_ZERO) { const mod = value % DIV; const val = Number( isNegative - ? mod ^ 0xffn + ? mod ^ NEG_MASK : mod ); @@ -58,7 +61,7 @@ export function biToU8a (value?: ExtToBn | BN const valueBi = biToBigInt(value); - if (valueBi === 0n) { + if (valueBi === BI_ZERO) { return createEmpty(opts); }