Additional adjustments on newBigInt (#1241)

* Additional adjustments on newBigInt

* typing
This commit is contained in:
Jaco
2021-11-25 11:34:34 +02:00
committed by GitHub
parent dbb77f35b6
commit a8f13bd387
7 changed files with 47 additions and 59 deletions
+22 -22
View File
@@ -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;
+16 -10
View File
@@ -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);
-1
View File
@@ -7,7 +7,6 @@
export * from './consts';
export { newBigInt } from './new';
export { biSqrt } from './sqrt';
export { biToBigInt } from './toBigInt';
export { biMax } from './max';
-16
View File
@@ -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;
}
+2 -3
View File
@@ -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
+3 -4
View File
@@ -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
+4 -3
View File
@@ -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);
}