UNPKG

@isdk/bigint

Version:

The BigInteger class wrapped bn.js and native BitInt

1,104 lines (611 loc) 20.3 kB
[@isdk/bigint](../README.md) / [Exports](../modules.md) / BigIntNative # Class: BigIntNative The Big Integer implementation of basic operations that wraps the native BigInt library. Operations are not constant time, but we try and limit timing leakage where we can ## Implements - [`IBigInt`](../interfaces/IBigInt.md) ## Table of contents ### Constructors - [constructor](BigIntNative.md#constructor) ### Properties - [value](BigIntNative.md#value) ### Methods - [\_\_egcd](BigIntNative.md#__egcd) - [\_egcd](BigIntNative.md#_egcd) - [abs](BigIntNative.md#abs) - [add](BigIntNative.md#add) - [bitLength](BigIntNative.md#bitlength) - [byteLength](BigIntNative.md#bytelength) - [clone](BigIntNative.md#clone) - [dec](BigIntNative.md#dec) - [equal](BigIntNative.md#equal) - [gcd](BigIntNative.md#gcd) - [getBit](BigIntNative.md#getbit) - [gt](BigIntNative.md#gt) - [gte](BigIntNative.md#gte) - [iadd](BigIntNative.md#iadd) - [idec](BigIntNative.md#idec) - [iinc](BigIntNative.md#iinc) - [ileftShift](BigIntNative.md#ileftshift) - [imod](BigIntNative.md#imod) - [imul](BigIntNative.md#imul) - [inc](BigIntNative.md#inc) - [irightShift](BigIntNative.md#irightshift) - [isEven](BigIntNative.md#iseven) - [isNegative](BigIntNative.md#isnegative) - [isOne](BigIntNative.md#isone) - [isZero](BigIntNative.md#iszero) - [isub](BigIntNative.md#isub) - [iumod](BigIntNative.md#iumod) - [leftShift](BigIntNative.md#leftshift) - [lt](BigIntNative.md#lt) - [lte](BigIntNative.md#lte) - [mod](BigIntNative.md#mod) - [modExp](BigIntNative.md#modexp) - [modInv](BigIntNative.md#modinv) - [mul](BigIntNative.md#mul) - [rightShift](BigIntNative.md#rightshift) - [sub](BigIntNative.md#sub) - [toNumber](BigIntNative.md#tonumber) - [toString](BigIntNative.md#tostring) - [toUint8Array](BigIntNative.md#touint8array) - [umod](BigIntNative.md#umod) ## Constructors ### constructor **new BigIntNative**(`n`) Get a BigInteger (input must be big endian for strings and arrays) **`Throws`** on null or undefined input #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `n` | `string` \| `number` \| `bigint` \| `boolean` \| `Uint8Array` | Value to convert | #### Defined in [src/native.ts:16](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L16) ## Properties ### value **value**: `bigint` #### Defined in [src/native.ts:10](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L10) ## Methods ### \_\_egcd **__egcd**(`b`): `Object` #### Parameters | Name | Type | | :------ | :------ | | `b` | `bigint` | #### Returns `Object` | Name | Type | | :------ | :------ | | `gcd` | `bigint` | | `x` | `bigint` | | `y` | `bigint` | #### Defined in [src/native.ts:201](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L201) ___ ### \_egcd **_egcd**(`b`): `Object` Extended Eucleadian algorithm (http://anh.cs.luc.edu/331/notes/xgcd.pdf) Given a = this and b, compute (x, y) such that ax + by = gdc(a, b) #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `b` | `any` | Second operand | #### Returns `Object` | Name | Type | | :------ | :------ | | `gcd` | [`BigIntNative`](BigIntNative.md) | | `x` | [`BigIntNative`](BigIntNative.md) | | `y` | [`BigIntNative`](BigIntNative.md) | #### Defined in [src/native.ts:234](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L234) ___ ### abs **abs**(): [`BigIntNative`](BigIntNative.md) #### Returns [`BigIntNative`](BigIntNative.md) #### Implementation of [IBigInt](../interfaces/IBigInt.md).[abs](../interfaces/IBigInt.md#abs) #### Defined in [src/native.ts:357](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L357) ___ ### add **add**(`x`): [`BigIntNative`](BigIntNative.md) BigInteger addition #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | Value to add | #### Returns [`BigIntNative`](BigIntNative.md) this + x. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[add](../interfaces/IBigInt.md#add) #### Defined in [src/native.ts:80](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L80) ___ ### bitLength **bitLength**(): `number` Compute bit length #### Returns `number` Bit length. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[bitLength](../interfaces/IBigInt.md#bitlength) #### Defined in [src/native.ts:403](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L403) ___ ### byteLength **byteLength**(): `number` Compute byte length #### Returns `number` Byte length. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[byteLength](../interfaces/IBigInt.md#bytelength) #### Defined in [src/native.ts:424](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L424) ___ ### clone **clone**(): [`BigIntNative`](BigIntNative.md) return a new IBigInteger object with the same value #### Returns [`BigIntNative`](BigIntNative.md) #### Implementation of [IBigInt](../interfaces/IBigInt.md).[clone](../interfaces/IBigInt.md#clone) #### Defined in [src/native.ts:34](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L34) ___ ### dec **dec**(`n?`): [`BigIntNative`](BigIntNative.md) IBigInteger decrement #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `n?` | `number` | (optional) Value to decrement, defaults to 1 | #### Returns [`BigIntNative`](BigIntNative.md) this - 1. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[dec](../interfaces/IBigInt.md#dec) #### Defined in [src/native.ts:62](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L62) ___ ### equal **equal**(`x`): `boolean` Whether this value is equal to x #### Parameters | Name | Type | | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | #### Returns `boolean` #### Implementation of [IBigInt](../interfaces/IBigInt.md).[equal](../interfaces/IBigInt.md#equal) #### Defined in [src/native.ts:301](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L301) ___ ### gcd **gcd**(`b`): [`BigIntNative`](BigIntNative.md) Compute greatest common divisor between this and n #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `b` | `any` | Operand | #### Returns [`BigIntNative`](BigIntNative.md) gcd #### Implementation of [IBigInt](../interfaces/IBigInt.md).[gcd](../interfaces/IBigInt.md#gcd) #### Defined in [src/native.ts:249](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L249) ___ ### getBit **getBit**(`i`): ``1`` \| ``0`` Get value of i-th bit #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `i` | `number` | Bit index | #### Returns ``1`` \| ``0`` Bit value. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[getBit](../interfaces/IBigInt.md#getbit) #### Defined in [src/native.ts:394](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L394) ___ ### gt **gt**(`x`): `boolean` Whether this value is greater than x #### Parameters | Name | Type | | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | #### Returns `boolean` #### Implementation of [IBigInt](../interfaces/IBigInt.md).[gt](../interfaces/IBigInt.md#gt) #### Defined in [src/native.ts:328](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L328) ___ ### gte **gte**(`x`): `boolean` Whether this value is greater than or equal to x #### Parameters | Name | Type | | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | #### Returns `boolean` #### Implementation of [IBigInt](../interfaces/IBigInt.md).[gte](../interfaces/IBigInt.md#gte) #### Defined in [src/native.ts:337](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L337) ___ ### iadd **iadd**(`x`): [`BigIntNative`](BigIntNative.md) BigInteger addition in place #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | Value to add | #### Returns [`BigIntNative`](BigIntNative.md) #### Implementation of [IBigInt](../interfaces/IBigInt.md).[iadd](../interfaces/IBigInt.md#iadd) #### Defined in [src/native.ts:70](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L70) ___ ### idec **idec**(`n?`): [`BigIntNative`](BigIntNative.md) IBigInteger decrement number n in place #### Parameters | Name | Type | Default value | Description | | :------ | :------ | :------ | :------ | | `n` | `number` | `1` | (optional) Value to decrement, defaults to 1 | #### Returns [`BigIntNative`](BigIntNative.md) #### Implementation of [IBigInt](../interfaces/IBigInt.md).[idec](../interfaces/IBigInt.md#idec) #### Defined in [src/native.ts:52](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L52) ___ ### iinc **iinc**(`n?`): [`BigIntNative`](BigIntNative.md) IBigInteger increment number n in place #### Parameters | Name | Type | Default value | Description | | :------ | :------ | :------ | :------ | | `n` | `number` | `1` | (optional) Value to increment, defaults to 1 | #### Returns [`BigIntNative`](BigIntNative.md) #### Implementation of [IBigInt](../interfaces/IBigInt.md).[iinc](../interfaces/IBigInt.md#iinc) #### Defined in [src/native.ts:38](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L38) ___ ### ileftShift **ileftShift**(`x`): [`BigIntNative`](BigIntNative.md) Shift this to the left by x, in place #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | Shift value | #### Returns [`BigIntNative`](BigIntNative.md) #### Implementation of [IBigInt](../interfaces/IBigInt.md).[ileftShift](../interfaces/IBigInt.md#ileftshift) #### Defined in [src/native.ts:264](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L264) ___ ### imod **imod**(`m`): [`BigIntNative`](BigIntNative.md) Compute value modulo m, in place #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `m` | [`BigIntNative`](BigIntNative.md) | Modulo | #### Returns [`BigIntNative`](BigIntNative.md) #### Implementation of [IBigInt](../interfaces/IBigInt.md).[imod](../interfaces/IBigInt.md#imod) #### Defined in [src/native.ts:120](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L120) ___ ### imul **imul**(`x`): [`BigIntNative`](BigIntNative.md) BigInteger multiplication in place #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | Value to multiply | #### Returns [`BigIntNative`](BigIntNative.md) #### Implementation of [IBigInt](../interfaces/IBigInt.md).[imul](../interfaces/IBigInt.md#imul) #### Defined in [src/native.ts:106](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L106) ___ ### inc **inc**(`n?`): [`BigIntNative`](BigIntNative.md) IBigInteger increment #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `n?` | `number` | (optional) Value to increment, defaults to 1 | #### Returns [`BigIntNative`](BigIntNative.md) this + 1. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[inc](../interfaces/IBigInt.md#inc) #### Defined in [src/native.ts:48](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L48) ___ ### irightShift **irightShift**(`x`): [`BigIntNative`](BigIntNative.md) Shift this to the right by x, in place #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | Shift value | #### Returns [`BigIntNative`](BigIntNative.md) #### Implementation of [IBigInt](../interfaces/IBigInt.md).[irightShift](../interfaces/IBigInt.md#irightshift) #### Defined in [src/native.ts:282](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L282) ___ ### isEven **isEven**(): `boolean` #### Returns `boolean` #### Implementation of [IBigInt](../interfaces/IBigInt.md).[isEven](../interfaces/IBigInt.md#iseven) #### Defined in [src/native.ts:353](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L353) ___ ### isNegative **isNegative**(): `boolean` #### Returns `boolean` #### Implementation of [IBigInt](../interfaces/IBigInt.md).[isNegative](../interfaces/IBigInt.md#isnegative) #### Defined in [src/native.ts:349](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L349) ___ ### isOne **isOne**(): `boolean` #### Returns `boolean` #### Implementation of [IBigInt](../interfaces/IBigInt.md).[isOne](../interfaces/IBigInt.md#isone) #### Defined in [src/native.ts:345](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L345) ___ ### isZero **isZero**(): `boolean` #### Returns `boolean` #### Implementation of [IBigInt](../interfaces/IBigInt.md).[isZero](../interfaces/IBigInt.md#iszero) #### Defined in [src/native.ts:341](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L341) ___ ### isub **isub**(`x`): [`BigIntNative`](BigIntNative.md) BigInteger subtraction in place #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | Value to subtract | #### Returns [`BigIntNative`](BigIntNative.md) #### Implementation of [IBigInt](../interfaces/IBigInt.md).[isub](../interfaces/IBigInt.md#isub) #### Defined in [src/native.ts:88](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L88) ___ ### iumod **iumod**(`m`): [`BigIntNative`](BigIntNative.md) Compute value modulo m, in place #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `m` | [`BigIntNative`](BigIntNative.md) | Modulo | #### Returns [`BigIntNative`](BigIntNative.md) #### Defined in [src/native.ts:129](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L129) ___ ### leftShift **leftShift**(`x`): [`BigIntNative`](BigIntNative.md) Shift this to the left by x #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | Shift value | #### Returns [`BigIntNative`](BigIntNative.md) this << x. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[leftShift](../interfaces/IBigInt.md#leftshift) #### Defined in [src/native.ts:274](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L274) ___ ### lt **lt**(`x`): `boolean` Whether this value is less than x #### Parameters | Name | Type | | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | #### Returns `boolean` #### Implementation of [IBigInt](../interfaces/IBigInt.md).[lt](../interfaces/IBigInt.md#lt) #### Defined in [src/native.ts:310](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L310) ___ ### lte **lte**(`x`): `boolean` Whether this value is less than or equal to x #### Parameters | Name | Type | | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | #### Returns `boolean` #### Implementation of [IBigInt](../interfaces/IBigInt.md).[lte](../interfaces/IBigInt.md#lte) #### Defined in [src/native.ts:319](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L319) ___ ### mod **mod**(`m`): [`BigIntNative`](BigIntNative.md) Compute value modulo m #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `m` | [`BigIntNative`](BigIntNative.md) | Modulo | #### Returns [`BigIntNative`](BigIntNative.md) this mod m. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[mod](../interfaces/IBigInt.md#mod) #### Defined in [src/native.ts:152](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L152) ___ ### modExp **modExp**(`e`, `n`): [`BigIntNative`](BigIntNative.md) Compute modular exponentiation Much faster than this.exp(e).mod(n) #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `e` | [`BigIntNative`](BigIntNative.md) | Exponent | | `n` | [`BigIntNative`](BigIntNative.md) | Modulo | #### Returns [`BigIntNative`](BigIntNative.md) this ** e mod n. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[modExp](../interfaces/IBigInt.md#modexp) #### Defined in [src/native.ts:156](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L156) ___ ### modInv **modInv**(`n`): [`BigIntNative`](BigIntNative.md) Compute the inverse of this value modulo n Note: this and and n must be relatively prime **`Throws`** if the inverse does not exist #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `n` | [`BigIntNative`](BigIntNative.md) | Modulo | #### Returns [`BigIntNative`](BigIntNative.md) x such that this*x = 1 mod n #### Implementation of [IBigInt](../interfaces/IBigInt.md).[modInv](../interfaces/IBigInt.md#modinv) #### Defined in [src/native.ts:191](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L191) ___ ### mul **mul**(`x`): [`BigIntNative`](BigIntNative.md) BigInteger multiplication #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | Value to multiply | #### Returns [`BigIntNative`](BigIntNative.md) this * x. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[mul](../interfaces/IBigInt.md#mul) #### Defined in [src/native.ts:116](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L116) ___ ### rightShift **rightShift**(`x`): [`BigIntNative`](BigIntNative.md) Shift this to the right by x #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | Shift value | #### Returns [`BigIntNative`](BigIntNative.md) this >> x. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[rightShift](../interfaces/IBigInt.md#rightshift) #### Defined in [src/native.ts:292](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L292) ___ ### sub **sub**(`x`): [`BigIntNative`](BigIntNative.md) BigInteger subtraction #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `x` | [`BigIntNative`](BigIntNative.md) | Value to subtract | #### Returns [`BigIntNative`](BigIntNative.md) this - x. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[sub](../interfaces/IBigInt.md#sub) #### Defined in [src/native.ts:98](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L98) ___ ### toNumber **toNumber**(): `number` Get this value as an exact Number (max 53 bits) Fails if this value is too large #### Returns `number` #### Implementation of [IBigInt](../interfaces/IBigInt.md).[toNumber](../interfaces/IBigInt.md#tonumber) #### Defined in [src/native.ts:379](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L379) ___ ### toString **toString**(): `string` Get this value as a string #### Returns `string` this value. #### Implementation of [IBigInt](../interfaces/IBigInt.md).[toString](../interfaces/IBigInt.md#tostring) #### Defined in [src/native.ts:370](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L370) ___ ### toUint8Array **toUint8Array**(`endian?`, `length?`): `Uint8Array` Get Uint8Array representation of this number #### Parameters | Name | Type | Default value | Description | | :------ | :------ | :------ | :------ | | `endian` | `string` | `'be'` | Endianess of output array (defaults to 'be') | | `length?` | `number` | `undefined` | Of output array | #### Returns `Uint8Array` #### Implementation of [IBigInt](../interfaces/IBigInt.md).[toUint8Array](../interfaces/IBigInt.md#touint8array) #### Defined in [src/native.ts:445](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L445) ___ ### umod **umod**(`m`): [`BigIntNative`](BigIntNative.md) Compute value modulo m #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `m` | [`BigIntNative`](BigIntNative.md) | Modulo | #### Returns [`BigIntNative`](BigIntNative.md) this mod m. #### Defined in [src/native.ts:143](https://github.com/isdk/bigint.js/blob/c098291/src/native.ts#L143)