viff

changeset 583:0fa8b538aca8

Move non-crypto parts of Runtime into BasicRuntime.
author Martin Geisler <mg@daimi.au.dk>
date Wed, 19 Mar 2008 21:58:23 +0100
parents 05b869ec4cd5
children c355c6ffdb0a
files viff/runtime.py
diffstat 1 files changed, 49 insertions(+), 37 deletions(-) [+]
line diff
     1.1 --- a/viff/runtime.py	Wed Mar 19 21:50:39 2008 +0100
     1.2 +++ b/viff/runtime.py	Wed Mar 19 21:58:23 2008 +0100
     1.3 @@ -326,21 +326,11 @@
     1.4      return inc_pc_wrapper
     1.5  
     1.6  
     1.7 -class Runtime:
     1.8 -    """The VIFF runtime.
     1.9 +class BasicRuntime:
    1.10 +    """Basic VIFF runtime with no crypto.
    1.11  
    1.12 -    The runtime is used for sharing values (L{shamir_share} or
    1.13 -    L{prss_share}) into L{Share} object and opening such shares
    1.14 -    (L{open}) again. Calculations on shares is normally done through
    1.15 -    overloaded arithmetic operations, but it is also possible to call
    1.16 -    L{add}, L{mul}, etc. directly if one prefers.
    1.17 -
    1.18 -    Each player in the protocol uses a Runtime object. To create in
    1.19 -    instance and connect it correctly with the other players, please
    1.20 -    use the L{create_runtime} function instead of instantiating a
    1.21 -    Runtime directly. The L{create_runtime} function will take care of
    1.22 -    setting up network connections and return a Deferred which
    1.23 -    triggers with the Runtime object when it is ready.
    1.24 +    This runtime contains only the most basic operations needed such
    1.25 +    as the program counter, the list of other players, etc.
    1.26      """
    1.27  
    1.28      @staticmethod
    1.29 @@ -476,15 +466,6 @@
    1.30          # communicating with ourselves.
    1.31          self.add_player(player, None)
    1.32  
    1.33 -        #: Echo counters for Bracha broadcast.
    1.34 -        self._bracha_echo = {}
    1.35 -        #: Ready counters for Bracha broadcast.
    1.36 -        self._bracha_ready = {}
    1.37 -        #: Have we sent a ready message?
    1.38 -        self._bracha_sent_ready = {}
    1.39 -        #: Have we delivered the message?
    1.40 -        self._bracha_delivered = {}
    1.41 -
    1.42      def add_player(self, player, protocol):
    1.43          self.players[player.id] = player
    1.44          self.num_players = len(self.players)
    1.45 @@ -565,6 +546,51 @@
    1.46          result.addCallback(lambda _: None)
    1.47          return result
    1.48  
    1.49 +    def _expect_data(self, peer_id, type, deferred):
    1.50 +        # Convert self.program_counter to a hashable value in order
    1.51 +        # to use it as a key in self.incoming_data.
    1.52 +        pc = tuple(self.program_counter)
    1.53 +        key = (pc, peer_id, type)
    1.54 +
    1.55 +        data = self.incoming_data.pop(key, None)
    1.56 +        if data is None:
    1.57 +            # We have not yet received data from the other side.
    1.58 +            self.incoming_data[key] = deferred
    1.59 +        else:
    1.60 +            # We have already received the data from the other side.
    1.61 +            deferred.callback(data)
    1.62 +
    1.63 +
    1.64 +class Runtime(BasicRuntime):
    1.65 +    """The VIFF runtime.
    1.66 +
    1.67 +    The runtime is used for sharing values (L{shamir_share} or
    1.68 +    L{prss_share}) into L{Share} object and opening such shares
    1.69 +    (L{open}) again. Calculations on shares is normally done through
    1.70 +    overloaded arithmetic operations, but it is also possible to call
    1.71 +    L{add}, L{mul}, etc. directly if one prefers.
    1.72 +
    1.73 +    Each player in the protocol uses a Runtime object. To create in
    1.74 +    instance and connect it correctly with the other players, please
    1.75 +    use the L{create_runtime} function instead of instantiating a
    1.76 +    Runtime directly. The L{create_runtime} function will take care of
    1.77 +    setting up network connections and return a Deferred which
    1.78 +    triggers with the Runtime object when it is ready.
    1.79 +    """
    1.80 +
    1.81 +    def __init__(self, player, threshold, options=None):
    1.82 +        """Initialize runtime."""
    1.83 +        BasicRuntime.__init__(self, player, threshold, options)
    1.84 +
    1.85 +        #: Echo counters for Bracha broadcast.
    1.86 +        self._bracha_echo = {}
    1.87 +        #: Ready counters for Bracha broadcast.
    1.88 +        self._bracha_ready = {}
    1.89 +        #: Have we sent a ready message?
    1.90 +        self._bracha_sent_ready = {}
    1.91 +        #: Have we delivered the message?
    1.92 +        self._bracha_delivered = {}
    1.93 +
    1.94      @increment_pc
    1.95      def open(self, share, receivers=None, threshold=None):
    1.96          """Open a secret sharing.
    1.97 @@ -1011,20 +1037,6 @@
    1.98          self._expect_data(peer_id, "share", share)
    1.99          return share
   1.100  
   1.101 -    def _expect_data(self, peer_id, type, deferred):
   1.102 -        # Convert self.program_counter to a hashable value in order
   1.103 -        # to use it as a key in self.incoming_data.
   1.104 -        pc = tuple(self.program_counter)
   1.105 -        key = (pc, peer_id, type)
   1.106 -
   1.107 -        data = self.incoming_data.pop(key, None)
   1.108 -        if data is None:
   1.109 -            # We have not yet received data from the other side.
   1.110 -            self.incoming_data[key] = deferred
   1.111 -        else:
   1.112 -            # We have already received the data from the other side.
   1.113 -            deferred.callback(data)
   1.114 -
   1.115      @increment_pc
   1.116      def _recombine(self, shares, threshold):
   1.117          """Shamir recombine a list of deferred (id,share) pairs."""