Skip to main content

A package implementing the generalized Chinese remainder theorem (CRT) algorithm.

Project description

gencrt

gencrt is a Python library that implements the Generalized Chinese Remainder Theorem (CRT) algorithm. It enables solving systems of modular equations efficiently and robustly, making it a valuable tool for applications in number theory, cryptography, and computational mathematics.

Features

  • Solve modular equations using the generalized CRT algorithm.
  • Support for multiple congruences of the form x ≡ a_i (mod n_i).
  • Includes utility functions for:
    • Extended Euclidean Algorithm to compute the greatest common divisor (GCD) and Bézout coefficients.
    • Validation of moduli and congruences for correctness.

Installation

To use gencrt in your project, you can install it via pip:

pip install gencrt

Usage

Importing the Library

from gencrt import crt, extended_gcd

Solving Modular Equations with generalized CRT

To solve a system of modular equations:

x ≡ a1 (mod n1)
x ≡ a2 (mod n2)
x ≡ a3 (mod n3)
from gencrt import crt

# Define the congruences as a list of (a_i, n_i)
congruences = [(2, 3), (3, 4), (1, 5)]

# Solve using the generalized CRT
solution = crt(congruences)

print(f"The solution is x ≡ {solution} (mod product of moduli)")

Extended Euclidean Algorithm

Compute the GCD and Bézout coefficients:

from gencrt import extended_gcd

a, b = 56, 15
gcd, x, y = extended_gcd(a, b)

print(f"GCD: {gcd}, x: {x}, y: {y} (Bézout coefficients)")

API Reference

crt(congruences: Iterable)

  • Description: Solves a system of modular equations using the generalized CRT.
  • Parameters:
    • congruences: An iterable of tuples (a_i, n_i).
  • Returns: Integer solution x modulo the product of moduli, or None if no solution exists.

extended_gcd(a: int, b: int)

  • Description: Computes the GCD of two integers and Bézout coefficients.
  • Parameters:
    • a, b: Integers.
  • Returns: A tuple (GCD, x, y).

License

This library is licensed under the MIT License. See LICENSE for more details.


Author: Simon Ljungbeck. Date: December 2024.

For issues or contributions, visit the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gencrt-0.1.1.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gencrt-0.1.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file gencrt-0.1.1.tar.gz.

File metadata

  • Download URL: gencrt-0.1.1.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.6

File hashes

Hashes for gencrt-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f8405e2ef9977a3dea44a5b31baa254a9e71a3c3fc85581e835670b38e6afa05
MD5 05c8cf598e050abb0cdc23b832f55c88
BLAKE2b-256 22d70c027d12849a20c9340bdb9761cd8fb3e258f1e0430183ac9bb2ad6dcddd

See more details on using hashes here.

File details

Details for the file gencrt-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: gencrt-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.6

File hashes

Hashes for gencrt-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9800b7bc1c778c1d47de0f6296e5cb88b5bd47acfefeb8769e347927c0f53760
MD5 c2c8464cf28ecbdbd8462139aa1323c2
BLAKE2b-256 eb9d966b19c2663e476aa7e7090f4ca2034c0ddaebc39fbe9a4558213505da15

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page