viff

changeset 1084:b4d9b373bbab

Added lin_comb() to compute linear combinations of shares with less overhead.
author Marcel Keller <mkeller@cs.au.dk>
date Tue, 20 Jan 2009 10:45:50 +0100
parents e530dfcb40cd
children 29d85ec50160
files viff/passive.py
diffstat 1 files changed, 28 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/viff/passive.py	Thu Jan 15 18:30:46 2009 +0100
     1.2 +++ b/viff/passive.py	Tue Jan 20 10:45:50 2009 +0100
     1.3 @@ -135,6 +135,34 @@
     1.4          result.addCallback(lambda (a, b): a - b)
     1.5          return result
     1.6  
     1.7 +    def lin_comb(self, coefficients, shares):
     1.8 +        """Linear combination of shares.
     1.9 +
    1.10 +        Communication cost: none. Saves the construction of unnecessary shares
    1.11 +        compared to using add() and mul()."""
    1.12 +
    1.13 +        for coeff in coefficients:
    1.14 +            assert not isinstance(coeff, Share), \
    1.15 +                "Coefficients should not be shares."
    1.16 +
    1.17 +        assert len(coefficients) == len(shares), \
    1.18 +            "Number of coefficients and shares should be equal."
    1.19 +
    1.20 +        field = None
    1.21 +        for share in shares:
    1.22 +            field = getattr(share, "field", field)
    1.23 +        for i, share in enumerate(shares):
    1.24 +            if not isinstance(share, Share):
    1.25 +                shares[i] = Share(self, field, share)
    1.26 +
    1.27 +        def computation(shares, coefficients):
    1.28 +            summands = [shares[i] * coefficients[i] for i in range(len(shares))]
    1.29 +            return reduce(lambda x, y: x + y, summands)
    1.30 +
    1.31 +        result = gather_shares(shares)
    1.32 +        result.addCallback(computation, coefficients)
    1.33 +        return result
    1.34 +
    1.35      @profile
    1.36      @increment_pc
    1.37      def mul(self, share_a, share_b):