Now with group stuff. ElGamal works ish
This commit is contained in:
parent
26439afd1a
commit
1136f7eedc
67
week4.py
67
week4.py
|
@ -2,7 +2,9 @@
|
|||
# The one represents our bloodtype. Bob will then encrypt 8 values using these PKs, where each value repredents
|
||||
# A truth value, thus either true or false, s.t. each cipher is an entry in the bloodtype comptability matrix.
|
||||
|
||||
import secrets
|
||||
from secrets import SystemRandom
|
||||
import time
|
||||
from math import pow
|
||||
import numpy as np
|
||||
from crypto.week1 import BloodType, convert_from_string_to_enum, blood_cell_compatibility_lookup
|
||||
|
||||
|
@ -18,51 +20,47 @@ convert_bloodtype_to_index = {
|
|||
}
|
||||
|
||||
|
||||
class Group:
|
||||
def __init__(self, q):
|
||||
self.order = q
|
||||
|
||||
def gen_group_stuff(self):
|
||||
my_group = []
|
||||
for i in range(1, self.order):
|
||||
if np.gcd(i, self.order) == 1:
|
||||
my_group.append(i)
|
||||
for group_ele in my_group:
|
||||
if group_ele**self.order == 1 % self.order:
|
||||
gen = group_ele
|
||||
return gen, self.order, my_group
|
||||
|
||||
class ElGamal:
|
||||
def __init__(self, G, g, q):
|
||||
self.group = G
|
||||
def __init__(self, g, q):
|
||||
self.gen_ = g
|
||||
self.order = q
|
||||
self.pk = None
|
||||
self.sk = None
|
||||
|
||||
def gen_key(self):
|
||||
key = SystemRandom().randint(1, self.order)
|
||||
while np.gcd(q, key) != 1:
|
||||
key = SystemRandom().randint(1, self.order)
|
||||
return key
|
||||
|
||||
def gen(self, sk):
|
||||
h = self.gen_**sk
|
||||
h = (self.gen_**sk) % self.order
|
||||
self.sk = sk
|
||||
self.pk = (self.gen_, h)
|
||||
return self.pk
|
||||
|
||||
def enc(self, m, pk):
|
||||
# sample random r \in Zq
|
||||
r = secrets.SystemRandom().randint(1, self.order-1)
|
||||
r = SystemRandom().randint(1, q)
|
||||
|
||||
g, h = pk
|
||||
c = (g ** r, m * h**r)
|
||||
return c
|
||||
s = (h**r) % q
|
||||
p = (g**r) % q
|
||||
c = s * m
|
||||
return c, p
|
||||
|
||||
def dec(self, c):
|
||||
c1, c2 = c
|
||||
m = c2 * c1**(-self.sk) # % self.order
|
||||
# c, p, key, q
|
||||
h = (c2**self.sk) % q
|
||||
m = c1 / h
|
||||
return m
|
||||
|
||||
def ogen(self, r):
|
||||
# Here, q = 2p+1, thus we actually need to use the p here, instead of
|
||||
# self.order, but as we do not know p yet, .e we
|
||||
# TODO: Use p instead of self.order, s.t. self.order = 2p+1
|
||||
s = secrets.SystemRandom().randint(1, self.order)
|
||||
s = SystemRandom().randint(1, self.order)
|
||||
h = s**2 % self.order
|
||||
return self.gen_, h
|
||||
|
||||
|
@ -73,10 +71,10 @@ class Alice:
|
|||
self.gen_ = 9
|
||||
self.order = 453
|
||||
self.b = convert_bloodtype_to_index[convert_from_string_to_enum[bloodtype]]
|
||||
self.sk = secrets.SystemRandom().randint(1, self.order)
|
||||
self.sk = SystemRandom().randint(1, self.order)
|
||||
|
||||
self.pk = self.elgamal.gen(self.sk)
|
||||
self.fake_pks = [self.elgamal.ogen(secrets.SystemRandom().randint(0, self.order))
|
||||
self.fake_pks = [self.elgamal.ogen(SystemRandom().randint(0, self.order))
|
||||
for _ in range(7)]
|
||||
|
||||
def send_pks(self):
|
||||
|
@ -112,8 +110,21 @@ class Bob:
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
group = Group(11)
|
||||
gen, order, my_group = group.gen_group_stuff()
|
||||
p = 199
|
||||
q = 2*p + 1
|
||||
g = SystemRandom().randint(2, q)
|
||||
|
||||
elgamal = ElGamal(g, q)
|
||||
sk = elgamal.gen_key()
|
||||
|
||||
m = 7
|
||||
pk = elgamal.gen(sk)
|
||||
c = elgamal.enc(m, pk)
|
||||
print(c)
|
||||
d_m = elgamal.dec(c)
|
||||
print("decrupted:", d_m)
|
||||
|
||||
|
||||
|
||||
|
||||
print(gen, order, my_group)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user