Skip to main content

libnum

This is a python library for some numbers functions:

  • working with primes (generating, primality tests)
  • common maths (gcd, lcm, n'th root)
  • modular arithmetics (inverse, Jacobi symbol, square root, solve CRT)
  • converting strings to numbers or binary strings

Library may be used for learning/experimenting/research purposes. Should NOT be used for secure crypto implementations.

Installation

$ pip install libnum

Note that only Python 3 version is maintained.

Development

For development or building this repository, poetry is needed.

Tests can be ran with

$ pytest --doctest-modules .

List of functions

Common maths

  • len_in_bits(n) - number of bits in binary representation of @n
  • randint_bits(size) - random number with a given bit size
  • extract_prime_power(a, p) - s,t such that a = p**s * t
  • nroot(x, n) - truncated n'th root of x
  • gcd(a, b, ...) - greatest common divisor of all arguments
  • lcm(a, b, ...) - least common multiplier of all arguments
  • xgcd(a, b) - Extented Euclid GCD algorithm, returns (x, y, g) : a * x + b * y = gcd(a, b) = g

Modular

  • has_invmod(a, n) - checks if a has modulo inverse
  • invmod(a, n) - modulo inverse
  • solve_crt(remainders, modules) - solve Chinese Remainder Theoreme
  • factorial_mod(n, factors) - compute factorial modulo composite number, needs factorization
  • nCk_mod(n, k, factors) - compute combinations number modulo composite number, needs factorization
  • nCk_mod_prime_power(n, k, p, e) - compute combinations number modulo prime power

Modular square roots

  • jacobi(a, b) - Jacobi symbol
  • has_sqrtmod_prime_power(a, p, k) - checks if a number has modular square root, modulus is p**k
  • sqrtmod_prime_power(a, p, k) - modular square root by p**k
  • has_sqrtmod(a, factors) - checks if a composite number has modular square root, needs factorization
  • sqrtmod(a, factors) - modular square root by a composite modulus, needs factorization

Primes

  • primes(n) - list of primes not greater than @n, slow method
  • generate_prime(size, k=25) - generates a pseudo-prime with @size bits length. @k is a number of tests.
  • generate_prime_from_string(s, size=None, k=25) - generate a pseudo-prime starting with @s in string representation

Factorization

  • is_power(n) - check if @n is p**k, k >= 2: return (p, k) or False
  • factorize(n) - factorize @n (currently with rho-Pollard method) warning: format of factorization is now dict like {p1: e1, p2: e2, ...}

ECC

  • Curve(a, b, p, g, order, cofactor, seed) - class for representing elliptic curve. Methods:
  • .is_null(p) - checks if point is null
  • .is_opposite(p1, p2) - checks if 2 points are opposite
  • .check(p) - checks if point is on the curve
  • .check_x(x) - checks if there are points with given x on the curve (and returns them if any)
  • .find_points_in_range(start, end) - list of points in range of x coordinate
  • .find_points_rand(count) - list of count random points
  • .add(p1, p2) - p1 + p2 on elliptic curve
  • .power(p, n) - n✕P or (P + P + ... + P) n times
  • .generate(n) - n✕G
  • .get_order(p, limit) - slow method, trying to determine order of p; limit is max order to try

Converting

  • s2n(s) - packed string to number
  • n2s(n) - number to packed string
  • s2b(s) - packed string to binary string
  • b2s(b) - binary string to packed string

Stuff

  • grey_code(n) - number in Grey code
  • rev_grey_code(g) - number from Grey code
  • nCk(n, k) - number of combinations
  • factorial(n) - factorial

About

Author: hellman

License: MIT License

Metadata

Release files for libnum 1.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for libnum 1.7.1
File Size Uploaded
libnum-1.7.1.tar.gz 13.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for libnum 1.7.1
File Interpreter ABI Platform
libnum-1.7.1-py3-none-any.whl Python 3 none any Details

Total release size: 28.3 kB

Release files / libnum-1.7.1.tar.gz

Download URL libnum-1.7.1.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c13d0001c8e67d0e310e36f16b59c14114840b718888a613e17ad36c6ecc9422
BLAKE2b-256 checksum
How to use checksums
d1b325a3af3537d5dbcd23c651b942e6715b55f3ffeada97a5be328c0f1ac7a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.0rc1 CPython/3.8.2 Linux/5.4.0-47-generic

Release files / libnum-1.7.1-py3-none-any.whl

Download URL libnum-1.7.1-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb56a64397e68571795792a6732102387dd74d16596a115e03c3d0215149fa35
BLAKE2b-256 checksum
How to use checksums
5fc717e4c6bf91e06c6ac1fbe2100f4761248cac960e63a4cc4d63a9c53afac0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.0rc1 CPython/3.8.2 Linux/5.4.0-47-generic

Release history Release notifications | RSS feed

This release

1.7.1 This release

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.9

2 release files

1.5.8

2 release files

1.5.7

2 release files

1.5.6

2 release files

1.5.5

2 release files

1.5.4

2 release files

1.5.3

2 release files

1.5.2

1 release file

1.5.1

2 release files

1.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page