viff

changeset 1179:cbc39e56b402

Reduce number of isinstance() calls. This is done by exploiting that in operator overloading functions like Share.__add__ the first parameter always is a Share.
author Marcel Keller <mkeller@cs.au.dk>
date Wed, 13 May 2009 15:32:00 +0200
parents e1e0c107c40b
children b44882c6d4f6
files viff/aes.py viff/passive.py viff/runtime.py
diffstat 3 files changed, 21 insertions(+), 30 deletions(-) [+]
line diff
     1.1 --- a/viff/aes.py	Wed May 13 11:12:20 2009 +0200
     1.2 +++ b/viff/aes.py	Wed May 13 15:32:00 2009 +0200
     1.3 @@ -205,7 +205,7 @@
     1.4  
     1.5                  # include the translation in the matrix multiplication
     1.6                  # (see definition of AES.A)
     1.7 -                bits.append(GF256(1))
     1.8 +                bits.append(Share(self.runtime, GF256, GF256(1)))
     1.9  
    1.10                  if (use_lin_comb):
    1.11                      bits = [self.runtime.lin_comb(AES.A.rows[j], bits) 
     2.1 --- a/viff/passive.py	Wed May 13 11:12:20 2009 +0200
     2.2 +++ b/viff/passive.py	Wed May 13 15:32:00 2009 +0200
     2.3 @@ -113,12 +113,14 @@
     2.4  
     2.5          Communication cost: none.
     2.6          """
     2.7 -        field = getattr(share_a, "field", getattr(share_b, "field", None))
     2.8 -        if not isinstance(share_a, Share):
     2.9 -            share_a = Share(self, field, share_a)
    2.10          if not isinstance(share_b, Share):
    2.11 -            share_b = Share(self, field, share_b)
    2.12 -
    2.13 +            # Addition with constant. share_a always is a Share by
    2.14 +            # operator overloading in Share. Clone share_a to avoid
    2.15 +            # changing it.
    2.16 +            result = share_a.clone()
    2.17 +            result.addCallback(lambda a, b: a + b, share_b)
    2.18 +            return result
    2.19 +        
    2.20          result = gather_shares([share_a, share_b])
    2.21          result.addCallback(lambda (a, b): a + b)
    2.22          return result
    2.23 @@ -149,16 +151,13 @@
    2.24              assert not isinstance(coeff, Share), \
    2.25                  "Coefficients should not be shares."
    2.26  
    2.27 +        for share in shares:
    2.28 +            assert isinstance(share, Share), \
    2.29 +                "Shares should be shares."
    2.30 +
    2.31          assert len(coefficients) == len(shares), \
    2.32              "Number of coefficients and shares should be equal."
    2.33  
    2.34 -        field = None
    2.35 -        for share in shares:
    2.36 -            field = getattr(share, "field", field)
    2.37 -        for i, share in enumerate(shares):
    2.38 -            if not isinstance(share, Share):
    2.39 -                shares[i] = Share(self, field, share)
    2.40 -
    2.41          def computation(shares, coefficients):
    2.42              summands = [shares[i] * coefficients[i] for i in range(len(shares))]
    2.43              return reduce(lambda x, y: x + y, summands)
    2.44 @@ -174,17 +173,13 @@
    2.45  
    2.46          Communication cost: 1 Shamir sharing.
    2.47          """
    2.48 -        assert isinstance(share_a, Share) or isinstance(share_b, Share), \
    2.49 -            "At least one of share_a and share_b must be a Share."
    2.50 +        assert isinstance(share_a, Share), \
    2.51 +            "share_a must be a Share."
    2.52  
    2.53 -        if not isinstance(share_a, Share):
    2.54 -            # Then share_b must be a Share => local multiplication. We
    2.55 -            # clone first to avoid changing share_b.
    2.56 -            result = share_b.clone()
    2.57 -            result.addCallback(lambda b: share_a * b)
    2.58 -            return result
    2.59          if not isinstance(share_b, Share):
    2.60 -            # Likewise when share_b is a constant.
    2.61 +            # Local multiplication. share_a always is a Share by
    2.62 +            # operator overloading in Share. We clone share_a first
    2.63 +            # to avoid changing it.
    2.64              result = share_a.clone()
    2.65              result.addCallback(lambda a: a * share_b)
    2.66              return result
    2.67 @@ -227,11 +222,7 @@
    2.68  
    2.69      @increment_pc
    2.70      def xor(self, share_a, share_b):
    2.71 -        field = getattr(share_a, "field", getattr(share_b, "field", None))
    2.72 -        if not isinstance(share_a, Share):
    2.73 -            if not isinstance(share_a, FieldElement):
    2.74 -                share_a = field(share_a)
    2.75 -            share_a = Share(self, field, share_a)
    2.76 +        field = share_a.field
    2.77          if not isinstance(share_b, Share):
    2.78              if not isinstance(share_b, FieldElement):
    2.79                  share_b = field(share_b)
     3.1 --- a/viff/runtime.py	Wed May 13 11:12:20 2009 +0200
     3.2 +++ b/viff/runtime.py	Wed May 13 15:32:00 2009 +0200
     3.3 @@ -92,7 +92,7 @@
     3.4  
     3.5      def __radd__(self, other):
     3.6          """Addition (reflected argument version)."""
     3.7 -        return self.runtime.add(other, self)
     3.8 +        return self.runtime.add(self, other)
     3.9  
    3.10      def __sub__(self, other):
    3.11          """Subtraction."""
    3.12 @@ -108,7 +108,7 @@
    3.13  
    3.14      def __rmul__(self, other):
    3.15          """Multiplication (reflected argument version)."""
    3.16 -        return self.runtime.mul(other, self)
    3.17 +        return self.runtime.mul(self, other)
    3.18  
    3.19      def __pow__(self, exponent):
    3.20          """Exponentation to known integer exponents."""
    3.21 @@ -120,7 +120,7 @@
    3.22  
    3.23      def __rxor__(self, other):
    3.24          """Exclusive-or (reflected argument version)."""
    3.25 -        return self.runtime.xor(other, self)
    3.26 +        return self.runtime.xor(self, other)
    3.27  
    3.28      def __lt__(self, other):
    3.29          """Strictly less-than comparison."""