64 green, 0 red

This commit is contained in:
Alexander Munch-Hansen 2019-09-18 18:14:19 +02:00
parent 3931da4947
commit a0986dce36

View File

@ -29,7 +29,7 @@ class ElGamal:
def gen_key(self): def gen_key(self):
key = SystemRandom().randint(1, self.order) key = SystemRandom().randint(1, self.order)
while np.gcd(q, key) != 1: while np.gcd(self.order, key) != 1:
key = SystemRandom().randint(1, self.order) key = SystemRandom().randint(1, self.order)
return key return key
@ -41,18 +41,17 @@ class ElGamal:
def enc(self, m, pk): def enc(self, m, pk):
# sample random r \in Zq # sample random r \in Zq
r = SystemRandom().randint(1, q) r = SystemRandom().randint(1, self.order)
g, h = pk g, h = pk
s = (h**r) % q s = (h**r) % self.order
p = (g**r) % q p = (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
# c, p, key, q h = (c2**self.sk) % self.order
h = (c2**self.sk) % q
m = c1 / h m = c1 / h
return m return m
@ -67,10 +66,8 @@ class Alice:
self.elgamal = elgamal self.elgamal = elgamal
self.sk = elgamal.gen_key() self.sk = elgamal.gen_key()
self.pk = elgamal.gen(self.sk) self.pk = elgamal.gen(self.sk)
self.b = list(convert_bloodtype_to_index.keys()).index(bloodtype)+1
self.b = convert_bloodtype_to_index[convert_from_string_to_enum[bloodtype]]
self.fake_pks = [self.elgamal.ogen() self.fake_pks = [self.elgamal.ogen()
for _ in range(7)] for _ in range(7)]
@ -86,12 +83,12 @@ class Alice:
class Bob: class Bob:
def __init__(self, bloodtype, elgamal): def __init__(self, bloodtype, elgamal):
self.bloodtype = convert_from_string_to_enum[bloodtype] self.bloodtype = list(convert_bloodtype_to_index.keys()).index(bloodtype)
self.truth_vals = [] self.truth_vals = []
self.elgamal = elgamal self.elgamal = elgamal
self.pks = None self.pks = None
for donor in BloodType: for donor in BloodType:
truth_val = blood_cell_compatibility_lookup(self.bloodtype, donor) truth_val = blood_cell_compatibility_lookup(bloodtype, donor)
self.truth_vals.append(truth_val) self.truth_vals.append(truth_val)
def receive_pks(self, pks): def receive_pks(self, pks):
@ -106,21 +103,35 @@ class Bob:
return ciphers return ciphers
if __name__ == "__main__": def run(donor : BloodType, recipient : BloodType):
p = 199 p = 199
q = 2*p + 1 q = 2 * p + 1
g = SystemRandom().randint(2, q) g = SystemRandom().randint(2, q)
elgamal = ElGamal(g, q, p) elgamal = ElGamal(g, q, p)
alice = Alice("B-", elgamal)
bob = Bob("B-", elgamal) alice = Alice(donor, elgamal)
bob = Bob(recipient, elgamal)
bob.receive_pks(alice.send_pks()) bob.receive_pks(alice.send_pks())
pls = alice.retrieve(bob.transfer_messages()) pls = alice.retrieve(bob.transfer_messages())
print(pls)
return bool(pls)
if __name__ == "__main__":
# 199 is a large prime
green = 0
red = 0
for i, recipient in enumerate(BloodType):
for j, donor in enumerate(BloodType):
z = run(donor, recipient)
lookup = blood_cell_compatibility_lookup(recipient, donor)
if lookup == z:
green += 1
else:
print(f"'{BloodType(donor).name} -> {BloodType(recipient).name}' should be {lookup}.")
red += 1
print("Green:", green)
print("Red :", red)