Giter VIP home page Giter VIP logo

mod's Introduction

mod Build Status Documentation Status

Modular arithmetic in Python.

mod

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

Note that:

  • Mod is based on integer modulo operation %, not math.fmod
  • the result of an operation between a Mod and an int is a Mod
  • the result of an operation between a Mod and a float is a float

Install

Run the following command to install mod package

pip3 install mod

Links

License

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

mod's People

Contributors

jonathan-gould avatar yoeo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mod's Issues

0 // 6 fails

>>> mod.Mod(0, 13) // 6
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/mod.py", line 198, in __floordiv__
    inverted = (other * self.inverse)
  File "/usr/local/lib/python3.9/site-packages/mod.py", line 104, in inverse
    raise ValueError("the value {} cannot be inverted".format(self))
ValueError: the value (0 % 13) cannot be inverted

I would expect this to succeed with value (0 % 13)

Modular logarithm

It would be great to support optimized modular logarithm computation as well.

Rogue print in mod.py

The floordiv method of the Mod class contains 'print(self, converted, converted.inverse())', which can create intrusive output if you use the '//' operator.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.