changeset 1448:3c21042745e7

Added a method for getting a random share.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Mon, 05 Jul 2010 11:33:58 +0200
parents cc963bc7ae5e
children 04a121c3b6a5
files viff/bedoza.py viff/test/test_bedoza_runtime.py
diffstat 2 files changed, 133 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/viff/bedoza.py	Mon Jul 05 11:33:58 2010 +0200
+++ b/viff/bedoza.py	Mon Jul 05 11:33:58 2010 +0200
@@ -47,8 +47,44 @@
         self.authentication_codes = authentication_codes
         Share.__init__(self, runtime, field, (value, authentication_codes))
 
+class RandomShareGenerator:
 
-class BeDOZaRuntime(Runtime, HashBroadcastMixin):
+    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]
+        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))
+            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))
+            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))
+        return shares
+
+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)])}
+
+class BeDOZaRuntime(Runtime, HashBroadcastMixin, KeyLoader, RandomShareGenerator):
     """The BeDOZa runtime.
 
     The runtime is used for sharing values (:meth:`secret_share` or
@@ -72,6 +108,27 @@
         """Initialize runtime."""
         Runtime.__init__(self, player, threshold, options)
         self.threshold = self.num_players - 1
+        self.random_share_number = 100
+        self.random_shares = []
+ 
+    def MAC(self, alpha, beta, v):
+         return alpha*v + beta
 
-    def MAC(self, alpha, beta, v):
-        return alpha*v + beta
+    def random_share(self, field):
+        """Retrieve a previously generated random share in the field, field.
+
+        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)
+
+        return self.random_shares.pop()
+
+    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]
--- a/viff/test/test_bedoza_runtime.py	Mon Jul 05 11:33:58 2010 +0200
+++ b/viff/test/test_bedoza_runtime.py	Mon Jul 05 11:33:58 2010 +0200
@@ -26,6 +26,79 @@
 from viff.field import FieldElement, GF
 from viff.util import rand
 
+class KeyLoaderTest(RuntimeTestCase):
+    """Test of KeyLoader."""
+
+    # Number of players.
+    num_players = 3
+
+    runtime_class = BeDOZaRuntime
+
+    @protocol
+    def test_load_keys(self, runtime):
+        """Test loading of keys."""
+
+        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
+
+        keys = runtime.load_keys(self.Zp)
+        keys1 = keys[1]
+        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)
+            self.assertEquals(betas[2], 9)
+
+    @protocol
+    def test_authentication_codes(self, runtime):
+        """Test generating random shares."""
+
+        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
+
+        runtime.keys = runtime.load_keys(self.Zp)
+
+        v = self.Zp(2)
+        alpha = runtime.get_keys()[0]
+        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)
+        
+        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))
+        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))
+        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))
+
+
 class BeDOZaBasicCommandsTest(RuntimeTestCase):
     """Test for basic commands."""