Created
April 8, 2014 00:47
-
-
Save adil-soubki/10078328 to your computer and use it in GitHub Desktop.
Example of a Paillier Cryptosystem voting scheme where voters simply vote yes (1) or no (0) and the votes are tallied up using the homomorphic properties of the Paillier PKC.
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
#Paillier Cryptovoting System | |
import math | |
from Crypto import Random | |
from Crypto.Random import random | |
# Modular exponentiation | |
def exp(base, exponent, modulus): | |
result = base; | |
for i in range (exponent-1): | |
result = (result*base)%modulus | |
return result | |
# Paillier encryption using PyCrypto's PRNG | |
def encrypt(m): | |
# Private key information (carmicheal function(n)) | |
p = 293 | |
q = 433 | |
# Public key pair | |
n = p*q # 126869 | |
g = 6497955158 | |
# Random number from PRNG | |
randint = Random.new() | |
r = randint.read(n).encode('hex') | |
r = int(r, 16)%n | |
print "Random Number: " + str(r) | |
# Begin Encryption | |
print "Ballot Cast: " + str(int(m, 10)) | |
c = (pow(g, int (m, 10) )*pow(r,n))%(n*n) | |
return c | |
# L function defined by Paillier PKC | |
def L(u): | |
n = 126869 | |
l = (u - 1)/n | |
return l | |
#http://www.programiz.com/python-programming/examples/lcm | |
# Used for the Carmicheal Function | |
def lcm(x, y): | |
"""This function takes two | |
integers and returns the L.C.M.""" | |
# choose the greater number | |
if x > y: | |
greater = x | |
else: | |
greater = y | |
while(True): | |
if((greater % x == 0) and (greater % y == 0)): | |
lcm = greater | |
break | |
greater += 1 | |
return lcm | |
# Decrypts the Paillier encryption using the given u = 53022 | |
def decrypt(c): | |
p = 293 | |
q = 433 | |
n = p*q # 126869 | |
g = 6497955158 | |
u = pow(L(pow(g, lcm(p-1,q-1))%(n*n)), -1)%n | |
m = ((L((pow(c, lcm(p-1,q-1)))%(n*n)))*u)%n | |
return m | |
# Tallys the votes by adding up the ciphertexts | |
def tally(ctxt): | |
tally = 1 | |
for i in range (0, len(ctxt)): | |
tally = tally*ctxt[i]%(126869*126869) | |
return tally | |
# Loops through the first 4 and prints their encryptions and decryptions | |
def main(): | |
ctxts = [] | |
# Begin Random Voting | |
for i in range (0, 5): | |
print "Iteration: m = " + str(i) | |
c = encrypt(random.choice(['0', '1'])) | |
print "Ciphertext: " + str(c) | |
print "" | |
ctxts.append(c) | |
# Tally Votes without decrypting them | |
print "The Cryptographic Tally of Votes: " + str(tally(ctxts)) | |
print "The Decryption of the Tally: " + str(decrypt(tally(ctxts))%126869) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment