changeset 1494:cff86d2dc0ee

BeDOZa: BeDOZaShare now contains a content.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Thu, 15 Jul 2010 11:35:18 +0200
parents e9465b35b0fe
children 8a2f88e9f800
files viff/bedoza.py viff/test/test_bedoza_runtime.py
diffstat 2 files changed, 85 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/viff/bedoza.py	Wed Jul 14 16:44:20 2010 +0200
+++ b/viff/bedoza.py	Thu Jul 15 11:35:18 2010 +0200
@@ -30,6 +30,52 @@
 class BeDOZaException(Exception):
     pass
 
+class BeDOZaShareContents(object):
+
+    def __init__(self, value, keyList, macs):
+        self.value = value
+        self.keyList = keyList
+        self.macs = macs
+
+    def get_value(self):
+        return self.value
+
+    def get_keys(self):
+        return self.keyList
+
+    def get_macs(self):
+        return self.macs
+
+    def __add__(self, other):
+        zi = self.value + other.value
+        zks = self.keyList + other.keyList
+        zms = self.macs + other.macs
+        return BeDOZaShareContents(zi, zks, zms)
+
+    def __sub__(self, other):
+        zi = self.value - other.value
+        zks = self.keyList - other.keyList
+        zms = self.macs - other.macs
+        return BeDOZaShareContents(zi, zks, zms)
+
+    def add_public(self, c, my_id):
+        if my_id == 1:
+            self.value = self.value + c
+        self.keyList.keys[0] = self.keyList.keys[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.keys[0] = self.keyList.keys[0] + self.keyList.alpha * c
+        return self
+
+    def cmul(self, c):
+        zi = c * self.value
+        zks = BeDOZaKeyList(self.keyList.alpha, map(lambda k: c * k, self.keyList.keys))
+        zms = BeDOZaMessageList(map(lambda m: c * m, self.macs.auth_codes))
+        return BeDOZaShareContents(zi, zks, zms)
+    
 class BeDOZaShare(Share):
     """A share in the BeDOZa runtime.
 
@@ -50,10 +96,8 @@
     """
 
     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, keyList, authentication_codes))
+        Share.__init__(self, runtime, field, BeDOZaShareContents(value, keyList, authentication_codes))
+        
 
 class BeDOZaKeyList(object):
 
@@ -228,10 +272,10 @@
                 # self.protocols[other_id].sendShare(pc, xi)
                 # self.protocols[other_id].sendShare(pc, codes.auth_codes[other_id - 1])
                 message_string = ""
-                for inx, (xi, keyList, codes) in enumerate(ls):
-                    keyLists.append(keyList)
+                for inx, beDOZaContents in enumerate(ls):
+                    keyLists.append(beDOZaContents.get_keys())
                     message_string += "%s:%s;" % \
-                           (xi.value, codes.auth_codes[other_id - 1].value)
+                           (beDOZaContents.get_value().value, beDOZaContents.get_macs().auth_codes[other_id - 1].value)
                 self.protocols[other_id].sendData(pc, TEXT, message_string)
 
             if self.id in receivers:
@@ -313,14 +357,12 @@
         
         def exchange((a, b), receivers):
             # Send share to all receivers.
-            (ai, keyList_a, codes_a) = a
-            (bi, keyList_b, codes_b) = b
             pc = tuple(self.program_counter)
             for other_id in receivers:
-                self.protocols[other_id].sendShare(pc, ai)
-                self.protocols[other_id].sendShare(pc, codes_a.auth_codes[other_id - 1])
-                self.protocols[other_id].sendShare(pc, bi)
-                self.protocols[other_id].sendShare(pc, codes_b.auth_codes[other_id - 1])
+                self.protocols[other_id].sendShare(pc, a.get_value())
+                self.protocols[other_id].sendShare(pc, a.get_macs().auth_codes[other_id - 1])
+                self.protocols[other_id].sendShare(pc, b.get_value())
+                self.protocols[other_id].sendShare(pc, b.get_macs().auth_codes[other_id - 1])
                 
             if self.id in receivers:
                 num_players = len(self.players.keys())
@@ -334,7 +376,7 @@
                     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)
-                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(keyList_a, keyList_b))
+                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(a.get_keys(), b.get_keys()))
                 return result
 
         result = gather_shares([share_a, share_b])
@@ -385,12 +427,12 @@
             ds.addCallbacks(check, self.error_handler, callbackArgs=(x,isOK))
             return ds
 
-        def exchange((xi, keyList, codes), receivers):
+        def exchange(shareContent, 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.auth_codes[other_id - 1])
+                self.protocols[other_id].sendShare(pc, shareContent.get_value())
+                self.protocols[other_id].sendShare(pc, shareContent.get_macs().auth_codes[other_id - 1])
             if self.id in receivers:
                 num_players = len(self.players.keys())
                 values = num_players * [None]
@@ -399,7 +441,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, callbackArgs=(keyList,))
+                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(shareContent.get_keys(),))
                 return result
 
         result = share.clone()
@@ -413,70 +455,35 @@
             return result
 
     def _plus_public(self, x, c, field):
-        (xi, xks, xms) = x
-        if self.id == 1:
-            xi = xi + c
-        xks.keys[0] = xks.keys[0] - xks.alpha * c
-        return BeDOZaShare(self, field, xi, xks, xms)
+        x = x.add_public(c, self.id)
+        return BeDOZaShare(self, field, x.get_value(), x.get_keys(), x.get_macs())
 
     def _plus(self, (x, y), field):
-        """Addition of share-tuples *x* and *y*.
-
-        Each party ``P_i`` computes:
-        ``[x]_i = (x_i, xk, xm)``
-        ``[y]_i = (y_i, yk, ym)``
-        ``[z]_i = [x]_i + [y]_i
-                = (x_i + y_i mod p, {xk^{i}_{j} + yk^{i}_{j} mod p}_{j=0}^{j=n}, {xm^{i}_{j} + ym^{i}_{j} mod p}_{j=0}^{j=n})
-        """
-        (xi, xks, xms) = x
-        (yi, yks, yms) = y
-        zi = xi + yi
-        zks = xks + yks
-        zms = xms + yms
-        return (zi, zks, zms)
+        """Addition of share-contents *x* and *y*."""
+        return x + y
 
     def _minus_public_right(self, x, c, field):
-        (zi, zks, zms) = self._minus_public_right_without_share(x, c, field)
-        return BeDOZaShare(self, field, zi, zks, zms)
+        z = self._minus_public_right_without_share(x, c, field)
+        return BeDOZaShare(self, field, z.get_value(), z.get_keys(), z.get_macs())
 
     def _minus_public_right_without_share(self, x, c, field):
-        (xi, xks, xms) = x
-        if self.id == 1:
-            xi = xi - c
-        xks.keys[0] = xks.keys[0] + xks.alpha * c
-        return xi, xks, xms
+        return x.sub_public(c, self.id)
 
-    def _wrap_in_share(self, (zi, zks, zms), field):
-        return BeDOZaShare(self, field, zi, zks, zms)
+    def _wrap_in_share(self, shareContents, field):
+        return BeDOZaShare(self, field, shareContents.get_value(), shareContents.get_keys(), shareContents.get_macs())
 
     def _minus_public_left(self, x, c, field):
         y = self._constant_multiply(x, field(-1))
         return self._plus_public(y, c, field)
     
     def _minus(self, (x, y), field):
-        """Subtraction of share-tuples *x* and *y*.
-
-        Each party ``P_i`` computes:
-        ``[x]_i = (x_i, xk, xm)``
-        ``[y]_i = (y_i, yk, ym)``
-        ``[z]_i = [x]_i - [y]_i
-                = (x_i - y_i mod p, {xk^{i}_{j} - yk^{i}_{j} mod p}_{j=0}^{j=n}, {xm^{i}_{j} - ym^{i}_{j} mod p}_{j=0}^{j=n})
-        """
-        (xi, xks, xms) = x
-        (yi, yks, yms) = y
-        zi = xi - yi
-        zks = xks - yks
-        zms = xms - yms
-        return (zi, zks, zms)
+        """Subtraction of share-tuples *x* and *y*."""
+        return x - y
 
     def _constant_multiply(self, x, c):
         """Multiplication of a share-tuple with a constant c."""
         assert(isinstance(c, FieldElement))
-        xi, xks, xms = x
-        zi = c * xi
-        zks = BeDOZaKeyList(xks.alpha, map(lambda k: c * k, xks.keys))
-        zms = BeDOZaMessageList(map(lambda m: c * m, xms.auth_codes))
-        return (zi, zks, zms)
+        return x.cmul(c)
 
     def _get_triple(self, field):
         """ TODO: This is a dummy implementation, and should be replaced with proper code."""
--- a/viff/test/test_bedoza_runtime.py	Wed Jul 14 16:44:20 2010 +0200
+++ b/viff/test/test_bedoza_runtime.py	Thu Jul 15 11:35:18 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, BeDOZaKeyList, BeDOZaMessageList
+from viff.bedoza import BeDOZaRuntime, BeDOZaShare, BeDOZaShareContents, BeDOZaKeyList, BeDOZaMessageList
 from viff.field import FieldElement, GF
 from viff.util import rand
 
@@ -80,13 +80,12 @@
 
         Zp = GF(6277101735386680763835789423176059013767194773182842284081)
        
-        x = (Zp(2), BeDOZaKeyList(Zp(23), [Zp(3), Zp(4), Zp(1)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
-        y = (Zp(2), BeDOZaKeyList(Zp(23), [Zp(5), Zp(2), Zp(7)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
-        zi, zks, zms = runtime._plus((x, y), Zp)
-        self.assertEquals(zi, Zp(4))
-        self.assertEquals(zks, BeDOZaKeyList(Zp(23), [Zp(8), Zp(6), Zp(8)]))
-        self.assertEquals(zms, BeDOZaMessageList([Zp(4), Zp(148), Zp(46), Zp(4)]))
-        return zi
+        x = BeDOZaShareContents(Zp(2), BeDOZaKeyList(Zp(23), [Zp(3), Zp(4), Zp(1)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
+        y = BeDOZaShareContents(Zp(2), BeDOZaKeyList(Zp(23), [Zp(5), Zp(2), Zp(7)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
+        z = runtime._plus((x, y), Zp)
+        self.assertEquals(z.get_value(), Zp(4))
+        self.assertEquals(z.get_keys(), BeDOZaKeyList(Zp(23), [Zp(8), Zp(6), Zp(8)]))
+        self.assertEquals(z.get_macs(), BeDOZaMessageList([Zp(4), Zp(148), Zp(46), Zp(4)]))
 
     @protocol
     def test_sum(self, runtime):
@@ -162,13 +161,12 @@
 
         Zp = GF(6277101735386680763835789423176059013767194773182842284081)
        
-        x = (Zp(2), BeDOZaKeyList(Zp(23), [Zp(5), Zp(4), Zp(7)]), BeDOZaMessageList([Zp(2), Zp(75), Zp(23), Zp(2)]))
-        y = (Zp(2), BeDOZaKeyList(Zp(23), [Zp(3), Zp(2), Zp(1)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
-        zi, zks, zms = runtime._minus((x, y), Zp)
-        self.assertEquals(zi, Zp(0))
-        self.assertEquals(zks, BeDOZaKeyList(Zp(23), [Zp(2), Zp(2), Zp(6)]))
-        self.assertEquals(zms, BeDOZaMessageList([Zp(0), Zp(1), Zp(0), Zp(0)]))
-        return zi
+        x = BeDOZaShareContents(Zp(2), BeDOZaKeyList(Zp(23), [Zp(5), Zp(4), Zp(7)]), BeDOZaMessageList([Zp(2), Zp(75), Zp(23), Zp(2)]))
+        y = BeDOZaShareContents(Zp(2), BeDOZaKeyList(Zp(23), [Zp(3), Zp(2), Zp(1)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
+        z = runtime._minus((x, y), Zp)
+        self.assertEquals(z.get_value(), Zp(0))
+        self.assertEquals(z.get_keys(), BeDOZaKeyList(Zp(23), [Zp(2), Zp(2), Zp(6)]))
+        self.assertEquals(z.get_macs(), BeDOZaMessageList([Zp(0), Zp(1), Zp(0), Zp(0)]))
 
     @protocol
     def test_sub(self, runtime):