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
|
# 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.
|
# 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
|
import numpy as np
|
||||||
from crypto.week1 import BloodType, convert_from_string_to_enum, blood_cell_compatibility_lookup
|
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:
|
class ElGamal:
|
||||||
def __init__(self, G, g, q):
|
def __init__(self, g, q):
|
||||||
self.group = G
|
|
||||||
self.gen_ = g
|
self.gen_ = g
|
||||||
self.order = q
|
self.order = q
|
||||||
self.pk = None
|
self.pk = None
|
||||||
self.sk = 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):
|
def gen(self, sk):
|
||||||
h = self.gen_**sk
|
h = (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
|
||||||
|
|
||||||
def enc(self, m, pk):
|
def enc(self, m, pk):
|
||||||
# sample random r \in Zq
|
# sample random r \in Zq
|
||||||
r = secrets.SystemRandom().randint(1, self.order-1)
|
r = SystemRandom().randint(1, q)
|
||||||
|
|
||||||
g, h = pk
|
g, h = pk
|
||||||
c = (g ** r, m * h**r)
|
s = (h**r) % q
|
||||||
return c
|
p = (g**r) % q
|
||||||
|
c = s * m
|
||||||
|
return c, p
|
||||||
|
|
||||||
def dec(self, c):
|
def dec(self, c):
|
||||||
c1, c2 = 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
|
return m
|
||||||
|
|
||||||
def ogen(self, r):
|
def ogen(self, r):
|
||||||
# Here, q = 2p+1, thus we actually need to use the p here, instead of
|
# 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
|
# 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
|
# 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
|
h = s**2 % self.order
|
||||||
return self.gen_, h
|
return self.gen_, h
|
||||||
|
|
||||||
|
@ -73,10 +71,10 @@ class Alice:
|
||||||
self.gen_ = 9
|
self.gen_ = 9
|
||||||
self.order = 453
|
self.order = 453
|
||||||
self.b = convert_bloodtype_to_index[convert_from_string_to_enum[bloodtype]]
|
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.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)]
|
for _ in range(7)]
|
||||||
|
|
||||||
def send_pks(self):
|
def send_pks(self):
|
||||||
|
@ -112,8 +110,21 @@ class Bob:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
group = Group(11)
|
p = 199
|
||||||
gen, order, my_group = group.gen_group_stuff()
|
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