viff

changeset 1225:bb9566f09d3f

Implementation of random share command.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Tue, 06 Oct 2009 10:05:24 +0200
parents 7ed324dff36b
children 94086392cb3c
files viff/orlandi.py viff/runtime.py viff/test/test_orlandi_runtime.py
diffstat 3 files changed, 63 insertions(+), 14 deletions(-) [+]
line diff
     1.1 --- a/viff/orlandi.py	Tue Oct 06 10:05:24 2009 +0200
     1.2 +++ b/viff/orlandi.py	Tue Oct 06 10:05:24 2009 +0200
     1.3 @@ -269,6 +269,53 @@
     1.4          if self.id in receivers:
     1.5              return result
     1.6  
     1.7 +    def random_share(self, field):
     1.8 +        """Generate a random share in the field, field.
     1.9 +
    1.10 +        To generate a share of a random element ``r in Z_p``, party ``P_i`` 
    1.11 +        chooses at random ``r_i, rho_ri in Z_p X (Z_p)^2`` and
    1.12 +        broadcast ``C_r^i = Com_ck(r_i, rho_ri)``.
    1.13 +
    1.14 +        Every party computes ``C_r = PRODUCT_i=1^n C_r^i = Com_ck(r, rho_r)``,
    1.15 +        where ``r_i = SUM_i=1^n r_i and rho_r = SUM_i=1^n rho_ri``.
    1.16 +
    1.17 +        Party ``P_i sets [r]_i = (r_i, rho_ri, C_r)``.
    1.18 +
    1.19 +        """
    1.20 +        self.program_counter[-1] += 1
    1.21 +
    1.22 +        # P_i chooses at random r_i, rho_ri in Z_p x (Z_p)^2
    1.23 +        ri = field(rand.randint(0, field.modulus - 1))     
    1.24 +        rhoi1 = field(rand.randint(0, field.modulus - 1))
    1.25 +        rhoi2 = field(rand.randint(0, field.modulus - 1))
    1.26 +
    1.27 +        # compute C_r^i = Com_ck(r_i, rho_ri).
    1.28 +        Cri = commitment.commit(ri.value, rhoi1, rhoi2)
    1.29 +
    1.30 +        # Broadcast C_r^i.
    1.31 +        sls = gatherResults(self.broadcast(self.players.keys(), self.players.keys(), repr(Cri)))
    1.32 +
    1.33 +        def compute_commitment(ls):
    1.34 +            Cr = ls.pop()
    1.35 +            for Cri in ls:
    1.36 +                Cr = Cr * Cri
    1.37 +            return OrlandiShare(self, field, ri, (rhoi1, rhoi2), Cr)
    1.38 +
    1.39 +        def deserialize(ls):
    1.40 +            return [ commitment.deserialize(x) for x in ls ]
    1.41 +
    1.42 +        sls.addCallbacks(deserialize, self.error_handler)
    1.43 +        sls.addCallbacks(compute_commitment, self.error_handler)
    1.44 +
    1.45 +        s = Share(self, field)
    1.46 +        # We add the result to the chains in triple.
    1.47 +        sls.chainDeferred(s)
    1.48 +
    1.49 +        # do actual communication
    1.50 +        self.activate_reactor()
    1.51 +
    1.52 +        return s
    1.53 +
    1.54      def error_handler(self, ex):
    1.55          print "Error: ", ex
    1.56          return ex
     2.1 --- a/viff/runtime.py	Tue Oct 06 10:05:24 2009 +0200
     2.2 +++ b/viff/runtime.py	Tue Oct 06 10:05:24 2009 +0200
     2.3 @@ -327,20 +327,13 @@
     2.4  
     2.5                  key = (program_counter, data_type)
     2.6  
     2.7 -#                 print "Player %s has received data %s from %s with pc: %s" % (str(self.factory.runtime.id), 
     2.8 -#                                                                               str(data), 
     2.9 -#                                                                               str(self.peer_id),
    2.10 -#                                                                               str(key))
    2.11 -
    2.12                  if key in self.waiting_deferreds:
    2.13 -#                    print "A deferred was waiting"
    2.14                      deq = self.waiting_deferreds[key]
    2.15                      deferred = deq.popleft()
    2.16                      if not deq:
    2.17                          del self.waiting_deferreds[key]
    2.18                      self.factory.runtime.handle_deferred_data(deferred, data)
    2.19                  else:
    2.20 -#                    print "A deferred is not waiting, lets put the data on the shelf"
    2.21                      deq = self.incoming_data.setdefault(key, deque())
    2.22                      deq.append(data)
    2.23              except struct.error, e:
    2.24 @@ -399,12 +392,6 @@
    2.25          """
    2.26          try:
    2.27              key = (program_counter, data_type)
    2.28 -
    2.29 -#            print self
    2.30 -#             print "Player %s has received data %s from %s with pc: %s" % (str(self.factory.runtime.id), 
    2.31 -#                                                                           str(data), 
    2.32 -#                                                                           str(self.peer_id),
    2.33 -#                                                                           program_counter)
    2.34                           
    2.35              if key in self.waiting_deferreds:
    2.36                  deq = self.waiting_deferreds[key]
    2.37 @@ -754,7 +741,6 @@
    2.38          else:
    2.39              # We have not yet received anything from the other side.
    2.40              deq = self.protocols[peer_id].waiting_deferreds.setdefault(key, deque())
    2.41 -#             print "The deferred %s is waiting on data from %i with key: %s" % (str(deferred), peer_id, str(key))
    2.42              deq.append(deferred)
    2.43  
    2.44      def _exchange_shares(self, peer_id, field_element):
    2.45 @@ -869,6 +855,7 @@
    2.46      def handle_deferred_data(self, deferred, data):
    2.47          """Put deferred and data into the queue if the ViffReactor is running. 
    2.48          Otherwise, just execute the callback."""
    2.49 +
    2.50          if self.using_viff_reactor:
    2.51              self.deferred_queue.append((deferred, data))
    2.52          else:
     3.1 --- a/viff/test/test_orlandi_runtime.py	Tue Oct 06 10:05:24 2009 +0200
     3.2 +++ b/viff/test/test_orlandi_runtime.py	Tue Oct 06 10:05:24 2009 +0200
     3.3 @@ -71,3 +71,18 @@
     3.4          d.addCallback(check)
     3.5          return d
     3.6  
     3.7 +    @protocol
     3.8 +    def test_random_share(self, runtime):
     3.9 +        """Test creation of a random shared number."""
    3.10 +
    3.11 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)        
    3.12 +
    3.13 +        def check(v):
    3.14 +            self.assertEquals(True, True)           
    3.15 +
    3.16 +        x = runtime.random_share(self.Zp)
    3.17 +        d = runtime.open(x)
    3.18 +        d.addCallback(check)
    3.19 +        return d
    3.20 + 
    3.21 +