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
xmodulo the product of moduli, orNoneif 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8405e2ef9977a3dea44a5b31baa254a9e71a3c3fc85581e835670b38e6afa05
|
|
| MD5 |
05c8cf598e050abb0cdc23b832f55c88
|
|
| BLAKE2b-256 |
22d70c027d12849a20c9340bdb9761cd8fb3e258f1e0430183ac9bb2ad6dcddd
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9800b7bc1c778c1d47de0f6296e5cb88b5bd47acfefeb8769e347927c0f53760
|
|
| MD5 |
c2c8464cf28ecbdbd8462139aa1323c2
|
|
| BLAKE2b-256 |
eb9d966b19c2663e476aa7e7090f4ca2034c0ddaebc39fbe9a4558213505da15
|