Created
February 2, 2018 21:40
-
-
Save pc-magas/d21f52465dc8a167082610753c2ea7bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding=utf-8 | |
# -*- Mode: python; c-basic-offset: 4 -*- | |
def convert_into_binary(c): | |
# We return the binary of c as a STRING | |
# Each character of the string can be converted it into integer using python's int() function. | |
return bin(c)[2:] | |
def square_and_multiply(x,c,n): | |
''' | |
Calculates the z = x^c mod n | |
:param x: Integer | |
:param c: Integer | |
:param n: Integer | |
:return: Integer | |
''' | |
c=convert_into_binary(c) | |
z=1 | |
for bit in c: | |
z=z*z%n | |
if bit is '1': #Remember: bit is a character | |
z=(z*x) % n | |
return z | |
print square_and_multiply(11,23,187) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment