bachelor-project/src/gale_shapley.mpc

151 lines
5.6 KiB
Python

# vim: ft=python
from Compiler import types
from Compiler.util import *
from Compiler.oram import OptimalORAM
from Compiler.library import for_range, do_while, time, if_, print_ln, crash, print_str
from Compiler.gs import OMatrix, OStack
class Matchmaker:
"""
Based on Matchmaker from Compiler/gs.py in MP-SPDZ, copyright (c) 2023,
Commonwealth Scientific and Industrial Research Organisation (CSIRO)
ABN 41 687 119 230, published under the BSD 3-Clause Licence
"""
def pair(self, patient, therapist, for_real):
self.paired_therapists.access(patient, therapist, for_real)
self.paired_patients.access(therapist, patient, for_real)
def unpair(self, patient, therapist, for_real):
self.paired_therapists.delete(patient, for_real)
self.paired_patients.delete(therapist, for_real)
self.unpaired.append(patient, for_real)
def request_therapist(self, patient, therapist, for_real):
(requested_therapist,), free = self.paired_patients.read(therapist)
paired = 1 - free
rank_patient = self.t_exps[therapist][patient]
(rank_requested_therapist,), worst_therapist = self.t_exps[therapist].read(paired*requested_therapist)
leaving = self.int_type(rank_patient) < self.int_type(rank_requested_therapist)
if self.M < self.N:
leaving = 1 - (1 - leaving) * (1 - worst_therapist)
print_str('therapist: %s, patient: %s, requested therapist: %s, worst therapist: %s, ',
*(x.reveal() for x in (therapist, patient, requested_therapist, worst_therapist)))
print_ln('rank patient: %s, rank requested therapist: %s, paired: %s, leaving: %s',
*(x.reveal() for x in
(rank_patient, rank_requested_therapist, paired, leaving)))
self.unpair(requested_therapist, therapist, paired * leaving * for_real)
self.pair(patient, therapist, (1 - (paired * (1 - leaving))) * for_real)
self.unpaired.append(patient, paired * (1 - leaving) * for_real)
def match(self, n_loops=None):
if n_loops is None or n_loops > self.N * self.M:
loop = do_while
init_rounds = self.N
else:
loop = for_range(n_loops)
init_rounds = n_loops / self.M
self.paired_therapists = \
self.oram_type(self.N, entry_size=log2(self.N),
init_rounds=0, value_type=self.basic_type)
self.paired_patients = \
self.oram_type(self.N, entry_size=log2(self.N),
init_rounds=0, value_type=self.basic_type)
request_therapist = \
self.oram_type(self.N, entry_size=log2(self.N),
init_rounds=0, value_type=self.basic_type)
self.unpaired = OStack(self.N, oram_type=self.oram_type,
int_type=self.int_type)
@for_range(init_rounds)
def _(i):
self.unpaired.append(i)
rounds = types.MemValue(types.regint(0))
@loop
def _(i=None):
rounds.iadd(1)
time()
patient = self.unpaired.pop()
pref = self.int_type(request_therapist[patient])
if self.M < self.N and n_loops is None:
@if_((pref == self.M).reveal())
def _():
print_ln('run out of acceptable therapists')
crash()
request_therapist[patient] = pref + 1
self.request_therapist(patient, self.p_cases[patient][pref], True)
print_ln('patient: %s, pref: %s, left: %s',
*(x.reveal() for x in (patient, pref, self.unpaired.size)))
return types.regint((self.unpaired.size > 0).reveal())
print_ln('%s rounds', rounds)
@for_range(init_rounds)
def _(i):
types.cint(i).print_reg('ther')
self.paired_patients[i].reveal().print_reg('pati')
def __init__(self, N, p_cases, t_exps, M=1, reverse=False,
oram_type=OptimalORAM, int_type=types.sint):
self.N = N
self.M = M
self.p_cases = p_cases
self.t_exps = t_exps
self.reverse = reverse
self.oram_type = oram_type
self.int_type = int_type
self.basic_type = int_type.basic_type
print('match', N, M)
# Constants
PLAYERS = 3
MATCHING_SIZE = 10
p_shares = Matrix(rows=PLAYERS, columns=MATCHING_SIZE, value_type=types.sint)
t_shares = Matrix(rows=PLAYERS, columns=MATCHING_SIZE, value_type=types.sint)
# Fill data from players into the matrices
# The matrix is ordered as m[row:player][col:share]
@for_range(PLAYERS)
def _(i):
p_index = MemValue(cint(0))
t_index = MemValue(cint(0))
@do_while
def _():
try:
typ = cint.get_input_from(i)
@if_e(typ == -100)
def _():
p_shares[i][p_index.read()] = sint.get_input_from(i)
p_index.iadd(1)
@else_
def _():
@if_(typ == -200)
def _():
t_shares[i][t_index.read()] = sint.get_input_from(i)
t_index.iadd(1)
return 1
except Exception:
return 0
# Add entire column together to reveal secret-shared input
p_cases = OMatrix(N=MATCHING_SIZE, M=1, oram_type=OptimalORAM, int_type=types.sint)
t_exps = OMatrix(N=MATCHING_SIZE, M=1, oram_type=OptimalORAM, int_type=types.sint)
@for_range(MATCHING_SIZE)
def _(i):
p_cases[i][0] = sum(p_shares.get_column(i))
t_exps[i][0] = sum(t_shares.get_column(i))
mm = Matchmaker(MATCHING_SIZE, p_cases, t_exps)
mm.match()