changeset 635:a7f03661f81d

FieldElements are now multiplied locally, skipping the resharing step.
author Mikkel Krøigård <mk@daimi.au.dk>
date Fri, 04 Apr 2008 21:16:26 +0200
parents 9d4a0ade3918
children ba9166a59c20
files viff/runtime.py
diffstat 1 files changed, 20 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/viff/runtime.py	Fri Apr 04 11:03:26 2008 +0200
+++ b/viff/runtime.py	Fri Apr 04 21:16:26 2008 +0200
@@ -717,15 +717,30 @@
 
         Communication cost: 1 Shamir sharing.
         """
-        # TODO:  mul accept FieldElements and do quick local
-        # multiplication in that case. If two FieldElements are given,
-        # return a FieldElement.
+        # If one of the arguments is a FieldElement, we do a local
+        # multiplication and skip the resharing step.
+
         field = getattr(share_a, "field", getattr(share_b, "field", None))
-        if not isinstance(share_a, Share):
+
+        if not isinstance(share_a, Share) and not isinstance(share_a,
+                                                             FieldElement):
             share_a = Share(self, field, share_a)
-        if not isinstance(share_b, Share):
+
+        if not isinstance(share_b, Share) and not isinstance(share_b,
+                                                             FieldElement):
             share_b = Share(self, field, share_b)
 
+        if isinstance(share_a, FieldElement) and isinstance(share_b,
+                                                            FieldElement):
+            return Share(self, field, share_a * share_b)
+        
+        if isinstance(share_a, FieldElement):
+            share_b.addCallback(lambda x: Share(self, field, share_a*x.value))
+            return share_b
+        if isinstance(share_b, FieldElement):
+            share_a.addCallback(lambda x: Share(self, field, x.value*share_b))
+            return share_a
+
         result = gather_shares([share_a, share_b])
         result.addCallback(lambda (a, b): a * b)
         self.schedule_callback(result, self._shamir_share)