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):
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)
return key
@ -41,18 +41,17 @@ class ElGamal:
def enc(self, m, pk):
# sample random r \in Zq
r = SystemRandom().randint(1, q)
r = SystemRandom().randint(1, self.order)
g, h = pk
s = (h**r) % q
p = (g**r) % q
s = (h**r) % self.order
p = (g**r) % self.order
c = s * m
return c, p
def dec(self, c):
c1, c2 = c
# c, p, key, q
h = (c2**self.sk) % q
h = (c2**self.sk) % self.order
m = c1 / h
return m
@ -67,10 +66,8 @@ class Alice:
self.elgamal = elgamal
self.sk = elgamal.gen_key()
self.pk = elgamal.gen(self.sk)
self.b = convert_bloodtype_to_index[convert_from_string_to_enum[bloodtype]]
self.b = list(convert_bloodtype_to_index.keys()).index(bloodtype)+1
self.fake_pks = [self.elgamal.ogen()
for _ in range(7)]
@ -86,12 +83,12 @@ class Alice:
class Bob:
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.elgamal = elgamal
self.pks = None
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)
def receive_pks(self, pks):
@ -106,21 +103,35 @@ class Bob:
return ciphers
if __name__ == "__main__":
def run(donor : BloodType, recipient : BloodType):
p = 199
q = 2*p + 1
q = 2 * p + 1
g = SystemRandom().randint(2, q)
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())
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)