Now works.. Bob would send 0 for false, which encrypts to 0.....
This commit is contained in:
parent
a0986dce36
commit
8e8f1107a5
20
week4.py
20
week4.py
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
from secrets import SystemRandom
|
from secrets import SystemRandom
|
||||||
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, blood_cell_compatibility_lookup
|
||||||
|
|
||||||
# We can't encrypt 0, so we have to index from 1
|
# We can't encrypt 0, so we have to index from 1
|
||||||
convert_bloodtype_to_index = {
|
convert_bloodtype_to_index = {
|
||||||
|
@ -67,6 +67,9 @@ class Alice:
|
||||||
|
|
||||||
self.sk = elgamal.gen_key()
|
self.sk = elgamal.gen_key()
|
||||||
self.pk = elgamal.gen(self.sk)
|
self.pk = elgamal.gen(self.sk)
|
||||||
|
# There is 100% a better way to get the index, also, it's to avoid encryption 0. Coincidentally, it's
|
||||||
|
# not an issue when we do it, as O- is in the 0th index and this bloodtype can donate to everyone
|
||||||
|
# so the decryption bugging, it not an issue
|
||||||
self.b = list(convert_bloodtype_to_index.keys()).index(bloodtype)+1
|
self.b = list(convert_bloodtype_to_index.keys()).index(bloodtype)+1
|
||||||
self.fake_pks = [self.elgamal.ogen()
|
self.fake_pks = [self.elgamal.ogen()
|
||||||
for _ in range(7)]
|
for _ in range(7)]
|
||||||
|
@ -77,8 +80,10 @@ class Alice:
|
||||||
return all_pks
|
return all_pks
|
||||||
|
|
||||||
def retrieve(self, ciphers):
|
def retrieve(self, ciphers):
|
||||||
|
print(ciphers)
|
||||||
mb = self.elgamal.dec(ciphers[self.b-1])
|
mb = self.elgamal.dec(ciphers[self.b-1])
|
||||||
return mb
|
# Bog sends 1 for false, 2 for true, so we have to subtract 1 here
|
||||||
|
return mb-1
|
||||||
|
|
||||||
|
|
||||||
class Bob:
|
class Bob:
|
||||||
|
@ -87,6 +92,7 @@ class Bob:
|
||||||
self.truth_vals = []
|
self.truth_vals = []
|
||||||
self.elgamal = elgamal
|
self.elgamal = elgamal
|
||||||
self.pks = None
|
self.pks = None
|
||||||
|
# Bob needs his row of the truth table, to create the 8 messages
|
||||||
for donor in BloodType:
|
for donor in BloodType:
|
||||||
truth_val = blood_cell_compatibility_lookup(bloodtype, donor)
|
truth_val = blood_cell_compatibility_lookup(bloodtype, donor)
|
||||||
self.truth_vals.append(truth_val)
|
self.truth_vals.append(truth_val)
|
||||||
|
@ -98,18 +104,18 @@ class Bob:
|
||||||
ciphers = []
|
ciphers = []
|
||||||
for idx, truth_val in enumerate(self.truth_vals):
|
for idx, truth_val in enumerate(self.truth_vals):
|
||||||
pk = self.pks[idx]
|
pk = self.pks[idx]
|
||||||
c = self.elgamal.enc(truth_val, pk)
|
# Bob can't send 0, as it will encrypt to 0
|
||||||
|
c = self.elgamal.enc(truth_val+1, pk)
|
||||||
ciphers.append(c)
|
ciphers.append(c)
|
||||||
return ciphers
|
return ciphers
|
||||||
|
|
||||||
|
|
||||||
def run(donor : BloodType, recipient : BloodType):
|
def run(donor : BloodType, recipient : BloodType):
|
||||||
p = 199
|
p = 35977
|
||||||
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(donor, elgamal)
|
alice = Alice(donor, elgamal)
|
||||||
bob = Bob(recipient, elgamal)
|
bob = Bob(recipient, elgamal)
|
||||||
|
|
||||||
|
@ -120,7 +126,9 @@ def run(donor : BloodType, recipient : BloodType):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 199 is a large prime
|
#z = run(BloodType.O_POSITIVE, BloodType.A_NEGATIVE)
|
||||||
|
#print(z)
|
||||||
|
#exit()
|
||||||
|
|
||||||
green = 0
|
green = 0
|
||||||
red = 0
|
red = 0
|
||||||
|
|
Loading…
Reference in New Issue
Block a user