viff

changeset 1158:ed3302fd93f1

Use symbolic constants for data types. Part of this patch: http://lists.viff.dk/pipermail/viff-patches-viff.dk/2008-October/000053.html Committed by Martin Geisler.
author Mikkel Krøigård <mk@daimi.au.dk>
date Tue, 14 Apr 2009 15:07:30 +0200
parents e94a7d1eae29
children a8206d017f43
files doc/runtime.txt viff/active.py viff/paillier.py viff/runtime.py viff/test/test_basic_runtime.py
diffstat 5 files changed, 40 insertions(+), 21 deletions(-) [+]
line diff
     1.1 --- a/doc/runtime.txt	Fri Apr 03 10:58:40 2009 +0200
     1.2 +++ b/doc/runtime.txt	Tue Apr 14 15:07:30 2009 +0200
     1.3 @@ -21,6 +21,16 @@
     1.4           or the data itself if data is received from the other player
     1.5           before we are ready to use it.
     1.6  
     1.7 +   .. attribute:: SHARE
     1.8 +                  ECHO
     1.9 +                  READY
    1.10 +                  SEND
    1.11 +                  PAILLIER
    1.12 +
    1.13 +      Constants used by :class:`ShareExchanger` when sending shares
    1.14 +      and other messages. They serve to distinguish messages sent with
    1.15 +      the same program counter from one another.
    1.16 +
    1.17     .. autofunction:: increment_pc
    1.18  
    1.19     .. autofunction:: preprocess
     2.1 --- a/viff/active.py	Fri Apr 03 10:58:40 2009 +0200
     2.2 +++ b/viff/active.py	Tue Apr 14 15:07:30 2009 +0200
     2.3 @@ -28,6 +28,7 @@
     2.4  from viff.matrix import Matrix, hyper
     2.5  from viff.passive import PassiveRuntime
     2.6  from viff.runtime import Share, increment_pc, preprocess, gather_shares
     2.7 +from viff.runtime import ECHO, READY, SEND
     2.8  
     2.9  
    2.10  class BrachaBroadcastMixin:
    2.11 @@ -80,7 +81,7 @@
    2.12                  ids.append(peer_id)
    2.13                  if len(ids) >= ceil((n+t+1)/2) and not ready:
    2.14                      bracha_sent_ready[message] = True
    2.15 -                    unsafe_broadcast("ready", message)
    2.16 +                    unsafe_broadcast(READY, message)
    2.17                      ready_received(message, self.id)
    2.18  
    2.19          def ready_received(message, peer_id):
    2.20 @@ -95,7 +96,7 @@
    2.21                  ids.append(peer_id)
    2.22                  if len(ids) == t+1 and not ready:
    2.23                      bracha_sent_ready[message] = True
    2.24 -                    unsafe_broadcast("ready", message)
    2.25 +                    unsafe_broadcast(READY, message)
    2.26                      ready_received(message, self.id)
    2.27  
    2.28                  elif len(ids) == 2*t+1 and not delivered:
    2.29 @@ -107,7 +108,7 @@
    2.30              # by sending an echo message to each player. Since the
    2.31              # unsafe broadcast doesn't send a message to this player,
    2.32              # we simulate it by calling the echo_received function.
    2.33 -            unsafe_broadcast("echo", message)
    2.34 +            unsafe_broadcast(ECHO, message)
    2.35              echo_received(message, self.id)
    2.36  
    2.37  
    2.38 @@ -117,20 +118,20 @@
    2.39          for peer_id in self.players:
    2.40              if peer_id != self.id:
    2.41                  d_echo = Deferred().addCallback(echo_received, peer_id)
    2.42 -                self._expect_data(peer_id, "echo", d_echo)
    2.43 +                self._expect_data(peer_id, ECHO, d_echo)
    2.44  
    2.45                  d_ready = Deferred().addCallback(ready_received, peer_id)
    2.46 -                self._expect_data(peer_id, "ready", d_ready)
    2.47 +                self._expect_data(peer_id, READY, d_ready)
    2.48  
    2.49          # If this player is the sender, we transmit a send message to
    2.50          # each player. We send one to this player by calling the
    2.51          # send_received function.
    2.52          if self.id == sender:
    2.53 -            unsafe_broadcast("send", message)
    2.54 +            unsafe_broadcast(SEND, message)
    2.55              send_received(message)
    2.56          else:
    2.57              d_send = Deferred().addCallback(send_received)
    2.58 -            self._expect_data(sender, "send", d_send)
    2.59 +            self._expect_data(sender, SEND, d_send)
    2.60  
    2.61  
    2.62          return result
     3.1 --- a/viff/paillier.py	Fri Apr 03 10:58:40 2009 +0200
     3.2 +++ b/viff/paillier.py	Tue Apr 14 15:07:30 2009 +0200
     3.3 @@ -28,6 +28,7 @@
     3.4  import gmpy
     3.5  
     3.6  from viff.runtime import Runtime, increment_pc, Share, gather_shares
     3.7 +from viff.runtime import PAILLIER
     3.8  from viff.util import rand, find_random_prime
     3.9  
    3.10  def L(u, n):
    3.11 @@ -170,11 +171,11 @@
    3.12                  a1, b1 = a, b
    3.13                  enc_a1 = encrypt(a1.value, self.player.pubkey)
    3.14                  enc_b1 = encrypt(b1.value, self.player.pubkey)
    3.15 -                send_data(pc, "paillier", enc_a1)
    3.16 -                send_data(pc, "paillier", enc_b1)
    3.17 +                send_data(pc, PAILLIER, enc_a1)
    3.18 +                send_data(pc, PAILLIER, enc_b1)
    3.19  
    3.20                  enc_c1 = Share(self, field)
    3.21 -                self._expect_data(self.peer.id, "paillier", enc_c1)
    3.22 +                self._expect_data(self.peer.id, PAILLIER, enc_c1)
    3.23                  c1 = enc_c1.addCallback(decrypt, self.player.seckey)
    3.24                  c1.addCallback(lambda c: long(c) + a1 * b1)
    3.25                  return c1
    3.26 @@ -182,9 +183,9 @@
    3.27                  # We play the role of P2.
    3.28                  a2, b2 = a, b
    3.29                  enc_a1 = Deferred()
    3.30 -                self._expect_data(self.peer.id, "paillier", enc_a1)
    3.31 +                self._expect_data(self.peer.id, PAILLIER, enc_a1)
    3.32                  enc_b1 = Deferred()
    3.33 -                self._expect_data(self.peer.id, "paillier", enc_b1)
    3.34 +                self._expect_data(self.peer.id, PAILLIER, enc_b1)
    3.35  
    3.36                  nsq = self.peer.pubkey[0]**2
    3.37                  # Calculate a1 * b2 and b1 * a2 inside the encryption.
    3.38 @@ -197,7 +198,7 @@
    3.39  
    3.40                  c1 = gatherResults([enc_a1_b2, enc_b1_a2])
    3.41                  c1.addCallback(lambda (a,b): a * b * enc_r)
    3.42 -                c1.addCallback(lambda c: send_data(pc, "paillier", c))
    3.43 +                c1.addCallback(lambda c: send_data(pc, PAILLIER, c))
    3.44  
    3.45                  c2 = a2 * b2 - r
    3.46                  return Share(self, field, c2)
     4.1 --- a/viff/runtime.py	Fri Apr 03 10:58:40 2009 +0200
     4.2 +++ b/viff/runtime.py	Tue Apr 14 15:07:30 2009 +0200
     4.3 @@ -49,6 +49,13 @@
     4.4  from twisted.internet.protocol import ReconnectingClientFactory, ServerFactory
     4.5  from twisted.protocols.basic import Int16StringReceiver
     4.6  
     4.7 +# Constants used by ShareExchanger.
     4.8 +SHARE    = 0
     4.9 +ECHO     = 1
    4.10 +READY    = 2
    4.11 +SEND     = 3
    4.12 +PAILLIER = 4
    4.13 +
    4.14  
    4.15  class Share(Deferred):
    4.16      """A shared number.
    4.17 @@ -323,7 +330,7 @@
    4.18          The program counter and the share are marshalled and sent to
    4.19          the peer.
    4.20          """
    4.21 -        self.sendData(program_counter, "share", share.value)
    4.22 +        self.sendData(program_counter, SHARE, share.value)
    4.23  
    4.24      def loseConnection(self):
    4.25          """Disconnect this protocol instance."""
    4.26 @@ -630,7 +637,7 @@
    4.27      def _expect_share(self, peer_id, field):
    4.28          share = Share(self, field)
    4.29          share.addCallback(lambda value: field(value))
    4.30 -        self._expect_data(peer_id, "share", share)
    4.31 +        self._expect_data(peer_id, SHARE, share)
    4.32          return share
    4.33  
    4.34      @increment_pc
     5.1 --- a/viff/test/test_basic_runtime.py	Fri Apr 03 10:58:40 2009 +0200
     5.2 +++ b/viff/test/test_basic_runtime.py	Tue Apr 14 15:07:30 2009 +0200
     5.3 @@ -140,9 +140,9 @@
     5.4          for peer_id in range(1, self.num_players+1):
     5.5              if peer_id != runtime.id:
     5.6                  pc = tuple(runtime.program_counter)
     5.7 -                runtime.protocols[peer_id].sendData(pc, "test", 100)
     5.8 -                runtime.protocols[peer_id].sendData(pc, "test", 200)
     5.9 -                runtime.protocols[peer_id].sendData(pc, "test", 300)
    5.10 +                runtime.protocols[peer_id].sendData(pc, 42, 100)
    5.11 +                runtime.protocols[peer_id].sendData(pc, 42, 200)
    5.12 +                runtime.protocols[peer_id].sendData(pc, 42, 300)
    5.13  
    5.14          # Then receive the data.
    5.15          deferreds = []
    5.16 @@ -151,9 +151,9 @@
    5.17                  d100 = Deferred().addCallback(self.assertEquals, 100)
    5.18                  d200 = Deferred().addCallback(self.assertEquals, 200)
    5.19                  d300 = Deferred().addCallback(self.assertEquals, 300)
    5.20 -                runtime._expect_data(peer_id, "test", d100)
    5.21 -                runtime._expect_data(peer_id, "test", d200)
    5.22 -                runtime._expect_data(peer_id, "test", d300)
    5.23 +                runtime._expect_data(peer_id, 42, d100)
    5.24 +                runtime._expect_data(peer_id, 42, d200)
    5.25 +                runtime._expect_data(peer_id, 42, d300)
    5.26                  deferreds.extend([d100, d200, d300])
    5.27  
    5.28          return gatherResults(deferreds)