Rename internal BigInt helper (#1244)

* Rename internal BigInt helper

* Consistency
This commit is contained in:
Jaco
2021-11-25 17:16:09 +02:00
committed by GitHub
parent b0b0151071
commit 9821eba11f
5 changed files with 44 additions and 46 deletions
+9 -9
View File
@@ -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);
+5 -5
View File
@@ -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 <ExtToBn extends ToBn | ToBigInt> (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 <ExtToBn extends ToBn | ToBigInt> (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;
}
+6 -6
View File
@@ -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 <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN
const valueBi = biToBigInt(value);
if (valueBi === BI_ZERO) {
if (valueBi === _0n) {
return createEmpty(opts);
}
+4 -4
View File
@@ -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);
}