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) 2017 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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

mod-0.2.3-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mod-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mod-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6ba05cfe46607b2c76e8cbba16cb014570a2f7619653022c479be3c7bdf4e852
MD5 7696bd0478f998226cff80e9a7e71829
BLAKE2b-256 8da73f4610a83c3043ee1e6f9ca5e8de15f65a5cb30a19855a4cf1fdf2190ce7

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