Replaced ** % with pow. Much faster. Now allows for higher primes

This commit is contained in:
Alexander Munch-Hansen 2019-09-20 12:16:58 +02:00
parent 8e8f1107a5
commit d688640a41

View File

@ -34,7 +34,7 @@ class ElGamal:
return key return key
def gen(self, sk): def gen(self, sk):
h = (self.gen_**sk) % self.order h = pow(self.gen_, sk, self.order)
self.sk = sk self.sk = sk
self.pk = (self.gen_, h) self.pk = (self.gen_, h)
return self.pk return self.pk
@ -44,20 +44,20 @@ class ElGamal:
r = SystemRandom().randint(1, self.order) r = SystemRandom().randint(1, self.order)
g, h = pk g, h = pk
s = (h**r) % self.order s = pow(h, r, self.order)
p = (g**r) % self.order p = pow(g, r, self.order)
c = s * m c = s * m
return c, p return c, p
def dec(self, c): def dec(self, c):
c1, c2 = c c1, c2 = c
h = (c2**self.sk) % self.order h = pow(c2, self.sk, self.order)
m = c1 / h m = c1 / h
return m return m
def ogen(self): def ogen(self):
s = SystemRandom().randint(1, self.order) s = SystemRandom().randint(1, self.order)
h = s**2 % self.order h = pow(s, 2, self.order)
return self.gen_, h return self.gen_, h
@ -111,7 +111,7 @@ class Bob:
def run(donor : BloodType, recipient : BloodType): def run(donor : BloodType, recipient : BloodType):
p = 35977 p = 15485863
q = 2 * p + 1 q = 2 * p + 1
g = SystemRandom().randint(2, q) g = SystemRandom().randint(2, q)