changeset 1526:a87fd09f8c38

BeDOZa: Split data send in add_macs into chunks that are smaller than 65536 bytes.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Thu, 29 Jul 2010 11:31:24 +0200
parents 80841840990f
children 7dd55d319d2b
files viff/bedoza/add_macs.py viff/test/test_bedoza_triple.py
diffstat 2 files changed, 42 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/viff/bedoza/add_macs.py	Mon Jul 26 14:46:15 2010 +0200
+++ b/viff/bedoza/add_macs.py	Thu Jul 29 11:31:24 2010 +0200
@@ -15,6 +15,8 @@
 # You should have received a copy of the GNU Lesser General Public
 # License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
 
+import struct
+
 from twisted.internet.defer import gatherResults
 from viff.runtime import Share
 
@@ -24,6 +26,7 @@
 
 from viff.bedoza.shares import BeDOZaShare, BeDOZaShareContents
 
+
 def add_macs(runtime, field, u_bound, alpha, random, paillier, partial_shares):
     """Adds macs to the set of PartialBeDOZaShares.
         
@@ -37,27 +40,50 @@
     runtime.increment_pc() # Huh!?
 
     def do_add_macs(partial_share_contents, result_shares):
+        """The transmission_restraint_constant is the number of
+        encrypted shares we can safely transmit in one call to
+        sendData. The sendData method can only transmit up to
+        65536 bytes.
+        """
+        transmission_restraint_constant = 50
+        
         num_players = runtime.num_players
-        lists_of_mac_keys = [ [] for x in runtime.players ]
-        lists_of_c_list = [ [] for x in runtime.players ]
-        for partial_share_content in partial_share_contents:
-            for j in xrange(0, num_players):
+
+        list_of_player_to_enc_shares_lists = []
+        
+        player_to_mac_keys = [ [] for x in runtime.players]
+        player_to_enc_shares = [ [] for x in runtime.players]
+        for inx, partial_share_content in enumerate(partial_share_contents):
+            if inx % transmission_restraint_constant == 0:
+                player_to_enc_shares = [ [] for x in runtime.players]
+                list_of_player_to_enc_shares_lists.append(player_to_enc_shares)
+            for j in xrange(num_players):
                 # TODO: This is probably not the fastes way to generate
                 # the betas.
                 beta = random.randint(0, u_bound)
                 # TODO: Outcommented until mod paillier works for negative numbers.
                 # if rand.choice([True, False]):
                 #    beta = -beta
-                enc_beta = paillier.encrypt(beta, player_id=j + 1)
-                c_j = partial_share_content.enc_shares[j]
-                n2 = paillier.get_modulus_square(j + 1)
+                enc_beta = paillier.encrypt(beta, player_id = j + 1)
+                c_j = partial_share_content.enc_shares[ j ]
+                n2 = paillier.get_modulus_square( j + 1 )
                 c = (pow(c_j, alpha, n2) * enc_beta) % n2
-                lists_of_c_list[j].append(c)
-                lists_of_mac_keys[j].append(field(beta))
+                player_to_enc_shares[j].append(c)
+                player_to_mac_keys[j].append(field(beta))
 
-        received_cs = _send(runtime, lists_of_c_list, deserialize=eval)
+        received_cs = []
+        for ls in list_of_player_to_enc_shares_lists:
+            received_cs.append(_send(runtime, ls, deserialize=eval))
+
+        def merge(received_cs):
+            r = [ [] for x in xrange(len(received_cs[0]))]
+            for ls in received_cs:
+                for inx, xs in enumerate(ls):
+                    r[inx] += xs
+            return r
 
         def finish_sharing(recevied_cs, partial_share_contents, lists_of_mac_keys, result_shares):
+            recevied_cs = merge(recevied_cs)
             shares = []               
             for inx in xrange(0, len(partial_share_contents)):
                 mac_keys = []
@@ -75,7 +101,11 @@
                                                                 mac_msg_list))
             return shares
 
-        runtime.schedule_callback(received_cs, finish_sharing, partial_share_contents, lists_of_mac_keys, result_shares)
+        runtime.schedule_callback(gatherResults(received_cs),
+                                  finish_sharing,
+                                  partial_share_contents,
+                                  player_to_mac_keys,
+                                  result_shares)
         return received_cs
 
     result_shares = [Share(runtime, field) for x in xrange(len(partial_shares))]
--- a/viff/test/test_bedoza_triple.py	Mon Jul 26 14:46:15 2010 +0200
+++ b/viff/test/test_bedoza_triple.py	Thu Jul 29 11:31:24 2010 +0200
@@ -411,7 +411,7 @@
         random = Random(283883)        
         triple_generator = TripleGenerator(runtime, p, random)
 
-        triples = triple_generator.generate_triples(5)
+        triples = triple_generator.generate_triples(100)
 
         def check((a, b, c)):
             self.assertEquals(c, a * b)