changeset 1531:5ac22a4919ec

BeDOZa: Shortened long lines.
author Thomas P Jakobsen <tpj@cs.au.dk>
date Fri, 06 Aug 2010 15:00:46 +0200
parents 408356ca17f4
children efbdd25601b8
files viff/bedoza/add_macs.py viff/bedoza/bedoza.py viff/bedoza/bedoza_triple.py viff/bedoza/modified_paillier.py viff/bedoza/share_generators.py viff/bedoza/shares.py
diffstat 6 files changed, 99 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/viff/bedoza/add_macs.py	Fri Aug 06 11:34:50 2010 +0200
+++ b/viff/bedoza/add_macs.py	Fri Aug 06 15:00:46 2010 +0200
@@ -65,7 +65,8 @@
                 # 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.
+                # 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)
@@ -86,7 +87,8 @@
                     r[inx] += xs
             return r
 
-        def finish_sharing(recevied_cs, partial_share_contents, lists_of_mac_keys, result_shares):
+        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)):
@@ -100,9 +102,9 @@
                 mac_key_list = BeDOZaKeyList(alpha, mac_keys)
 
                 mac_msg_list = BeDOZaMACList(decrypted_cs)
-                result_shares[inx].callback(BeDOZaShareContents(partial_share.value,
-                                                                mac_key_list,
-                                                                mac_msg_list))
+                result_shares[inx].callback(
+                    BeDOZaShareContents(partial_share.value, mac_key_list,
+                                        mac_msg_list))
             return shares
 
         runtime.schedule_callback(gatherResults(received_cs),
@@ -112,7 +114,8 @@
                                   result_shares)
         return received_cs
 
-    result_shares = [Share(runtime, field) for x in xrange(len(partial_shares))]
+    result_shares = [Share(runtime, field)
+                     for x in xrange(len(partial_shares))]
     runtime.schedule_callback(gatherResults(partial_shares),
                               do_add_macs,
                               result_shares)
--- a/viff/bedoza/bedoza.py	Fri Aug 06 11:34:50 2010 +0200
+++ b/viff/bedoza/bedoza.py	Fri Aug 06 15:00:46 2010 +0200
@@ -64,7 +64,9 @@
                 if reduce(lambda x, y: true_str == y, ls):
                     return x
                 else:
-                    raise BeDOZaException("Wrong commitment. Some player revieved a wrong commitment. My commitments were: %s", isOK)
+                    raise BeDOZaException("Wrong commitment. Some player "
+                                          "revieved a wrong commitment. My "
+                                          "commitments were: %s", isOK)
 
             n = len(self.players)
             alpha = keyLists[0].alpha
@@ -84,7 +86,8 @@
             ds = self.broadcast(self.players.keys(), self.players.keys(),
                                 str(isOK))
             ds = gatherResults(ds)
-            ds.addCallbacks(check, self.error_handler, callbackArgs=(values, isOK))
+            ds.addCallbacks(check, self.error_handler,
+                            callbackArgs=(values, isOK))
             return ds
         
         def exchange(ls, receivers):
@@ -96,7 +99,8 @@
                 for inx, beDOZaContents in enumerate(ls):
                     keyLists.append(beDOZaContents.get_keys())
                     message_string += "%s:%s;" % \
-                           (beDOZaContents.get_value().value, beDOZaContents.get_mac(other_id - 1).value)
+                           (beDOZaContents.get_value().value,
+                            beDOZaContents.get_mac(other_id - 1).value)
                 self.protocols[other_id].sendData(pc, TEXT, message_string)
 
             if self.id in receivers:
@@ -114,7 +118,8 @@
                     self._expect_data(other_id, TEXT, d)
                     values[inx] = d
                 result = gatherResults(values)
-                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(keyLists, len(shares)))
+                result.addCallbacks(recombine_value, self.error_handler,
+                                    callbackArgs=(keyLists, len(shares)))
                 return result
 
         result = gather_shares(shares)
@@ -145,7 +150,9 @@
                 if reduce(lambda x, y: true_str == y, ls):
                     return a, b
                 else:
-                    raise BeDOZaException("Wrong commitment. Some player revieved a wrong commitment. My commitments were: %s", isOK)
+                    raise BeDOZaException("Wrong commitment. Some player "
+                                          "revieved a wrong commitment. My "
+                                          "commitments were: %s", isOK)
 
             n = len(self.players)
             alpha_a = keyList_a.alpha
@@ -171,7 +178,8 @@
             ds = self.broadcast(self.players.keys(), self.players.keys(),
                                 str(isOK))
             ds = gatherResults(ds)
-            ds.addCallbacks(check, self.error_handler, callbackArgs=(a, b, isOK))
+            ds.addCallbacks(check, self.error_handler,
+                            callbackArgs=(a, b, isOK))
             return ds
         
         def exchange((a, b), receivers):
@@ -195,7 +203,8 @@
                     values_b[inx] =  self._expect_share(other_id, field)
                     codes_b[inx] = self._expect_share(other_id, field)
                 result = gatherResults(values_a + codes_a + values_b + codes_b)
-                self.schedule_callback(result, recombine_value, a.get_keys(), b.get_keys())
+                self.schedule_callback(result, recombine_value, a.get_keys(),
+                                       b.get_keys())
                 return result
 
         result = gather_shares([share_a, share_b])
@@ -236,7 +245,9 @@
                 if reduce(lambda x, y: true_str == y, ls):
                     return x
                 else:
-                    raise BeDOZaException("Wrong commitment. Some player revieved a wrong commitment. My commitments were: %s", isOK)
+                    raise BeDOZaException("Wrong commitment. Some player "
+                                          "revieved a wrong commitment. My "
+                                          "commitments were: %s", isOK)
                 
             ds = self.broadcast(self.players.keys(), self.players.keys(),
                                 str(isOK))
@@ -248,8 +259,10 @@
             # Send share to all receivers.
             pc = tuple(self.program_counter)
             for other_id in receivers:
-                self.protocols[other_id].sendShare(pc, shareContent.get_value())
-                self.protocols[other_id].sendShare(pc, shareContent.get_mac(other_id - 1))
+                self.protocols[other_id].sendShare(
+                    pc, shareContent.get_value())
+                self.protocols[other_id].sendShare(
+                    pc, shareContent.get_mac(other_id - 1))
             if self.id in receivers:
                 num_players = len(self.players.keys())
                 values = num_players * [None]
@@ -258,7 +271,8 @@
                     values[inx] =  self._expect_share(other_id, field)
                     codes[inx] = self._expect_share(other_id, field)
                 result = gatherResults(values + codes)
-                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(shareContent.get_keys(),))
+                result.addCallbacks(recombine_value, self.error_handler,
+                                    callbackArgs=(shareContent.get_keys(),))
                 return result
 
         result = share.clone()
@@ -273,7 +287,8 @@
 
     def _plus_public(self, x, c, field):
         x = x.add_public(c, self.id)
-        return BeDOZaShare(self, field, x.get_value(), x.get_keys(), x.get_macs())
+        return BeDOZaShare(self, field, x.get_value(), x.get_keys(),
+                           x.get_macs())
 
     def _plus(self, (x, y), field):
         """Addition of share-contents *x* and *y*."""
@@ -281,7 +296,8 @@
 
     def _minus_public_right(self, x, c, field):
         z = self._minus_public_right_without_share(x, c, field)
-        return BeDOZaShare(self, field, z.get_value(), z.get_keys(), z.get_macs())
+        return BeDOZaShare(self, field, z.get_value(), z.get_keys(),
+                           z.get_macs())
 
     def _minus_public_right_without_share(self, x, c, field):
         return x.sub_public(c, self.id)
--- a/viff/bedoza/bedoza_triple.py	Fri Aug 06 11:34:50 2010 +0200
+++ b/viff/bedoza/bedoza_triple.py	Fri Aug 06 15:00:46 2010 +0200
@@ -27,7 +27,8 @@
 from viff.field import FieldElement, GF
 from viff.constants import TEXT
 from viff.util import rand
-from viff.bedoza.shares import BeDOZaShare, BeDOZaShareContents, PartialShare, PartialShareContents
+from viff.bedoza.shares import BeDOZaShare, BeDOZaShareContents, PartialShare
+from viff.bedoza.shares import PartialShareContents
 from viff.bedoza.share_generators import PartialShareGenerator, ShareGenerator
 from viff.bedoza.keylist import BeDOZaKeyList
 from viff.bedoza.maclist import BeDOZaMACList
@@ -68,7 +69,8 @@
         # Debug output.
         #print "n_%d**2:%d" % (runtime.id, self.paillier.pubkey['n_square'])
         #print "n_%d:%d" % (runtime.id, self.paillier.pubkey['n'])
-        #print "n_%d bitlength: %d" % (runtime.id, self._bit_length_of(self.paillier.pubkey['n']))
+        #print "n_%d bitlength: %d" % \
+        #    (runtime.id, self._bit_length_of(self.paillier.pubkey['n']))
 
         #self.Zp = GF(p)
         #self.Zn2 = GF(self.paillier.pubkey['n_square'])
@@ -98,7 +100,8 @@
         
         def check(v, a, b, c):
             if v.value != 0:
-                raise Exception("TripleTest failed - The two triples were inconsistent.")
+                raise Exception("TripleTest failed - The two triples were "
+                                "inconsistent.")
             return Triple(a, b, c)
         
         def compute_value(r, a, b, c, x, y, z):
@@ -110,7 +113,8 @@
             v.addCallback(check, a, b, c)
             return v
 
-        gen = ShareGenerator(self.Zp, self.runtime, self.random, self.paillier, self.u_bound, self.alpha)
+        gen = ShareGenerator(self.Zp, self.runtime, self.random, self.paillier,
+                             self.u_bound, self.alpha)
         
         random_shares = gen.generate_random_shares(n)
 
@@ -142,15 +146,21 @@
 
         self.runtime.increment_pc()
         
-        gen = PartialShareGenerator(self.Zp, self.runtime, self.random, self.paillier)
+        gen = PartialShareGenerator(self.Zp, self.runtime, self.random,
+                                    self.paillier)
         partial_shares = []
         for _ in xrange(2 * n):
-             partial_shares.append(gen.generate_share(self.random.randint(0, self.Zp.modulus - 1)))
+             partial_shares.append(
+                 gen.generate_share(
+                     self.random.randint(0, self.Zp.modulus - 1)))
 
 
-        partial_shares_c = self._full_mul(partial_shares[0:n], partial_shares[n:2*n])
+        partial_shares_c = self._full_mul(partial_shares[0:n],
+                                          partial_shares[n:2*n])
 
-        full_shares = add_macs(self.runtime, self.Zp, self.u_bound, self.alpha, self.random, self.paillier, partial_shares + partial_shares_c)
+        full_shares = add_macs(self.runtime, self.Zp, self.u_bound, self.alpha,
+                               self.random, self.paillier,
+                               partial_shares + partial_shares_c)
 
         return full_shares  
 
@@ -211,7 +221,8 @@
 
             for dis in all_dis:
                 for player_id in self.runtime.players:
-                    self.runtime.protocols[player_id].sendData(pc, DiKIND, str(dis))
+                    self.runtime.protocols[player_id].sendData(pc, DiKIND,
+                                                               str(dis))
 
         if self.runtime.id == jnx:
             all_cs = []
@@ -234,7 +245,8 @@
                     djs.append(self.paillier.encrypt(zj.value, jnx))
                 for djs in all_djs:
                     for player_id in self.runtime.players:
-                        self.runtime.protocols[player_id].sendData(pc, DjKIND, str(djs))
+                        self.runtime.protocols[player_id].sendData(pc, DjKIND,
+                                                                   str(djs))
                 if not zis == []:
                     return [x + y for x, y in zip(zis, zjs)]
                 else:
@@ -266,15 +278,18 @@
             djs = reduce(lambda x, y: x + eval(y), djs, [])
             n_square_i = self.paillier.get_modulus_square(inx)
             n_square_j = self.paillier.get_modulus_square(jnx)
-            N_squared_list = [self.paillier.get_modulus_square(player_id) for player_id in self.runtime.players]
+            N_squared_list = [self.paillier.get_modulus_square(player_id)
+                              for player_id in self.runtime.players]
             ps = []
             
-            for v, di, dj in itertools.izip_longest(values, dis, djs, fillvalue=self.Zp(0)):
+            for v, di, dj in itertools.izip_longest(values, dis, djs,
+                                                    fillvalue=self.Zp(0)):
                 value = v
                 enc_shares = len(self.runtime.players) * [1]
                 enc_shares[inx - 1] = (enc_shares[inx - 1] * di) % n_square_i
                 enc_shares[jnx - 1] = (enc_shares[jnx - 1] * dj) % n_square_j
-                ps.append(PartialShareContents(value, enc_shares, N_squared_list))
+                ps.append(PartialShareContents(value, enc_shares,
+                                               N_squared_list))
             return ps
         r.addCallback(wrap, inx, jnx)
         return r
@@ -296,7 +311,8 @@
             a_values = [s.value for s in shares[0:len_shares/2]]
             b_enc_shares = []
             for inx in self.runtime.players:              
-                b_enc_shares.append([s.enc_shares[inx - 1] for s in shares[len_shares/2:]])
+                b_enc_shares.append([s.enc_shares[inx - 1]
+                                     for s in shares[len_shares/2:]])
             for inx in xrange(0, len(self.runtime.players)):
                 for jnx in xrange(0, len(self.runtime.players)):
                     deferreds.append(self._mul(inx + 1,
--- a/viff/bedoza/modified_paillier.py	Fri Aug 06 11:34:50 2010 +0200
+++ b/viff/bedoza/modified_paillier.py	Fri Aug 06 15:00:46 2010 +0200
@@ -54,15 +54,16 @@
         Defaults to own public key.
         """
         assert isinstance(value, int) or isinstance(value, long), \
-            "paillier: encrypts only integers and longs, got %s" % value.__class__
+            "paillier: encrypts only integers and longs, got %s" % \
+                value.__class__
         if not player_id:
             player_id = self.runtime.id
         n = self.runtime.players[player_id].pubkey['n']
         min = -(n - 1) / 2
         max = (n - 1) / 2
         assert min <= value <= max, \
-            "paillier: plaintext %d outside legal range [-(n-1)/2 ; (n-1)/2] = " \
-            "[%d ; %d]"  % (value, min, max)
+            "paillier: plaintext %d outside legal range [-(n-1)/2 " \
+            "; (n-1)/2] = [%d ; %d]"  % (value, min, max)
         pubkey = self.runtime.players[player_id].pubkey
         randomness = self.random.randint(1, long(n))
         return pypaillier.encrypt_r(self._f(value, n), randomness, pubkey)
--- a/viff/bedoza/share_generators.py	Fri Aug 06 11:34:50 2010 +0200
+++ b/viff/bedoza/share_generators.py	Fri Aug 06 15:00:46 2010 +0200
@@ -15,7 +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/>.
 
-from viff.bedoza.shares import BeDOZaShare, BeDOZaShareContents, PartialShare, PartialShareContents
+from viff.bedoza.shares import BeDOZaShare, BeDOZaShareContents, PartialShare
+from viff.bedoza.shares import PartialShareContents
 from viff.bedoza.util import _convolute
 from viff.bedoza.add_macs import add_macs
 
@@ -30,7 +31,8 @@
     def generate_share(self, value):
         self.runtime.increment_pc()
         
-        r = [self.Zp(self.random.randint(0, self.Zp.modulus - 1)) # TODO: Exclusve?
+        # TODO: Exclusve?
+        r = [self.Zp(self.random.randint(0, self.Zp.modulus - 1))
              for _ in range(self.runtime.num_players - 1)]
         if self.runtime.id == 1:
             share = value - sum(r)
@@ -45,7 +47,8 @@
 
     def generate_random_shares(self, n):
         self.runtime.increment_pc()
-        N_squared_list = [ self.runtime.players[player_id].pubkey['n_square'] for player_id in self.runtime.players]
+        N_squared_list = [self.runtime.players[player_id].pubkey['n_square']
+                          for player_id in self.runtime.players]
         shares = [PartialShare(self.runtime, self.Zp) for _ in xrange(n)]
         for inx in xrange(n):
             r = self.random.randint(0, self.Zp.modulus - 1)
@@ -53,7 +56,8 @@
             enc_share = self.paillier.encrypt(ri.value)
             enc_shares = _convolute(self.runtime, enc_share)
             def create_partial_share(enc_shares, ri, s, N_squared_list):
-                s.callback(PartialShareContents(ri, enc_shares, N_squared_list))
+                s.callback(PartialShareContents(ri, enc_shares,
+                                                N_squared_list))
             self.runtime.schedule_callback(enc_shares,
                                            create_partial_share,
                                            ri,
--- a/viff/bedoza/shares.py	Fri Aug 06 11:34:50 2010 +0200
+++ b/viff/bedoza/shares.py	Fri Aug 06 15:00:46 2010 +0200
@@ -35,12 +35,14 @@
         self.N_squared_list = N_squared_list
 
     def __str__(self):
-        return "PartialShareContents(%d; %s; %s)" % (self.value, self.enc_shares, self.N_squared_list)
+        return "PartialShareContents(%d; %s; %s)" % \
+            (self.value, self.enc_shares, self.N_squared_list)
 
     def __add__(self, other):
         z = self.value + other.value
         z_enc_shares = []
-        for x, y, N_squared in zip(self.enc_shares, other.enc_shares, self.N_squared_list):
+        for x, y, N_squared in zip(self.enc_shares, other.enc_shares,
+                                   self.N_squared_list):
             z_enc_shares.append((x * y) % N_squared)
         return PartialShareContents(z, z_enc_shares, self.N_squared_list)
 
@@ -53,8 +55,10 @@
         if value == None and enc_shares == None:
             Share.__init__(self, runtime, field)
         else:
-            N_squared_list = [ runtime.players[player_id].pubkey['n_square'] for player_id in runtime.players.keys()]
-            partial_share_contents = PartialShareContents(value, enc_shares, N_squared_list)
+            N_squared_list = [ runtime.players[player_id].pubkey['n_square']
+                              for player_id in runtime.players.keys()]
+            partial_share_contents = PartialShareContents(value, enc_shares,
+                                                          N_squared_list)
             Share.__init__(self, runtime, field, partial_share_contents)
 
 
@@ -92,13 +96,15 @@
     def add_public(self, c, my_id):
         if my_id == 1:
             self.value = self.value + c
-        self.keyList.set_key(0, self.keyList.get_key(0) - self.keyList.alpha * c)
+        self.keyList.set_key(0, self.keyList.get_key(0) -
+                             self.keyList.alpha * c)
         return self
     
     def sub_public(self, c, my_id):
         if my_id == 1:
             self.value = self.value - c
-        self.keyList.set_key(0, self.keyList.get_key(0) + self.keyList.alpha * c)
+        self.keyList.set_key(0, self.keyList.get_key(0) +
+                             self.keyList.alpha * c)
         return self
 
     def cmul(self, c):
@@ -108,7 +114,8 @@
         return BeDOZaShareContents(zi, zks, zms)
 
     def __str__(self):
-        return "(%s, %s, %s)" % (str(self.value), str(self.keyList), str(self.macs))
+        return "(%s, %s, %s)" % (str(self.value), str(self.keyList),
+                                 str(self.macs))
     
 class BeDOZaShare(Share):
     """A share in the BeDOZa runtime.
@@ -129,8 +136,11 @@
     that runtime.
     """
 
-    def __init__(self, runtime, field, value=None, keyList=None, authentication_codes=None):
+    def __init__(self, runtime, field, value=None, keyList=None,
+                 authentication_codes=None):
         if value == None and keyList == None and authentication_codes == None:
             Share.__init__(self, runtime, field)
         else:
-            Share.__init__(self, runtime, field, BeDOZaShareContents(value, keyList, authentication_codes))
+            Share.__init__(self, runtime, field,
+                           BeDOZaShareContents(value, keyList,
+                                               authentication_codes))