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 wrap: on
line diff
--- a/viff/passive.py	Thu Jan 15 18:30:46 2009 +0100
+++ b/viff/passive.py	Tue Jan 20 10:45:50 2009 +0100
@@ -135,6 +135,34 @@
         result.addCallback(lambda (a, b): a - b)
         return result
 
+    def lin_comb(self, coefficients, shares):
+        """Linear combination of shares.
+
+        Communication cost: none. Saves the construction of unnecessary shares
+        compared to using add() and mul()."""
+
+        for coeff in coefficients:
+            assert not isinstance(coeff, Share), \
+                "Coefficients should not be shares."
+
+        assert len(coefficients) == len(shares), \
+            "Number of coefficients and shares should be equal."
+
+        field = None
+        for share in shares:
+            field = getattr(share, "field", field)
+        for i, share in enumerate(shares):
+            if not isinstance(share, Share):
+                shares[i] = Share(self, field, share)
+
+        def computation(shares, coefficients):
+            summands = [shares[i] * coefficients[i] for i in range(len(shares))]
+            return reduce(lambda x, y: x + y, summands)
+
+        result = gather_shares(shares)
+        result.addCallback(computation, coefficients)
+        return result
+
     @profile
     @increment_pc
     def mul(self, share_a, share_b):