Skip to content

Commit

Permalink
Transition from n-party to m-party. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
lschoe authored Sep 29, 2018
1 parent 0b9face commit feb0719
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 185 deletions.
6 changes: 3 additions & 3 deletions demos/.config/generate-certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def save_certificate(cert, filename):
help='output filename prefix')
parser.add_argument('-k', '--key-size', type=int,
help='key size')
parser.add_argument('-n', '--parties', dest='n', type=int,
parser.add_argument('-m', '--parties', dest='m', type=int,
help='number of parties')
parser.set_defaults(n=5, key_size=2048, prefix='party_')
parser.set_defaults(m=5, key_size=2048, prefix='party_')
options = parser.parse_args()

# self-signed certificate
Expand All @@ -71,7 +71,7 @@ def save_certificate(cert, filename):
save_key(ca_key, 'mpyc_ca.key')
save_certificate(ca_cert, 'mpyc_ca.crt')

for i in range(options.n):
for i in range(options.m):
key = create_key(options.key_size)
req = create_request(key, f'MPyC party {i}')
cert = create_certificate(req, ca_cert, ca_key, 256 + i)
Expand Down
10 changes: 5 additions & 5 deletions demos/.config/generate-config-files.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--prefix',
help='output filename prefix')
parser.add_argument('-n', '--parties', dest='n', type=int,
parser.add_argument('-m', '--parties', dest='m', type=int,
help='number of parties')
parser.add_argument('args', nargs='*')

parser.set_defaults(n=3, prefix='party')
parser.set_defaults(m=3, prefix='party')

options = parser.parse_args()
args = options.args

if len(args) != options.n:
if len(args) != options.m:
parser.error('A hostname:port argument required for each party.')

addresses = [arg.split(':', 1) for arg in args]
configs = generate_configs(options.n, addresses)
configs = generate_configs(options.m, addresses)

for party, config in enumerate(configs):
filename = f'{options.prefix}{options.n}_{party}.ini'
filename = f'{options.prefix}{options.m}_{party}.ini'
config.write(open(filename, 'w'))
14 changes: 7 additions & 7 deletions demos/ot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import sys
from mpyc.runtime import mpc

n = len(mpc.parties)
m = len(mpc.parties)

if n % 2 == 0:
if m % 2 == 0:
print('OT runs with odd number of parties only.')
sys.exit()

t = n // 2
t = m // 2
message = [(None, None)] * t
choice = [None] * t
if mpc.id == 0:
Expand All @@ -25,10 +25,10 @@

secnum = mpc.SecInt()
for i in range(1, t + 1):
m = mpc.input([secnum(message[i - 1][0]), secnum(message[i - 1][1])], i)
x = mpc.input([secnum(message[i - 1][0]), secnum(message[i - 1][1])], i)
b = mpc.input(secnum(choice[i - t - 1]), t + i)
m = mpc.run(mpc.output(m[0] + b * (m[1] - m[0]), t + i))
if m:
print(f'You have received message {m}.')
a = mpc.run(mpc.output(x[0] + b * (x[1] - x[0]), t + i))
if a:
print(f'You have received message {a}.')

mpc.shutdown()
14 changes: 7 additions & 7 deletions demos/secretsanta.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,26 @@ async def random_derangement(n, sectype):

def main():
if sys.argv[1:]:
m = int(sys.argv[1])
N = int(sys.argv[1])
else:
m = 8
print('Setting input to default =', m)
N = 8
print('Setting input to default =', N)

mpc.start()

secfld = mpc.SecFld(l=max(len(mpc.parties), (m - 1)).bit_length() + 1)
secfld = mpc.SecFld(l=max(len(mpc.parties), (N - 1)).bit_length() + 1)
print('Using secure fields:', secfld)
for n in range(2, m + 1):
for n in range(2, N + 1):
print(n, mpc.run(mpc.output(random_derangement(n, secfld))))

secint = mpc.SecInt()
print('Using secure integers:', secint)
for n in range(2, m + 1):
for n in range(2, N + 1):
print(n, mpc.run(mpc.output(random_derangement(n, secint))))

secfxp = mpc.SecFxp()
print('Using secure fixed-point numbers:', secfxp)
for n in range(2, m + 1):
for n in range(2, N + 1):
print(n, mpc.run(mpc.output(random_derangement(n, secfxp))))

mpc.shutdown()
Expand Down
2 changes: 1 addition & 1 deletion demos/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def bitonic_compare(i, j, up):

def main():
if sys.argv[1:]:
m = int(sys.argv[1])
n = int(sys.argv[1])
else:
n = 5
print('Setting input to default =', n)
Expand Down
2 changes: 1 addition & 1 deletion mpyc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
are provided as well (e.g., some matrix-vector operations).
"""

__version__ = '0.3.5'
__version__ = '0.3.6'
__license__ = 'Apache License 2.0'
2 changes: 1 addition & 1 deletion mpyc/asyncoro.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def returnType(rettype=None, *args):
Used in first await expression in an MPC coroutine.
"""
if rettype == None:
if rettype is None:
return _afuture(None)
if isinstance(rettype, Future):
return _afuture(Future)
Expand Down
26 changes: 12 additions & 14 deletions mpyc/pfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,34 +105,32 @@ def __abs__(self):
return self.unsigned()

@classmethod
def to_bytes(cls, values):
"""Return an array of bytes representing the given list of values.
def to_bytes(cls, x):
"""Return an array of bytes representing the given list of values x.
Values are either integers or field elements.
"""
m = len(values)
r = (cls.modulus.bit_length() + 7) // 8
data = bytearray(2 + m * r)
data = bytearray(2 + len(x) * r)
data[:2] = r.to_bytes(2, byteorder='little')
j = 2
for i in range(m):
e = values[i]
if not isinstance(e, int): e = e.value
data[j:j + r] = e.to_bytes(r, byteorder='little')
for v in x:
if not isinstance(v, int): v = v.value
data[j:j + r] = v.to_bytes(r, byteorder='little')
j += r
return data

@staticmethod
def from_bytes(data):
"""Return the list of elements represented by the given array of bytes."""
"""Return the list of integers represented by the given array of bytes."""
r = int.from_bytes(data[:2], byteorder='little')
m = (len(data) - 2) // r
elements = [None] * m
n = (len(data) - 2) // r
x = [None] * n
j = 2
for i in range(m):
elements[i] = int.from_bytes(data[j:j + r], byteorder='little')
for i in range(n):
x[i] = int.from_bytes(data[j:j + r], byteorder='little')
j += r
return elements
return x

def __add__(self, other):
"""Addition."""
Expand Down
Loading

0 comments on commit feb0719

Please sign in to comment.