viff

changeset 637:d1d4c633fca8

Make "Share * int" multiplication local too. Making Runtime.mul stricter in the type of arguments it accepts unveiled a little strangeness in prss_share_random: it called Runtime.mul with the same FieldElement used twice as argument. That is not necessary, so we now save a resharing there too.
author Martin Geisler <mg@daimi.au.dk>
date Fri, 04 Apr 2008 22:25:03 +0200
parents ba9166a59c20
children 45ce0d082747
files viff/runtime.py
diffstat 1 files changed, 17 insertions(+), 21 deletions(-) [+]
line diff
     1.1 --- a/viff/runtime.py	Fri Apr 04 21:20:19 2008 +0200
     1.2 +++ b/viff/runtime.py	Fri Apr 04 22:25:03 2008 +0200
     1.3 @@ -717,28 +717,23 @@
     1.4  
     1.5          Communication cost: 1 Shamir sharing.
     1.6          """
     1.7 -        # If one of the arguments is a FieldElement, we do a local
     1.8 -        # multiplication and skip the resharing step.
     1.9 +        assert isinstance(share_a, Share) or isinstance(share_b, Share), \
    1.10 +            "At least one of share_a and share_b must be a Share."
    1.11  
    1.12 -        field = getattr(share_a, "field", getattr(share_b, "field", None))
    1.13 +        if not isinstance(share_a, Share):
    1.14 +            # Then share_b must be a Share => local multiplication. We
    1.15 +            # clone first to avoid changing share_b.
    1.16 +            result = share_b.clone()
    1.17 +            result.addCallback(lambda b: share_a * b)
    1.18 +            return result
    1.19 +        if not isinstance(share_b, Share):
    1.20 +            # Likewise when share_b is a constant.
    1.21 +            result = share_a.clone()
    1.22 +            result.addCallback(lambda a: a * share_b)
    1.23 +            return result
    1.24  
    1.25 -        if not isinstance(share_a, (Share, FieldElement)):
    1.26 -            share_a = Share(self, field, share_a)
    1.27 -
    1.28 -        if not isinstance(share_b, (Share, FieldElement)):
    1.29 -            share_b = Share(self, field, share_b)
    1.30 -
    1.31 -        if isinstance(share_a, FieldElement) and isinstance(share_b,
    1.32 -                                                            FieldElement):
    1.33 -            return Share(self, field, share_a * share_b)
    1.34 -
    1.35 -        if isinstance(share_a, FieldElement):
    1.36 -            share_b.addCallback(lambda x: Share(self, field, share_a*x.value))
    1.37 -            return share_b
    1.38 -        if isinstance(share_b, FieldElement):
    1.39 -            share_a.addCallback(lambda x: Share(self, field, x.value*share_b))
    1.40 -            return share_a
    1.41 -
    1.42 +        # At this point both share_a and share_b must be Share
    1.43 +        # objects. So we wait on them, multiply and reshare.
    1.44          result = gather_shares([share_a, share_b])
    1.45          result.addCallback(lambda (a, b): a * b)
    1.46          self.schedule_callback(result, self._shamir_share)
    1.47 @@ -865,7 +860,8 @@
    1.48              return Share(self, field, share)
    1.49  
    1.50          # Open the square and compute a square-root
    1.51 -        result = self.open(self.mul(share, share), threshold=2*self.threshold)
    1.52 +        result = self.open(Share(self, field, share*share),
    1.53 +                           threshold=2*self.threshold)
    1.54  
    1.55          def finish(square, share, binary):
    1.56              if square == 0: