{"id":17124610,"url":"https://github.com/asika32764/bigint-toolkit","last_synced_at":"2026-04-20T17:02:29.312Z","repository":{"id":207365572,"uuid":"719077163","full_name":"asika32764/bigint-toolkit","owner":"asika32764","description":"ES2020 BigInt crypto utilities, convertion and arithmetic.","archived":false,"fork":false,"pushed_at":"2024-08-26T14:42:42.000Z","size":135,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-19T18:21:02.702Z","etag":null,"topics":["arithmetic","bigint","biginteger","js","math","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/bigint-toolkit","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asika32764.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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-11-15T12:02:57.000Z","updated_at":"2025-10-11T08:04:35.000Z","dependencies_parsed_at":"2023-11-29T08:43:28.017Z","dependency_job_id":null,"html_url":"https://github.com/asika32764/bigint-toolkit","commit_stats":null,"previous_names":["asika32764/bigint-toolkit"],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/asika32764/bigint-toolkit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asika32764%2Fbigint-toolkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asika32764%2Fbigint-toolkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asika32764%2Fbigint-toolkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asika32764%2Fbigint-toolkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asika32764","download_url":"https://codeload.github.com/asika32764/bigint-toolkit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asika32764%2Fbigint-toolkit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32056876,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T11:35:06.609Z","status":"ssl_error","status_checked_at":"2026-04-20T11:34:48.899Z","response_time":94,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["arithmetic","bigint","biginteger","js","math","typescript"],"created_at":"2024-10-14T18:43:00.229Z","updated_at":"2026-04-20T17:02:29.295Z","avatar_url":"https://github.com/asika32764.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BigInt Toolkit (ES2020)\n\nThis library provides some useful arithmetic and converting functions for ES2020 BigInt.\n\n## Installation\n\n### Node\n\n```shell\nnpm i bigint-toolkit\n\n# OR\n\nyarn add bigint-toolkit\n```\n\n### Browser\n\nUMD\n\n```html\n\u003cscript src=\"dist/bigint-toolkit.min.js\"\u003e\u003c/script\u003e\n```\n\n```js\nBigIntToolkit.modPow(...);\n```\n\nES Module\n\n```html\n\u003cscript type=\"module\"\u003e\nimport { modPow } from './dist/bigint-toolkit.es.min.js';\n\nmodPow(...);\n\u003c/script\u003e\n```\n\n\n## Usages\n\nIf you are using a bundler with ES like syntax, just import it.\n\n```ts\nimport { modPow } from 'bigint-toolkit';\n\nconst r = modPow(100n, 50n, 70n);\n```\n\nThe Math related functions also provides a `BigMath` namespace:\n\n```ts\nimport { max, min, BigMath } from 'bigint-toolkit';\n\nmax(5n, 10n, 12n);\n\n// Same as\n\nBigMath.max(5n, 10n, 12n);\n```\n\nThis library only supports `bigint`, it can not mix use with `number`.\n\n```ts\nimport { max } from 'bigint-toolkit';\n\nmax(5n, 10, 12); // Error\n```\n\nThe conversion functions are useful if you want to hash your BigInt or convert hashed hex string to numbers:\n\n```ts\nimport { sha256 } from 'crypto-hash';\nimport { bigInt2Uint8Array, uint8Array2BigInt, uint8Array2Hex } from 'bigint-toolkit';\n\nconst hashStr: string = sha256(bigInt2Uint8Array(123456789n));\n\n// Convert some hash result to bigint format\nconst hashBuffer = blake2b('This is some string', null, 256);\n\nuint8Array2BigInt(hashBuffer);\nuint8Array2Hex(hashBuffer);\n```\n\nThe conversion functions are useful if you want to hash your BigInt or convert hashed hex string to numbers:\n\n```ts\nimport { sha256 } from 'crypto-hash';\nimport { bigInt2Uint8Array, uint8Array2BigInt, uint8Array2Hex } from 'bigint-toolkit';\n\nconst hashStr: string = sha256(bigInt2Uint8Array(123456789n));\n\n// Convert some hash result to bigint format\nconst hashBuffer = blake2b('This is some string', null, 256);\n\nuint8Array2BigInt(hashBuffer);\nuint8Array2Hex(hashBuffer);\n```\n\n## Contribution\n\n### Report a BUG\n\nSimply open an issue on GitHub, any PR will be welcome and speed up the update/fixes process.\n\n### Test\n\nIf you want to clone this project and test it, Node.js v20up may be required, since we run Jest\nwith ES module, the `--experimental-vm-modules` mode may be more stable after Node 20.\n\n-----\n\n\n## :toolbox: Functions\n\n- [toZn](#gear-tozn)\n- [mod](#gear-mod)\n- [randomBytes](#gear-randombytes)\n- [random](#gear-random)\n- [phi](#gear-phi)\n- [abs](#gear-abs)\n- [negate](#gear-negate)\n- [isOdd](#gear-isodd)\n- [eGcd](#gear-egcd)\n- [modInv](#gear-modinv)\n- [modPow](#gear-modpow)\n- [modMultiply](#gear-modmultiply)\n- [modAdd](#gear-modadd)\n- [min](#gear-min)\n- [max](#gear-max)\n- [gcd](#gear-gcd)\n- [lcm](#gear-lcm)\n- [isUnit](#gear-isunit)\n- [isEven](#gear-iseven)\n- [crt](#gear-crt)\n- [hexPadZero](#gear-hexpadzero)\n- [bigintToHex](#gear-biginttohex)\n- [uint8ToBigint](#gear-uint8tobigint)\n- [uint8ToBigintWithNegative](#gear-uint8tobigintwithnegative)\n- [uint8ToHexWithNegative](#gear-uint8tohexwithnegative)\n- [uint8ToHex](#gear-uint8tohex)\n- [uint8ToBuffer](#gear-uint8tobuffer)\n- [hexToBigint](#gear-hextobigint)\n- [toBigint](#gear-tobigint)\n- [bigintToHexPadZero](#gear-biginttohexpadzero)\n- [hexToUint8](#gear-hextouint8)\n- [bigintToUint8](#gear-biginttouint8)\n- [bufferToUint8](#gear-buffertouint8)\n\n### :gear: toZn\n\nFinds the smallest positive element that is congruent to a in modulo m.\n\n| Function | Type |\n| ---------- | ---------- |\n| `toZn` | `(a: bigint, m: bigint) =\u003e bigint` |\n\n### :gear: mod\n\nAn alias of toZn()\n\n| Function | Type |\n| ---------- | ---------- |\n| `mod` | `(a: bigint, m: bigint) =\u003e bigint` |\n\n### :gear: randomBytes\n\nGenerates cryptographically strong pseudorandom data, it will return\nan Uint8Array object. This function use `crypto.randomBytes()` in node.js\nand `window.crypto.getRandomValues()` in Web browser.\n\nYou can convert it to hex by `uint8Array2Hex()` or use some base64\nlibrary to convert it to string.\n\n| Function | Type |\n| ---------- | ---------- |\n| `randomBytes` | `(bufferSize: number) =\u003e Uint8Array` |\n\n### :gear: random\n\nGenerate a random bigint number between 2 numbers.\n\n| Function | Type |\n| ---------- | ---------- |\n| `random` | `(start: bigint, end: bigint) =\u003e bigint` |\n\n### :gear: phi\n\nCalculates Euler's totient function (phi function) of a BigInt 'n'.\n\nEuler's totient function calculates the count of positive integers less than or equal to 'n'\nthat are coprime (have greatest common divisor 1) with 'n'.\n\n| Function | Type |\n| ---------- | ---------- |\n| `phi` | `(n: bigint) =\u003e bigint` |\n\n### :gear: abs\n\nReturn an absolute value of bigint.\n\n| Function | Type |\n| ---------- | ---------- |\n| `abs` | `(num: bigint) =\u003e bigint` |\n\n### :gear: negate\n\nMake a bigint negative.\n\n| Function | Type |\n| ---------- | ---------- |\n| `negate` | `(num: bigint) =\u003e bigint` |\n\n### :gear: isOdd\n\nCheck a bigint is odd.\n\n| Function | Type |\n| ---------- | ---------- |\n| `isOdd` | `(n: bigint) =\u003e boolean` |\n\n### :gear: eGcd\n\nCalculates the extended greatest common divisor (eGCD) of two BigInt numbers.\n\nThis function computes the eGCD of two BigInt numbers 'a' and 'b', and returns an object\ncontaining the GCD ('gcd') and coefficients 'x' and 'y' such that 'ax + by = gcd'.\n\n```ts\nconst result = eGcd(16n, 10n);\nresult.g === 2n;\nresult.x === -3n;\nresult.y === 5n;\n```\n\n| Function | Type |\n| ---------- | ---------- |\n| `eGcd` | `(a: bigint, b: bigint) =\u003e EgcdResult` |\n\n### :gear: modInv\n\nCalculates the modular multiplicative inverse of a BigInt 'a' modulo 'm'.\n\nThis function computes the value 'x' such that '(a * x) % m === 1' where 'a' and 'm' are BigInt numbers.\n\n| Function | Type |\n| ---------- | ---------- |\n| `modInv` | `(a: bigint, m: bigint) =\u003e bigint` |\n\n### :gear: modPow\n\nCalculates the modular exponentiation of a BigInt 'base' to the power of a BigInt 'exponent' modulo 'm'.\n\nThis function computes the result of `base^exp % m` where 'base', 'exponent', and 'm' are BigInt numbers.\n\n| Function | Type |\n| ---------- | ---------- |\n| `modPow` | `(base: bigint, exp: bigint, m: bigint) =\u003e bigint` |\n\n### :gear: modMultiply\n\nCalculates the modular multiplication of two BigInt numbers.\n\nThis function computes the result of `(a * b) % m` where `a`, `b`, and `m` are BigInt numbers.\n\n| Function | Type |\n| ---------- | ---------- |\n| `modMultiply` | `(a: bigint, b: bigint, mod: bigint) =\u003e bigint` |\n\n### :gear: modAdd\n\nCalculates the modular addition of two BigInt numbers.\n\nThis function computes the result of `(a + b) % m` where `a`, `b`, and `m` are BigInt numbers.\n\n| Function | Type |\n| ---------- | ---------- |\n| `modAdd` | `(a: bigint, b: bigint, mod: bigint) =\u003e bigint` |\n\n### :gear: min\n\nFind min from a set of bigint.\n\n| Function | Type |\n| ---------- | ---------- |\n| `min` | `(...nums: bigint[]) =\u003e bigint` |\n\n### :gear: max\n\nFind max from a set of bigint.\n\n| Function | Type |\n| ---------- | ---------- |\n| `max` | `(...nums: bigint[]) =\u003e bigint` |\n\n### :gear: gcd\n\nCalculates the greatest common divisor (GCD) of two or more BigInt numbers.\n\nThis function computes the largest positive integer that divides all the input numbers without remainder.\n\n| Function | Type |\n| ---------- | ---------- |\n| `gcd` | `(a: bigint, b: bigint) =\u003e bigint` |\n\n### :gear: lcm\n\nCalculates the least common multiple (LCM) of two or more BigInt numbers.\n\n| Function | Type |\n| ---------- | ---------- |\n| `lcm` | `(a: bigint, b: bigint) =\u003e bigint` |\n\n### :gear: isUnit\n\nCheck a bigint is unit.\n\n| Function | Type |\n| ---------- | ---------- |\n| `isUnit` | `(n: bigint) =\u003e boolean` |\n\n### :gear: isEven\n\nCheck a bigint is even.\n\n| Function | Type |\n| ---------- | ---------- |\n| `isEven` | `(n: bigint) =\u003e boolean` |\n\n### :gear: crt\n\nFunction to implement Chinese Remainder Theorem.\n\n| Function | Type |\n| ---------- | ---------- |\n| `crt` | `(num: bigint[], rem: bigint[]) =\u003e bigint` |\n\n### :gear: hexPadZero\n\nPad `0` to start if hex string length is odd.\n\n| Function | Type |\n| ---------- | ---------- |\n| `hexPadZero` | `(hex: string) =\u003e string` |\n\n### :gear: bigintToHex\n\nBigint to hex conversion.\n\nThe second argument `padZero = true` will pad a `0` on start if return length is odd.\n\n| Function | Type |\n| ---------- | ---------- |\n| `bigintToHex` | `(num: bigint, padZero?: boolean) =\u003e string` |\n\n### :gear: uint8ToBigint\n\nConvert Uint8Array back to bigint.\n\nIf an Uint8Array has 2's complement (Mostly converted from a negative number),\nset second argument as TRUE to inverse it.\n\n| Function | Type |\n| ---------- | ---------- |\n| `uint8ToBigint` | `(bytes: Uint8Array, handleNegative?: boolean) =\u003e bigint` |\n\n### :gear: uint8ToBigintWithNegative\n\nConvert Uint8Array back to bigint and inverse the 2's complement (negative).\n\n| Function | Type |\n| ---------- | ---------- |\n| `uint8ToBigintWithNegative` | `(bytes: Uint8Array) =\u003e bigint` |\n\n### :gear: uint8ToHexWithNegative\n\nConvert Uint8Array to hex and inverse the 2's complement (negative).\n\n| Function | Type |\n| ---------- | ---------- |\n| `uint8ToHexWithNegative` | `(bytes: Uint8Array) =\u003e string` |\n\n### :gear: uint8ToHex\n\nConvert Uint8Array to hex string.\n\nIf an Uint8Array has 2's complement (Mostly converted from a negative number),\nset second argument as TRUE to inverse it.\n\n| Function | Type |\n| ---------- | ---------- |\n| `uint8ToHex` | `(bytes: Uint8Array, handleNegative?: boolean) =\u003e string` |\n\n### :gear: uint8ToBuffer\n\nConvert Uint8Array to ArrayBufferLike.\n\n| Function | Type |\n| ---------- | ---------- |\n| `uint8ToBuffer` | `(bytes: Uint8Array) =\u003e ArrayBufferLike` |\n\n### :gear: hexToBigint\n\nConvert hex to bigint and add `-` sign if origin bigint is negative.\n\n| Function | Type |\n| ---------- | ---------- |\n| `hexToBigint` | `(hex: string) =\u003e bigint` |\n\n### :gear: toBigint\n\nConvert any base of numbers to bigint.\n\n```\ntoBigInt(123456789)\ntoBigInt('75bcd15', 16)\ntoBigInt('111010110111100110100010101', 2)\n```\n\nThis function will auto add negative to hex string if input value less than 0.\n\n| Function | Type |\n| ---------- | ---------- |\n| `toBigint` | `(num: string or number | bigint, from?: number) =\u003e bigint` |\n\n### :gear: bigintToHexPadZero\n\nBigint to hex conversion and pad a `0` on start if return length is odd.\n\n| Function | Type |\n| ---------- | ---------- |\n| `bigintToHexPadZero` | `(num: bigint) =\u003e string` |\n\n### :gear: hexToUint8\n\nConvert hex string to Uint8Array.\n\n| Function | Type |\n| ---------- | ---------- |\n| `hexToUint8` | `(hex: string) =\u003e Uint8Array` |\n\n### :gear: bigintToUint8\n\nBigint to Uint8Array conversion.\n\nBy default, this function unable to handle negative bigint, and will throw an Error.\nIf you want to convert a negative bigint to Uint8Array, set second argument as TRUE,\nthat this functions will try making 2's complement on the bigint to make it\npositive.\n\nNOTE: If you convert a negative bigint to Uint8Array, you must use\n\n- `uint8ToBigint(num, true)`\n- `uint8ToBigintWithNegative(num)`\n\nto inverse the Uint8Array so you can get negative bigint back.\n\n| Function | Type |\n| ---------- | ---------- |\n| `bigintToUint8` | `(num: bigint, handleNegative?: boolean) =\u003e Uint8Array` |\n\n### :gear: bufferToUint8\n\nConvert an `ArrayBufferLike` interface to `Uint8Array`.\n\n| Function | Type |\n| ---------- | ---------- |\n| `bufferToUint8` | `(buffer: ArrayBufferLike) =\u003e Uint8Array` |\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasika32764%2Fbigint-toolkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasika32764%2Fbigint-toolkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasika32764%2Fbigint-toolkit/lists"}