viff

changeset 1564:1b3c7c485cd0

BeDOZa: Use test share generators separate from real generators.
author Thomas P Jakobsen <tpj@cs.au.dk>
date Thu, 30 Sep 2010 10:59:37 +0200
parents cb568167b140
children 1df43db7d7cf
files viff/test/bedoza/test_bedoza_runtime.py viff/test/bedoza/test_bedoza_triple.py viff/test/bedoza/util.py
diffstat 3 files changed, 120 insertions(+), 40 deletions(-) [+]
line diff
     1.1 --- a/viff/test/bedoza/test_bedoza_runtime.py	Thu Sep 30 10:28:09 2010 +0200
     1.2 +++ b/viff/test/bedoza/test_bedoza_runtime.py	Thu Sep 30 10:59:37 2010 +0200
     1.3 @@ -23,6 +23,8 @@
     1.4  from twisted.internet.defer import gatherResults, DeferredList
     1.5  
     1.6  from viff.test.util import RuntimeTestCase, protocol
     1.7 +from viff.test.bedoza.util import TestShareGenerator
     1.8 +
     1.9  from viff.runtime import gather_shares, Share
    1.10  from viff.config import generate_configs
    1.11  from viff.bedoza.bedoza import BeDOZaRuntime
    1.12 @@ -32,7 +34,6 @@
    1.13  from viff.field import FieldElement, GF
    1.14  from viff.util import rand
    1.15  from viff.bedoza.modified_paillier import ModifiedPaillier
    1.16 -from viff.bedoza.share_generators import ShareGenerator
    1.17  from viff.bedoza.bedoza_triple import TripleGenerator
    1.18  from viff.bedoza.zero_knowledge import ZKProof
    1.19  
    1.20 @@ -75,8 +76,8 @@
    1.21          share_random = Random(random.getrandbits(128))
    1.22          
    1.23          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
    1.24 -        gen = ShareGenerator(self.Zp, runtime, share_random,
    1.25 -                             paillier, self.u_bound, self.alpha)
    1.26 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
    1.27 +                                 paillier, self.u_bound, self.alpha)
    1.28          
    1.29          x2 = gen.generate_share(8)
    1.30          y2 = gen.generate_share(9)
    1.31 @@ -96,8 +97,8 @@
    1.32          share_random = Random(random.getrandbits(128))
    1.33          
    1.34          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
    1.35 -        gen = ShareGenerator(self.Zp, runtime, share_random,
    1.36 -                             paillier, self.u_bound, self.alpha)
    1.37 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
    1.38 +                                 paillier, self.u_bound, self.alpha)
    1.39          
    1.40          x2 = gen.generate_share(2)
    1.41          y2 = gen.generate_share(9)
    1.42 @@ -119,8 +120,8 @@
    1.43          share_random = Random(random.getrandbits(128))
    1.44          
    1.45          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
    1.46 -        gen = ShareGenerator(self.Zp, runtime, share_random,
    1.47 -                             paillier, self.u_bound, self.alpha)
    1.48 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
    1.49 +                                 paillier, self.u_bound, self.alpha)
    1.50          
    1.51          x2 = gen.generate_share(8)
    1.52          z2 = x2 + y1
    1.53 @@ -141,8 +142,8 @@
    1.54          share_random = Random(random.getrandbits(128))
    1.55          
    1.56          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
    1.57 -        gen = ShareGenerator(self.Zp, runtime, share_random,
    1.58 -                             paillier, self.u_bound, self.alpha)
    1.59 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
    1.60 +                                 paillier, self.u_bound, self.alpha)
    1.61          
    1.62          x2 = gen.generate_share(8)
    1.63          z2 = y1 + x2
    1.64 @@ -174,8 +175,8 @@
    1.65          share_random = Random(random.getrandbits(128))
    1.66          
    1.67          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
    1.68 -        gen = ShareGenerator(self.Zp, runtime, share_random,
    1.69 -                             paillier, self.u_bound, self.alpha)
    1.70 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
    1.71 +                                 paillier, self.u_bound, self.alpha)
    1.72          
    1.73          x2 = gen.generate_share(9)
    1.74          y2 = gen.generate_share(8)
    1.75 @@ -195,8 +196,8 @@
    1.76          share_random = Random(random.getrandbits(128))
    1.77          
    1.78          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
    1.79 -        gen = ShareGenerator(self.Zp, runtime, share_random,
    1.80 -                             paillier, self.u_bound, self.alpha)
    1.81 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
    1.82 +                                 paillier, self.u_bound, self.alpha)
    1.83          
    1.84          x2 = gen.generate_share(9)
    1.85          y2 = gen.generate_share(8)
    1.86 @@ -218,8 +219,8 @@
    1.87          share_random = Random(random.getrandbits(128))
    1.88          
    1.89          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
    1.90 -        gen = ShareGenerator(self.Zp, runtime, share_random,
    1.91 -                             paillier, self.u_bound, self.alpha)
    1.92 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
    1.93 +                                 paillier, self.u_bound, self.alpha)
    1.94          
    1.95          x2 = gen.generate_share(8)
    1.96          z2 = x2 - y
    1.97 @@ -240,8 +241,8 @@
    1.98          share_random = Random(random.getrandbits(128))
    1.99          
   1.100          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
   1.101 -        gen = ShareGenerator(self.Zp, runtime, share_random,
   1.102 -                             paillier, self.u_bound, self.alpha)
   1.103 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
   1.104 +                                 paillier, self.u_bound, self.alpha)
   1.105          
   1.106          x2 = gen.generate_share(5)
   1.107          z2 = y - x2
   1.108 @@ -263,8 +264,8 @@
   1.109          share_random = Random(random.getrandbits(128))
   1.110          
   1.111          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
   1.112 -        gen = ShareGenerator(self.Zp, runtime, share_random,
   1.113 -                             paillier, self.u_bound, self.alpha)
   1.114 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
   1.115 +                                 paillier, self.u_bound, self.alpha)
   1.116          
   1.117          x2 = gen.generate_share(x1)
   1.118  
   1.119 @@ -287,8 +288,8 @@
   1.120          share_random = Random(random.getrandbits(128))
   1.121          
   1.122          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
   1.123 -        gen = ShareGenerator(self.Zp, runtime, share_random,
   1.124 -                             paillier, self.u_bound, self.alpha)
   1.125 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
   1.126 +                                 paillier, self.u_bound, self.alpha)
   1.127          
   1.128          x2 = gen.generate_share(x1)
   1.129  
   1.130 @@ -333,8 +334,8 @@
   1.131              share_random = Random(random.getrandbits(128))
   1.132          
   1.133              paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
   1.134 -            gen = ShareGenerator(self.Zp, runtime, share_random,
   1.135 -                                 paillier, self.u_bound, alpha)
   1.136 +            gen = TestShareGenerator(self.Zp, runtime, share_random,
   1.137 +                                     paillier, self.u_bound, alpha)
   1.138          
   1.139              x2 = gen.generate_share(x1)
   1.140              y2 = gen.generate_share(y1)
   1.141 @@ -371,8 +372,8 @@
   1.142              random = Random(3423993)
   1.143              share_random = Random(random.getrandbits(128))
   1.144              paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
   1.145 -            gen = ShareGenerator(self.Zp, runtime, share_random,
   1.146 -                                 paillier, self.u_bound, alpha)
   1.147 +            gen = TestShareGenerator(self.Zp, runtime, share_random,
   1.148 +                                     paillier, self.u_bound, alpha)
   1.149          
   1.150              x2 = gen.generate_share(x1)
   1.151              y2 = gen.generate_share(y1)
   1.152 @@ -400,8 +401,8 @@
   1.153              share_random = Random(random.getrandbits(128))
   1.154          
   1.155              paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
   1.156 -            gen = ShareGenerator(self.Zp, runtime, share_random,
   1.157 -                                 paillier, self.u_bound, alpha)
   1.158 +            gen = TestShareGenerator(self.Zp, runtime, share_random,
   1.159 +                                     paillier, self.u_bound, alpha)
   1.160          
   1.161              x2 = gen.generate_share(x1)
   1.162              y2 = gen.generate_share(y1)
   1.163 @@ -434,8 +435,8 @@
   1.164              share_random = Random(random.getrandbits(128))
   1.165          
   1.166              paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
   1.167 -            gen = ShareGenerator(self.Zp, runtime, share_random,
   1.168 -                                 paillier, self.u_bound, alpha)
   1.169 +            gen = TestShareGenerator(self.Zp, runtime, share_random,
   1.170 +                                     paillier, self.u_bound, alpha)
   1.171          
   1.172              x2 = gen.generate_share(x1)
   1.173              y2 = gen.generate_share(y1)
   1.174 @@ -465,8 +466,8 @@
   1.175          share_random = Random(random.getrandbits(128))
   1.176          
   1.177          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
   1.178 -        gen = ShareGenerator(self.Zp, runtime, share_random,
   1.179 -                             paillier, self.u_bound, self.alpha)
   1.180 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
   1.181 +                                 paillier, self.u_bound, self.alpha)
   1.182          
   1.183          x = gen.generate_share(6)
   1.184          y = gen.generate_share(6)
   1.185 @@ -486,8 +487,8 @@
   1.186          share_random = Random(random.getrandbits(128))
   1.187          
   1.188          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
   1.189 -        gen = ShareGenerator(self.Zp, runtime, share_random,
   1.190 -                             paillier, self.u_bound, self.alpha)
   1.191 +        gen = TestShareGenerator(self.Zp, runtime, share_random,
   1.192 +                                 paillier, self.u_bound, self.alpha)
   1.193          
   1.194          x = gen.generate_share(6)
   1.195          y = gen.generate_share(6)
     2.1 --- a/viff/test/bedoza/test_bedoza_triple.py	Thu Sep 30 10:28:09 2010 +0200
     2.2 +++ b/viff/test/bedoza/test_bedoza_triple.py	Thu Sep 30 10:59:37 2010 +0200
     2.3 @@ -29,19 +29,20 @@
     2.4  from viff.constants import TEXT
     2.5  from viff.runtime import gather_shares, Share
     2.6  from viff.config import generate_configs
     2.7 -
     2.8 -from viff.test.bedoza.util import BeDOZaTestCase, skip_if_missing_packages
     2.9 +from viff.field import FieldElement, GF
    2.10 +from viff.config import generate_configs
    2.11  
    2.12  from viff.bedoza.bedoza import BeDOZaShare
    2.13  from viff.bedoza.keylist import BeDOZaKeyList
    2.14  from viff.bedoza.bedoza_triple import TripleGenerator, ModifiedPaillier
    2.15 -from viff.bedoza.share_generators import PartialShareGenerator, ShareGenerator
    2.16  from viff.bedoza.shares import PartialShare, PartialShareContents
    2.17  from viff.bedoza.util import _send, _convolute, _convolute_gf_elm
    2.18  from viff.bedoza.add_macs import add_macs
    2.19 +from viff.bedoza.share_generators import ShareGenerator, PartialShareGenerator
    2.20  
    2.21 -from viff.field import FieldElement, GF
    2.22 -from viff.config import generate_configs
    2.23 +from viff.test.bedoza.util import BeDOZaTestCase, skip_if_missing_packages
    2.24 +from viff.test.bedoza.util import TestShareGenerator
    2.25 +
    2.26  
    2.27  # Ok to use non-secure random generator in tests.
    2.28  #from viff.util import rand
    2.29 @@ -301,7 +302,7 @@
    2.30          paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))
    2.31  
    2.32          share_random = Random(random.getrandbits(128))
    2.33 -        gen = ShareGenerator(Zp, runtime, share_random, paillier, u_bound, alpha)
    2.34 +        gen = TestShareGenerator(Zp, runtime, share_random, paillier, u_bound, alpha)
    2.35          shares = gen.generate_random_shares(7)
    2.36          expected_result = [9,16,7,12,3,5,6]
    2.37          results = []
     3.1 --- a/viff/test/bedoza/util.py	Thu Sep 30 10:28:09 2010 +0200
     3.2 +++ b/viff/test/bedoza/util.py	Thu Sep 30 10:59:37 2010 +0200
     3.3 @@ -1,8 +1,13 @@
     3.4 +
     3.5 +from viff.config import generate_configs
     3.6  
     3.7  from viff.test.util import RuntimeTestCase
     3.8 -from viff.config import generate_configs
     3.9  
    3.10  from viff.bedoza.bedoza import BeDOZaRuntime
    3.11 +from viff.bedoza.util import _convolute
    3.12 +from viff.bedoza.add_macs import add_macs
    3.13 +from viff.bedoza.shares import PartialShare, PartialShareContents
    3.14 +
    3.15  
    3.16  # HACK: The paillier keys that are available as standard in VIFF tests
    3.17  # are not suited for use with pypaillier. Hence, we use NaClPaillier
    3.18 @@ -64,3 +69,76 @@
    3.19      if missing:
    3.20          for test_case in test_cases:
    3.21              test_case.skip =  "Skipped due to missing packages: %s" % missing
    3.22 +
    3.23 +
    3.24 +class TestPartialShareGenerator(object):
    3.25 +    """Class for quick generation of partial shares with no
    3.26 +    security. Suited only for use when partial shares are needed as
    3.27 +    input to a test.
    3.28 +    """
    3.29 +
    3.30 +    def __init__(self, Zp, runtime, random, paillier):
    3.31 +        self.paillier = paillier
    3.32 +        self.Zp = Zp
    3.33 +        self.runtime = runtime
    3.34 +        self.random = random
    3.35 +
    3.36 +    def generate_share(self, value):
    3.37 +        self.runtime.increment_pc()
    3.38 +        
    3.39 +        # TODO: Exclusive?
    3.40 +        r = [self.Zp(self.random.randint(0, self.Zp.modulus - 1))
    3.41 +             for _ in range(self.runtime.num_players - 1)]
    3.42 +        if self.runtime.id == 1:
    3.43 +            share = value - sum(r)
    3.44 +        else:
    3.45 +            share = r[self.runtime.id - 2]
    3.46 +        enc_share = self.paillier.encrypt(share.value)
    3.47 +        enc_shares = _convolute(self.runtime, enc_share)
    3.48 +        def create_partial_share(enc_shares, share):
    3.49 +            return PartialShare(self.runtime, self.Zp, share, enc_shares)
    3.50 +        self.runtime.schedule_callback(enc_shares, create_partial_share, share)
    3.51 +        return enc_shares
    3.52 +
    3.53 +    def generate_random_shares(self, n):
    3.54 +        self.runtime.increment_pc()
    3.55 +        N_squared_list = [self.runtime.players[player_id].pubkey['n_square']
    3.56 +                          for player_id in self.runtime.players]
    3.57 +        shares = [PartialShare(self.runtime, self.Zp) for _ in xrange(n)]
    3.58 +        for inx in xrange(n):
    3.59 +            r = self.random.randint(0, self.Zp.modulus - 1)
    3.60 +            ri = self.Zp(r)
    3.61 +            enc_share = self.paillier.encrypt(ri.value)
    3.62 +            enc_shares = _convolute(self.runtime, enc_share)
    3.63 +            def create_partial_share(enc_shares, ri, s, N_squared_list):
    3.64 +                s.callback(PartialShareContents(ri, enc_shares,
    3.65 +                                                N_squared_list))
    3.66 +            self.runtime.schedule_callback(enc_shares,
    3.67 +                                           create_partial_share,
    3.68 +                                           ri,
    3.69 +                                           shares[inx],
    3.70 +                                           N_squared_list)
    3.71 +        return shares
    3.72 +
    3.73 +class TestShareGenerator(TestPartialShareGenerator):
    3.74 +    """Class for quick generation of shares with no security. Suited
    3.75 +    only for use when shares are needed as input to a test.
    3.76 +    """
    3.77 +
    3.78 +    def __init__(self, Zp, runtime, random, paillier, u_bound, alpha):
    3.79 +        self.u_bound = u_bound
    3.80 +        self.alpha = alpha
    3.81 +        TestPartialShareGenerator.__init__(self, Zp, runtime, random, paillier)
    3.82 +
    3.83 +    def generate_share(self, value):
    3.84 +        self.runtime.increment_pc()
    3.85 +        partial_share = TestPartialShareGenerator.generate_share(self, value)
    3.86 +        full_share = add_macs(self.runtime, self.Zp, self.u_bound, self.alpha,
    3.87 +                             self.random, self.paillier, [partial_share])
    3.88 +        return full_share[0]
    3.89 +    
    3.90 +    def generate_random_shares(self, n):
    3.91 +        self.runtime.increment_pc()
    3.92 +        partial_shares = TestPartialShareGenerator.generate_random_shares(self, n)
    3.93 +        return add_macs(self.runtime, self.Zp, self.u_bound, self.alpha,
    3.94 +                        self.random, self.paillier, partial_shares)