{"id":22551108,"url":"https://github.com/michel-leonard/c-integers","last_synced_at":"2026-04-18T00:01:51.390Z","repository":{"id":112809921,"uuid":"490741684","full_name":"michel-leonard/C-integers","owner":"michel-leonard","description":"A tiny Big Integer implementation in C for operations beyond 64 bits.","archived":false,"fork":false,"pushed_at":"2025-12-30T09:25:14.000Z","size":14,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2026-01-02T22:23:02.157Z","etag":null,"topics":["algorithm","arbitr","arbitrary-precision","arithmetic","big-integer","c","education","example","linux","math","number-theory","public-domain","research","simple","snippets","windows"],"latest_commit_sha":null,"homepage":"","language":"C","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/michel-leonard.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-05-10T14:49:24.000Z","updated_at":"2025-12-30T09:25:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"4d375da7-2184-45b2-aa11-62a5ef5b4952","html_url":"https://github.com/michel-leonard/C-integers","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/michel-leonard/C-integers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michel-leonard%2FC-integers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michel-leonard%2FC-integers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michel-leonard%2FC-integers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michel-leonard%2FC-integers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/michel-leonard","download_url":"https://codeload.github.com/michel-leonard/C-integers/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michel-leonard%2FC-integers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31950891,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-17T17:29:20.459Z","status":"ssl_error","status_checked_at":"2026-04-17T17:28:47.801Z","response_time":62,"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":["algorithm","arbitr","arbitrary-precision","arithmetic","big-integer","c","education","example","linux","math","number-theory","public-domain","research","simple","snippets","windows"],"created_at":"2024-12-07T17:09:51.341Z","updated_at":"2026-04-18T00:01:51.361Z","avatar_url":"https://github.com/michel-leonard.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Big Numbers in C\n\nThe **tiny Big Integer Library** (released \"as is\", into the public domain, without any warranty, express or implied) is provided for handling large integers. It includes a variety of basic and advanced mathematical functions to support calculations. This solution does not use global variables but computation sheets, so it is stateless and thread-safe.\n\n## Get Started\n\nTo build the executable, macOS and Linux users should use the **Terminal**, while Windows users should use **PowerShell**. The procedure takes just a few minutes :\n\n1. Open a Terminal or PowerShell.  \n2. Navigate to the directory containing the source code using the `cd` command.  \n3. Compile the demo using the command `gcc -Wall -pedantic -O2 -std=c99 main.c -o demo`.\n   \nIf `gcc` is not installed on Windows, you can install [MinGW](https://winlibs.com/), which provides it. Then, simply replace `demo` with `demo.exe` in the command above.\n  \n- **macOS users**: Replace `gcc` with `clang`, which acts the same and is available natively.\n- **Linux users**: Install `gcc` with `sudo apt update \u0026\u0026 sudo apt install gcc`.  \n\n### Running the demo  \n\nCompilation takes a few seconds, then you can start the demo :\n- **Windows**: `./demo.exe`\n- **macOS** and **Linux**: `./demo`\n\nThe software will quickly display the results of the tests.\n\n```\n                   Arithmetic identities ... [PASS]\n        Distributivity and Associativity ... [PASS]\n                   Left and Right shifts ... [PASS]\n                       String conversion ... [PASS]\n                        Random generator ... [PASS]\n                                Division ... [PASS]\n                        Power and modulo ... [PASS]\n                Double precision numbers ... [PASS]\n                            Corner cases ... [PASS]\n                               Primality ... [PASS]\n\nCompleted with 10 success and 0 failures.\n```\n## Developer Message\n\nThis library strikes a balance between real-world needs and code simplicity, it is most efficient when dealing with integers that are a few hundred bits long, but can handle large numbers such as computing 10000!. Designed to be lightweight, it consists of only about 1000 lines of code and has no dependencies, adhering to the C99 standard.\n\n## Key Structures\n\n### `cint` Structure\n\nThe `cint` structure is used to represent large integers, with its value stored in a dynamically allocated array of `h_cint_t` (typically `int64_t`):\n\n```c\ntypedef struct {\n    h_cint_t *mem;    // Memory storing the least significant bits (little-endian format)\n    h_cint_t *end;    // Memory storing the most significant bits (end-1)\n    h_cint_t nat;     // -1 for negative, +1 for positive (zero is positive)\n    size_t size;      // Allocated size, at least (end - mem)\n} cint;\n```\n\n### `cint_sheet` Structure\n\nThe `cint_sheet` structure is used to manage temporary variables required for certain operations. It allows efficient memory usage when performing computations that require multiple intermediate results.\n\n```c\ntypedef struct {\n    cint temp[10];    // Array of temporary variables for large number operations\n} cint_sheet;\n```\n\n## Functions\n\n### Memory Management\n\n- **`cint_new_sheet(size_t bits)`**  \n  Allocates a new `cint_sheet` for storing temporary variables needed during calculations.\n  \n- **`cint_clear_sheet(cint_sheet *sheet)`**  \n  Clears the memory used by a `cint_sheet`, releasing all allocated resources.\n\n- **`h_cint_tmp(cint_sheet *sheet, int id, const cint *least)`**  \n  Allocates memory for a temporary `cint` variable within the provided `cint_sheet`.\n\n### Integer Initialization \u0026 Conversion\n\n- **`cint_init(cint *num, size_t bits, long long int val)`**  \n  Initializes a `cint` with a specific size (in bits) and a long integer value.\n\n- **`cint_init_by_string(cint *num, size_t bits, const char *str, int base)`**  \n  Initializes a `cint` from a string representation of a number in a given base.\n\n- **`cint_to_int(cint *num)`**  \n  Converts a `cint` to a standard `long long int` (64-bit), truncating the value if necessary.\n\n- **`cint_to_string(const cint *num, int base)`**  \n  Converts a `cint` to a string in the specified base (e.g., decimal, hexadecimal).\n\n### Arithmetic Operations\n\nThese operations modify the original `cint` (in-place), meaning the result of the operation is stored directly in one of the input variables:\n\n- **`cint_addi(cint *lhs, const cint *rhs)`**  \n  Adds `rhs` to `lhs` in place.\n\n- **`cint_subi(cint *lhs, const cint *rhs)`**  \n  Subtracts `rhs` from `lhs` in place.\n\n- **`cint_muli(cint *lhs, const cint *rhs)`**  \n  Multiplies `lhs` by `rhs` in place.\n\n- **`cint_divi(cint *lhs, const cint *rhs)`**  \n  Divides `lhs` by `rhs` in place.\n\n- **`cint_left_shifti(cint *num, size_t bits)`**  \n  Left shifts `num` by the specified number of bits.\n\n- **`cint_right_shifti(cint *num, size_t bits)`**  \n  Right shifts `num` by the specified number of bits.\n\n### Modular Arithmetic\n\n- **`cint_mul_mod(cint_sheet *sheet, const cint *lhs, const cint *rhs, const cint *mod, cint *res)`**  \n  Computes the product of `lhs` and `rhs` modulo `mod`, storing the result in `res`.\n\n- **`cint_pow_mod(cint_sheet *sheet, const cint *n, const cint *exp, const cint *mod, cint *res)`**  \n  Computes `n` raised to the power `exp` modulo `mod`, storing the result in `res`.\n\n- **`cint_modular_inverse(cint_sheet *sheet, const cint *lhs, const cint *rhs, cint *res)`**  \n  Computes the modular inverse of `lhs` modulo `rhs`, storing the result in `res`.\n\n### Advanced Operations\n\n- **`cint_is_prime(cint_sheet *sheet, const cint *N, int iterations, uint64_t *seed)`**  \n  Uses the Miller-Rabin primality test to check if `N` is prime. Temporary variables are allocated from `sheet`.\n\n- **`cint_gcd(cint_sheet *sheet, const cint *lhs, const cint *rhs, cint *gcd)`**  \n  Computes the greatest common divisor (GCD) of `lhs` and `rhs`, storing the result in `gcd`.\n\n- **`cint_sqrt(cint_sheet *sheet, const cint *num, cint *res, cint *rem)`**  \n  Computes the square root of `num`, storing the result in `res` and the remainder in `rem`.\n\n### Helper Functions\n\n- **`cint_checksum(const cint *num)`**  \n  Computes a checksum for the given `cint`.\n\n- **`cint_count_bits(const cint *num)`**  \n  Returns the number of bits required to represent `num`.\n\n- **`cint_count_zeros(const cint *num)`**  \n  Returns the number of trailing zeros in `num`.\n\n- **`cint_compare(const cint *lhs, const cint *rhs)`**  \n  Compares two `cint` values.\n\n- **`cint_to_double(const cint *num)`**  \n  Converts a `cint` to a `double`.\n\n- **`cint_nth_root(cint_sheet *sheet, const cint *num, unsigned nth, cint *res)`**  \n  Computes the nth root of `num`, storing the result in `res`.\n\n\n## Real use\n\nThis **tiny Big Integer Library** is used by a factorization software to operate its [Quadratic Sieve](https://github.com/michel-leonard/C-Quadratic-Sieve), intended for users who factor numbers up to 75+ digits and enjoy applying mathematics to software.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichel-leonard%2Fc-integers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichel-leonard%2Fc-integers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichel-leonard%2Fc-integers/lists"}