{"id":27290762,"url":"https://github.com/hellman/libnum","last_synced_at":"2025-10-06T21:36:37.592Z","repository":{"id":46141136,"uuid":"2208878","full_name":"hellman/libnum","owner":"hellman","description":"Working with numbers (primes, modular, etc.)","archived":false,"fork":false,"pushed_at":"2022-07-15T20:32:37.000Z","size":82,"stargazers_count":241,"open_issues_count":3,"forks_count":60,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-04T11:05:52.471Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","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/hellman.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}},"created_at":"2011-08-15T08:49:36.000Z","updated_at":"2025-02-18T09:23:40.000Z","dependencies_parsed_at":"2022-08-20T05:00:19.162Z","dependency_job_id":null,"html_url":"https://github.com/hellman/libnum","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellman%2Flibnum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellman%2Flibnum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellman%2Flibnum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellman%2Flibnum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hellman","download_url":"https://codeload.github.com/hellman/libnum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248485368,"owners_count":21111841,"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":"2025-04-11T21:38:53.023Z","updated_at":"2025-10-06T21:36:32.548Z","avatar_url":"https://github.com/hellman.png","language":"Python","readme":"# libnum\n\nThis is a python library for some numbers functions:\n\n*  working with primes (generating, primality tests)\n*  common maths (gcd, lcm, n'th root)\n*  modular arithmetics (inverse, Jacobi symbol, square root, solve CRT)\n*  converting strings to numbers or binary strings\n\nLibrary may be used for learning/experimenting/research purposes. Should NOT be used for secure crypto implementations.\n\n## Installation\n\n```bash\n$ pip install libnum\n```\n\nNote that only Python 3 version is maintained.\n\n## Development\n\nFor development or building this repository, [poetry](https://python-poetry.org/) is needed.\n\nTests can be ran with\n\n```bash\n$ pytest --doctest-modules .\n```\n\n## List of functions\n\n\u003cb\u003eCommon maths\u003c/b\u003e\n\n*  len\\_in\\_bits(n) - number of bits in binary representation of @n\n*  randint\\_bits(size) - random number with a given bit size\n*  extract\\_prime\\_power(a, p) - s,t such that a = p**s * t\n*  nroot(x, n) - truncated n'th root of x\n*  gcd(a, b, ...) - greatest common divisor of all arguments\n*  lcm(a, b, ...) - least common multiplier of all arguments\n*  xgcd(a, b) - Extented Euclid GCD algorithm, returns (x, y, g) : a * x + b * y = gcd(a, b) = g\n\n\u003cb\u003eModular\u003c/b\u003e\n\n*  has\\_invmod(a, n) - checks if a has modulo inverse\n*  invmod(a, n) - modulo inverse\n*  solve\\_crt(remainders, modules) - solve Chinese Remainder Theoreme\n*  factorial\\_mod(n, factors) - compute factorial modulo composite number, needs factorization\n*  nCk\\_mod(n, k, factors) - compute combinations number modulo composite number, needs factorization\n*  nCk\\_mod\\_prime\\_power(n, k, p, e) - compute combinations number modulo prime power\n\n\u003cb\u003eModular square roots\u003c/b\u003e\n\n*  jacobi(a, b) - Jacobi symbol\n*  has\\_sqrtmod\\_prime\\_power(a, p, k) - checks if a number has modular square root, modulus is p**k\n*  sqrtmod\\_prime\\_power(a, p, k) - modular square root by p**k\n*  has\\_sqrtmod(a, factors) - checks if a composite number has modular square root, needs factorization\n*  sqrtmod(a, factors) - modular square root by a composite modulus, needs factorization\n\n\u003cb\u003ePrimes\u003c/b\u003e\n\n*  primes(n) - list of primes not greater than @n, slow method\n*  generate\\_prime(size, k=25) - generates a pseudo-prime with @size bits length. @k is a number of tests.\n*  generate\\_prime\\_from\\_string(s, size=None, k=25) - generate a pseudo-prime starting with @s in string representation\n\n\u003cb\u003eFactorization\u003c/b\u003e\n*  is\\_power(n) - check if @n is p**k, k \u003e= 2: return (p, k) or False\n*  factorize(n) - factorize @n (currently with rho-Pollard method)\nwarning: format of factorization is now dict like {p1: e1, p2: e2, ...}\n\n\u003cb\u003eECC\u003c/b\u003e\n\n*  Curve(a, b, p, g, order, cofactor, seed) - class for representing elliptic curve. Methods:\n*   .is\\_null(p) - checks if point is null\n*   .is\\_opposite(p1, p2) - checks if 2 points are opposite\n*   .check(p) - checks if point is on the curve\n*   .check\\_x(x) - checks if there are points with given x on the curve (and returns them if any)\n*   .find\\_points\\_in\\_range(start, end) - list of points in range of x coordinate\n*   .find\\_points\\_rand(count) - list of count random points\n*   .add(p1, p2) - p1 + p2 on elliptic curve\n*   .power(p, n) - n✕P or (P + P + ... + P) n times\n*   .generate(n) - n✕G\n*   .get\\_order(p, limit) - slow method, trying to determine order of p; limit is max order to try\n\n\u003cb\u003eConverting\u003c/b\u003e\n\n*  s2n(s) - packed string to number\n*  n2s(n) - number to packed string\n*  s2b(s) - packed string to binary string\n*  b2s(b) - binary string to packed string\n\n\u003cb\u003eStuff\u003c/b\u003e\n\n*  grey\\_code(n) - number in Grey code\n*  rev\\_grey\\_code(g) - number from Grey code\n*  nCk(n, k) - number of combinations\n*  factorial(n) - factorial\n\n## About\n\nAuthor: hellman\n\nLicense: [MIT License](http://opensource.org/licenses/MIT)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellman%2Flibnum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhellman%2Flibnum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellman%2Flibnum/lists"}