viff

changeset 1449:04a121c3b6a5

Added an open operation.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Mon, 05 Jul 2010 11:34:00 +0200
parents 3c21042745e7
children d910c8a78a2e
files viff/bedoza.py viff/test/test_bedoza_runtime.py
diffstat 2 files changed, 81 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/viff/bedoza.py	Mon Jul 05 11:33:58 2010 +0200
     1.2 +++ b/viff/bedoza.py	Mon Jul 05 11:34:00 2010 +0200
     1.3 @@ -19,10 +19,13 @@
     1.4  
     1.5  from twisted.internet.defer import Deferred, gatherResults, succeed
     1.6  
     1.7 -from viff.runtime import Runtime, Share
     1.8 +from viff.runtime import Runtime, Share, ShareList
     1.9  
    1.10  from hash_broadcast import HashBroadcastMixin
    1.11  
    1.12 +class BeDOZaException(Exception):
    1.13 +    pass
    1.14 +
    1.15  class BeDOZaShare(Share):
    1.16      """A share in the BeDOZa runtime.
    1.17  
    1.18 @@ -125,6 +128,69 @@
    1.19  
    1.20          return self.random_shares.pop()
    1.21  
    1.22 +    def open(self, share, receivers=None):
    1.23 +        """Share reconstruction.
    1.24 + 
    1.25 +        Every partyi broadcasts a share pair ``(x_i', rho_x,i')``.
    1.26 +
    1.27 +        The parties compute the sums ``x'``, ``rho_x'`` and check
    1.28 +        ``Com_ck(x',rho_x') = C_x``.
    1.29 +
    1.30 +        If yes, return ``x = x'``, else else return :const:`None`.
    1.31 +        """
    1.32 +        assert isinstance(share, Share)
    1.33 +        # all players receive result by default
    1.34 +        if receivers is None:
    1.35 +            receivers = self.players.keys()
    1.36 +
    1.37 +        field = share.field
    1.38 +
    1.39 +        self.increment_pc()
    1.40 +
    1.41 +        def recombine_value(shares_codes):
    1.42 +            isOK = True
    1.43 +            n = len(self.players)
    1.44 +            alpha = self.get_keys()[0]
    1.45 +            keys = self.get_keys()[1]
    1.46 +            x = 0
    1.47 +            for inx in xrange(0, n):
    1.48 +                xi = shares_codes[inx]
    1.49 +                mi = shares_codes[n + inx]
    1.50 +                beta = keys[inx]
    1.51 +                x += xi
    1.52 +                isOK = isOK and mi == self.MAC(alpha, beta, xi)
    1.53 +            if not isOK:
    1.54 +                raise BeDOZaException("Wrong commitment for value %s." % x)
    1.55 +            return x
    1.56 +
    1.57 +        def exchange((xi, codes), receivers):
    1.58 +            # Send share to all receivers.
    1.59 +            pc = tuple(self.program_counter)
    1.60 +            for other_id in receivers:
    1.61 +                self.protocols[other_id].sendShare(pc, xi)
    1.62 +                self.protocols[other_id].sendShare(pc, codes[other_id - 1])
    1.63 +
    1.64 +            if self.id in receivers:
    1.65 +                num_players = len(self.players.keys())
    1.66 +                values = num_players * [None]
    1.67 +                codes = num_players * [None]
    1.68 +                for inx, other_id in enumerate(self.players.keys()):
    1.69 +                    values[inx] =  self._expect_share(other_id, field)
    1.70 +                    codes[inx] = self._expect_share(other_id, field)
    1.71 +                result = gatherResults(values + codes)
    1.72 +                result.addCallbacks(recombine_value, self.error_handler)
    1.73 +                return result
    1.74 +
    1.75 +        result = share.clone()
    1.76 +        self.schedule_callback(result, exchange, receivers)
    1.77 +        result.addErrback(self.error_handler)
    1.78 +
    1.79 +        # do actual communication
    1.80 +        self.activate_reactor()
    1.81 +
    1.82 +        if self.id in receivers:
    1.83 +            return result
    1.84 +
    1.85      def get_keys(self):
    1.86          if self.id == 1:
    1.87              return self.keys[1]
     2.1 --- a/viff/test/test_bedoza_runtime.py	Mon Jul 05 11:33:58 2010 +0200
     2.2 +++ b/viff/test/test_bedoza_runtime.py	Mon Jul 05 11:34:00 2010 +0200
     2.3 @@ -107,4 +107,17 @@
     2.4  
     2.5      runtime_class = BeDOZaRuntime
     2.6  
     2.7 -BeDOZaBasicCommandsTest.skip = "Skipped because the tested code is not implemented."
     2.8 +    @protocol
     2.9 +    def test_random_share(self, runtime):
    2.10 +        """Test creation of a random shared number."""
    2.11 +
    2.12 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
    2.13 +
    2.14 +        def check(v):
    2.15 +            self.assertEquals(True, True)
    2.16 +
    2.17 +        x = runtime.random_share(self.Zp)
    2.18 +        d = runtime.open(x)
    2.19 +        d.addCallback(check)
    2.20 +        return d
    2.21 +