changeset 1486:6b448315eedf

BeDOZa: Merged KeyLoader and RandomShareGenerator.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Mon, 12 Jul 2010 11:31:50 +0200
parents 0e1f9275e7ac
children c6f29ad5c91e
files viff/bedoza.py viff/test/test_bedoza_runtime.py
diffstat 2 files changed, 19 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/viff/bedoza.py	Mon Jul 12 11:02:14 2010 +0200
+++ b/viff/bedoza.py	Mon Jul 12 11:31:50 2010 +0200
@@ -116,6 +116,7 @@
 class RandomShareGenerator:
 
     def generate_random_shares(self, field, number_of_shares):
+        self.init_keys(field)
         shares = []
         for i in xrange(0, number_of_shares):
             if self.id == 1:
@@ -130,8 +131,8 @@
         return shares
 
     def generate_share(self, field, value):
+        my_keys = self.generate_keys(field)
         auth_codes = self.generate_auth_codes(self.id, value)
-        my_keys = self.generate_keys()
         return BeDOZaShare(self, field, value, my_keys, auth_codes)
 
     def generate_auth_codes(self, playerId, value):
@@ -145,18 +146,19 @@
             auth_codes.append(alpha * v + beta)
         return BeDOZaMessageList(auth_codes)
 
-    def generate_keys(self):
+    def generate_keys(self, field):
         alpha, betas = self.get_keys()
         return BeDOZaKeyList(alpha, betas)
 
-class KeyLoader:
+    def init_keys(self, field):
+        self.keys = {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)])}
 
-    def load_keys(self, field):
-        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)])}
+    def get_keys(self):   
+        return self.keys[self.id]
 
-class BeDOZaRuntime(SimpleArithmetic, Runtime, HashBroadcastMixin, KeyLoader, RandomShareGenerator):
+class BeDOZaRuntime(SimpleArithmetic, Runtime, HashBroadcastMixin, RandomShareGenerator):
     """The BeDOZa runtime.
 
     The runtime is used for sharing values (:meth:`secret_share` or
@@ -191,7 +193,6 @@
 
         If no more shares are left, generate self.random_share_number new ones.
         """
-        self.keys = self.load_keys(field)
         if len(self.random_shares) == 0:
             self.random_shares = self.generate_random_shares(field, self.random_share_number)
 
@@ -432,14 +433,6 @@
         if self.id in receivers:
             return result
 
-    def get_keys(self):
-        if self.id == 1:
-            return self.keys[1]
-        if self.id == 2:
-            return self.keys[2]
-        if self.id == 3:
-            return self.keys[3]
-
     def _plus_public(self, x, c, field):
         (xi, xks, xms) = x
         if self.id == 1:
@@ -507,6 +500,7 @@
         return (zi, zks, zms)
 
     def _get_triple(self, field):
+        self.init_keys(field)
         a, b, c = 0, 0, 0
         share_a = field(2)
         share_b = field(4)
--- a/viff/test/test_bedoza_runtime.py	Mon Jul 12 11:02:14 2010 +0200
+++ b/viff/test/test_bedoza_runtime.py	Mon Jul 12 11:31:50 2010 +0200
@@ -40,22 +40,20 @@
 
         self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
 
-        keys = runtime.load_keys(self.Zp)
-        keys1 = keys[1]
-        keys2 = keys[2]
-        keys3 = keys[3]
+        runtime.init_keys(self.Zp)
+        keys = runtime.get_keys()
         if runtime.id == 1:
-            betas = keys1[1]
+            betas = keys[1]
             self.assertEquals(betas[0], 1)
             self.assertEquals(betas[1], 2)
             self.assertEquals(betas[2], 3)
         if runtime.id == 2:
-            betas = keys2[1]
+            betas = keys[1]
             self.assertEquals(betas[0], 4)
             self.assertEquals(betas[1], 5)
             self.assertEquals(betas[2], 6)
         if runtime.id == 3:
-            betas = keys3[1]
+            betas = keys[1]
             self.assertEquals(betas[0], 7)
             self.assertEquals(betas[1], 8)
             self.assertEquals(betas[2], 9)
@@ -66,10 +64,9 @@
 
         self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
 
-        runtime.keys = runtime.load_keys(self.Zp)
-
         v = self.Zp(2)
-        alpha = runtime.get_keys()[0]
+        runtime.init_keys(self.Zp)
+        alpha, betas = runtime.get_keys()
         codes = self.num_players * [None]
 
         for xid in runtime.players.keys():
@@ -337,9 +334,7 @@
         """Test generation of a triple."""
 
         self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
-
-        runtime.keys = runtime.load_keys(self.Zp)
-        
+      
         def check((a, b, c)):
             self.assertEquals(c, a * b)