viff

changeset 1482:79ff80ee9810

Orlandi: Added a method for opening two shares in one batch.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Thu, 08 Jul 2010 14:13:03 +0200
parents 2aa3bec0492e
children 32af54d63bce
files viff/orlandi.py viff/test/test_orlandi_runtime.py
diffstat 2 files changed, 89 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/viff/orlandi.py	Thu Jul 08 10:58:52 2010 +0200
     1.2 +++ b/viff/orlandi.py	Thu Jul 08 14:13:03 2010 +0200
     1.3 @@ -305,6 +305,73 @@
     1.4          if self.id in receivers:
     1.5              return result
     1.6  
     1.7 +    def open_two_values(self, share_a, share_b, receivers=None):
     1.8 +        """Share reconstruction of two shares."""
     1.9 +        assert isinstance(share_a, Share)
    1.10 +        assert isinstance(share_b, Share)
    1.11 +        # all players receive result by default
    1.12 +        if receivers is None:
    1.13 +            receivers = self.players.keys()
    1.14 +
    1.15 +        field = share_a.field
    1.16 +
    1.17 +        self.increment_pc()
    1.18 +
    1.19 +        def recombine_value(shares, Ca, Cb):
    1.20 +            a, b = 0, 0
    1.21 +            rhoa1, rhob1 = 0, 0
    1.22 +            rhoa2, rhob2 = 0, 0
    1.23 +            for ai, rhoai1, rhoai2, bi, rhobi1, rhobi2 in shares:
    1.24 +                a += ai
    1.25 +                b += bi
    1.26 +                rhoa1 += rhoai1
    1.27 +                rhob1 += rhobi1
    1.28 +                rhoa2 += rhoai2
    1.29 +                rhob2 += rhobi2
    1.30 +            Ca1 = commitment.commit(a.value, rhoa1.value, rhoa2.value)
    1.31 +            Cb1 = commitment.commit(b.value, rhob1.value, rhob2.value)
    1.32 +            if Ca1 == Ca and Cb1 == Cb:
    1.33 +                return a, b
    1.34 +            else:
    1.35 +                #return x
    1.36 +                raise OrlandiException("Wrong commitment for value %s, %s, %s, %s, %s, %s, found %s, %s expected %s, %s." %
    1.37 +                                       (a, rhoa1, rhoa2, b, rhob1, rhob2, Ca1, Cb1, Ca, Cb))
    1.38 +
    1.39 +        def deserialize(ls):
    1.40 +            shares = [(field(long(ai)), field(long(rhoa1)), field(long(rhoa2)),
    1.41 +                       field(long(bi)), field(long(rhob1)), field(long(rhob2)))
    1.42 +                      for ai, rhoa1, rhoa2, bi, rhob1, rhob2 in map(self.list_str, ls)]
    1.43 +            return shares
    1.44 +
    1.45 +        def exchange((a, b), receivers):
    1.46 +            (ai, (rhoai1, rhoai2), Ca) = a
    1.47 +            (bi, (rhobi1, rhobi2), Cb) = b
    1.48 +            # Send share to all receivers.
    1.49 +            ds = self.broadcast(self.players.keys(), receivers,
    1.50 +                                str((str(ai.value),
    1.51 +                                     str(rhoai1.value),
    1.52 +                                     str(rhoai2.value),
    1.53 +                                     str(bi.value),
    1.54 +                                     str(rhobi1.value),
    1.55 +                                     str(rhobi2.value))))
    1.56 +
    1.57 +            if self.id in receivers:
    1.58 +                result = gatherResults(ds)
    1.59 +                result.addCallbacks(deserialize, self.error_handler)
    1.60 +                result.addCallbacks(recombine_value, self.error_handler,
    1.61 +                                    callbackArgs=(Ca, Cb))
    1.62 +                return result
    1.63 +
    1.64 +        result = gather_shares([share_a, share_b])
    1.65 +        self.schedule_callback(result, exchange, receivers)
    1.66 +        result.addErrback(self.error_handler)
    1.67 +
    1.68 +        # do actual communication
    1.69 +        self.activate_reactor()
    1.70 +
    1.71 +        if self.id in receivers:
    1.72 +            return result
    1.73 +
    1.74      def open_multiple_values(self, shares, receivers=None):
    1.75          """Share reconstruction.
    1.76  
     2.1 --- a/viff/test/test_orlandi_runtime.py	Thu Jul 08 10:58:52 2010 +0200
     2.2 +++ b/viff/test/test_orlandi_runtime.py	Thu Jul 08 14:13:03 2010 +0200
     2.3 @@ -126,6 +126,28 @@
     2.4          return d
     2.5  
     2.6      @protocol
     2.7 +    def test_open_two_secret_share(self, runtime):
     2.8 +        """Test sharing and open of a number."""
     2.9 +
    2.10 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
    2.11 +
    2.12 +        def check((a, b)):
    2.13 +            self.assertEquals(a, 42)
    2.14 +            self.assertEquals(b, 84)
    2.15 +
    2.16 +        if 1 == runtime.id:
    2.17 +            x = runtime.secret_share([1], self.Zp, 42)
    2.18 +        else:
    2.19 +            x = runtime.secret_share([1], self.Zp)
    2.20 +        if 1 == runtime.id:
    2.21 +            y = runtime.secret_share([1], self.Zp, 84)
    2.22 +        else:
    2.23 +            y = runtime.secret_share([1], self.Zp)
    2.24 +        d = runtime.open_two_values(x, y)
    2.25 +        d.addCallback(check)
    2.26 +        return d
    2.27 +
    2.28 +    @protocol
    2.29      def test_random_share(self, runtime):
    2.30          """Test creation of a random shared number."""
    2.31