viff

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 diff
     1.1 --- a/viff/bedoza.py	Wed Jul 14 16:44:20 2010 +0200
     1.2 +++ b/viff/bedoza.py	Thu Jul 15 11:35:18 2010 +0200
     1.3 @@ -30,6 +30,52 @@
     1.4  class BeDOZaException(Exception):
     1.5      pass
     1.6  
     1.7 +class BeDOZaShareContents(object):
     1.8 +
     1.9 +    def __init__(self, value, keyList, macs):
    1.10 +        self.value = value
    1.11 +        self.keyList = keyList
    1.12 +        self.macs = macs
    1.13 +
    1.14 +    def get_value(self):
    1.15 +        return self.value
    1.16 +
    1.17 +    def get_keys(self):
    1.18 +        return self.keyList
    1.19 +
    1.20 +    def get_macs(self):
    1.21 +        return self.macs
    1.22 +
    1.23 +    def __add__(self, other):
    1.24 +        zi = self.value + other.value
    1.25 +        zks = self.keyList + other.keyList
    1.26 +        zms = self.macs + other.macs
    1.27 +        return BeDOZaShareContents(zi, zks, zms)
    1.28 +
    1.29 +    def __sub__(self, other):
    1.30 +        zi = self.value - other.value
    1.31 +        zks = self.keyList - other.keyList
    1.32 +        zms = self.macs - other.macs
    1.33 +        return BeDOZaShareContents(zi, zks, zms)
    1.34 +
    1.35 +    def add_public(self, c, my_id):
    1.36 +        if my_id == 1:
    1.37 +            self.value = self.value + c
    1.38 +        self.keyList.keys[0] = self.keyList.keys[0] - self.keyList.alpha * c
    1.39 +        return self
    1.40 +    
    1.41 +    def sub_public(self, c, my_id):
    1.42 +        if my_id == 1:
    1.43 +            self.value = self.value - c
    1.44 +        self.keyList.keys[0] = self.keyList.keys[0] + self.keyList.alpha * c
    1.45 +        return self
    1.46 +
    1.47 +    def cmul(self, c):
    1.48 +        zi = c * self.value
    1.49 +        zks = BeDOZaKeyList(self.keyList.alpha, map(lambda k: c * k, self.keyList.keys))
    1.50 +        zms = BeDOZaMessageList(map(lambda m: c * m, self.macs.auth_codes))
    1.51 +        return BeDOZaShareContents(zi, zks, zms)
    1.52 +    
    1.53  class BeDOZaShare(Share):
    1.54      """A share in the BeDOZa runtime.
    1.55  
    1.56 @@ -50,10 +96,8 @@
    1.57      """
    1.58  
    1.59      def __init__(self, runtime, field, value=None, keyList=None, authentication_codes=None):
    1.60 -        self.share = value
    1.61 -        self.keyList = keyList
    1.62 -        self.authentication_codes = authentication_codes
    1.63 -        Share.__init__(self, runtime, field, (value, keyList, authentication_codes))
    1.64 +        Share.__init__(self, runtime, field, BeDOZaShareContents(value, keyList, authentication_codes))
    1.65 +        
    1.66  
    1.67  class BeDOZaKeyList(object):
    1.68  
    1.69 @@ -228,10 +272,10 @@
    1.70                  # self.protocols[other_id].sendShare(pc, xi)
    1.71                  # self.protocols[other_id].sendShare(pc, codes.auth_codes[other_id - 1])
    1.72                  message_string = ""
    1.73 -                for inx, (xi, keyList, codes) in enumerate(ls):
    1.74 -                    keyLists.append(keyList)
    1.75 +                for inx, beDOZaContents in enumerate(ls):
    1.76 +                    keyLists.append(beDOZaContents.get_keys())
    1.77                      message_string += "%s:%s;" % \
    1.78 -                           (xi.value, codes.auth_codes[other_id - 1].value)
    1.79 +                           (beDOZaContents.get_value().value, beDOZaContents.get_macs().auth_codes[other_id - 1].value)
    1.80                  self.protocols[other_id].sendData(pc, TEXT, message_string)
    1.81  
    1.82              if self.id in receivers:
    1.83 @@ -313,14 +357,12 @@
    1.84          
    1.85          def exchange((a, b), receivers):
    1.86              # Send share to all receivers.
    1.87 -            (ai, keyList_a, codes_a) = a
    1.88 -            (bi, keyList_b, codes_b) = b
    1.89              pc = tuple(self.program_counter)
    1.90              for other_id in receivers:
    1.91 -                self.protocols[other_id].sendShare(pc, ai)
    1.92 -                self.protocols[other_id].sendShare(pc, codes_a.auth_codes[other_id - 1])
    1.93 -                self.protocols[other_id].sendShare(pc, bi)
    1.94 -                self.protocols[other_id].sendShare(pc, codes_b.auth_codes[other_id - 1])
    1.95 +                self.protocols[other_id].sendShare(pc, a.get_value())
    1.96 +                self.protocols[other_id].sendShare(pc, a.get_macs().auth_codes[other_id - 1])
    1.97 +                self.protocols[other_id].sendShare(pc, b.get_value())
    1.98 +                self.protocols[other_id].sendShare(pc, b.get_macs().auth_codes[other_id - 1])
    1.99                  
   1.100              if self.id in receivers:
   1.101                  num_players = len(self.players.keys())
   1.102 @@ -334,7 +376,7 @@
   1.103                      values_b[inx] =  self._expect_share(other_id, field)
   1.104                      codes_b[inx] = self._expect_share(other_id, field)
   1.105                  result = gatherResults(values_a + codes_a + values_b + codes_b)
   1.106 -                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(keyList_a, keyList_b))
   1.107 +                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(a.get_keys(), b.get_keys()))
   1.108                  return result
   1.109  
   1.110          result = gather_shares([share_a, share_b])
   1.111 @@ -385,12 +427,12 @@
   1.112              ds.addCallbacks(check, self.error_handler, callbackArgs=(x,isOK))
   1.113              return ds
   1.114  
   1.115 -        def exchange((xi, keyList, codes), receivers):
   1.116 +        def exchange(shareContent, receivers):
   1.117              # Send share to all receivers.
   1.118              pc = tuple(self.program_counter)
   1.119              for other_id in receivers:
   1.120 -                self.protocols[other_id].sendShare(pc, xi)
   1.121 -                self.protocols[other_id].sendShare(pc, codes.auth_codes[other_id - 1])
   1.122 +                self.protocols[other_id].sendShare(pc, shareContent.get_value())
   1.123 +                self.protocols[other_id].sendShare(pc, shareContent.get_macs().auth_codes[other_id - 1])
   1.124              if self.id in receivers:
   1.125                  num_players = len(self.players.keys())
   1.126                  values = num_players * [None]
   1.127 @@ -399,7 +441,7 @@
   1.128                      values[inx] =  self._expect_share(other_id, field)
   1.129                      codes[inx] = self._expect_share(other_id, field)
   1.130                  result = gatherResults(values + codes)
   1.131 -                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(keyList,))
   1.132 +                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(shareContent.get_keys(),))
   1.133                  return result
   1.134  
   1.135          result = share.clone()
   1.136 @@ -413,70 +455,35 @@
   1.137              return result
   1.138  
   1.139      def _plus_public(self, x, c, field):
   1.140 -        (xi, xks, xms) = x
   1.141 -        if self.id == 1:
   1.142 -            xi = xi + c
   1.143 -        xks.keys[0] = xks.keys[0] - xks.alpha * c
   1.144 -        return BeDOZaShare(self, field, xi, xks, xms)
   1.145 +        x = x.add_public(c, self.id)
   1.146 +        return BeDOZaShare(self, field, x.get_value(), x.get_keys(), x.get_macs())
   1.147  
   1.148      def _plus(self, (x, y), field):
   1.149 -        """Addition of share-tuples *x* and *y*.
   1.150 -
   1.151 -        Each party ``P_i`` computes:
   1.152 -        ``[x]_i = (x_i, xk, xm)``
   1.153 -        ``[y]_i = (y_i, yk, ym)``
   1.154 -        ``[z]_i = [x]_i + [y]_i
   1.155 -                = (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})
   1.156 -        """
   1.157 -        (xi, xks, xms) = x
   1.158 -        (yi, yks, yms) = y
   1.159 -        zi = xi + yi
   1.160 -        zks = xks + yks
   1.161 -        zms = xms + yms
   1.162 -        return (zi, zks, zms)
   1.163 +        """Addition of share-contents *x* and *y*."""
   1.164 +        return x + y
   1.165  
   1.166      def _minus_public_right(self, x, c, field):
   1.167 -        (zi, zks, zms) = self._minus_public_right_without_share(x, c, field)
   1.168 -        return BeDOZaShare(self, field, zi, zks, zms)
   1.169 +        z = self._minus_public_right_without_share(x, c, field)
   1.170 +        return BeDOZaShare(self, field, z.get_value(), z.get_keys(), z.get_macs())
   1.171  
   1.172      def _minus_public_right_without_share(self, x, c, field):
   1.173 -        (xi, xks, xms) = x
   1.174 -        if self.id == 1:
   1.175 -            xi = xi - c
   1.176 -        xks.keys[0] = xks.keys[0] + xks.alpha * c
   1.177 -        return xi, xks, xms
   1.178 +        return x.sub_public(c, self.id)
   1.179  
   1.180 -    def _wrap_in_share(self, (zi, zks, zms), field):
   1.181 -        return BeDOZaShare(self, field, zi, zks, zms)
   1.182 +    def _wrap_in_share(self, shareContents, field):
   1.183 +        return BeDOZaShare(self, field, shareContents.get_value(), shareContents.get_keys(), shareContents.get_macs())
   1.184  
   1.185      def _minus_public_left(self, x, c, field):
   1.186          y = self._constant_multiply(x, field(-1))
   1.187          return self._plus_public(y, c, field)
   1.188      
   1.189      def _minus(self, (x, y), field):
   1.190 -        """Subtraction of share-tuples *x* and *y*.
   1.191 -
   1.192 -        Each party ``P_i`` computes:
   1.193 -        ``[x]_i = (x_i, xk, xm)``
   1.194 -        ``[y]_i = (y_i, yk, ym)``
   1.195 -        ``[z]_i = [x]_i - [y]_i
   1.196 -                = (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})
   1.197 -        """
   1.198 -        (xi, xks, xms) = x
   1.199 -        (yi, yks, yms) = y
   1.200 -        zi = xi - yi
   1.201 -        zks = xks - yks
   1.202 -        zms = xms - yms
   1.203 -        return (zi, zks, zms)
   1.204 +        """Subtraction of share-tuples *x* and *y*."""
   1.205 +        return x - y
   1.206  
   1.207      def _constant_multiply(self, x, c):
   1.208          """Multiplication of a share-tuple with a constant c."""
   1.209          assert(isinstance(c, FieldElement))
   1.210 -        xi, xks, xms = x
   1.211 -        zi = c * xi
   1.212 -        zks = BeDOZaKeyList(xks.alpha, map(lambda k: c * k, xks.keys))
   1.213 -        zms = BeDOZaMessageList(map(lambda m: c * m, xms.auth_codes))
   1.214 -        return (zi, zks, zms)
   1.215 +        return x.cmul(c)
   1.216  
   1.217      def _get_triple(self, field):
   1.218          """ TODO: This is a dummy implementation, and should be replaced with proper code."""
     2.1 --- a/viff/test/test_bedoza_runtime.py	Wed Jul 14 16:44:20 2010 +0200
     2.2 +++ b/viff/test/test_bedoza_runtime.py	Thu Jul 15 11:35:18 2010 +0200
     2.3 @@ -22,7 +22,7 @@
     2.4  from viff.test.util import RuntimeTestCase, protocol
     2.5  from viff.runtime import gather_shares, Share
     2.6  from viff.config import generate_configs
     2.7 -from viff.bedoza import BeDOZaRuntime, BeDOZaShare, BeDOZaKeyList, BeDOZaMessageList
     2.8 +from viff.bedoza import BeDOZaRuntime, BeDOZaShare, BeDOZaShareContents, BeDOZaKeyList, BeDOZaMessageList
     2.9  from viff.field import FieldElement, GF
    2.10  from viff.util import rand
    2.11  
    2.12 @@ -80,13 +80,12 @@
    2.13  
    2.14          Zp = GF(6277101735386680763835789423176059013767194773182842284081)
    2.15         
    2.16 -        x = (Zp(2), BeDOZaKeyList(Zp(23), [Zp(3), Zp(4), Zp(1)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
    2.17 -        y = (Zp(2), BeDOZaKeyList(Zp(23), [Zp(5), Zp(2), Zp(7)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
    2.18 -        zi, zks, zms = runtime._plus((x, y), Zp)
    2.19 -        self.assertEquals(zi, Zp(4))
    2.20 -        self.assertEquals(zks, BeDOZaKeyList(Zp(23), [Zp(8), Zp(6), Zp(8)]))
    2.21 -        self.assertEquals(zms, BeDOZaMessageList([Zp(4), Zp(148), Zp(46), Zp(4)]))
    2.22 -        return zi
    2.23 +        x = BeDOZaShareContents(Zp(2), BeDOZaKeyList(Zp(23), [Zp(3), Zp(4), Zp(1)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
    2.24 +        y = BeDOZaShareContents(Zp(2), BeDOZaKeyList(Zp(23), [Zp(5), Zp(2), Zp(7)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
    2.25 +        z = runtime._plus((x, y), Zp)
    2.26 +        self.assertEquals(z.get_value(), Zp(4))
    2.27 +        self.assertEquals(z.get_keys(), BeDOZaKeyList(Zp(23), [Zp(8), Zp(6), Zp(8)]))
    2.28 +        self.assertEquals(z.get_macs(), BeDOZaMessageList([Zp(4), Zp(148), Zp(46), Zp(4)]))
    2.29  
    2.30      @protocol
    2.31      def test_sum(self, runtime):
    2.32 @@ -162,13 +161,12 @@
    2.33  
    2.34          Zp = GF(6277101735386680763835789423176059013767194773182842284081)
    2.35         
    2.36 -        x = (Zp(2), BeDOZaKeyList(Zp(23), [Zp(5), Zp(4), Zp(7)]), BeDOZaMessageList([Zp(2), Zp(75), Zp(23), Zp(2)]))
    2.37 -        y = (Zp(2), BeDOZaKeyList(Zp(23), [Zp(3), Zp(2), Zp(1)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
    2.38 -        zi, zks, zms = runtime._minus((x, y), Zp)
    2.39 -        self.assertEquals(zi, Zp(0))
    2.40 -        self.assertEquals(zks, BeDOZaKeyList(Zp(23), [Zp(2), Zp(2), Zp(6)]))
    2.41 -        self.assertEquals(zms, BeDOZaMessageList([Zp(0), Zp(1), Zp(0), Zp(0)]))
    2.42 -        return zi
    2.43 +        x = BeDOZaShareContents(Zp(2), BeDOZaKeyList(Zp(23), [Zp(5), Zp(4), Zp(7)]), BeDOZaMessageList([Zp(2), Zp(75), Zp(23), Zp(2)]))
    2.44 +        y = BeDOZaShareContents(Zp(2), BeDOZaKeyList(Zp(23), [Zp(3), Zp(2), Zp(1)]), BeDOZaMessageList([Zp(2), Zp(74), Zp(23), Zp(2)]))
    2.45 +        z = runtime._minus((x, y), Zp)
    2.46 +        self.assertEquals(z.get_value(), Zp(0))
    2.47 +        self.assertEquals(z.get_keys(), BeDOZaKeyList(Zp(23), [Zp(2), Zp(2), Zp(6)]))
    2.48 +        self.assertEquals(z.get_macs(), BeDOZaMessageList([Zp(0), Zp(1), Zp(0), Zp(0)]))
    2.49  
    2.50      @protocol
    2.51      def test_sub(self, runtime):