Skip to main content

Montgomery Reduction Algorithm python package to verify and study hardware implementations.

Project description

spec

Montgomery-Reduction-Algorithm

Montgomery Reduction Algorithm python package to verify and study hardware implementations.

Setup, Build and Installation

  • Setup: pip install -r requirements.txt
  • Build: python -m build
  • Installation: pip install <path-to-wheel>

Config MMR Package

  • MMR.N: Default value is 32
  • MMR.MME_LOG: Default is False. Set to True to log the mme m and r values for each calculation step.
  • MMR.MMM_LOG: Default is False. Set to True to log the mmm q and s values for each calculation step.

GUI

The GUI-script is currently placed in the gui.py file.

mra-gui

Example 1:

Call graphical user interface from the command line interface

mra-gui

CLI

The CLI-script is currently placed in the cli.py file.

MMR

  • mmm -a {int}, -b {int}, -n {int}   Montgomery Modular Multiplication
  • mme -m {int}, -e {int}, -n {int}, -k {int}   Montgomery Modular Exponentiation
Example 1:

Call Montgomery Modular Multiplication from the command line interface

mra-cli mmm -a 0x00112233 -b 0x44556677 -n 0x93849ca7

Output: 1699857101 0x6551c2cd

Example 2

Call Montgomery Modular Exponentiation from the command line interface

mra-cli mme -m 0x00112233 -e 0x44556677 -n 0x93849ca7 -k 0x8c8d9129

Output: 613470512 0x2490d130

RSA

  • gcd -p {int}, -q {int}   Greatest Common Divisor
  • lcm -p {int}, -q {int}   Least Common Multiple
  • soe [--qmin {int}, --qmax {int}]   Sieve of Eratosthenes
  • n -p {int}, -q {int}   Modulus n
  • tot -p {int}, -q {int}   tot(n)
  • cop -p {int}, -q {int}   Coprime
  • e --totn {int}   Fermat Prime
  • chke -e {int}, --totn {int}   Check if e is coprime to tot(n) and between 1 and tot(n)
  • d -e {int}, --totn {int}   Modular Multiplicative Inverse
  • muli -e {int}, --totn {int}   Multiplicative Inverse
  • egcd --totn {int}, -e {int}   Extended Greatest Common Divisor
  • encrypt -m {int}, -e {int}, -n {int}   RSA Encryption
  • decrypt -c {int}, -d {int}, -n {int}   RSA Decryption
Example 1

Call Greatest Common Divisor from the command line interface

mra-cli gcd -p 2 -q 4 

Output: 2 0x2

Example 2

Call Least Common Multiple from the command line interface

mra-cli lcm -p 2 -q 4

Output: 10 0xa

Example 3

Call encrypt from the command line interface

mra-cli encrypt -m 18 -e 257 -n 21

Output: 9 0x9

Example 4

Call decrypt from the command line interface

mra-cli decrypt -c 9 -d 17 -n 21

Output: 18 0x12

Montgomery Modular Reduction

RSA is defined for

  • encryption as $c = m^e \mod n$

  • decryption as $m = c^d \mod n$

where:

  • $m$: message with N-bits

  • $c$: ciphertext with N-bits

  • $n$: modulus with N-bits

  • $e$: public key with E-bits

  • $d$: private key with N-bits

Pseudocode of the Montgomery Modular Multiplication

$\mathrm{MMM}(a,b,n) = a * b * 2^{-\mathrm{N}}$

Hint: $k$ constant is removed by $2^{-\mathrm{N}}$

s(0) = 0
for i = 0 to N-1
  q(i)   = ( s(i) + a(i) x b ) mod 2
  s(i+1) = ( s(i) + q(i) x n + a(i) x b ) / 2
end
if ( s(N) ≥ n )
  d = s(N) – n
else
  d = s(N)
end
MMM(a, b, n) = d

Pseudocode of the Montgomery Modular Exponentiation!

$\mathrm{MME}(m,e,n) = c = m^e \mod n $

$k = 2^{2\mathrm{N}} \mod n$

m(0) = MMM(m, k, n)
r(0) = MMM(1, k, n)
for i = 0 to E-1
  if ( e(i) == 1 )
    r(i+1) = MMM(r(i), m(i), n)
  else
    r(i+1) = r(i)
  end
  m(i+1) = MMM(m(i), m(i), n)
end
c = MMM(r(E), 1, n)

Roadmap to v1.0.0-alpha

  • implement Montgomery Modular Multiplication (MMM)
  • implement Montgomery Modular Exponentiation (MME)
  • implement logging of calculation steps
  • implement tests
  • implement the calculation of rsa values for encrypting and decrypting
  • implement the calculation of test vectors
  • implement the generation of random test vector
  • implement hex to decimal and decimal to hex conversion for gui
  • implement hex to decimal and decimal to hex conversion for cli
  • implement a cli for standalone usage (MMR, RSA)
  • add help text for MMR and RSA cli
  • implement a gui for standalone usage (MMR, RSA)
  • implement a cli for standalone usage (test vector generator)
  • implement a gui for standalone usage (test vector generator)
  • add use description to readme.md
  • add an example and description of how to use the package
  • add build description to readme.md
  • implement build, test and release workflow
  • enhance logging
  • enhance package structure
  • enhance testing

References

[1] Christof Paar, Jan Pelzl. Understanding Cryptography: A Textbook for Students and Practitioners, Springer, 2010.

[2] Christof Paar. Implementation of Cryptographic Schemes 1, Lecture Notes, 2015.

[3] https://www.emsec.ruhr-uni-bochum.de/media/attachments/files/2015/09/IKV-1_2015-04-28.pdf.

[4] Elif Bilge Kavun. Security Processor Design - Lecture and Exercise Slides, 2024.

[5] https://www.extendedeuclideanalgorithm.com

[6] https://www.geeksforgeeks.org

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

montgomery_reduction_algorithm-0.0.5a0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file montgomery_reduction_algorithm-0.0.5a0.tar.gz.

File metadata

File hashes

Hashes for montgomery_reduction_algorithm-0.0.5a0.tar.gz
Algorithm Hash digest
SHA256 2c46c967046c01fac61ea46beb167a0495fd76ca0f5eca292a7c64eaa263fdef
MD5 1345df732f7a87415c54dd1109293c88
BLAKE2b-256 d633df90b9ecf43e5db1a3a389eeaa3f27228ba6944f37bc896f7ce822006532

See more details on using hashes here.

File details

Details for the file montgomery_reduction_algorithm-0.0.5a0-py3-none-any.whl.

File metadata

File hashes

Hashes for montgomery_reduction_algorithm-0.0.5a0-py3-none-any.whl
Algorithm Hash digest
SHA256 606bbef5bbdf392b22e7531862ee38530084992550f411d2d2a65f59c46b248b
MD5 af1a9e9fc83457f590d263d95f5d6bd7
BLAKE2b-256 07537307b3f3234304bd6c5e1f0b0ede562af1db20e92a0b03083102f993fcad

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