Skip to main content

Modular arithmetic in Python

Project description

Build Status Documentation Status

Description

Modular arithmetic is arithmetic for integers, where numbers wrap around when reaching a given value called modulus. For example 6 ≡ 1 (mod 5). Modular arithmetic has several practical applications including: music, banking, book publishing, cryptography… and of course math.

The purpose of this package is to simplify the use of modular arithmetic in Python3.

Usage

This package provides Mod integers that compute arithmetic operations like + - * // ** with a modulus:

from mod import Mod

# Funny math here

x = Mod(5, 7)      # x ≡ 5 (mod 7)

(x + 2) == 0       # True: 5 + 2 ≡ 7 ≡ 0 (mod 7)
(x + 7) == x       # True: 5 + 7 ≡ 12 ≡ 5 (mod 7)
(x**3) == (x + 1)  # True: 5³ ≡ 125 ≡ 6 (mod 7)
(1 // x) == 3      # True: 5 × 3 ≡ 15 ≡ 1 (mod 7) ⇒ 5⁻¹ ≡ 3 (mod 7)

A naive implementation of RSA encryption algorithm using mod package:

from mod import Mod


# My RSA keys
public_key = Mod(3, 61423)
private_key = Mod(40619, 61423)

# My very secret message
top_secret_message = 666

# RSA encryption
encrypted = top_secret_message**public_key

# RSA decryption
decrypted = encrypted**private_key

# My secret message have been correctly encrypted and decrypted :-)
assert decrypted == top_secret_message

Package documentation: mod.Mod

Install

Run the following command to install mod package

pip3 install mod

Indices

mod — Copyright (c) 2020 Y. SOMDA, MIT License

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

mod-0.3.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

mod-0.3.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file mod-0.3.0.tar.gz.

File metadata

  • Download URL: mod-0.3.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.5

File hashes

Hashes for mod-0.3.0.tar.gz
Algorithm Hash digest
SHA256 b3462cb997ba4cb393fe91ebc8b94e574d93ae59ef6df8b13fdd44192b242842
MD5 e24158b2566126c9644f110d566e0f39
BLAKE2b-256 f172ef6f03a29c7b5da29fe05856716985efb51b3095e3ea31433638e3e3f64f

See more details on using hashes here.

File details

Details for the file mod-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: mod-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.5

File hashes

Hashes for mod-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0416727a7172d426860185cfbf416268eb9dd1b44677eda000c0703304b23e2
MD5 d8386940b25d5ab16910f0ae8e8e2310
BLAKE2b-256 aca4bcd7f8c9bd1c4bc1c6ea7d1aa39cea0b38d8303f6f06994cac5264ecae22

See more details on using hashes here.

Supported by

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