viff

changeset 1511:90338602985d

BeDOZa: Implemented generate_passive_triple.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Tue, 20 Jul 2010 16:52:06 +0200
parents e1aacaf19a54
children bd9cdea06338
files viff/bedoza_triple.py viff/test/test_bedoza_triple.py
diffstat 2 files changed, 62 insertions(+), 4 deletions(-) [+]
line diff
     1.1 --- a/viff/bedoza_triple.py	Tue Jul 20 15:01:54 2010 +0200
     1.2 +++ b/viff/bedoza_triple.py	Tue Jul 20 16:52:06 2010 +0200
     1.3 @@ -123,6 +123,27 @@
     1.4          Share.__init__(self, runtime, value.field, partial_share_contents)
     1.5  
     1.6  
     1.7 +class PartialShareGenerator:
     1.8 +
     1.9 +    def __init__(self, Zp, runtime, random, paillier):
    1.10 +        self.paillier = paillier
    1.11 +        self.Zp = Zp
    1.12 +        self.runtime = runtime
    1.13 +        self.random = random
    1.14 +
    1.15 +    def generate_share(self, value):
    1.16 +        r = [self.Zp(self.random.randint(0, self.Zp.modulus - 1)) # TODO: Exclusve?
    1.17 +             for _ in range(self.runtime.num_players - 1)]
    1.18 +        if self.runtime.id == 1:
    1.19 +            share = value - sum(r)
    1.20 +        else:
    1.21 +            share = r[self.runtime.id - 2]
    1.22 +        enc_share = self.paillier.encrypt(share.value)
    1.23 +        enc_shares = _convolute(self.runtime, enc_share)
    1.24 +        def create_partial_share(enc_shares, share):
    1.25 +            return PartialShare(self.runtime, share, enc_shares)
    1.26 +        self.runtime.schedule_callback(enc_shares, create_partial_share, share)
    1.27 +        return enc_shares
    1.28  
    1.29  class ModifiedPaillier(object):
    1.30      """A slight modification of the Paillier cryptosystem.
    1.31 @@ -234,12 +255,23 @@
    1.32          # TODO: Do some ZK stuff.
    1.33  
    1.34      def _generate_passive_triples(self, n):
    1.35 -        """Generates and returns a set of n passive tuples.
    1.36 +        """Generates and returns a list of 3n shares corresponding to
    1.37 +        n passive tuples. The first n are the a's, then comes n b's
    1.38 +        followed by n c's.
    1.39          
    1.40 -        E.g. where consistency is only guaranteed if all players follow the
    1.41 -        protool.
    1.42 +        Consistency is only guaranteed if all players follow the protool.
    1.43          """
    1.44 -        pass
    1.45 +        gen = PartialShareGenerator(self.Zp, self.runtime, self.random, self.paillier)
    1.46 +        partial_shares = []
    1.47 +        for _ in xrange(2 * n):
    1.48 +             partial_shares.append(gen.generate_share(self.random.randint(0, self.Zp.modulus - 1)))
    1.49 +
    1.50 +
    1.51 +        partial_shares_c = self._full_mul(partial_shares[0:n], partial_shares[n:2*n], self.Zp)
    1.52 +
    1.53 +        full_shares = self._add_macs(partial_shares + partial_shares_c, self.Zp)
    1.54 +
    1.55 +        return full_shares
    1.56      
    1.57      def _add_macs(self, partial_shares, field):
    1.58          """Adds macs to the set of PartialBeDOZaShares.
     2.1 --- a/viff/test/test_bedoza_triple.py	Tue Jul 20 15:01:54 2010 +0200
     2.2 +++ b/viff/test/test_bedoza_triple.py	Tue Jul 20 16:52:06 2010 +0200
     2.3 @@ -269,6 +269,32 @@
     2.4          return share
     2.5  
     2.6  
     2.7 +class PassiveTripleTest(BeDOZaTestCase): 
     2.8 +    num_players = 3
     2.9 +
    2.10 +    timeout = 10
    2.11 +    
    2.12 +    @protocol
    2.13 +    def test_passive_triples_generates_correct_triples(self, runtime):
    2.14 +        p = 17
    2.15 +
    2.16 +        Zp = GF(p)
    2.17 +        
    2.18 +        random = Random(283883)        
    2.19 +        triple_generator = TripleGenerator(runtime, p, random)
    2.20 +
    2.21 +        triples = triple_generator._generate_passive_triples(5)
    2.22 +        def verify(triples):
    2.23 +            for inx in xrange(len(triples) // 3):
    2.24 +                self.assertEquals(triples[10 + inx], triples[inx] * triples[5 + inx])
    2.25 +        opened_shares = []
    2.26 +        for s in triples:
    2.27 +            opened_shares.append(runtime.open(s))
    2.28 +        d = gather_shares(opened_shares)
    2.29 +        d.addCallback(verify)
    2.30 +        return d
    2.31 +
    2.32 +
    2.33  class TripleTest(BeDOZaTestCase): 
    2.34      num_players = 3
    2.35