From a8f13bd387ad1e8fe67389b6f715cace4344bc95 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 25 Nov 2021 10:34:34 +0100 Subject: [PATCH] Additional adjustments on newBigInt (#1241) * Additional adjustments on newBigInt * typing --- packages/util-crypto/src/xxhash/xxhash64.ts | 44 ++++++++++----------- packages/util/src/bi/consts.ts | 26 +++++++----- packages/util/src/bi/index.ts | 1 - packages/util/src/bi/new.ts | 16 -------- packages/util/src/bi/sqrt.ts | 5 +-- packages/util/src/bi/toU8a.ts | 7 ++-- packages/util/src/u8a/toBigInt.ts | 7 ++-- 7 files changed, 47 insertions(+), 59 deletions(-) delete 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 ad0cb4e49..7167fbb3a 100644 --- a/packages/util-crypto/src/xxhash/xxhash64.ts +++ b/packages/util-crypto/src/xxhash/xxhash64.ts @@ -1,7 +1,7 @@ // Copyright 2017-2021 @polkadot/util-crypto authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { newBigInt } from '@polkadot/util'; +import { _BigInt } from '@polkadot/util'; // Adapted from https://github.com/pierrec/js-xxhash/blob/0504e76f3d31a21ae8528a7f590c7289c9e431d2/lib/xxhash64.js // @@ -26,31 +26,31 @@ interface State { v4: bigint; } -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'); +const P64_1 = _BigInt('11400714785074694791'); +const P64_2 = _BigInt('14029467366897019727'); +const P64_3 = _BigInt('1609587929392839161'); +const P64_4 = _BigInt('9650029242287828579'); +const P64_5 = _BigInt('2870177450012600261'); // mask for a u64, all bits set -const U64 = newBigInt('0xffffffffffffffff'); +const U64 = _BigInt('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); +const _0n = _BigInt(0); +const _1n = _BigInt(1); +const _7n = _BigInt(7); +const _11n = _BigInt(11); +const _12n = _BigInt(12); +const _16n = _BigInt(16); +const _18n = _BigInt(18); +const _23n = _BigInt(23); +const _27n = _BigInt(27); +const _29n = _BigInt(29); +const _31n = _BigInt(31); +const _32n = _BigInt(32); +const _33n = _BigInt(33); +const _64n = _BigInt(64); +const _256n = _BigInt(256); function rotl (a: bigint, b: bigint): bigint { const c = a & U64; diff --git a/packages/util/src/bi/consts.ts b/packages/util/src/bi/consts.ts index 01f6988e1..cd232f215 100644 --- a/packages/util/src/bi/consts.ts +++ b/packages/util/src/bi/consts.ts @@ -1,42 +1,48 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { newBigInt } from './new'; +type NewBigInt = (value: string | number | bigint | boolean) => bigint; + +/** + * @name _BigInt + * @description Creates a new instance of BigInt in environments that do support it + */ +export const _BigInt: NewBigInt = typeof BigInt !== 'undefined' + ? BigInt + : () => Number.NaN as unknown as bigint; /** * @name BI_ZERO * @summary BigInt constant for 0. */ -export const BI_ZERO = newBigInt(0); +export const BI_ZERO = _BigInt(0); /** * @name BI_ONE - * @summary BigInt constant for 0. + * @summary BigInt constant for 1. */ -export const BI_ONE = newBigInt(1); +export const BI_ONE = _BigInt(1); /** * @name BI_MILLION * @summary BigInt constant for 1,000,000. */ -export const BI_MILLION = newBigInt(1_000_000); +export const BI_MILLION = _BigInt(1_000_000); /** * @name BI_BILLION * @summary BigInt constant for 1,000,000,000. */ -export const BI_BILLION = newBigInt(1_000_000_000); +export const BI_BILLION = _BigInt(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 * BI_BILLION - : undefined; +export const BI_QUINTILL = BI_BILLION * BI_BILLION; /** * @name BI_MAX_INTEGER * @summary BigInt constant for MAX_SAFE_INTEGER */ -export const BI_MAX_INTEGER = newBigInt(Number.MAX_SAFE_INTEGER); +export const BI_MAX_INTEGER = _BigInt(Number.MAX_SAFE_INTEGER); diff --git a/packages/util/src/bi/index.ts b/packages/util/src/bi/index.ts index fd5737bf1..d454f05a2 100644 --- a/packages/util/src/bi/index.ts +++ b/packages/util/src/bi/index.ts @@ -7,7 +7,6 @@ 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 deleted file mode 100644 index d7faeea6b..000000000 --- a/packages/util/src/bi/new.ts +++ /dev/null @@ -1,16 +0,0 @@ -// 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 1752efc21..f46b7514d 100644 --- a/packages/util/src/bi/sqrt.ts +++ b/packages/util/src/bi/sqrt.ts @@ -5,11 +5,10 @@ import type { BN } from '../bn'; import type { ToBigInt, ToBn } from '../types'; import { assert } from '../assert'; -import { BI_MAX_INTEGER, BI_ONE, BI_ZERO } from './consts'; -import { newBigInt } from './new'; +import { _BigInt, BI_MAX_INTEGER, BI_ONE, BI_ZERO } from './consts'; import { biToBigInt } from './toBigInt'; -const SQRT_MAX_SAFE_INTEGER = newBigInt(94906265); +const SQRT_MAX_SAFE_INTEGER = _BigInt(94906265); /** * @name biSqrt diff --git a/packages/util/src/bi/toU8a.ts b/packages/util/src/bi/toU8a.ts index 80bd46ed1..6ee78026a 100644 --- a/packages/util/src/bi/toU8a.ts +++ b/packages/util/src/bi/toU8a.ts @@ -5,16 +5,15 @@ 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 { _BigInt, BI_ONE, BI_ZERO } from './consts'; import { biToBigInt } from './toBigInt'; interface Options extends ToBnOptions { bitLength?: number; } -const DIV = newBigInt(256); -const NEG_MASK = newBigInt(0xff); +const DIV = _BigInt(256); +const NEG_MASK = _BigInt(0xff); function createEmpty ({ bitLength = 0 }: Options): Uint8Array { return bitLength === -1 diff --git a/packages/util/src/u8a/toBigInt.ts b/packages/util/src/u8a/toBigInt.ts index 1d4e13408..8175b9024 100644 --- a/packages/util/src/u8a/toBigInt.ts +++ b/packages/util/src/u8a/toBigInt.ts @@ -3,10 +3,11 @@ import type { ToBnOptions } from '../types'; +import { _BigInt, BI_ONE } from '../bi/consts'; import { objectSpread } from '../object/spread'; -const U8_MAX = 256n; -const U16_MAX = U8_MAX * U8_MAX; +const U8_MAX = _BigInt(256); +const U16_MAX = _BigInt(256 * 256); function xor (input: Uint8Array): Uint8Array { const result = new Uint8Array(input.length); @@ -58,6 +59,6 @@ export function u8aToBigInt (value: Uint8Array, options: ToBnOptions = {}): bigi : value; return isNegative - ? ((toBigInt(xor(u8a)) * -1n) - 1n) + ? ((toBigInt(xor(u8a)) * -BI_ONE) - BI_ONE) : toBigInt(u8a); }