changeset 1451:25c57c4f6947

BeDOZa: Added infrastructure for better handling of keys.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Mon, 05 Jul 2010 15:46:31 +0200
parents d910c8a78a2e
children b21e08471eb4
files viff/bedoza.py viff/test/test_bedoza_runtime.py
diffstat 2 files changed, 59 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/viff/bedoza.py	Mon Jul 05 11:55:20 2010 +0200
+++ b/viff/bedoza.py	Mon Jul 05 15:46:31 2010 +0200
@@ -45,47 +45,69 @@
     that runtime.
     """
 
-    def __init__(self, runtime, field, value=None, authentication_codes=None):
+    def __init__(self, runtime, field, value=None, keyList=None, authentication_codes=None):
         self.share = value
+        self.keyList = keyList
         self.authentication_codes = authentication_codes
-        Share.__init__(self, runtime, field, (value, authentication_codes))
+        Share.__init__(self, runtime, field, (value, keyList, authentication_codes))
 
+class BeDOZaKeyList(object):
+
+    def __init__(self, alpha, keys):
+        self.alpha = alpha
+        self.keys = keys
+
+    def __add__(self, other):
+        """Addition."""
+        keys = []
+        for k1, k2 in zip(self.keys, other.keys):
+            keys.append(k1 + k2)
+        return BeDOZaKeyList(keys)
+
+    def __str__(self):
+        return "(%s, %s)" % (self.alpha, str(self.keys))
+    
 class RandomShareGenerator:
 
-    def authentication_codes(self, alpha, betas, v):
-        auth_codes = []
-        for beta in betas:
-            auth_codes.append(alpha*v + beta)
-        return auth_codes
-
     def generate_random_shares(self, field, number_of_shares):
-        alpha = self.get_keys()[0]
+        def keys_N_codes(value):
+            auth_codes = self.generate_auth_codes(self.id, v)
+            my_keys = self.generate_keys()
+            return BeDOZaShare(self, field, v, my_keys, auth_codes)
         shares = []
         for i in xrange(0, number_of_shares):
             if self.id == 1:
-                betas = map(lambda (alpha, betas): betas[0], self.keys.values())
                 v = field(1)
-                auth_codes= self.authentication_codes(alpha, betas, v)
-                shares.append(BeDOZaShare(self, field, v, auth_codes))
+                shares.append(keys_N_codes(v))
             if self.id == 2:
-                betas = map(lambda (alpha, betas): betas[1], self.keys.values())
                 v = field(2)
-                auth_codes= self.authentication_codes(alpha, betas, v)
-                shares.append(BeDOZaShare(self, field, v, auth_codes))
+                shares.append(keys_N_codes(v))
             if self.id == 3:
-                betas = map(lambda (alpha, betas): betas[2], self.keys.values())
                 v = field(3)
-                auth_codes= self.authentication_codes(alpha, betas, v)
-                shares.append(BeDOZaShare(self, field, v, auth_codes))
+                shares.append(keys_N_codes(v))
         return shares
 
+    def generate_auth_codes(self, playerId, value):
+        keys = map(lambda (alpha, akeys): (alpha, akeys[playerId - 1]), self.keys.values())
+        auth_codes = self.authentication_codes(keys, value)
+        return auth_codes
+
+    def authentication_codes(self, keys, v):
+        auth_codes = []
+        for alpha, beta in keys:
+            auth_codes.append(alpha * v + beta)
+        return auth_codes
+
+    def generate_keys(self):
+        alpha, betas = self.get_keys()
+        return BeDOZaKeyList(alpha, betas)
+
 class KeyLoader:
 
     def load_keys(self, field):
-        alpha = field(2)
-        return {1: (alpha, [field(1), field(2), field(3)]),
-                2: (alpha, [field(4), field(5), field(6)]),
-                3: (alpha, [field(7), field(8), field(9)])}
+        return {1: (field(2), [field(1), field(2), field(3)]),
+                2: (field(3), [field(4), field(5), field(6)]),
+                3: (field(4), [field(7), field(8), field(9)])}
 
 class BeDOZaRuntime(Runtime, HashBroadcastMixin, KeyLoader, RandomShareGenerator):
     """The BeDOZa runtime.
@@ -115,7 +137,7 @@
         self.random_shares = []
  
     def MAC(self, alpha, beta, v):
-         return alpha*v + beta
+        return alpha * v + beta
 
     def random_share(self, field):
         """Retrieve a previously generated random share in the field, field.
@@ -150,11 +172,11 @@
 
         self.increment_pc()
 
-        def recombine_value(shares_codes):
+        def recombine_value(shares_codes, keyList):
             isOK = True
             n = len(self.players)
-            alpha = self.get_keys()[0]
-            keys = self.get_keys()[1]
+            alpha = keyList.alpha
+            keys = keyList.keys
             x = 0
             for inx in xrange(0, n):
                 xi = shares_codes[inx]
@@ -166,13 +188,12 @@
                 raise BeDOZaException("Wrong commitment for value %s." % x)
             return x
 
-        def exchange((xi, codes), receivers):
+        def exchange((xi, keyList, codes), receivers):
             # Send share to all receivers.
             pc = tuple(self.program_counter)
             for other_id in receivers:
                 self.protocols[other_id].sendShare(pc, xi)
                 self.protocols[other_id].sendShare(pc, codes[other_id - 1])
-
             if self.id in receivers:
                 num_players = len(self.players.keys())
                 values = num_players * [None]
@@ -181,7 +202,7 @@
                     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)
+                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(keyList,))
                 return result
 
         result = share.clone()
--- a/viff/test/test_bedoza_runtime.py	Mon Jul 05 11:55:20 2010 +0200
+++ b/viff/test/test_bedoza_runtime.py	Mon Jul 05 15:46:31 2010 +0200
@@ -22,7 +22,7 @@
 from viff.test.util import RuntimeTestCase, protocol
 from viff.runtime import gather_shares, Share
 from viff.config import generate_configs
-from viff.bedoza import BeDOZaRuntime, BeDOZaShare
+from viff.bedoza import BeDOZaRuntime, BeDOZaShare, BeDOZaKeyList
 from viff.field import FieldElement, GF
 from viff.util import rand
 
@@ -45,22 +45,16 @@
         keys2 = keys[2]
         keys3 = keys[3]
         if runtime.id == 1:
-            alpha = keys1[0]
-            self.assertEquals(alpha, 2)
             betas = keys1[1]
             self.assertEquals(betas[0], 1)
             self.assertEquals(betas[1], 2)
             self.assertEquals(betas[2], 3)
         if runtime.id == 2:
-            alpha = keys2[0]
-            self.assertEquals(alpha, 2)
             betas = keys2[1]
             self.assertEquals(betas[0], 4)
             self.assertEquals(betas[1], 5)
             self.assertEquals(betas[2], 6)
         if runtime.id == 3:
-            alpha = keys3[0]
-            self.assertEquals(alpha, 2)
             betas = keys3[1]
             self.assertEquals(betas[0], 7)
             self.assertEquals(betas[1], 8)
@@ -79,24 +73,24 @@
         codes = self.num_players * [None]
 
         for xid in runtime.players.keys():
-            betas = map(lambda (alpha, betas): betas[xid-1], runtime.keys.values())
-            codes[xid-1] = runtime.authentication_codes(alpha, betas, v)
+            keys = map(lambda (alpha, akeys): (alpha, akeys[xid - 1]), runtime.keys.values())
+            codes[xid-1] = runtime.authentication_codes(keys, v)
         
         if runtime.id == 1:
             my_codes = codes[0]
             self.assertEquals(my_codes[0], self.Zp(5))
-            self.assertEquals(my_codes[1], self.Zp(8))
-            self.assertEquals(my_codes[2], self.Zp(11))
+            self.assertEquals(my_codes[1], self.Zp(10))
+            self.assertEquals(my_codes[2], self.Zp(15))
         if runtime.id == 2:
             my_codes = codes[1]
             self.assertEquals(my_codes[0], self.Zp(6))
-            self.assertEquals(my_codes[1], self.Zp(9))
-            self.assertEquals(my_codes[2], self.Zp(12))
+            self.assertEquals(my_codes[1], self.Zp(11))
+            self.assertEquals(my_codes[2], self.Zp(16))
         if runtime.id == 3:
             my_codes = codes[2]
             self.assertEquals(my_codes[0], self.Zp(7))
-            self.assertEquals(my_codes[1], self.Zp(10))
-            self.assertEquals(my_codes[2], self.Zp(13))
+            self.assertEquals(my_codes[1], self.Zp(12))
+            self.assertEquals(my_codes[2], self.Zp(17))
 
 
 class BeDOZaBasicCommandsTest(RuntimeTestCase):