{"id":15608650,"url":"https://github.com/hypercubed/as-mpz","last_synced_at":"2025-10-18T14:33:46.649Z","repository":{"id":213891373,"uuid":"735194398","full_name":"Hypercubed/as-mpz","owner":"Hypercubed","description":"Arbitrary precision integer library for AssemblyScript.","archived":false,"fork":false,"pushed_at":"2024-02-23T04:08:55.000Z","size":958,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-12-21T23:18:20.953Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/Hypercubed.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-24T02:18:54.000Z","updated_at":"2024-02-10T19:15:06.000Z","dependencies_parsed_at":"2023-12-28T07:42:10.498Z","dependency_job_id":"c3f4d56d-b940-43a3-a84f-b77fb4c39546","html_url":"https://github.com/Hypercubed/as-mpz","commit_stats":null,"previous_names":["hypercubed/as-mpz"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hypercubed%2Fas-mpz","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hypercubed%2Fas-mpz/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hypercubed%2Fas-mpz/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hypercubed%2Fas-mpz/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Hypercubed","download_url":"https://codeload.github.com/Hypercubed/as-mpz/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233217152,"owners_count":18643036,"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-10-03T05:21:46.527Z","updated_at":"2025-10-18T14:33:41.577Z","avatar_url":"https://github.com/Hypercubed.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- ⚠️ This README has been generated from the file(s) \"blueprint.md\" ⚠️--\u003e\n\n[](#-pkgname-)\n\n# @hypercubed/as-mpz\n\nImmutable multiple precision integer library for AssemblyScript.\n\n[](#features)\n\n## Features\n\n- Multiple precision integers (theoretically limited by memory)\n- Fast (extensivly benchmarked)\n- Accurate\\* (tested against JavaScript BigInt)\n- For use in AssemblyScript projects\n- No dependencies\n\n[](#-disclaimer)\n\n## \\* Disclaimer\n\n\u003e While `as-mpz`` has undergone rigorous testing and benchmarking to ensure its reliability and performance, the developers would like to emphasize that the library is provided \"as is,\" and they assume no responsibility for any issues that may arise from its use.\n\n[](#installations)\n\n## Installations\n\n```sh\nnpm install @hypercubed/as-mpz\n```\n\n[](#quick-start)\n\n## Quick Start\n\n```ts\nimport { MpZ } from '@hypercubed/as-mpz';\n\nconst a = MpZ.from(18448972);\nconst b = MpZ.from('7881297289452930');\nconst c = MpZ.from('0x583F99D51C76AB3DEAB75');\n\nconst c = a.add(b).mul(c);\n```\n\nOr using operators:\n\n```ts\nimport { MpZ } from '@hypercubed/as-mpz';\n\nconst a = MpZ.from(18448972);\nconst b = MpZ.from('7881297289452930');\nconst c = MpZ.from('0x583F99D51C76AB3DEAB75');\n\nconst c = (a + b) * c;\n```\n\n\u003e Note: Arithmatic methods and operators can be used interchangably, with operators acting as shorthand for the methods.\n\u003e However, unlike instance methods, the operators do not coerce inputs to an MpZ.\n\n[](#as-mpz-api)\n\n## `as-mpz` API\n\nValue is stored as a sign and magnitude.\n\n\u003e Note: Arithmatic methods and operators can be used interchangably, with operators acting as shorthand for the methods.\n\u003e However, unlike instance methods, the operators do not coerce inputs to an MpZ.\n\n### Constructor\n\n#### `MpZ.from(value: i32 | u32 | i64 | u64 | string): MpZ`\n\nCreates a new MpZ from a number or string. The `MpZ.from` method accepts a number or string. The string can be in decimal or hexadecimal format (prefixed with `0x`). The string can also be prefixed with `-` to indicate a negative number.\n\n\u003e Note: The MpZ class should not be instantiated directly (using `new`). Instead use the static `MpZ.from` method to create a new MpZ.\n\n### Instance Methods\n\n#### `#isNeg(): boolean`\n\nReturns `true` if `this` MpZ is negative, otherwise `false`.\n\n#### `#abs(): MpZ`\n\nReturns the absolute value of `this` MpZ.\n\n#### `#sign(): MpZ`\n\nReturns the sign of `this` MpZ, indicating whether x is positive (`1`), negative (`-1`), or zero (`0`).\n\n#### `#isOdd(): MpZ`\n\nReturns `true` if `this` MpZ is odd, otherwise `false`.\n\n#### `#isEven(): boolean`\n\nReturns `true` if `this` MpZ is even, otherwise `false`.\n\n#### `#negate(): MpZ`\n\nReturns the negation of `this` MpZ (`-this`).\n\n#### `#add(rhs: i32 | u32 | i64 | u64 | MpZ): MpZ`\n\nReturns the sum of `this` MpZ and `rhs`.\n\n#### `#inc(): MpZ`\n\nReturns the increment of `this` MpZ (`this + 1`).\n\n#### `#sub(rhs: i32 | u32 | i64 | u64 | MpZ): MpZ`\n\nReturns the difference of `this` MpZ and the `rhs`.\n\n#### `#dec(): MpZ`\n\nReturns the decrement of `this` MpZ (`this - 1`).\n\n#### `#mul(rhs: i32 | u32 | i64 | u64 | MpZ): MpZ`\n\nReturns the product of `this` MpZ and the `rhs` (`this * rhs`).\n\n#### `#mul_pow2(rhs: i32 | u32 | i64 | u64 | MpZ): MpZ`\n\nReturns the product of `this` MpZ multiplied and `2**rhs` (`this * 2 ** rhs`).\n\n#### `#div(rhs: MpZ): MpZ`\n\nReturns the quotient of `this` MpZ divided by the `rhs` (`trunc(this / rhs)`) truncated towards zero.\nThrows RangeError if `rhs` is zero.\n\n#### `#div_pow2(rhs: i32 | u32 | i64 | u64 | MpZ): MpZ`\n\nReturns the quotant of `this` MpZand `2**rhs` (`this / 2 ** rhs`) truncated towards zero.\n\n#### `#mod(rhs: MpZ): MpZ`\n\nReturns the modulus of `this` MpZ divided by the `rhs`.\nThrows RangeError if `rhs` is zero.\n\n\u003e Note: The `#mod` method is not the same as the `%` operator. The `%` operator returns the `#rem` of the division of the lhs and rhs, while the `#mod` method returns the modulo of the lhs and rhs.\n\n#### `#rem(rhs: MpZ): MpZ`\n\nReturns the remainder of `this` MpZ divided by the `rhs` (`this % rhs`).\nThrows RangeError if `rhs` is zero.\n\n\u003e Note: The `#rem` method is the same as the `%` operator. The `%` operator returns the `#rem` of the division of the lhs and rhs, while the `#mod` method returns the modulo of the lhs and rhs.\n\n#### `#pow(rhs: i32 | u32 | i64 | u64 | MpZ): MpZ`\n\nReturns the value of `this` MpZ raised to the power of `rhs` (`this ** rhs`).\nThrows RangeError if `rhs` is negative.\n\n#### `# powMod(rhs: i32 | u32 | i64 | u64 | MpZ, m: i32 | u32 | i64 | u64 | MpZ): MpZ`\n\nReturns the value of `this` MpZ raised to the power of `rhs` mod `m` (`this ** rhs mod m`).\nThrows RangeError if `m` is \u003c= 0.\nThrows RangeError if `rhs` is negative.\n\n#### `#sqrt(): MpZ`\n\nReturns the greatest integer less than or equal to the square root of `this`.\nThrows RangeError if `this` is negative.\n\n#### `#iroot(n: u32): MpZ`\n\nReturns the greatest integer less than or equal to the nth root of `this`.\nThrows RangeError if `n` is zero or `this` is negative and `n` is even.\n\n#### `#log2(): MpZ`\n\nReturns the base 2 logarithm of `this`.\nThrows RangeError if `this` is negative or zero.\n\n#### `#log10(): MpZ`\n\nReturns the base 10 logarithm of `this`.\nThrows RangeError if `this` is negative or zero.\n\n#### `#fact(): MpZ`\n\nReturns the factorial of `this` MpZ (`this!`).\nThrows RangeError if `this` is negative or too large (greater than `MAX_INTEGER`).\n\n#### `#gcd(rhs: i32 | u32 | i64 | u64 | MpZ): MpZ`\n\nReturns the greatest common divisor of `this` MpZ and `rhs`.\n\n#### `#lcm(rhs: i32 | u32 | i64 | u64 | MpZ): MpZ`\n\nReturns the least common multiple of `this` MpZ and `rhs`.\n\n#### `#shiftLeft(rhs: i32 | u32 | i64 | u64 | MpZ): MpZ`\n\nReturns the value of `this` MpZ left shifted by `rhs` (`this \u003c\u003c rhs`). Negative `rhs` values shift right.\nThrows RangeError if the result exceeds the maximum MpZ size.\n\n\u003e Note: The `#shiftLeft` method return the result of the bitwise shift as if the MpZ was a 2's complement signed integer; matching JavaScript's BigInt `\u003c\u003c` operator.\n\n#### `#shiftRight(rhs: i32 | u32 | i64 | u64 | MpZ): MpZ`\n\nReturns the value of `this` MpZ right shifted by `rhs` (`this \u003e\u003e rhs`). Negative `rhs` values shift left.\nThrows RangeError if the result exceeds the maximum MpZ size.\n\n\u003e Note: The `#shiftLeft` method return the result of the bitwise shift as if the MpZ was a 2's complement signed integer; matching JavaScript's BigInt `\u003e\u003e` operator.\n\n#### `#not(): MpZ`\n\nReturns the bitwise NOT of `this` MpZ (`~this`).\n\n\u003e Note: The `#not` method returns the result as if the MpZ was a 2's complement signed integer (yeilding `-(x + 1)`); matching JavaScript's BigInt `~` operator.\n\n#### `#and(rhs: MpZ): MpZ`\n\nReturns the bitwise AND of `this` MpZ and `rhs`.\n\n\u003e Note: The `#and` method returns the result of the bitwise `AND` as if the MpZ was a 2's complement signed integer; matching JavaScript's `\u0026` BigInt operator.\n\n#### `#or(rhs: MpZ): MpZ`\n\nReturns the bitwise OR of `this` MpZ and `rhs`.\n\n\u003e Note: The `#or` method returns the result of the bitwise `OR` as if the MpZ was a 2's complement signed integer; matching JavaScript's BigInt `|` operator.\n\n#### `#xor(rhs: MpZ): MpZ`\n\nReturns the bitwise XOR of `this` MpZ and `rhs`.\n\nNote: The `#xor` method returns the result of the bitwise `XOR` as if the MpZ was a 2's complement signed integer; matching JavaScript's BigInt `^` operator.\n\n#### `#toString(radix: i32 = 10): string`\n\nReturns the value of `this` MpZ as a string. The radix can be from 2 and 36 (inclusive). The default radix is 10. Negative numbers are prefixed with a `-`. Negitive radix values return the result in uppercase.\nThrows Error if the radix is not between 2 and 36.\n\nNote: The resulting string is not prefixed with the radix (e.g. `0x` or `0b`) and therefore not compatible as input to `MpZ.from` (radix of 10 excluded).\n\n#### `#toHex(): string`\n\nReturns the value of `this` MpZ as a hexadecimal string.\n\n\u003e Note: The resulting string is prefixed with `0x` and is therefore compatible as input to `MpZ.from`.\n\n#### `#toDecimal(): string`\n\nReturns the value of `this` MpZ as a decimal string.\n\n#### `#valueOf(): number`\n\nReturns the value of `this` MpZ as a `number`.\n\n#### `#toExponential(n: u32): string`\n\nReturns the value of `this` MpZ as a string in exponential notation.\nIf `this` MpZ has more digits than requested, the number is rounded to the nearest number represented by `n` digits.\n\n#### `#toU32Array(): u32[]`\n\nReturns the value of `this` MpZ as an unsigned 32-bit integer array. Ther sign of the MpZ is ignored.\n\n#### `#toU32(): u32`\n\nReturns the value of `this` MpZ as an unsigned 32-bit integer. If `this` MpZ is too big to fit in an int32, only the low-order 32 bits are returned.\nIf `this` MpZ is negative, the returned value is the 2's complement representation of the MpZ.\n\n#### `#toI32(): i32`\n\nReturns the value of `this` MpZ as a signed 32-bit integer. If `this` MpZ is too big to fit in an int32, only the low-order 32 bits are returned.\n\n#### `#toU64(): u64`\n\nReturns the value of `this` MpZ as an unsigned 64-bit integer. If `this` MpZ is too big to fit in an int64, only the low-order 64 bits are returned.\n\n#### `#toI64(): i64`\n\nReturns the value as a signed 64-bit integer. If `this` MpZ is too big to fit in an int64, only the low-order 64 bits are returned.\n\n#### `#eqz(): boolean`\n\nReturns `true` if `this` MpZ is equal to zero.\n\n#### `#compareTo(rhs: MpZ | i32 | u32 | i64 | u64 | string): i32`\n\nReturns `-1` if `this` MpZ is less than the rhs, `0` if `this` MpZ is equal to the rhs, or `1` if `this` MpZ is greater than the rhs.\n\n#### `#eq(rhs: MpZ | i32 | u32 | i64 | u64 | string): boolean`\n\nReturns `true` if `this` MpZ is equal to the rhs.\n\n#### `#ne(rhs: MpZ | i32 | u32 | i64 | u64 | string): boolean`\n\nReturns `true` if `this` MpZ is not equal to the rhs.\n\n#### `#gt(rhs: MpZ | i32 | u32 | i64 | u64 | string): boolean`\n\nReturns `true` if `this` MpZ is greater than the rhs.\n\n#### `#ge(rhs: MpZ | i32 | u32 | i64 | u64 | string): boolean`\n\nReturns `true` if `this` MpZ is greater than or equal to the rhs.\n\n#### `#lt(rhs: MpZ | i32 | u32 | i64 | u64 | string): boolean`\n\nReturns `true` if `this` MpZ is less than the rhs.\n\n#### `#le(rhs: MpZ | i32 | u32 | i64 | u64 | string): boolean`\n\nReturns `true` if `this` MpZ is less than or equal to the rhs.\n\n#### Static values\n\nThe following static values are provided for convenience:\n\n- `MpZ.ZERO` - The MpZ value `0`.\n- `MpZ.ONE` - The MpZ value `1`.\n- `MpZ.TWO` - The MpZ value `2`.\n- `MpZ.TEN` - The MpZ value `10`.\n\n### Operators\n\n#### Unary `-` operator\n\nReturns the negation of `this` MpZ (`-this`).\n\n#### Binary `+`, `-`, `*`, `/` operators\n\nSame as the `#add`, `#sub`, `#mul`, `#div` methods.\n\n### Comparison Operators\n\n#### `==`, `\u003e`, `\u003e=`, `\u003c`, `\u003c=`, `!=`\n\nSame as the `#eq`, `#gt`, `#ge`, `#lt`, `#le`, `#ne` methods.\n\n#### `%` operator\n\nReturns the remainder of the lhs and rhs (`lhs % rhs`).\nThrows RangeError if `rhs` is zero.\n\n\u003e Note: The `%` operator is not the same as the `#mod` method. The `%` operator returns the `#rem` of the division of the lhs and rhs; matching JavaScript's BigInt `%` operator.\n\n#### `**` operator\n\nReturns the power of the lhs to the rhs (`lhs ** rhs`).\n\n#### `\u003c\u003c`, `\u003e\u003e` operators\n\nReturns the result of the left/right shift of the lhs by the rhs. Negitive rhs values will result in a opposite shift.\nThrows RangeError if the result exceeds the maximum MpZ size.\n\n\u003e Shift operators behave as if they were represented in two's-complement notation; like JavaScripts's `\u003c\u003c` and `\u003e\u003e` operators.\n\n#### `~` operator\n\nReturns the bitwise NOT of `this` MpZ (`~this`).\n\n#### `\u0026`, `|`, `^ operators\n\nReturns the bitwise `AND`, `OR`, `XOR` operation on the two operands.\n\n\u003e This operator returns the result of the bitwise `AND`, `OR`, `XOR` as if the values were 2's complement signed integers; matching JavaScript's BigInt `\u0026`, `|`, `^` operators.\n\n### `!` operator\n\nReturns the logical NOT of `this` MpZ (`!this`). This is equivalent to `#eqz()`.\n\n#### `MpZ.asIntN(bits: u32, a: MpZ): MpZ`\n\nReturns a BigInt value truncated to the given number of least significant bits and returns that value as a signed integer.\nIf the leading bit of the remaining number is 1, the result is negative.\n\n### `MpZ.asUintN(bits: u32, a: MpZ): MpZ`\n\nReturns a BigInt value truncated to the given number of least significant bits and returns that value as an unsigned integer.\nResults are always non-negative and two's complement in binary.\n\n#### `MpZ.random(bits: u64): MpZ`\n\nReturns a random MpZ value with the specified maximum number of bits.\nThrows RangeError if `bits` exceeds the maximum MpZ size.\n\n[](#license)\n\n## License\n\nLicensed under [MIT](https://opensource.org/licenses/MIT).\nMIT License\n\nCopyright (c) 2024 Jayson Harshbarger\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhypercubed%2Fas-mpz","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhypercubed%2Fas-mpz","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhypercubed%2Fas-mpz/lists"}