From 56a5a079dd34434e47808b8b7bb9d179acd3f724 Mon Sep 17 00:00:00 2001 From: Jaco Date: Fri, 26 Nov 2021 08:03:14 +0100 Subject: [PATCH] Rename bi constants (consistency) (#1247) * Rename bi constants (consistency) * sqrt constant * Rename bi* functions * secp256k1 also part of renames --- CHANGELOG.md | 3 ++- packages/util/src/bi/consts.ts | 16 ++++++++-------- packages/util/src/bi/index.ts | 12 ++++++------ packages/util/src/bi/max.spec.ts | 12 ++++++------ packages/util/src/bi/max.ts | 4 ++-- packages/util/src/bi/min.spec.ts | 10 +++++----- packages/util/src/bi/min.ts | 4 ++-- packages/util/src/bi/sqrt.spec.ts | 8 ++++---- packages/util/src/bi/sqrt.ts | 16 ++++++++-------- packages/util/src/bi/toBigInt.spec.ts | 22 +++++++++++----------- packages/util/src/bi/toBigInt.ts | 4 ++-- packages/util/src/bi/toHex.spec.ts | 18 +++++++++--------- packages/util/src/bi/toHex.ts | 8 ++++---- packages/util/src/bi/toU8a.spec.ts | 22 +++++++++++----------- packages/util/src/bi/toU8a.ts | 8 ++++---- 15 files changed, 84 insertions(+), 83 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebc3cb115..fdc6e9104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ **Important** In the 8.0 version the hashing utilities have been consolidated around an optimized and audited version from `@noble/hashes`. As in the past the WASM hashing will be used by default if the environment is initialized. -**Breaking change** The `schnorrkel*` functions have been renamed to `sr25519*`. Likewise the `ed25519` operations have been renamed from `nacl*` to `ed25519*`. Additionally the `{ed, sr}25519KeyPairFrom*` functions have been renamed to `{ed, sr}25519PairFrom*`. +**Breaking change** The `schnorrkel*` functions have been renamed to `sr25519*`. Likewise the `ed25519` operations have been renamed from `nacl*` to `ed25519*`. Additionally the `{ed, sr, secp256k1}25519KeyPairFrom*` functions have been renamed to `{ed, sr, secp256k1}25519PairFrom*`. Changes: @@ -13,6 +13,7 @@ Changes: - Replace (most) hashing operations with `@noble/hashes` - Replace JS fallback for `xxhash` - Adjust `BigInt` utility exports (with capability detection) +- Previously added `bi*` functions renamed to `n*` - Extend performance tests with implementation compares - Remove non-significant Node Buffer concat fill diff --git a/packages/util/src/bi/consts.ts b/packages/util/src/bi/consts.ts index 09adcfa9f..70a36bccf 100644 --- a/packages/util/src/bi/consts.ts +++ b/packages/util/src/bi/consts.ts @@ -24,25 +24,25 @@ export const _0n = _n(0); export const _1n = _n(1); /** - * @name BI_MILLION + * @name _1Mn * @summary BigInt constant for 1,000,000. */ -export const BI_MILLION = _n(1_000_000); +export const _1Mn = _n(1_000_000); /** -* @name BI_BILLION +* @name _1Bn * @summary BigInt constant for 1,000,000,000. */ -export const BI_BILLION = _n(1_000_000_000); +export const _1Bn = _n(1_000_000_000); /** -* @name BI_QUINTILL +* @name _1Qn * @summary BigInt constant for 1,000,000,000,000,000,000. */ -export const BI_QUINTILL = BI_BILLION * BI_BILLION; +export const _1Qn = _1Bn * _1Bn; /** -* @name BI_MAX_INTEGER +* @name _2pow53n * @summary BigInt constant for MAX_SAFE_INTEGER */ -export const BI_MAX_INTEGER = _n(Number.MAX_SAFE_INTEGER); +export const _2pow53n = _n(Number.MAX_SAFE_INTEGER); diff --git a/packages/util/src/bi/index.ts b/packages/util/src/bi/index.ts index d454f05a2..cfe986dde 100644 --- a/packages/util/src/bi/index.ts +++ b/packages/util/src/bi/index.ts @@ -7,9 +7,9 @@ export * from './consts'; -export { biSqrt } from './sqrt'; -export { biToBigInt } from './toBigInt'; -export { biMax } from './max'; -export { biMin } from './min'; -export { biToHex } from './toHex'; -export { biToU8a } from './toU8a'; +export { nSqrt } from './sqrt'; +export { nToBigInt } from './toBigInt'; +export { nMax } from './max'; +export { nMin } from './min'; +export { nToHex } from './toHex'; +export { nToU8a } from './toU8a'; diff --git a/packages/util/src/bi/max.spec.ts b/packages/util/src/bi/max.spec.ts index 19494ae07..bc0f25b0f 100644 --- a/packages/util/src/bi/max.spec.ts +++ b/packages/util/src/bi/max.spec.ts @@ -1,30 +1,30 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { biMax } from '.'; +import { nMax } from '.'; -describe('biMax', (): void => { +describe('nMax', (): void => { it('finds maximum', (): void => { expect( - biMax(1n, 2n, 3n) + nMax(1n, 2n, 3n) ).toEqual(3n); }); it('finds maximum', (): void => { expect( - biMax(2n, 3n, 1n) + nMax(2n, 3n, 1n) ).toEqual(3n); }); it('returns a single item', (): void => { expect( - biMax(1n) + nMax(1n) ).toEqual(1n); }); it('fails when no items are available', (): void => { expect( - () => biMax() + () => nMax() ).toThrow(/Must provide one or more bigint arguments/); }); }); diff --git a/packages/util/src/bi/max.ts b/packages/util/src/bi/max.ts index 4d604f03a..61b37424a 100644 --- a/packages/util/src/bi/max.ts +++ b/packages/util/src/bi/max.ts @@ -4,10 +4,10 @@ import { assert } from '../assert'; /** - * @name biMax + * @name nMax * @summary Finds and returns the highest value in an array of bigint. */ -export function biMax (...items: bigint[]): bigint { +export function nMax (...items: bigint[]): bigint { assert(items.length >= 1, 'Must provide one or more bigint arguments'); let result = items[0]; diff --git a/packages/util/src/bi/min.spec.ts b/packages/util/src/bi/min.spec.ts index 8fef630c0..fb1c1896d 100644 --- a/packages/util/src/bi/min.spec.ts +++ b/packages/util/src/bi/min.spec.ts @@ -1,24 +1,24 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { biMin } from '.'; +import { nMin } from '.'; -describe('biMin', (): void => { +describe('nMin', (): void => { it('finds BN minimum', (): void => { expect( - biMin(2n, 1n, 3n) + nMin(2n, 1n, 3n) ).toEqual(1n); }); it('returns a single item', (): void => { expect( - biMin(1n) + nMin(1n) ).toEqual(1n); }); it('fails when no items are available', (): void => { expect( - () => biMin() + () => nMin() ).toThrow(/Must provide one or more bigint arguments/); }); }); diff --git a/packages/util/src/bi/min.ts b/packages/util/src/bi/min.ts index 57e2bc3e8..5ccb00a2a 100644 --- a/packages/util/src/bi/min.ts +++ b/packages/util/src/bi/min.ts @@ -4,10 +4,10 @@ import { assert } from '../assert'; /** - * @name biMin + * @name nMin * @summary Finds and returns the lowest value in an array of bigint. */ -export function biMin (...items: bigint[]): bigint { +export function nMin (...items: bigint[]): bigint { assert(items.length >= 1, 'Must provide one or more bigint arguments'); let result = items[0]; diff --git a/packages/util/src/bi/sqrt.spec.ts b/packages/util/src/bi/sqrt.spec.ts index 33981dcc2..e79d46978 100644 --- a/packages/util/src/bi/sqrt.spec.ts +++ b/packages/util/src/bi/sqrt.spec.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { BN } from '../bn'; -import { biSqrt } from '.'; +import { nSqrt } from '.'; const TESTS: [string | number | BN | bigint, string | number][] = [ [0, 0], @@ -27,16 +27,16 @@ const TESTS: [string | number | BN | bigint, string | number][] = [ ] ]; -describe('biSqrt', (): void => { +describe('nSqrt', (): void => { it('fails on < 0 roots', (): void => { expect( - () => biSqrt(-1n) + () => nSqrt(-1n) ).toThrow(/negative numbers is not supported/); }); TESTS.forEach(([value, expected], index): void => { it(`calcs for test #${index}`, (): void => { - expect(biSqrt(value) === BigInt(expected)).toEqual(true); + expect(nSqrt(value) === BigInt(expected)).toEqual(true); }); }); }); diff --git a/packages/util/src/bi/sqrt.ts b/packages/util/src/bi/sqrt.ts index cfc5571dc..7069d061b 100644 --- a/packages/util/src/bi/sqrt.ts +++ b/packages/util/src/bi/sqrt.ts @@ -5,29 +5,29 @@ import type { BN } from '../bn'; import type { ToBigInt, ToBn } from '../types'; import { assert } from '../assert'; -import { _0n, _1n, _n, BI_MAX_INTEGER } from './consts'; -import { biToBigInt } from './toBigInt'; +import { _0n, _1n, _2pow53n, _n } from './consts'; +import { nToBigInt } from './toBigInt'; -const SQRT_MAX_SAFE_INTEGER = _n(94906265); +const _sqrt2pow53n = _n(94906265); /** - * @name biSqrt + * @name nSqrt * @summary Calculates the integer square root of a bigint */ -export function biSqrt (value: ExtToBn | BN | bigint | string | number | null): bigint { - const n = biToBigInt(value); +export function nSqrt (value: ExtToBn | BN | bigint | string | number | null): bigint { + const n = nToBigInt(value); 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 - if (n <= BI_MAX_INTEGER) { + if (n <= _2pow53n) { return BigInt(Math.floor(Math.sqrt(Number(n)))); } // Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the // output will be larger than this, we expect this to be a safe start - let x0 = SQRT_MAX_SAFE_INTEGER; + let x0 = _sqrt2pow53n; while (true) { const x1 = ((n / x0) + x0) >> _1n; diff --git a/packages/util/src/bi/toBigInt.spec.ts b/packages/util/src/bi/toBigInt.spec.ts index e17cde2b7..c936b3718 100644 --- a/packages/util/src/bi/toBigInt.spec.ts +++ b/packages/util/src/bi/toBigInt.spec.ts @@ -2,54 +2,54 @@ // SPDX-License-Identifier: Apache-2.0 import { BN } from '../bn'; -import { biToBigInt } from '.'; +import { nToBigInt } from '.'; -describe('biToBigInt', (): void => { +describe('nToBigInt', (): void => { it('converts null values to 0x00', (): void => { expect( - biToBigInt(null) + nToBigInt(null) ).toEqual(0n); }); it('converts 0x values to 0x00', (): void => { expect( - biToBigInt('0x') + nToBigInt('0x') ).toEqual(0n); }); it('converts BN values to bigint', (): void => { expect( - biToBigInt(new BN(128)) + nToBigInt(new BN(128)) ).toEqual(128n); }); it('converts BigInt values to bigint', (): void => { expect( - biToBigInt(128821n) + nToBigInt(128821n) ).toEqual(128821n); }); it('converts number values to bigint', (): void => { expect( - biToBigInt(128) + nToBigInt(128) ).toEqual(128n); }); it('converts string to bigint', (): void => { expect( - biToBigInt('123') + nToBigInt('123') ).toEqual(123n); }); it('converts hex to bigint', (): void => { expect( - biToBigInt('0x0123') + nToBigInt('0x0123') ).toEqual(0x123n); }); it('converts Compact to bigint (via toBn)', (): void => { expect( - biToBigInt({ + nToBigInt({ something: 'test', toBn: () => new BN(1234) }) @@ -58,7 +58,7 @@ describe('biToBigInt', (): void => { it('converts Compact to bigint (via toBigInt)', (): void => { expect( - biToBigInt({ + nToBigInt({ something: 'test', toBigInt: () => 1234n }) diff --git a/packages/util/src/bi/toBigInt.ts b/packages/util/src/bi/toBigInt.ts index 0d8d537ae..1824e408f 100644 --- a/packages/util/src/bi/toBigInt.ts +++ b/packages/util/src/bi/toBigInt.ts @@ -11,10 +11,10 @@ import { isToBigInt } from '../is/toBigInt'; import { isToBn } from '../is/toBn'; /** - * @name biToBigInt + * @name nToBigInt * @summary Creates a bigInt value from a BN, bigint, string (base 10 or hex) or number input. */ -export function biToBigInt (value?: HexString | ExtToBn | BN | bigint | string | number | null): bigint { +export function nToBigInt (value?: HexString | ExtToBn | BN | bigint | string | number | null): bigint { return typeof value === 'bigint' ? value : !value diff --git a/packages/util/src/bi/toHex.spec.ts b/packages/util/src/bi/toHex.spec.ts index 23dc8a0d2..fb2f5b382 100644 --- a/packages/util/src/bi/toHex.spec.ts +++ b/packages/util/src/bi/toHex.spec.ts @@ -1,48 +1,48 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { biToHex } from '.'; +import { nToHex } from '.'; -describe('biToHex', (): void => { +describe('nToHex', (): void => { it('converts null values to 0x00', (): void => { expect( - biToHex(null) + nToHex(null) ).toBe('0x00'); }); it('converts values to a prefixed hex representation', (): void => { expect( - biToHex(128) + nToHex(128) ).toBe('0x80'); }); it('converts values to a prefixed hex representation (bitLength)', (): void => { expect( - biToHex(128n, { bitLength: 16 }) + nToHex(128n, { bitLength: 16 }) ).toBe('0x0080'); }); it('converts values to a prefixed hex representation (bitLength + le)', (): void => { expect( - biToHex(128, { bitLength: 16, isLe: true }) + nToHex(128, { bitLength: 16, isLe: true }) ).toBe('0x8000'); }); it('converts values to a prefixed hex representation (LE)', (): void => { expect( - biToHex(128, { bitLength: 16, isLe: true }) + nToHex(128, { bitLength: 16, isLe: true }) ).toBe('0x8000'); }); it('handles negative numbers', (): void => { expect( - biToHex(-1234, { isNegative: true }) + nToHex(-1234, { isNegative: true }) ).toBe('0xfb2e'); }); it('handles negative numbers (with bitLength)', (): void => { expect( - biToHex(-1234, { bitLength: 32, isNegative: true }) + nToHex(-1234, { bitLength: 32, isNegative: true }) ).toBe('0xfffffb2e'); }); }); diff --git a/packages/util/src/bi/toHex.ts b/packages/util/src/bi/toHex.ts index 0c026738b..d6ad3f197 100644 --- a/packages/util/src/bi/toHex.ts +++ b/packages/util/src/bi/toHex.ts @@ -6,7 +6,7 @@ import type { HexString, ToBigInt, ToBn, ToBnOptions } from '../types'; import { objectSpread } from '../object/spread'; import { u8aToHex } from '../u8a'; -import { biToU8a } from './toU8a'; +import { nToU8a } from './toU8a'; interface Options extends ToBnOptions { bitLength?: number; @@ -15,16 +15,16 @@ interface Options extends ToBnOptions { const ZERO_STR = '0x00'; /** - * @name biToHex + * @name nToHex * @summary Creates a hex value from a bigint object. */ -export function biToHex (value?: ExtToBn | BN | bigint | number | null, options?: Options): HexString { +export function nToHex (value?: ExtToBn | BN | bigint | number | null, options?: Options): HexString { if (!value) { return ZERO_STR; } return u8aToHex( - biToU8a(value, objectSpread( + nToU8a(value, objectSpread( // We spread here, the default for hex values is BE (JSONRPC via substrate) { isLe: false, isNegative: false }, options diff --git a/packages/util/src/bi/toU8a.spec.ts b/packages/util/src/bi/toU8a.spec.ts index 0f1ec9b7f..dddd720bb 100644 --- a/packages/util/src/bi/toU8a.spec.ts +++ b/packages/util/src/bi/toU8a.spec.ts @@ -3,58 +3,58 @@ import { performance } from '../../test/performance'; import { arrayRange } from '../array'; -import { biToU8a } from '.'; +import { nToU8a } from '.'; const ptest = arrayRange(65536).map((v) => [v]); -describe('biToU8a', (): void => { +describe('nToU8a', (): void => { it('converts null values to 0x00', (): void => { expect( - biToU8a(null) + nToU8a(null) ).toEqual(new Uint8Array()); }); it('converts null values to 0x00000000 (bitLength)', (): void => { expect( - biToU8a(null, { bitLength: 32 }) + nToU8a(null, { bitLength: 32 }) ).toEqual(new Uint8Array([0, 0, 0, 0])); }); it('converts values to a prefixed hex representation', (): void => { expect( - biToU8a(0x123456n, { isLe: false }) + nToU8a(0x123456n, { isLe: false }) ).toEqual(new Uint8Array([0x12, 0x34, 0x56])); }); it('converts values to a prefixed hex representation (bitLength)', (): void => { expect( - biToU8a(0x123456n, { bitLength: 32, isLe: false }) + nToU8a(0x123456n, { bitLength: 32, isLe: false }) ).toEqual(new Uint8Array([0x00, 0x12, 0x34, 0x56])); }); it('converts using little endian (as set)', (): void => { expect( - biToU8a(0x123456n, { bitLength: 32, isLe: true }) + nToU8a(0x123456n, { bitLength: 32, isLe: true }) ).toEqual(new Uint8Array([0x56, 0x34, 0x12, 0x00])); }); it('converts negative numbers', (): void => { expect( - biToU8a(-1234n, { isNegative: true }) + nToU8a(-1234n, { isNegative: true }) ).toEqual(new Uint8Array([46, 251])); }); it('converts negative numbers (BE)', (): void => { expect( - biToU8a(-1234n, { isLe: false, isNegative: true }) + nToU8a(-1234n, { isLe: false, isNegative: true }) ).toEqual(new Uint8Array([251, 46])); }); it('converts negative numbers (bitLength)', (): void => { expect( - biToU8a(-1234n, { bitLength: 32, isNegative: true }) + nToU8a(-1234n, { bitLength: 32, isNegative: true }) ).toEqual(new Uint8Array([46, 251, 255, 255])); }); - performance('biToU8a', 250000, ptest, biToU8a); + performance('nToU8a', 250000, ptest, nToU8a); }); diff --git a/packages/util/src/bi/toU8a.ts b/packages/util/src/bi/toU8a.ts index 5c1c6b0b4..1663c9633 100644 --- a/packages/util/src/bi/toU8a.ts +++ b/packages/util/src/bi/toU8a.ts @@ -6,7 +6,7 @@ import type { ToBigInt, ToBn, ToBnOptions } from '../types'; import { objectSpread } from '../object/spread'; import { _0n, _1n, _n } from './consts'; -import { biToBigInt } from './toBigInt'; +import { nToBigInt } from './toBigInt'; interface Options extends ToBnOptions { bitLength?: number; @@ -49,16 +49,16 @@ function toU8a (value: bigint, { isLe, isNegative }: Options): Uint8Array { } /** - * @name biToU8a + * @name nToU8a * @summary Creates a Uint8Array object from a bigint. */ -export function biToU8a (value?: ExtToBn | BN | bigint | number | null, options?: Options): Uint8Array { +export function nToU8a (value?: ExtToBn | BN | bigint | number | null, options?: Options): Uint8Array { const opts: Options = objectSpread( { bitLength: -1, isLe: true, isNegative: false }, options ); - const valueBi = biToBigInt(value); + const valueBi = nToBigInt(value); if (valueBi === _0n) { return createEmpty(opts);