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 wrap: on
line diff
--- a/doc/runtime.txt	Fri Apr 03 10:58:40 2009 +0200
+++ b/doc/runtime.txt	Tue Apr 14 15:07:30 2009 +0200
@@ -21,6 +21,16 @@
          or the data itself if data is received from the other player
          before we are ready to use it.
 
+   .. attribute:: SHARE
+                  ECHO
+                  READY
+                  SEND
+                  PAILLIER
+
+      Constants used by :class:`ShareExchanger` when sending shares
+      and other messages. They serve to distinguish messages sent with
+      the same program counter from one another.
+
    .. autofunction:: increment_pc
 
    .. autofunction:: preprocess
--- a/viff/active.py	Fri Apr 03 10:58:40 2009 +0200
+++ b/viff/active.py	Tue Apr 14 15:07:30 2009 +0200
@@ -28,6 +28,7 @@
 from viff.matrix import Matrix, hyper
 from viff.passive import PassiveRuntime
 from viff.runtime import Share, increment_pc, preprocess, gather_shares
+from viff.runtime import ECHO, READY, SEND
 
 
 class BrachaBroadcastMixin:
@@ -80,7 +81,7 @@
                 ids.append(peer_id)
                 if len(ids) >= ceil((n+t+1)/2) and not ready:
                     bracha_sent_ready[message] = True
-                    unsafe_broadcast("ready", message)
+                    unsafe_broadcast(READY, message)
                     ready_received(message, self.id)
 
         def ready_received(message, peer_id):
@@ -95,7 +96,7 @@
                 ids.append(peer_id)
                 if len(ids) == t+1 and not ready:
                     bracha_sent_ready[message] = True
-                    unsafe_broadcast("ready", message)
+                    unsafe_broadcast(READY, message)
                     ready_received(message, self.id)
 
                 elif len(ids) == 2*t+1 and not delivered:
@@ -107,7 +108,7 @@
             # by sending an echo message to each player. Since the
             # unsafe broadcast doesn't send a message to this player,
             # we simulate it by calling the echo_received function.
-            unsafe_broadcast("echo", message)
+            unsafe_broadcast(ECHO, message)
             echo_received(message, self.id)
 
 
@@ -117,20 +118,20 @@
         for peer_id in self.players:
             if peer_id != self.id:
                 d_echo = Deferred().addCallback(echo_received, peer_id)
-                self._expect_data(peer_id, "echo", d_echo)
+                self._expect_data(peer_id, ECHO, d_echo)
 
                 d_ready = Deferred().addCallback(ready_received, peer_id)
-                self._expect_data(peer_id, "ready", d_ready)
+                self._expect_data(peer_id, READY, d_ready)
 
         # If this player is the sender, we transmit a send message to
         # each player. We send one to this player by calling the
         # send_received function.
         if self.id == sender:
-            unsafe_broadcast("send", message)
+            unsafe_broadcast(SEND, message)
             send_received(message)
         else:
             d_send = Deferred().addCallback(send_received)
-            self._expect_data(sender, "send", d_send)
+            self._expect_data(sender, SEND, d_send)
 
 
         return result
--- a/viff/paillier.py	Fri Apr 03 10:58:40 2009 +0200
+++ b/viff/paillier.py	Tue Apr 14 15:07:30 2009 +0200
@@ -28,6 +28,7 @@
 import gmpy
 
 from viff.runtime import Runtime, increment_pc, Share, gather_shares
+from viff.runtime import PAILLIER
 from viff.util import rand, find_random_prime
 
 def L(u, n):
@@ -170,11 +171,11 @@
                 a1, b1 = a, b
                 enc_a1 = encrypt(a1.value, self.player.pubkey)
                 enc_b1 = encrypt(b1.value, self.player.pubkey)
-                send_data(pc, "paillier", enc_a1)
-                send_data(pc, "paillier", enc_b1)
+                send_data(pc, PAILLIER, enc_a1)
+                send_data(pc, PAILLIER, enc_b1)
 
                 enc_c1 = Share(self, field)
-                self._expect_data(self.peer.id, "paillier", enc_c1)
+                self._expect_data(self.peer.id, PAILLIER, enc_c1)
                 c1 = enc_c1.addCallback(decrypt, self.player.seckey)
                 c1.addCallback(lambda c: long(c) + a1 * b1)
                 return c1
@@ -182,9 +183,9 @@
                 # We play the role of P2.
                 a2, b2 = a, b
                 enc_a1 = Deferred()
-                self._expect_data(self.peer.id, "paillier", enc_a1)
+                self._expect_data(self.peer.id, PAILLIER, enc_a1)
                 enc_b1 = Deferred()
-                self._expect_data(self.peer.id, "paillier", enc_b1)
+                self._expect_data(self.peer.id, PAILLIER, enc_b1)
 
                 nsq = self.peer.pubkey[0]**2
                 # Calculate a1 * b2 and b1 * a2 inside the encryption.
@@ -197,7 +198,7 @@
 
                 c1 = gatherResults([enc_a1_b2, enc_b1_a2])
                 c1.addCallback(lambda (a,b): a * b * enc_r)
-                c1.addCallback(lambda c: send_data(pc, "paillier", c))
+                c1.addCallback(lambda c: send_data(pc, PAILLIER, c))
 
                 c2 = a2 * b2 - r
                 return Share(self, field, c2)
--- a/viff/runtime.py	Fri Apr 03 10:58:40 2009 +0200
+++ b/viff/runtime.py	Tue Apr 14 15:07:30 2009 +0200
@@ -49,6 +49,13 @@
 from twisted.internet.protocol import ReconnectingClientFactory, ServerFactory
 from twisted.protocols.basic import Int16StringReceiver
 
+# Constants used by ShareExchanger.
+SHARE    = 0
+ECHO     = 1
+READY    = 2
+SEND     = 3
+PAILLIER = 4
+
 
 class Share(Deferred):
     """A shared number.
@@ -323,7 +330,7 @@
         The program counter and the share are marshalled and sent to
         the peer.
         """
-        self.sendData(program_counter, "share", share.value)
+        self.sendData(program_counter, SHARE, share.value)
 
     def loseConnection(self):
         """Disconnect this protocol instance."""
@@ -630,7 +637,7 @@
     def _expect_share(self, peer_id, field):
         share = Share(self, field)
         share.addCallback(lambda value: field(value))
-        self._expect_data(peer_id, "share", share)
+        self._expect_data(peer_id, SHARE, share)
         return share
 
     @increment_pc
--- a/viff/test/test_basic_runtime.py	Fri Apr 03 10:58:40 2009 +0200
+++ b/viff/test/test_basic_runtime.py	Tue Apr 14 15:07:30 2009 +0200
@@ -140,9 +140,9 @@
         for peer_id in range(1, self.num_players+1):
             if peer_id != runtime.id:
                 pc = tuple(runtime.program_counter)
-                runtime.protocols[peer_id].sendData(pc, "test", 100)
-                runtime.protocols[peer_id].sendData(pc, "test", 200)
-                runtime.protocols[peer_id].sendData(pc, "test", 300)
+                runtime.protocols[peer_id].sendData(pc, 42, 100)
+                runtime.protocols[peer_id].sendData(pc, 42, 200)
+                runtime.protocols[peer_id].sendData(pc, 42, 300)
 
         # Then receive the data.
         deferreds = []
@@ -151,9 +151,9 @@
                 d100 = Deferred().addCallback(self.assertEquals, 100)
                 d200 = Deferred().addCallback(self.assertEquals, 200)
                 d300 = Deferred().addCallback(self.assertEquals, 300)
-                runtime._expect_data(peer_id, "test", d100)
-                runtime._expect_data(peer_id, "test", d200)
-                runtime._expect_data(peer_id, "test", d300)
+                runtime._expect_data(peer_id, 42, d100)
+                runtime._expect_data(peer_id, 42, d200)
+                runtime._expect_data(peer_id, 42, d300)
                 deferreds.extend([d100, d200, d300])
 
         return gatherResults(deferreds)