Rename bi constants (consistency) (#1247)
* Rename bi constants (consistency) * sqrt constant * Rename bi* functions * secp256k1 also part of renames
This commit is contained in:
+2
-1
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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 <ExtToBn extends ToBn | ToBigInt> (value: ExtToBn | BN | bigint | string | number | null): bigint {
|
||||
const n = biToBigInt(value);
|
||||
export function nSqrt <ExtToBn extends ToBn | ToBigInt> (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;
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
@@ -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 <ExtToBn extends ToBigInt | ToBn> (value?: HexString | ExtToBn | BN | bigint | string | number | null): bigint {
|
||||
export function nToBigInt <ExtToBn extends ToBigInt | ToBn> (value?: HexString | ExtToBn | BN | bigint | string | number | null): bigint {
|
||||
return typeof value === 'bigint'
|
||||
? value
|
||||
: !value
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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 <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN | bigint | number | null, options?: Options): HexString {
|
||||
export function nToHex <ExtToBn extends ToBn | ToBigInt> (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
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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 <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN | bigint | number | null, options?: Options): Uint8Array {
|
||||
export function nToU8a <ExtToBn extends ToBn | ToBigInt> (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);
|
||||
|
||||
Reference in New Issue
Block a user