Skip to content

Commit

Permalink
PRSS details from asyncoro to runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
lschoe committed Feb 25, 2024
1 parent 62256f4 commit 12e678a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion mpyc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
and statistics (securely mimicking Python’s statistics module).
"""

__version__ = '0.9.8'
__version__ = '0.9.9'
__license__ = 'MIT License'

import os
Expand Down
24 changes: 5 additions & 19 deletions mpyc/asyncoro.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import sys
import traceback
from struct import pack, unpack_from
import itertools
import functools
import typing
from asyncio import Protocol, Future, Task
Expand Down Expand Up @@ -54,11 +53,7 @@ def connection_made(self, transport):
rt = self.runtime
pid_keys = [rt.pid.to_bytes(2, 'little')] # send pid
if not rt.options.no_prss:
m = len(rt.parties)
t = rt.threshold
for subset in itertools.combinations(range(m), m - t):
if subset[0] == rt.pid and self.peer_pid in subset:
pid_keys.append(rt._prss_keys[subset]) # send PRSS keys
pid_keys.extend(rt._prss_keys_to_peer(self.peer_pid)) # send PRSS keys
transport.writelines(pid_keys)
self._key_transport_done()

Expand Down Expand Up @@ -89,27 +84,18 @@ def data_received(self, data):
return

peer_pid = int.from_bytes(data[:2], 'little')
len_packet = 2
del data[:2]
rt = self.runtime
if not rt.options.no_prss:
m = len(rt.parties)
t = rt.threshold
for subset in itertools.combinations(range(m), m - t):
if subset[0] == peer_pid and rt.pid in subset:
len_packet += 16
len_packet = rt._prss_keys_from_peer(peer_pid)
if len(data) < len_packet:
return

# record new protocol peer
self.peer_pid = peer_pid
if not rt.options.no_prss:
# store keys received from peer
len_packet = 2
for subset in itertools.combinations(range(m), m - t):
if subset[0] == peer_pid and rt.pid in subset:
rt._prss_keys[subset] = data[len_packet:len_packet + 16]
len_packet += 16
del data[:len_packet]
rt._prss_keys_from_peer(peer_pid, data) # store PRSS keys from peer
del data[:len_packet]
self._key_transport_done()

while len(data) >= 12:
Expand Down
7 changes: 4 additions & 3 deletions mpyc/mpctools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

runtime = None

_no_value = type('', (object,), {'__repr__': lambda self: '<no value>'})
_no_value = type('mpyc.mpctools.NoValueType', (object,), {'__repr__': lambda self: '<no value>'})()
_no_value.__doc__ = 'Represents "empty" value, different from any other object including None.'


def reduce(f, x, initial=_no_value):
Expand All @@ -27,8 +28,8 @@ def reduce(f, x, initial=_no_value):
may even be of different types.
If initial is provided (possibly equal to None), it is placed before the
items of x (hence effectively serves as a default when x is empty). If
initial is not given and x contains only one item, that item is returned.
items of x (hence effectively serves as a default when x is empty). If no
initial value is given and x contains only one item, that item is returned.
"""
x = list(x)
if initial is not _no_value:
Expand Down
25 changes: 25 additions & 0 deletions mpyc/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,31 @@ def threshold(self, t):
keys[subset] = secrets.token_bytes(16) # 128-bit key
self._prss_keys = keys

def _prss_keys_to_peer(self, peer_pid):
"""Return PRSS keys to be sent to peer."""
m = len(self.parties)
t = self.threshold
keys = []
for subset in itertools.combinations(range(m), m - t):
if subset[0] == self.pid and peer_pid in subset:
keys.append(self._prss_keys[subset])
return keys

def _prss_keys_from_peer(self, peer_pid, data=None):
"""Store PRSS keys received from peer.
If data is not given, return the size of PRSS keys to be stored.
"""
m = len(self.parties)
t = self.threshold
len_packet = 0
for subset in itertools.combinations(range(m), m - t):
if subset[0] == peer_pid and self.pid in subset:
if data is not None:
self._prss_keys[subset] = data[len_packet:len_packet + 16]
len_packet += 16
return len_packet

@functools.cache
def prfs(self, bound):
"""PRFs with codomain range(bound) for pseudorandom secret sharing.
Expand Down

0 comments on commit 12e678a

Please sign in to comment.