viff

changeset 1354:f0ef1b584a44

Orlandi:Some minor improvements to the code.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Tue, 27 Oct 2009 21:41:00 +0100
parents 3f9b8ebb7de6
children 58ebae853020
files viff/orlandi.py
diffstat 1 files changed, 10 insertions(+), 21 deletions(-) [+]
line diff
     1.1 --- a/viff/orlandi.py	Tue Oct 27 21:22:38 2009 +0100
     1.2 +++ b/viff/orlandi.py	Tue Oct 27 21:41:00 2009 +0100
     1.3 @@ -15,6 +15,8 @@
     1.4  # You should have received a copy of the GNU Lesser General Public
     1.5  # License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
     1.6  
     1.7 +import operator
     1.8 +
     1.9  from twisted.internet.defer import Deferred, gatherResults, succeed
    1.10  
    1.11  from viff.runtime import Runtime, Share, ShareList, gather_shares, preprocess
    1.12 @@ -303,9 +305,7 @@
    1.13          sls = gatherResults(self.broadcast(self.players.keys(), self.players.keys(), repr(Cri)))
    1.14  
    1.15          def compute_commitment(ls):
    1.16 -            Cr = ls.pop()
    1.17 -            for Cri in ls:
    1.18 -                Cr = Cr * Cri
    1.19 +            Cr = reduce(operator.mul, ls)
    1.20              return OrlandiShare(self, field, ri, (rhoi1, rhoi2), Cr)
    1.21  
    1.22          def deserialize(ls):
    1.23 @@ -343,9 +343,8 @@
    1.24              return s
    1.25  
    1.26          # Either share_a or share_b must have an attribute called "field".
    1.27 -        field = getattr(share_a, "field", getattr(share_b, "field", None))
    1.28 +        field = share_a.field
    1.29  
    1.30 -        share_a = is_share(share_a, field)
    1.31          share_b = is_share(share_b, field)
    1.32  
    1.33          # Add rho_xi and rho_yi and compute the commitment.
    1.34 @@ -811,17 +810,7 @@
    1.35  
    1.36          def product(ls):
    1.37              """Compute the product of the elements in the list *ls*."""
    1.38 -            b = commitment.deserialize(ls[0])
    1.39 -            for x in ls[1:]:
    1.40 -                b *= commitment.deserialize(x)
    1.41 -            return b
    1.42 -
    1.43 -        def sum(ls):
    1.44 -            """Compute the sum of the elements in the list *ls*."""
    1.45 -            b = field(0)
    1.46 -            for x in ls:
    1.47 -                b += x
    1.48 -            return b
    1.49 +            return  reduce(operator.mul, map(commitment.deserialize, ls))
    1.50  
    1.51          def step45(Cs, alphas, gammas, alpha_randomness,
    1.52                     As, Bs, ai, bi, ci, r, s, t, dijs):
    1.53 @@ -858,7 +847,7 @@
    1.54              """
    1.55              # c_i = SUM_j Dec_sk_i(gamma_ij) - SUM_j d_ji mod p.
    1.56              ls = decrypt_gammas(gammas)
    1.57 -            ci = sum(ls) - sum(dijs)
    1.58 +            ci = sum(ls, field(0)) - sum(dijs, field(0))
    1.59              # (b) pick random t_i in (Z_p)^2.
    1.60              t1 = random_number(field. modulus)
    1.61              t2 = random_number(field. modulus)
    1.62 @@ -886,18 +875,19 @@
    1.63              dijs = [None] * len(self.players.keys())
    1.64              results = [None] * len(self.players.keys())
    1.65              pc = tuple(self.program_counter)
    1.66 +            p3 = field.modulus**3
    1.67              for pi in self.players.keys():
    1.68                  n = self.players[pi].pubkey[0]
    1.69                  nsq = n * n
    1.70                  # choose random d_i,j in Z_p^3
    1.71 -                dij = random_number(field.modulus**3)
    1.72 +                dij = random_number(p3)
    1.73                  # Enc_ek_i(1;1)^d_ij
    1.74                  enc = encrypt_r(1, 1, self.players[pi].pubkey)
    1.75                  t1 = pow(enc, dij.value, nsq)
    1.76                  # alpha_i^b_j.
    1.77                  t2 = pow(alphas[pi - 1], bj.value, nsq)
    1.78                  # gamma_ij = alpha_i^b_j Enc_ek_i(1;1)^d_ij
    1.79 -                gammaij = (t2) * (t1) % nsq
    1.80 +                gammaij = (t2 * t1) % nsq
    1.81                  # Broadcast gamma_ij
    1.82                  if pi != self.id:
    1.83                      self.protocols[pi].sendData(pc, PAILLIER, str(gammaij))
    1.84 @@ -905,8 +895,7 @@
    1.85                      d.addCallbacks(lambda value: long(value), self.error_handler)
    1.86                      self._expect_data(pi, PAILLIER, d)
    1.87                  else:
    1.88 -                    d = Deferred()
    1.89 -                    d.callback(gammaij)
    1.90 +                    d = succeed(gammaij)
    1.91                  dijs[pi - 1] = dij
    1.92                  results[pi - 1] = d
    1.93              result = gatherResults(results)