{"id":13817064,"url":"https://github.com/polywrap/as-bigint","last_synced_at":"2025-04-13T01:01:44.620Z","repository":{"id":43490237,"uuid":"354972043","full_name":"polywrap/as-bigint","owner":"polywrap","description":"BigInt is an AssemblyScript class for math with arbitrarily large integers","archived":false,"fork":false,"pushed_at":"2023-07-19T21:27:01.000Z","size":221,"stargazers_count":18,"open_issues_count":0,"forks_count":3,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-03-21T17:08:39.051Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/polywrap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-04-05T21:02:41.000Z","updated_at":"2024-10-12T15:53:41.000Z","dependencies_parsed_at":"2024-01-20T16:49:23.367Z","dependency_job_id":"9f01142f-d144-4e8d-866e-bf1105c132a6","html_url":"https://github.com/polywrap/as-bigint","commit_stats":{"total_commits":90,"total_committers":3,"mean_commits":30.0,"dds":0.5555555555555556,"last_synced_commit":"fd7ba9dce85b7183f2a3ac135dafe9b2927d59e6"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polywrap%2Fas-bigint","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polywrap%2Fas-bigint/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polywrap%2Fas-bigint/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polywrap%2Fas-bigint/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/polywrap","download_url":"https://codeload.github.com/polywrap/as-bigint/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650427,"owners_count":21139672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T06:00:32.759Z","updated_at":"2025-04-13T01:01:44.593Z","avatar_url":"https://github.com/polywrap.png","language":"TypeScript","funding_links":[],"categories":["Packages"],"sub_categories":[],"readme":"# BigInt\nBigInt is an AssemblyScript class for math with arbitrarily large integers.\n\n## Features\n\n- Fast arithmetic operations\n- Lightweight\n- Immutable instances\n- Core operations thoroughly tested\n\n## Getting Started\n\n### Installation\n`npm install as-bigint`  \nor  \n`yarn add as-bigint`\n\n### Quick start\n\n```typescript\nimport { BigInt } from \"as-bigint\"\n\n// generic constructor supports string and all native integer types\nconst generic: BigInt = BigInt.from(42);\n// read BigInt from string\nconst a: BigInt = BigInt.fromString(\"19374529734987892634530927528739060327972904713094389147895891798347509179347517\");\n// fromString and toString methods optionally take a radix argument\nconst b: BigInt = BigInt.fromString(\"9F59E5Ed123C10D57E92629612511b14628D2799\", 16);\n// for hex strings, a radix argument is not required if value is prefixed by 0x (or -0x for negative numbers)\nconst fromHex: BigInt = BigInt.fromString(\"0x9F59E5Ed123C10D57E92629612511b14628D2799\");\n\n// arithmetic (operator overloads: +, -, *, /, %, **)\nconst sum: BigInt = a.add(b);\nconst difference: BigInt = a.sub(b);\nconst product: BigInt = a.mul(b);\nconst quotient: BigInt = a.div(b);\nconst remainder: BigInt = a.mod(b);\nconst exponential: BigInt = a.pow(3);\nconst squared: BigInt = a.square();\nconst squareRoot: BigInt = a.sqrt();\nconst roundedQuotient: BigInt = a.roundedDiv(b);\n\n// faster operations when right-side variable is a 32 bit unsigned integer:\nconst c: u32 = 1234;\nconst intSum: BigInt = a.addInt(c);\nconst intDifference: BigInt = a.subInt(c);\nconst intProduct: BigInt = a.mulInt(c);\nconst intQuotient: BigInt = a.divInt(c);\nconst intRemainder: BigInt = a.modInt(c);\nconst intRoundedQuotient: BigInt = a.roundedDivInt(c);\n\n// fast multiply and divide by 2 or power of 2\nconst mulByTwo: BigInt = a.mul2();\nconst mulByEight: BigInt = a.mulPowTwo(3);\nconst divBuTwo: BigInt = a.div2();\nconst divBySixteen: BigInt = a.divPowTwo(4);\n\n// signed arithmetic bit shifts (operator overloads: \u003c\u003c, \u003e\u003e)\nconst shiftLeft3bits: BigInt = a.leftShift(3);\nconst shiftRight4bits: BigInt = a.rightShift(4);\n\n// bitwise operations (operator overloads: ~, \u0026, |, ^)\nconst not: BigInt = BigInt.bitwiseNot(bigIntA);\nconst and: BigInt = BigInt.bitwiseAnd(bigIntA, bigIntB);\nconst or: BigInt = BigInt.bitwiseOr(bigIntA, bigIntB);\nconst xor: BigInt = BigInt.bitwiseXor(bigIntA, bigIntB);\n\n// comparison operations (operator overloads: ==, !=, \u003c, \u003c=, \u003e, \u003e=)\nconst isEqual: boolean = a.eq(b);\nconst isNotEqual: boolean = a.ne(b);\nconst isLessThan: boolean = a.lt(b);\nconst isLessThanOrEqualTo: boolean = a.lte(b);\nconst isGreaterThan: boolean = a.gt(b);\nconst isGreaterThanOrEqualTo: boolean = a.gte(b);\n\n// binary arithmetic, comparison, and bitwise operators also have static implementations\nconst staticProduct: BigInt = BigInt.mul(a, b);\nconst staticIsEqual: boolean = BigInt.eq(a, b);\nconst staticAnd: boolean = BigInt.bitwiseAnd(a, b);\n\n// instantiate new copy, absolute value, or opposite\nconst sameNumber: BigInt = a.copy();\nconst positiveNumber: BigInt = a.abs();\nconst oppositeSign: BigInt = a.opposite();\n\n// convenience functions\nconst sizeOfNumber: i32 = a.countBits();\nconst isZeroNumber: boolean = a.isZero();\nconst zero: BigInt = BigInt.ZERO;\nconst one: BigInt = BigInt.ONE;\nconst negOne: BigInt = BigInt.NEG_ONE;\n\n// even faster constructors for small numbers (max values shown here)\nconst verySmall: BigInt = BigInt.fromUInt16(65535);\nconst verySmallSigned: BigInt = BigInt.fromInt16(-65535);\nconst prettySmall: BigInt = BigInt.fromUInt32(4294967295);\nconst prettySmallSigned: BigInt = BigInt.fromInt32(-4294967295);\nconst stillSmall: BigInt = BigInt.fromUInt64(18446744073709551615);\nconst stillSmallSigned: BigInt = BigInt.fromInt64(-18446744073709551615);\n\n// output to integers\nconst myInt32: i32 = BigInt.toInt32();\nconst myInt64: i64 = BigInt.toInt64();\nconst myUInt32: u32 = BigInt.toUInt32();\nconst myUInt64: u64 = BigInt.toUInt64();\n```\n\n## Development Status \u0026 Roadmap\n![CI](https://github.com/polywrap/as-bigint/actions/workflows/ci.yaml/badge.svg?branch=main)\n\n### Current Status\nOperation | Tests | Optimization\n--- | --- | ---\nAddition | Implemented | Complete\nSubtraction | Implemented | Complete\nMultiplication | Implemented | Up to ~1,500 bit numbers\nExponentiation | Implemented | Complete\nDivision | Implemented | Incomplete\nRemainder | Implemented | Incomplete\nSquare root | Implemented | Complete\nModular reduction | N/A | Not implemented\nRandom number generation | N/A | Not implemented\nCryptographic functions | N/A | Not implemented\n\nNote that operator overloads `\u003c\u003c`, `\u003e\u003e`, and `**` only support right-hand operands that fit in an i32--i.e. between the range (0, 2147483647].\n\n### TODO List\n*Priority based on blockchain-related use case; 1 is highest priority, 5 is lowest*\nTask | Description | Priority\n--- | --- | ---\nDivision optimization | A faster division algorithm is needed | 1\nModular reduction methods | Currently using division remainder for modulus; Implement Barret reduction, Montgomery reduction, Diminished Radix algorithms | 3\nRandom number generation | Implement function to generate random integers of arbitrary size | 4\nCryptographic algorithms | Implement functions used for cryptography (e.g., Greatest common divisor, primality tests, sha3) | 5\nMore multiplication optimization | Implement Karatsuba and Tom-Cook three-way multiplication for faster multiplication of numbers larger than 1,500 bits | 5\n\n## Contributing  \n\n### Build  \n`yarn build`  \n\n### Test  \n`yarn test`  \n\n### Lint\n`yarn lint`\n\nTo autofix lint errors:\n`yarn lint:fix`\n\n## Handling decimal numbers\n\nIf 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.\n\n## Handling fractions\n\nIf 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.\n\n## Acknowledgements\n\nPolywrap 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.\n\nThe BigInt method implementations are largely based on *BigNum Math: Implementing Cryptographic Multiple Precision Arithmetic 1st Edition*\nby Tom St Denis.\n\nAll bitwise operation methods are based on Google's [JSBI](https://github.com/GoogleChromeLabs/jsbi/blob/main/lib/jsbi.ts).\n\n## Contact\nPlease create an issue in this repository or email kris@dorg.tech\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolywrap%2Fas-bigint","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpolywrap%2Fas-bigint","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolywrap%2Fas-bigint/lists"}