viff

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 diff
     1.1 --- a/viff/bedoza.py	Wed Jul 21 09:40:53 2010 +0200
     1.2 +++ b/viff/bedoza.py	Wed Jul 21 15:07:33 2010 +0200
     1.3 @@ -404,7 +404,7 @@
     1.4                      values_b[inx] =  self._expect_share(other_id, field)
     1.5                      codes_b[inx] = self._expect_share(other_id, field)
     1.6                  result = gatherResults(values_a + codes_a + values_b + codes_b)
     1.7 -                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(a.get_keys(), b.get_keys()))
     1.8 +                self.schedule_callback(result, recombine_value, a.get_keys(), b.get_keys())
     1.9                  return result
    1.10  
    1.11          result = gather_shares([share_a, share_b])
    1.12 @@ -440,7 +440,6 @@
    1.13                  x += xi
    1.14                  mi_prime = self.MAC(alpha, beta, xi)
    1.15                  isOK = isOK and mi == mi_prime
    1.16 -
    1.17              def check(ls, x, isOK):
    1.18                  true_str = str(True)
    1.19                  if reduce(lambda x, y: true_str == y, ls):
     2.1 --- a/viff/bedoza_triple.py	Wed Jul 21 09:40:53 2010 +0200
     2.2 +++ b/viff/bedoza_triple.py	Wed Jul 21 15:07:33 2010 +0200
     2.3 @@ -132,6 +132,8 @@
     2.4          self.random = random
     2.5  
     2.6      def generate_share(self, value):
     2.7 +        self.runtime.increment_pc()
     2.8 +        
     2.9          r = [self.Zp(self.random.randint(0, self.Zp.modulus - 1)) # TODO: Exclusve?
    2.10               for _ in range(self.runtime.num_players - 1)]
    2.11          if self.runtime.id == 1:
    2.12 @@ -251,7 +253,47 @@
    2.13          triples in memory at the same time. Is there a nice way to
    2.14          stream this, e.g. by using Python generators?
    2.15          """
    2.16 -        triples = self._generate_passive_triples(n)
    2.17 +
    2.18 +        self.runtime.increment_pc()
    2.19 +        
    2.20 +        gen = PartialShareGenerator(self.Zp, self.runtime, self.random, self.paillier)
    2.21 +
    2.22 +        def check(v, a, b, c):
    2.23 +            if v.value != 0:
    2.24 +                raise Exception("TripleTest failed - The two triples were inconsistent.")
    2.25 +            return (a, b, c)
    2.26 +        
    2.27 +        def compute_value(r, a, b, c, x, y, z):
    2.28 +            l = self.runtime._cmul(r, x, self.Zp)
    2.29 +            m = self.runtime._cmul(r, y, self.Zp)
    2.30 +            k = self.runtime._cmul(r*r, z, self.Zp)
    2.31 +            v = c - self.runtime._basic_multiplication(a, b, l, m, k)
    2.32 +            v = self.runtime.open(v)
    2.33 +            v.addCallback(check, a, b, c)
    2.34 +            return v
    2.35 +
    2.36 +        random_shares = []
    2.37 +        for _ in xrange(n):
    2.38 +             random_shares.append(gen.generate_share(self.random.randint(0, self.Zp.modulus - 1)))
    2.39 +
    2.40 +        random_shares = self._add_macs(random_shares)
    2.41 +
    2.42 +        results = [Deferred() for _ in xrange(n)]
    2.43 +        
    2.44 +        triples = self._generate_passive_triples(2 * n)
    2.45 +
    2.46 +        for inx in xrange(n):
    2.47 +            a = triples[inx]
    2.48 +            b = triples[inx + 2 * n]
    2.49 +            c = triples[inx + 4 * n]
    2.50 +            x = triples[inx + n]
    2.51 +            y = triples[inx + 3 * n]
    2.52 +            z = triples[inx + 5 * n]
    2.53 +            r = self.runtime.open(random_shares[inx])
    2.54 +            self.runtime.schedule_callback(r, compute_value, a, b, c, x, y, z)
    2.55 +            r.chainDeferred(results[inx])
    2.56 +        return results
    2.57 +          
    2.58          # TODO: Do some ZK stuff.
    2.59  
    2.60      def _generate_passive_triples(self, n):
    2.61 @@ -261,6 +303,9 @@
    2.62          
    2.63          Consistency is only guaranteed if all players follow the protool.
    2.64          """
    2.65 +
    2.66 +        self.runtime.increment_pc()
    2.67 +        
    2.68          gen = PartialShareGenerator(self.Zp, self.runtime, self.random, self.paillier)
    2.69          partial_shares = []
    2.70          for _ in xrange(2 * n):
     3.1 --- a/viff/simplearithmetic.py	Wed Jul 21 09:40:53 2010 +0200
     3.2 +++ b/viff/simplearithmetic.py	Wed Jul 21 15:07:33 2010 +0200
     3.3 @@ -143,7 +143,8 @@
     3.4          if cmul_result is  not None:
     3.5              return cmul_result
     3.6  
     3.7 -        def multiply((x, y, c, (d, e))):
     3.8 +        def multiply(ls):
     3.9 +            x, y, c, (d, e) = ls
    3.10              # [de]
    3.11              de = d * e
    3.12              # e[x]
    3.13 @@ -153,6 +154,7 @@
    3.14              # d[y] - [de]
    3.15              t3 = self._minus_public_right_without_share(t2, de, field)
    3.16              # d[y] - [de] + [c]
    3.17 +            z = self._plus((t3, c), field)
    3.18              t4 = self._plus((t3, c), field)
    3.19              # [z] = e[x] + d[y] - [de] + [c]
    3.20              z = self._plus((t1, t4), field)
     4.1 --- a/viff/test/test_bedoza_triple.py	Wed Jul 21 09:40:53 2010 +0200
     4.2 +++ b/viff/test/test_bedoza_triple.py	Wed Jul 21 15:07:33 2010 +0200
     4.3 @@ -269,11 +269,37 @@
     4.4          return share
     4.5  
     4.6  
     4.7 -class PassiveTripleTest(BeDOZaTestCase): 
     4.8 +class TripleTest(BeDOZaTestCase): 
     4.9      num_players = 3
    4.10  
    4.11 -    timeout = 10
    4.12 -    
    4.13 +    timeout = 120
    4.14 +
    4.15 +    @protocol
    4.16 +    def test_generate_triples_generates_correct_triples(self, runtime):
    4.17 +        p = 17
    4.18 +
    4.19 +        Zp = GF(p)
    4.20 +        
    4.21 +        random = Random(283883)        
    4.22 +        triple_generator = TripleGenerator(runtime, p, random)
    4.23 +
    4.24 +        triples = triple_generator.generate_triples(5)
    4.25 +
    4.26 +        def check((a, b, c)):
    4.27 +            self.assertEquals(c, a * b)
    4.28 +
    4.29 +        def open((a, b, c)):
    4.30 +            d1 = runtime.open(a)
    4.31 +            d2 = runtime.open(b)
    4.32 +            d3 = runtime.open(c)
    4.33 +            d = gatherResults([d1, d2, d3])
    4.34 +            runtime.schedule_callback(d, check)
    4.35 +            return d
    4.36 +
    4.37 +        for triple in triples:
    4.38 +            runtime.schedule_callback(triple, open)
    4.39 +        return gatherResults(triples)
    4.40 +
    4.41      @protocol
    4.42      def test_passive_triples_generates_correct_triples(self, runtime):
    4.43          p = 17
    4.44 @@ -283,7 +309,7 @@
    4.45          random = Random(283883)        
    4.46          triple_generator = TripleGenerator(runtime, p, random)
    4.47  
    4.48 -        triples = triple_generator._generate_passive_triples(5)
    4.49 +        triples = triple_generator._generate_passive_triples(1)
    4.50          def verify(triples):
    4.51              for inx in xrange(len(triples) // 3):
    4.52                  self.assertEquals(triples[10 + inx], triples[inx] * triples[5 + inx])
    4.53 @@ -294,11 +320,6 @@
    4.54          d.addCallback(verify)
    4.55          return d
    4.56  
    4.57 -
    4.58 -class TripleTest(BeDOZaTestCase): 
    4.59 -    num_players = 3
    4.60 -
    4.61 -    timeout = 4
    4.62      @protocol
    4.63      def test_add_macs_produces_correct_sharing(self, runtime):
    4.64          # TODO: Here we use the open method of the BeDOZa runtime in