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.0.tar.gz (13.7 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.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for gencrt-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3bd3c9abc86742bf41db773636e099bc167d325bf51acb5df4b8c9828f5b04cb
MD5 35e169a120a05a84860f38fd784d8257
BLAKE2b-256 95f286f5ce9ee6a28950ee995b2199d56183e8c47623f12d413313d914336c9a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gencrt-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a4b579ce89de1a6033b07948ff5f5c1dc2b807ba91f0e81db2d92e28b630046c
MD5 88ffca935d02f405c9fc4fb9715359e0
BLAKE2b-256 a9f8f707c485e078fa7d31f09e6b852e02e6f4bf9c912808bb5fe8e4909b4a04

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