viff

changeset 1518:4d229908e78f

BeDOZa: Implemented ShareGenerator.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Fri, 23 Jul 2010 11:09:42 +0200
parents 054a27514891
children d7840af5cbf8
files viff/bedoza_triple.py viff/test/test_bedoza_triple.py
diffstat 2 files changed, 61 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/viff/bedoza_triple.py	Thu Jul 22 15:53:25 2010 +0200
     1.2 +++ b/viff/bedoza_triple.py	Fri Jul 23 11:09:42 2010 +0200
     1.3 @@ -170,12 +170,23 @@
     1.4  
     1.5  class ShareGenerator(PartialShareGenerator):
     1.6  
     1.7 -    def __init__(self, Zp, runtime, random, paillier):
     1.8 +    def __init__(self, Zp, runtime, random, paillier, u_bound, alpha):
     1.9 +        self.u_bound = u_bound
    1.10 +        self.alpha = alpha
    1.11          PartialShareGenerator.__init__(self, Zp, runtime, random, paillier)
    1.12  
    1.13 +    def generate_share(self, value):
    1.14 +        self.runtime.increment_pc()
    1.15 +        partial_share = PartialShareGenerator.generate_share(self, value)
    1.16 +        full_share = add_macs(self.runtime, self.Zp, self.u_bound, self.alpha,
    1.17 +                             self.random, self.paillier, [partial_share])
    1.18 +        return full_share[0]
    1.19 +    
    1.20      def generate_random_shares(self, n):
    1.21 +        self.runtime.increment_pc()
    1.22          partial_shares = PartialShareGenerator.generate_random_shares(self, n)
    1.23 -
    1.24 +        return add_macs(self.runtime, self.Zp, self.u_bound, self.alpha,
    1.25 +                        self.random, self.paillier, partial_shares)
    1.26  
    1.27  class ModifiedPaillier(object):
    1.28      """A slight modification of the Paillier cryptosystem.
     2.1 --- a/viff/test/test_bedoza_triple.py	Thu Jul 22 15:53:25 2010 +0200
     2.2 +++ b/viff/test/test_bedoza_triple.py	Fri Jul 23 11:09:42 2010 +0200
     2.3 @@ -31,7 +31,7 @@
     2.4  from viff.config import generate_configs
     2.5  from viff.bedoza import BeDOZaRuntime, BeDOZaShare, BeDOZaKeyList
     2.6  
     2.7 -from viff.bedoza_triple import TripleGenerator, PartialShare, PartialShareContents, ModifiedPaillier, PartialShareGenerator
     2.8 +from viff.bedoza_triple import TripleGenerator, PartialShare, PartialShareContents, ModifiedPaillier, PartialShareGenerator, ShareGenerator
     2.9  from viff.bedoza_triple import _send, _convolute, _convolute_gf_elm, add_macs
    2.10  
    2.11  from viff.field import FieldElement, GF
    2.12 @@ -280,6 +280,53 @@
    2.13              
    2.14          return shares
    2.15  
    2.16 +class ShareGeneratorTest(BeDOZaTestCase):
    2.17 +    num_players = 3
    2.18 +
    2.19 +    @protocol
    2.20 +    def test_encrypted_real_share_open_correctly(self, runtime):
    2.21 +        random = Random(3423993)
    2.22 +        modulus = 17
    2.23 +        Zp = GF(modulus)
    2.24 +        bits_in_p = 5
    2.25 +        u_bound = 2**(4 * bits_in_p)
    2.26 +        alpha = 15
    2.27 +        
    2.28 +        paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))
    2.29 +
    2.30 +        share_random = Random(random.getrandbits(128))
    2.31 +        gen = ShareGenerator(Zp, runtime, share_random, paillier, u_bound, alpha)
    2.32 +        share = gen.generate_share(7)
    2.33 +        def check(v):
    2.34 +            self.assertEquals(7, v)
    2.35 +        r = runtime.open(share)
    2.36 +        runtime.schedule_callback(r, check)
    2.37 +        return r
    2.38 +
    2.39 +    @protocol
    2.40 +    def test_encrypted_random_real_shares_open_correctly(self, runtime):
    2.41 +        random = Random(3423993)
    2.42 +        modulus = 17
    2.43 +        Zp = GF(modulus)
    2.44 +        bits_in_p = 5
    2.45 +        u_bound = 2**(4 * bits_in_p)
    2.46 +        alpha = 15
    2.47 +        
    2.48 +        paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))
    2.49 +
    2.50 +        share_random = Random(random.getrandbits(128))
    2.51 +        gen = ShareGenerator(Zp, runtime, share_random, paillier, u_bound, alpha)
    2.52 +        shares = gen.generate_random_shares(7)
    2.53 +        expected_result = [9,16,7,12,3,5,6]
    2.54 +        results = []
    2.55 +        for inx, share in enumerate(shares):
    2.56 +            def check(v, expected_result):
    2.57 +                self.assertEquals(expected_result, v)
    2.58 +            r = runtime.open(share)
    2.59 +            results.append(r)
    2.60 +            runtime.schedule_callback(r, check, expected_result[inx])
    2.61 +        return gather_shares(results)
    2.62 +
    2.63  class AddMacsTest(BeDOZaTestCase): 
    2.64      num_players = 3
    2.65