{"id":28392033,"url":"https://github.com/kokke/tiny-bignum-c","last_synced_at":"2025-06-26T01:31:32.159Z","repository":{"id":44344566,"uuid":"108605890","full_name":"kokke/tiny-bignum-c","owner":"kokke","description":"Small portable multiple-precision unsigned integer arithmetic in C","archived":false,"fork":false,"pushed_at":"2024-10-04T09:44:32.000Z","size":111,"stargazers_count":446,"open_issues_count":17,"forks_count":88,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-05-31T22:10:16.277Z","etag":null,"topics":["arbitrary-precision","arbitrary-precision-integers","big-int","bignum","bignumber","bignumbers","c","multi-precision"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kokke.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":null,"security":null,"support":null}},"created_at":"2017-10-27T23:51:35.000Z","updated_at":"2025-05-30T04:49:17.000Z","dependencies_parsed_at":"2023-01-23T02:46:00.634Z","dependency_job_id":null,"html_url":"https://github.com/kokke/tiny-bignum-c","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kokke%2Ftiny-bignum-c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kokke%2Ftiny-bignum-c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kokke%2Ftiny-bignum-c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kokke%2Ftiny-bignum-c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kokke","download_url":"https://codeload.github.com/kokke/tiny-bignum-c/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kokke%2Ftiny-bignum-c/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":258552607,"owners_count":22719369,"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":["arbitrary-precision","arbitrary-precision-integers","big-int","bignum","bignumber","bignumbers","c","multi-precision"],"created_at":"2025-05-31T11:14:11.481Z","updated_at":"2025-06-26T01:31:32.149Z","avatar_url":"https://github.com/kokke.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"![CI](https://github.com/kokke/tiny-bignum-c/actions/workflows/c-cpp.yml/badge.svg)\n\n# tiny-bignum-c\n# A small multiple-precision integer implementation in C\n### Description\nSmall portable [Arbitrary-precision unsigned integer arithmetic](https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic) in C, for calculating with large numbers.\n\nUses an array of `uint8_t`, `uint16_t` or `uint32_t` as underlying data-type utilizing all bits in each word.\n\nThe number-base is 0x100, 0x10000 or 0x100000000 depending on chosen word-size - see the header file [bn.h](https://github.com/kokke/tiny-bignum-c/blob/master/bn.h) for clarification.\n\nNo dynamic memory management is utilized, and `stdio.h` is only used for testing functions parsing to and from hex-strings.\n\n\n### Current status\n\nBasic arithmetic (+, -, *, /, %) and bitwise operations (\u0026, |, ^. \u003c\u003c, \u003e\u003e) plus increments, decrements and comparisons are supported. \n\n\n### Design goals\nThe main design goal of this library is to be small, correct, self contained and use few resources while retaining acceptable performance and feature completeness. Clarity of the code is also highly valued.\n\n\n### Notable features and omissions\n- Small code and binary size: ~500 SLOC, ~3kb binary for x86. Statically #define'd memory usage / allocation.\n- No use of dynamic memory allocation (i.e. no calls to `malloc` / `free`).\n- Randomized testing validated against Python's big-integers\n- Optimal memory utilization, number base is 1 + UINT{8,16,32}_MAX.\n\n\n### API\nThis is the data-structure used, where DTYPE is `#define`'d to `uint8_t`, `uint16_t` or `uint32_t`.\n```C\nstruct bn\n{\n  DTYPE array[BN_ARRAY_SIZE];\n};\n```\n\nThis is the public / exported API:\n```C\n/* Initialization functions: */\nvoid bignum_init(struct bn* n); /* n gets zero-initialized */\nvoid bignum_from_int(struct bn* n, DTYPE_TMP i);\nint  bignum_to_int(struct bn* n);\n/* NOTE: The functions below are meant for testing mainly and expects input in hex-format and of a certain length */\n/*       See the implementation for details or the test-files for examples of how to use them. */\nvoid bignum_from_string(struct bn* n, char* str, int nbytes);\nvoid bignum_to_string(struct bn* n, char* str, int maxsize);\n\n/* Basic arithmetic operations: */\nvoid bignum_add(struct bn* a, struct bn* b, struct bn* c); /* c = a + b */\nvoid bignum_sub(struct bn* a, struct bn* b, struct bn* c); /* c = a - b */\nvoid bignum_mul(struct bn* a, struct bn* b, struct bn* c); /* c = a * b */\nvoid bignum_div(struct bn* a, struct bn* b, struct bn* c); /* c = a / b */\nvoid bignum_mod(struct bn* a, struct bn* b, struct bn* c); /* c = a % b */\nvoid bignum_divmod(struct bn* a, struct bn* b, struct bn* c, struct bn* d); /* c = a/b, d = a%b */\n\n/* Bitwise operations: */\nvoid bignum_and(struct bn* a, struct bn* b, struct bn* c); /* c = a \u0026 b */\nvoid bignum_or(struct bn* a, struct bn* b, struct bn* c);  /* c = a | b */\nvoid bignum_xor(struct bn* a, struct bn* b, struct bn* c); /* c = a ^ b */\nvoid bignum_lshift(struct bn* a, struct bn* b, int nbits); /* b = a \u003c\u003c nbits */\nvoid bignum_rshift(struct bn* a, struct bn* b, int nbits); /* b = a \u003e\u003e nbits */\n\n/* Special operators and comparison */\nint  bignum_cmp(struct bn* a, struct bn* b);               /* Compare: returns LARGER, EQUAL or SMALLER */\nint  bignum_is_zero(struct bn* n);                         /* For comparison with zero */\nvoid bignum_inc(struct bn* n);                             /* Increment: add one to n */\nvoid bignum_dec(struct bn* n);                             /* Decrement: subtract one from n */\nvoid bignum_pow(struct bn* a, struct bn* b, struct bn* c); /* Calculate a^b -- e.g. 2^10 =\u003e 1024 */\nvoid bignum_isqrt(struct bn* a, struct bn* b);             /* Integer square root -- e.g. isqrt(5) =\u003e 2 */\nvoid bignum_assign(struct bn* dst, struct bn* src);        /* Copy src into dst -- dst := src */\n```\n    \n### Usage\n\nSet `BN_ARRAY_SIZE` in `bn.h` to determine the size of the numbers you want to use. Default choice is 1024 bit numbers.\nSet `WORD_SIZE` to {1,2,4} to use`uint8_t`, `uint16_t` or `uint32_t`as underlying data structure.\n\nRun `make clean all test` for examples of usage and for some random testing.\n\n\n### Examples\n\nSee [`tests/factorial.c`](https://github.com/kokke/tiny-bignum-c/blob/master/tests/factorial.c) for an example of how to calculate factorial(100) or 100! (a 150+ digit number).\n\n\n### FAQ\n- *Q: What differentiates this library from other C big integer implementations?*\n\n  A: Small size for one. ~500 lines of C-code compiling to 2-3kb ROM, using only modest amounts of RAM.\n     Utilizing all bits by using a number base 2^{8,16,32} instead of 10 which is a usual choice.\n\n- *Q: Why no support for 64-bit word-size?*\n\n  A: All calculations are done in a temporary variable, which needs to be bigger than the word-size (to detect overflow etc.).\n     So 64-bit word-size would need e.g. 128-bit temp-var. C99 only supports portable integers up to 64-bits.\n\n\n### License\nAll material in this repository is in the public domain.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkokke%2Ftiny-bignum-c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkokke%2Ftiny-bignum-c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkokke%2Ftiny-bignum-c/lists"}