Week7 done.
This commit is contained in:
parent
4f28892d58
commit
2bcb72d82c
|
@ -1,3 +1,3 @@
|
||||||
from .week6 import main
|
from .week7 import main
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
118
week7.py
118
week7.py
|
@ -1,15 +1,9 @@
|
||||||
from secrets import SystemRandom
|
from secrets import SystemRandom
|
||||||
|
|
||||||
|
from .week1 import BloodType, blood_cell_compatibility_lookup
|
||||||
|
|
||||||
random = SystemRandom()
|
random = SystemRandom()
|
||||||
|
|
||||||
'''
|
|
||||||
def find_big_odd(k):
|
|
||||||
|
|
||||||
num = random.getrandbits(k)
|
|
||||||
while num % 2 == 0:
|
|
||||||
num =
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def enc(m, pk):
|
def enc(m, pk):
|
||||||
S = 1000
|
S = 1000
|
||||||
|
@ -17,59 +11,73 @@ def enc(m, pk):
|
||||||
return c
|
return c
|
||||||
|
|
||||||
|
|
||||||
class Alice:
|
def dec(c, p):
|
||||||
def __init__(self, ya, yb, yr):
|
m = (c % p) % 2
|
||||||
self.ya = ya
|
|
||||||
self.yb = yb
|
|
||||||
self.yr = yr
|
|
||||||
# p is the secret key
|
|
||||||
self.p = random.getrandbits(2000)*2 + 1 # odd integer
|
|
||||||
n = 2000
|
|
||||||
bigInts = [random.getrandbits(10**7) for _ in range(n)]
|
|
||||||
smallInts = [random.getrandbits(60) for _ in range(n)]
|
|
||||||
|
|
||||||
self.pk = [self.p*bigInts[i] + 2*smallInts[i] for i in range(n)]
|
|
||||||
self.cya = enc(ya, self.pk)
|
|
||||||
self.cyb = enc(yb, self.pk)
|
|
||||||
self.cyr = enc(yr, self.pk)
|
|
||||||
|
|
||||||
def send(self):
|
|
||||||
return self.cya, self.cyb, self.cyr, self.pk
|
|
||||||
|
|
||||||
def dec(self, c):
|
|
||||||
m = (c % self.p) % 2
|
|
||||||
return m
|
return m
|
||||||
|
|
||||||
|
|
||||||
|
class Alice:
|
||||||
|
def __init__(self, ra, rb, rs):
|
||||||
|
self.ra = ra
|
||||||
|
self.rb = rb
|
||||||
|
self.rs = rs
|
||||||
|
|
||||||
|
self.p = random.getrandbits(2000)
|
||||||
|
if self.p % 2 == 0:
|
||||||
|
self.p -= 1
|
||||||
|
|
||||||
|
n = 2000
|
||||||
|
big_ints = [random.getrandbits(10**7) for _ in range(n)]
|
||||||
|
small_ints = [random.getrandbits(60) for _ in range(n)]
|
||||||
|
|
||||||
|
self.pk = [self.p * big_ints[i] + 2 * small_ints[i] for i in range(n)]
|
||||||
|
|
||||||
|
def send(self):
|
||||||
|
cra = enc(self.ra, self.pk)
|
||||||
|
crb = enc(self.rb, self.pk)
|
||||||
|
crs = enc(self.rs, self.pk)
|
||||||
|
return self.pk, cra, crb, crs
|
||||||
|
|
||||||
|
def receive(self, result_cipher):
|
||||||
|
return dec(result_cipher, self.p)
|
||||||
|
|
||||||
|
|
||||||
class Bob:
|
class Bob:
|
||||||
def __init__(self, xa, xb, xr):
|
def __init__(self, da, db, ds):
|
||||||
self.xa = xa
|
self.da = da
|
||||||
self.xb = xb
|
self.db = db
|
||||||
self.xr = xr
|
self.ds = ds
|
||||||
|
self.result_cipher = None
|
||||||
|
|
||||||
def receive(self, cya, cyb, cyr, pk):
|
def receive(self, pk, cra, crb, crs):
|
||||||
self.pk = pk
|
cda = enc(self.da, pk)
|
||||||
|
cdb = enc(self.db, pk)
|
||||||
|
cds = enc(self.ds, pk)
|
||||||
|
self.result_cipher = (1 + (cda * (1 + cra))) * (1 + (cdb * (1 + crb))) * (1 + (cds * (1 + crs)))
|
||||||
|
|
||||||
# these are ciphered values from alice
|
def send(self):
|
||||||
self.cya = cya
|
return self.result_cipher
|
||||||
self.cyb = cyb
|
|
||||||
self.cyr = cyr
|
|
||||||
|
|
||||||
# these are ciphered values from bob
|
|
||||||
self.cxa = enc(self.xa, pk)
|
|
||||||
self.cxb = enc(self.xb, pk)
|
|
||||||
self.cxr = enc(self.xr, pk)
|
|
||||||
|
|
||||||
def compute_bloodcompatiblity(self):
|
|
||||||
|
|
||||||
|
|
||||||
#f = (1 ^ (self.xa & (1 ^ self.cya))) & (1 ^ (self.xb & (1 ^ self.cyb))) & (1 ^ (self.xr & (1 ^ self.cyr)))
|
def run(da, db, ds, ra, rb, rs):
|
||||||
#z = enc(f, self.pk)
|
alice = Alice(ra, rb, rs)
|
||||||
#return z
|
bob = Bob(da, db, ds)
|
||||||
pass
|
bob.receive(*alice.send())
|
||||||
|
z = alice.receive(bob.send())
|
||||||
|
return z
|
||||||
|
|
||||||
def protocol(donor, recipient):
|
|
||||||
# Alice is recipient
|
def main():
|
||||||
alice = Alice(0,1,1)
|
green = 0
|
||||||
bob = Bob(1,0,0)
|
red = 0
|
||||||
pass
|
for i, recipient in enumerate(BloodType):
|
||||||
|
for j, donor in enumerate(BloodType):
|
||||||
|
z = run(*donor.value, *recipient.value)
|
||||||
|
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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user