Skip to content

Instantly share code, notes, and snippets.

@pc-magas
Created February 2, 2018 21:40
Show Gist options
  • Save pc-magas/d21f52465dc8a167082610753c2ea7bd to your computer and use it in GitHub Desktop.
Save pc-magas/d21f52465dc8a167082610753c2ea7bd to your computer and use it in GitHub Desktop.
#!/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