changeset 1514:c3f671892261

BeDOZa: Inital implementation of generate_triples.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Wed, 21 Jul 2010 15:07:33 +0200
parents 71038263d036
children 2fb1e2fc8707
files viff/bedoza.py viff/bedoza_triple.py viff/simplearithmetic.py viff/test/test_bedoza_triple.py
diffstat 4 files changed, 80 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/viff/bedoza.py	Wed Jul 21 09:40:53 2010 +0200
+++ b/viff/bedoza.py	Wed Jul 21 15:07:33 2010 +0200
@@ -404,7 +404,7 @@
                     values_b[inx] =  self._expect_share(other_id, field)
                     codes_b[inx] = self._expect_share(other_id, field)
                 result = gatherResults(values_a + codes_a + values_b + codes_b)
-                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(a.get_keys(), b.get_keys()))
+                self.schedule_callback(result, recombine_value, a.get_keys(), b.get_keys())
                 return result
 
         result = gather_shares([share_a, share_b])
@@ -440,7 +440,6 @@
                 x += xi
                 mi_prime = self.MAC(alpha, beta, xi)
                 isOK = isOK and mi == mi_prime
-
             def check(ls, x, isOK):
                 true_str = str(True)
                 if reduce(lambda x, y: true_str == y, ls):
--- a/viff/bedoza_triple.py	Wed Jul 21 09:40:53 2010 +0200
+++ b/viff/bedoza_triple.py	Wed Jul 21 15:07:33 2010 +0200
@@ -132,6 +132,8 @@
         self.random = random
 
     def generate_share(self, value):
+        self.runtime.increment_pc()
+        
         r = [self.Zp(self.random.randint(0, self.Zp.modulus - 1)) # TODO: Exclusve?
              for _ in range(self.runtime.num_players - 1)]
         if self.runtime.id == 1:
@@ -251,7 +253,47 @@
         triples in memory at the same time. Is there a nice way to
         stream this, e.g. by using Python generators?
         """
-        triples = self._generate_passive_triples(n)
+
+        self.runtime.increment_pc()
+        
+        gen = PartialShareGenerator(self.Zp, self.runtime, self.random, self.paillier)
+
+        def check(v, a, b, c):
+            if v.value != 0:
+                raise Exception("TripleTest failed - The two triples were inconsistent.")
+            return (a, b, c)
+        
+        def compute_value(r, a, b, c, x, y, z):
+            l = self.runtime._cmul(r, x, self.Zp)
+            m = self.runtime._cmul(r, y, self.Zp)
+            k = self.runtime._cmul(r*r, z, self.Zp)
+            v = c - self.runtime._basic_multiplication(a, b, l, m, k)
+            v = self.runtime.open(v)
+            v.addCallback(check, a, b, c)
+            return v
+
+        random_shares = []
+        for _ in xrange(n):
+             random_shares.append(gen.generate_share(self.random.randint(0, self.Zp.modulus - 1)))
+
+        random_shares = self._add_macs(random_shares)
+
+        results = [Deferred() for _ in xrange(n)]
+        
+        triples = self._generate_passive_triples(2 * n)
+
+        for inx in xrange(n):
+            a = triples[inx]
+            b = triples[inx + 2 * n]
+            c = triples[inx + 4 * n]
+            x = triples[inx + n]
+            y = triples[inx + 3 * n]
+            z = triples[inx + 5 * n]
+            r = self.runtime.open(random_shares[inx])
+            self.runtime.schedule_callback(r, compute_value, a, b, c, x, y, z)
+            r.chainDeferred(results[inx])
+        return results
+          
         # TODO: Do some ZK stuff.
 
     def _generate_passive_triples(self, n):
@@ -261,6 +303,9 @@
         
         Consistency is only guaranteed if all players follow the protool.
         """
+
+        self.runtime.increment_pc()
+        
         gen = PartialShareGenerator(self.Zp, self.runtime, self.random, self.paillier)
         partial_shares = []
         for _ in xrange(2 * n):
--- a/viff/simplearithmetic.py	Wed Jul 21 09:40:53 2010 +0200
+++ b/viff/simplearithmetic.py	Wed Jul 21 15:07:33 2010 +0200
@@ -143,7 +143,8 @@
         if cmul_result is  not None:
             return cmul_result
 
-        def multiply((x, y, c, (d, e))):
+        def multiply(ls):
+            x, y, c, (d, e) = ls
             # [de]
             de = d * e
             # e[x]
@@ -153,6 +154,7 @@
             # d[y] - [de]
             t3 = self._minus_public_right_without_share(t2, de, field)
             # d[y] - [de] + [c]
+            z = self._plus((t3, c), field)
             t4 = self._plus((t3, c), field)
             # [z] = e[x] + d[y] - [de] + [c]
             z = self._plus((t1, t4), field)
--- a/viff/test/test_bedoza_triple.py	Wed Jul 21 09:40:53 2010 +0200
+++ b/viff/test/test_bedoza_triple.py	Wed Jul 21 15:07:33 2010 +0200
@@ -269,11 +269,37 @@
         return share
 
 
-class PassiveTripleTest(BeDOZaTestCase): 
+class TripleTest(BeDOZaTestCase): 
     num_players = 3
 
-    timeout = 10
-    
+    timeout = 120
+
+    @protocol
+    def test_generate_triples_generates_correct_triples(self, runtime):
+        p = 17
+
+        Zp = GF(p)
+        
+        random = Random(283883)        
+        triple_generator = TripleGenerator(runtime, p, random)
+
+        triples = triple_generator.generate_triples(5)
+
+        def check((a, b, c)):
+            self.assertEquals(c, a * b)
+
+        def open((a, b, c)):
+            d1 = runtime.open(a)
+            d2 = runtime.open(b)
+            d3 = runtime.open(c)
+            d = gatherResults([d1, d2, d3])
+            runtime.schedule_callback(d, check)
+            return d
+
+        for triple in triples:
+            runtime.schedule_callback(triple, open)
+        return gatherResults(triples)
+
     @protocol
     def test_passive_triples_generates_correct_triples(self, runtime):
         p = 17
@@ -283,7 +309,7 @@
         random = Random(283883)        
         triple_generator = TripleGenerator(runtime, p, random)
 
-        triples = triple_generator._generate_passive_triples(5)
+        triples = triple_generator._generate_passive_triples(1)
         def verify(triples):
             for inx in xrange(len(triples) // 3):
                 self.assertEquals(triples[10 + inx], triples[inx] * triples[5 + inx])
@@ -294,11 +320,6 @@
         d.addCallback(verify)
         return d
 
-
-class TripleTest(BeDOZaTestCase): 
-    num_players = 3
-
-    timeout = 4
     @protocol
     def test_add_macs_produces_correct_sharing(self, runtime):
         # TODO: Here we use the open method of the BeDOZa runtime in