npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fetsorn/as-bigint

v0.6.0

Published

AssemblyScript package for math with arbitrarily large integers

Downloads

3

Readme

BigInt

BigInt is an AssemblyScript class for math with arbitrarily large integers.

Features

  • Fast arithmetic operations
  • Lightweight
  • Immutable instances
  • Core operations thoroughly tested

Getting Started

Installation

npm install as-bigint
or
yarn add as-bigint

Quick start

import { BigInt } from "as-bigint"

// generic constructor supports string and all native integer types
const generic: BigInt = BigInt.from(42);
// read BigInt from string
const a: BigInt = BigInt.fromString("19374529734987892634530927528739060327972904713094389147895891798347509179347517");
// fromString and toString methods optionally take a radix argument
const b: 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)
const fromHex: BigInt = BigInt.fromString("0x9F59E5Ed123C10D57E92629612511b14628D2799");

// arithmetic (operator overloads: +, -, *, /, %, **)
const sum: BigInt = a.add(b);
const difference: BigInt = a.sub(b);
const product: BigInt = a.mul(b);
const quotient: BigInt = a.div(b);
const remainder: BigInt = a.mod(b);
const exponential: BigInt = a.pow(3);
const squared: BigInt = a.square();
const squareRoot: BigInt = a.sqrt();
const roundedQuotient: BigInt = a.roundedDiv(b);

// faster operations when right-side variable is a 32 bit unsigned integer:
const c: u32 = 1234;
const intSum: BigInt = a.addInt(c);
const intDifference: BigInt = a.subInt(c);
const intProduct: BigInt = a.mulInt(c);
const intQuotient: BigInt = a.divInt(c);
const intRemainder: BigInt = a.modInt(c);
const intRoundedQuotient: BigInt = a.roundedDivInt(c);

// fast multiply and divide by 2 or power of 2
const mulByTwo: BigInt = a.mul2();
const mulByEight: BigInt = a.mulPowTwo(3);
const divBuTwo: BigInt = a.div2();
const divBySixteen: BigInt = a.divPowTwo(4);

// signed arithmetic bit shifts (operator overloads: <<, >>)
const shiftLeft3bits: BigInt = a.leftShift(3);
const shiftRight4bits: BigInt = a.rightShift(4);

// bitwise operations (operator overloads: ~, &, |, ^)
const not: BigInt = BigInt.bitwiseNot(bigIntA);
const and: BigInt = BigInt.bitwiseAnd(bigIntA, bigIntB);
const or: BigInt = BigInt.bitwiseOr(bigIntA, bigIntB);
const xor: BigInt = BigInt.bitwiseXor(bigIntA, bigIntB);

// comparison operations (operator overloads: ==, !=, <, <=, >, >=)
const isEqual: boolean = a.eq(b);
const isNotEqual: boolean = a.ne(b);
const isLessThan: boolean = a.lt(b);
const isLessThanOrEqualTo: boolean = a.lte(b);
const isGreaterThan: boolean = a.gt(b);
const isGreaterThanOrEqualTo: boolean = a.gte(b);

// binary arithmetic, comparison, and bitwise operators also have static implementations
const staticProduct: BigInt = BigInt.mul(a, b);
const staticIsEqual: boolean = BigInt.eq(a, b);
const staticAnd: boolean = BigInt.bitwiseAnd(a, b);

// instantiate new copy, absolute value, or opposite
const sameNumber: BigInt = a.copy();
const positiveNumber: BigInt = a.abs();
const oppositeSign: BigInt = a.opposite();

// convenience functions
const sizeOfNumber: i32 = a.countBits();
const isZeroNumber: boolean = a.isZero();
const zero: BigInt = BigInt.ZERO;
const one: BigInt = BigInt.ONE;
const negOne: BigInt = BigInt.NEG_ONE;

// even faster constructors for small numbers (max values shown here)
const verySmall: BigInt = BigInt.fromUInt16(65535);
const verySmallSigned: BigInt = BigInt.fromInt16(-65535);
const prettySmall: BigInt = BigInt.fromUInt32(4294967295);
const prettySmallSigned: BigInt = BigInt.fromInt32(-4294967295);
const stillSmall: BigInt = BigInt.fromUInt64(18446744073709551615);
const stillSmallSigned: BigInt = BigInt.fromInt64(-18446744073709551615);

// output to integers
const myInt32: i32 = BigInt.toInt32();
const myInt64: i64 = BigInt.toInt64();
const myUInt32: u32 = BigInt.toUInt32();
const myUInt64: u64 = BigInt.toUInt64();

Development Status & Roadmap

CI

Current Status

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].

TODO List

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

Contributing

Build

yarn build

Test

yarn test

Lint

yarn lint

To autofix lint errors: yarn lint:fix

Handling decimal numbers

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.

Handling fractions

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.

Acknowledgements

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.

Contact

Please create an issue in this repository or email [email protected]