Ensure BigInt(...) is called conditionally (#1240)

* Ensure BigInt(...) is called consitionally

* adjust

* comments

* adjust
This commit is contained in:
Jaco
2021-11-25 10:59:26 +02:00
committed by GitHub
parent b102d9c2b3
commit c60e35ee9e
6 changed files with 89 additions and 33 deletions
+20 -4
View File
@@ -1,26 +1,42 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { newBigInt } from './new';
/**
* @name BI_ZERO
* @summary BigInt constant for 0.
*/
export const BI_ZERO = newBigInt(0);
/**
* @name BI_ONE
* @summary BigInt constant for 0.
*/
export const BI_ONE = newBigInt(1);
/**
* @name BI_MILLION
* @summary BigInt constant for 1,000,000.
*/
export const BI_MILLION = 1_000_000n;
export const BI_MILLION = newBigInt(1_000_000);
/**
* @name BI_BILLION
* @summary BigInt constant for 1,000,000,000.
*/
export const BI_BILLION = 1_000_000_000n;
export const BI_BILLION = newBigInt(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;
export const BI_QUINTILL = BI_BILLION
? BI_BILLION * BI_BILLION
: undefined;
/**
* @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 = newBigInt(Number.MAX_SAFE_INTEGER);
+1
View File
@@ -7,6 +7,7 @@
export * from './consts';
export { newBigInt } from './new';
export { biSqrt } from './sqrt';
export { biToBigInt } from './toBigInt';
export { biMax } from './max';
+16
View File
@@ -0,0 +1,16 @@
// 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;
}
+6 -5
View File
@@ -5,10 +5,11 @@ import type { BN } from '../bn';
import type { ToBigInt, ToBn } from '../types';
import { assert } from '../assert';
import { BI_MAX_INTEGER } from './consts';
import { BI_MAX_INTEGER, BI_ONE, BI_ZERO } from './consts';
import { newBigInt } from './new';
import { biToBigInt } from './toBigInt';
const SQRT_MAX_SAFE_INTEGER = 94906265n;
const SQRT_MAX_SAFE_INTEGER = newBigInt(94906265);
/**
* @name biSqrt
@@ -17,7 +18,7 @@ const SQRT_MAX_SAFE_INTEGER = 94906265n;
export function biSqrt <ExtToBn extends ToBn | ToBigInt> (value: ExtToBn | BN | bigint | string | number | null): bigint {
const n = biToBigInt(value);
assert(n >= 0n, 'square root of negative numbers is not supported');
assert(n >= BI_ZERO, '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 +31,9 @@ export function biSqrt <ExtToBn extends ToBn | ToBigInt> (value: ExtToBn | BN |
let x0 = SQRT_MAX_SAFE_INTEGER;
while (true) {
const x1 = ((n / x0) + x0) >> 1n;
const x1 = ((n / x0) + x0) >> BI_ONE;
if (x0 === x1 || (x0 === (x1 - 1n))) {
if (x0 === x1 || (x0 === (x1 - BI_ONE))) {
return x0;
}
+8 -5
View File
@@ -5,13 +5,16 @@ 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 { biToBigInt } from './toBigInt';
interface Options extends ToBnOptions {
bitLength?: number;
}
const DIV = 256n;
const DIV = newBigInt(256);
const NEG_MASK = newBigInt(0xff);
function createEmpty ({ bitLength = 0 }: Options): Uint8Array {
return bitLength === -1
@@ -23,14 +26,14 @@ function toU8a (value: bigint, { isLe, isNegative }: Options): Uint8Array {
const arr: number[] = [];
if (isNegative) {
value = (value + 1n) * -1n;
value = (value + BI_ONE) * -BI_ONE;
}
while (value !== 0n) {
while (value !== BI_ZERO) {
const mod = value % DIV;
const val = Number(
isNegative
? mod ^ 0xffn
? mod ^ NEG_MASK
: mod
);
@@ -58,7 +61,7 @@ export function biToU8a <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN
const valueBi = biToBigInt(value);
if (valueBi === 0n) {
if (valueBi === BI_ZERO) {
return createEmpty(opts);
}