BigInt is an AssemblyScript class for math with arbitrarily large integers.
- Fast arithmetic operations
- Lightweight
- Immutable instances
- Core operations thoroughly tested
npm install as-bigint
oryarn add as-bigint
import{BigInt}from"as-bigint"// generic constructor supports string and all native integer typesconstgeneric: BigInt=BigInt.from(42);// read BigInt from stringconsta: BigInt=BigInt.fromString("19374529734987892634530927528739060327972904713094389147895891798347509179347517");// fromString and toString methods optionally take a radix argumentconstb: BigInt=BigInt.fromString("9F59E5Ed123C10D57E92629612511b14628D2799",16);// for hex strings, a radix argument is not required if value is prefixed by 0x (or -0x for negative numbers)constfromHex: BigInt=BigInt.fromString("0x9F59E5Ed123C10D57E92629612511b14628D2799");// arithmetic (operator overloads: +, -, *, /, %, **)constsum: BigInt=a.add(b);constdifference: BigInt=a.sub(b);constproduct: BigInt=a.mul(b);constquotient: BigInt=a.div(b);constremainder: BigInt=a.mod(b);constexponential: BigInt=a.pow(3);constsquared: BigInt=a.square();constsquareRoot: BigInt=a.sqrt();constroundedQuotient: BigInt=a.roundedDiv(b);// faster operations when right-side variable is a 32 bit unsigned integer:constc: u32=1234;constintSum: BigInt=a.addInt(c);constintDifference: BigInt=a.subInt(c);constintProduct: BigInt=a.mulInt(c);constintQuotient: BigInt=a.divInt(c);constintRemainder: BigInt=a.modInt(c);constintRoundedQuotient: BigInt=a.roundedDivInt(c);// fast multiply and divide by 2 or power of 2constmulByTwo: BigInt=a.mul2();constmulByEight: BigInt=a.mulPowTwo(3);constdivBuTwo: BigInt=a.div2();constdivBySixteen: BigInt=a.divPowTwo(4);// signed arithmetic bit shifts (operator overloads: <<, >>)constshiftLeft3bits: BigInt=a.leftShift(3);constshiftRight4bits: BigInt=a.rightShift(4);// bitwise operations (operator overloads: ~, &, |, ^)constnot: BigInt=BigInt.bitwiseNot(bigIntA);constand: BigInt=BigInt.bitwiseAnd(bigIntA,bigIntB);constor: BigInt=BigInt.bitwiseOr(bigIntA,bigIntB);constxor: BigInt=BigInt.bitwiseXor(bigIntA,bigIntB);// comparison operations (operator overloads: ==, !=, <, <=, >, >=)constisEqual: boolean=a.eq(b);constisNotEqual: boolean=a.ne(b);constisLessThan: boolean=a.lt(b);constisLessThanOrEqualTo: boolean=a.lte(b);constisGreaterThan: boolean=a.gt(b);constisGreaterThanOrEqualTo: boolean=a.gte(b);// binary arithmetic, comparison, and bitwise operators also have static implementationsconststaticProduct: BigInt=BigInt.mul(a,b);conststaticIsEqual: boolean=BigInt.eq(a,b);conststaticAnd: boolean=BigInt.bitwiseAnd(a,b);// instantiate new copy, absolute value, or oppositeconstsameNumber: BigInt=a.copy();constpositiveNumber: BigInt=a.abs();constoppositeSign: BigInt=a.opposite();// convenience functionsconstsizeOfNumber: i32=a.countBits();constisZeroNumber: boolean=a.isZero();constzero: BigInt=BigInt.ZERO;constone: BigInt=BigInt.ONE;constnegOne: BigInt=BigInt.NEG_ONE;// even faster constructors for small numbers (max values shown here)constverySmall: BigInt=BigInt.fromUInt16(65535);constverySmallSigned: BigInt=BigInt.fromInt16(-65535);constprettySmall: BigInt=BigInt.fromUInt32(4294967295);constprettySmallSigned: BigInt=BigInt.fromInt32(-4294967295);conststillSmall: BigInt=BigInt.fromUInt64(18446744073709551615);conststillSmallSigned: BigInt=BigInt.fromInt64(-18446744073709551615);// output to integersconstmyInt32: i32=BigInt.toInt32();constmyInt64: i64=BigInt.toInt64();constmyUInt32: u32=BigInt.toUInt32();constmyUInt64: u64=BigInt.toUInt64();| Operation | Tests | Optimization |
|---|---|---|
| Addition | Implemented | Complete |
| Subtraction | Implemented | Complete |
| Multiplication | Implemented | Up to ~1,500 bit numbers |
| Exponentiation | Implemented | Complete |
| Division | Implemented | Incomplete |
| Remainder | Implemented | Incomplete |
| Square root | Implemented | Complete |
| Modular reduction | N/A | Not implemented |
| Random number generation | N/A | Not implemented |
| Cryptographic functions | N/A | Not implemented |
Note that operator overloads <<, >>, and ** only support right-hand operands that fit in an i32--i.e. between the range (0, 2147483647].
Priority based on blockchain-related use case; 1 is highest priority, 5 is lowest
| Task | Description | Priority |
|---|---|---|
| Division optimization | A faster division algorithm is needed | 1 |
| Modular reduction methods | Currently using division remainder for modulus; Implement Barret reduction, Montgomery reduction, Diminished Radix algorithms | 3 |
| Random number generation | Implement function to generate random integers of arbitrary size | 4 |
| Cryptographic algorithms | Implement functions used for cryptography (e.g., Greatest common divisor, primality tests, sha3) | 5 |
| More multiplication optimization | Implement Karatsuba and Tom-Cook three-way multiplication for faster multiplication of numbers larger than 1,500 bits | 5 |
yarn build
yarn test
yarn lint
To autofix lint errors:
yarn lint:fix
If you need to work with arbitrarily large decimal numbers, check out as-bignumber: https://github.com/polywrap/as-bignumber. The BigNumber class is built on top of BigInt for high-performance decimal arithmetic.
If you need to work with numbers represented as fractions, check out as-fraction: https://github.com/polywrap/as-fraction. The Fraction class is built on top of BigInt for high-performance fraction arithmetic.
Polywrap developed BigInt to use in the development tools we produce for fast, language-agnostic decentralized API development. Polywrap allows developers to interact with any web3 protocol from any language, making between-protocol composition easy. Learn more at https://polywrap.io.
The BigInt method implementations are largely based on BigNum Math: Implementing Cryptographic Multiple Precision Arithmetic 1st Edition by Tom St Denis.
All bitwise operation methods are based on Google's JSBI.
Please create an issue in this repository or email kris@dorg.tech