From 9821eba11f2aa68cc9c2bebca0cf1bc16e55efa7 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 25 Nov 2021 16:16:09 +0100 Subject: [PATCH] Rename internal BigInt helper (#1244) * Rename internal BigInt helper * Consistency --- packages/util-crypto/src/xxhash/xxhash64.ts | 42 ++++++++++----------- packages/util/src/bi/consts.ts | 18 ++++----- packages/util/src/bi/sqrt.ts | 10 ++--- packages/util/src/bi/toU8a.ts | 12 +++--- packages/util/src/u8a/toBigInt.ts | 8 ++-- 5 files changed, 44 insertions(+), 46 deletions(-) diff --git a/packages/util-crypto/src/xxhash/xxhash64.ts b/packages/util-crypto/src/xxhash/xxhash64.ts index 7167fbb3a..f450d1a65 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 { _BigInt } from '@polkadot/util'; +import { _0n, _1n, _n } from '@polkadot/util'; // Adapted from https://github.com/pierrec/js-xxhash/blob/0504e76f3d31a21ae8528a7f590c7289c9e431d2/lib/xxhash64.js // @@ -26,31 +26,29 @@ interface State { v4: bigint; } -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'); +const P64_1 = _n('11400714785074694791'); +const P64_2 = _n('14029467366897019727'); +const P64_3 = _n('1609587929392839161'); +const P64_4 = _n('9650029242287828579'); +const P64_5 = _n('2870177450012600261'); // mask for a u64, all bits set -const U64 = _BigInt('0xffffffffffffffff'); +const U64 = _n('0xffffffffffffffff'); // various constants -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); +const _7n = _n(7); +const _11n = _n(11); +const _12n = _n(12); +const _16n = _n(16); +const _18n = _n(18); +const _23n = _n(23); +const _27n = _n(27); +const _29n = _n(29); +const _31n = _n(31); +const _32n = _n(32); +const _33n = _n(33); +const _64n = _n(64); +const _256n = _n(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 cd232f215..09adcfa9f 100644 --- a/packages/util/src/bi/consts.ts +++ b/packages/util/src/bi/consts.ts @@ -4,36 +4,36 @@ type NewBigInt = (value: string | number | bigint | boolean) => bigint; /** - * @name _BigInt + * @name _n * @description Creates a new instance of BigInt in environments that do support it */ -export const _BigInt: NewBigInt = typeof BigInt !== 'undefined' +export const _n: NewBigInt = typeof BigInt !== 'undefined' ? BigInt : () => Number.NaN as unknown as bigint; /** - * @name BI_ZERO + * @name _0n * @summary BigInt constant for 0. */ -export const BI_ZERO = _BigInt(0); +export const _0n = _n(0); /** - * @name BI_ONE + * @name _1n * @summary BigInt constant for 1. */ -export const BI_ONE = _BigInt(1); +export const _1n = _n(1); /** * @name BI_MILLION * @summary BigInt constant for 1,000,000. */ -export const BI_MILLION = _BigInt(1_000_000); +export const BI_MILLION = _n(1_000_000); /** * @name BI_BILLION * @summary BigInt constant for 1,000,000,000. */ -export const BI_BILLION = _BigInt(1_000_000_000); +export const BI_BILLION = _n(1_000_000_000); /** * @name BI_QUINTILL @@ -45,4 +45,4 @@ export const BI_QUINTILL = BI_BILLION * BI_BILLION; * @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 = _n(Number.MAX_SAFE_INTEGER); diff --git a/packages/util/src/bi/sqrt.ts b/packages/util/src/bi/sqrt.ts index f46b7514d..cfc5571dc 100644 --- a/packages/util/src/bi/sqrt.ts +++ b/packages/util/src/bi/sqrt.ts @@ -5,10 +5,10 @@ import type { BN } from '../bn'; import type { ToBigInt, ToBn } from '../types'; import { assert } from '../assert'; -import { _BigInt, BI_MAX_INTEGER, BI_ONE, BI_ZERO } from './consts'; +import { _0n, _1n, _n, BI_MAX_INTEGER } from './consts'; import { biToBigInt } from './toBigInt'; -const SQRT_MAX_SAFE_INTEGER = _BigInt(94906265); +const SQRT_MAX_SAFE_INTEGER = _n(94906265); /** * @name biSqrt @@ -17,7 +17,7 @@ const SQRT_MAX_SAFE_INTEGER = _BigInt(94906265); export function biSqrt (value: ExtToBn | BN | bigint | string | number | null): bigint { const n = biToBigInt(value); - assert(n >= BI_ZERO, 'square root of negative numbers is not supported'); + assert(n >= _0n, '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 +30,9 @@ export function biSqrt (value: ExtToBn | BN | let x0 = SQRT_MAX_SAFE_INTEGER; while (true) { - const x1 = ((n / x0) + x0) >> BI_ONE; + const x1 = ((n / x0) + x0) >> _1n; - if (x0 === x1 || (x0 === (x1 - BI_ONE))) { + if (x0 === x1 || (x0 === (x1 - _1n))) { return x0; } diff --git a/packages/util/src/bi/toU8a.ts b/packages/util/src/bi/toU8a.ts index 6ee78026a..5c1c6b0b4 100644 --- a/packages/util/src/bi/toU8a.ts +++ b/packages/util/src/bi/toU8a.ts @@ -5,15 +5,15 @@ import type { BN } from '../bn/bn'; import type { ToBigInt, ToBn, ToBnOptions } from '../types'; import { objectSpread } from '../object/spread'; -import { _BigInt, BI_ONE, BI_ZERO } from './consts'; +import { _0n, _1n, _n } from './consts'; import { biToBigInt } from './toBigInt'; interface Options extends ToBnOptions { bitLength?: number; } -const DIV = _BigInt(256); -const NEG_MASK = _BigInt(0xff); +const DIV = _n(256); +const NEG_MASK = _n(0xff); function createEmpty ({ bitLength = 0 }: Options): Uint8Array { return bitLength === -1 @@ -25,10 +25,10 @@ function toU8a (value: bigint, { isLe, isNegative }: Options): Uint8Array { const arr: number[] = []; if (isNegative) { - value = (value + BI_ONE) * -BI_ONE; + value = (value + _1n) * -_1n; } - while (value !== BI_ZERO) { + while (value !== _0n) { const mod = value % DIV; const val = Number( isNegative @@ -60,7 +60,7 @@ export function biToU8a (value?: ExtToBn | BN const valueBi = biToBigInt(value); - if (valueBi === BI_ZERO) { + if (valueBi === _0n) { return createEmpty(opts); } diff --git a/packages/util/src/u8a/toBigInt.ts b/packages/util/src/u8a/toBigInt.ts index 8175b9024..1a52ce6f8 100644 --- a/packages/util/src/u8a/toBigInt.ts +++ b/packages/util/src/u8a/toBigInt.ts @@ -3,11 +3,11 @@ import type { ToBnOptions } from '../types'; -import { _BigInt, BI_ONE } from '../bi/consts'; +import { _1n, _n } from '../bi/consts'; import { objectSpread } from '../object/spread'; -const U8_MAX = _BigInt(256); -const U16_MAX = _BigInt(256 * 256); +const U8_MAX = _n(256); +const U16_MAX = _n(256 * 256); function xor (input: Uint8Array): Uint8Array { const result = new Uint8Array(input.length); @@ -59,6 +59,6 @@ export function u8aToBigInt (value: Uint8Array, options: ToBnOptions = {}): bigi : value; return isNegative - ? ((toBigInt(xor(u8a)) * -BI_ONE) - BI_ONE) + ? ((toBigInt(xor(u8a)) * -_1n) - _1n) : toBigInt(u8a); }