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 wrap: on
line diff
--- a/viff/test/bedoza/test_bedoza_runtime.py	Thu Sep 30 10:28:09 2010 +0200
+++ b/viff/test/bedoza/test_bedoza_runtime.py	Thu Sep 30 10:59:37 2010 +0200
@@ -23,6 +23,8 @@
 from twisted.internet.defer import gatherResults, DeferredList
 
 from viff.test.util import RuntimeTestCase, protocol
+from viff.test.bedoza.util import TestShareGenerator
+
 from viff.runtime import gather_shares, Share
 from viff.config import generate_configs
 from viff.bedoza.bedoza import BeDOZaRuntime
@@ -32,7 +34,6 @@
 from viff.field import FieldElement, GF
 from viff.util import rand
 from viff.bedoza.modified_paillier import ModifiedPaillier
-from viff.bedoza.share_generators import ShareGenerator
 from viff.bedoza.bedoza_triple import TripleGenerator
 from viff.bedoza.zero_knowledge import ZKProof
 
@@ -75,8 +76,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x2 = gen.generate_share(8)
         y2 = gen.generate_share(9)
@@ -96,8 +97,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x2 = gen.generate_share(2)
         y2 = gen.generate_share(9)
@@ -119,8 +120,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x2 = gen.generate_share(8)
         z2 = x2 + y1
@@ -141,8 +142,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x2 = gen.generate_share(8)
         z2 = y1 + x2
@@ -174,8 +175,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x2 = gen.generate_share(9)
         y2 = gen.generate_share(8)
@@ -195,8 +196,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x2 = gen.generate_share(9)
         y2 = gen.generate_share(8)
@@ -218,8 +219,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x2 = gen.generate_share(8)
         z2 = x2 - y
@@ -240,8 +241,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x2 = gen.generate_share(5)
         z2 = y - x2
@@ -263,8 +264,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x2 = gen.generate_share(x1)
 
@@ -287,8 +288,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x2 = gen.generate_share(x1)
 
@@ -333,8 +334,8 @@
             share_random = Random(random.getrandbits(128))
         
             paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-            gen = ShareGenerator(self.Zp, runtime, share_random,
-                                 paillier, self.u_bound, alpha)
+            gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                     paillier, self.u_bound, alpha)
         
             x2 = gen.generate_share(x1)
             y2 = gen.generate_share(y1)
@@ -371,8 +372,8 @@
             random = Random(3423993)
             share_random = Random(random.getrandbits(128))
             paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-            gen = ShareGenerator(self.Zp, runtime, share_random,
-                                 paillier, self.u_bound, alpha)
+            gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                     paillier, self.u_bound, alpha)
         
             x2 = gen.generate_share(x1)
             y2 = gen.generate_share(y1)
@@ -400,8 +401,8 @@
             share_random = Random(random.getrandbits(128))
         
             paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-            gen = ShareGenerator(self.Zp, runtime, share_random,
-                                 paillier, self.u_bound, alpha)
+            gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                     paillier, self.u_bound, alpha)
         
             x2 = gen.generate_share(x1)
             y2 = gen.generate_share(y1)
@@ -434,8 +435,8 @@
             share_random = Random(random.getrandbits(128))
         
             paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-            gen = ShareGenerator(self.Zp, runtime, share_random,
-                                 paillier, self.u_bound, alpha)
+            gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                     paillier, self.u_bound, alpha)
         
             x2 = gen.generate_share(x1)
             y2 = gen.generate_share(y1)
@@ -465,8 +466,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x = gen.generate_share(6)
         y = gen.generate_share(6)
@@ -486,8 +487,8 @@
         share_random = Random(random.getrandbits(128))
         
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))          
-        gen = ShareGenerator(self.Zp, runtime, share_random,
-                             paillier, self.u_bound, self.alpha)
+        gen = TestShareGenerator(self.Zp, runtime, share_random,
+                                 paillier, self.u_bound, self.alpha)
         
         x = gen.generate_share(6)
         y = gen.generate_share(6)
--- a/viff/test/bedoza/test_bedoza_triple.py	Thu Sep 30 10:28:09 2010 +0200
+++ b/viff/test/bedoza/test_bedoza_triple.py	Thu Sep 30 10:59:37 2010 +0200
@@ -29,19 +29,20 @@
 from viff.constants import TEXT
 from viff.runtime import gather_shares, Share
 from viff.config import generate_configs
-
-from viff.test.bedoza.util import BeDOZaTestCase, skip_if_missing_packages
+from viff.field import FieldElement, GF
+from viff.config import generate_configs
 
 from viff.bedoza.bedoza import BeDOZaShare
 from viff.bedoza.keylist import BeDOZaKeyList
 from viff.bedoza.bedoza_triple import TripleGenerator, ModifiedPaillier
-from viff.bedoza.share_generators import PartialShareGenerator, ShareGenerator
 from viff.bedoza.shares import PartialShare, PartialShareContents
 from viff.bedoza.util import _send, _convolute, _convolute_gf_elm
 from viff.bedoza.add_macs import add_macs
+from viff.bedoza.share_generators import ShareGenerator, PartialShareGenerator
 
-from viff.field import FieldElement, GF
-from viff.config import generate_configs
+from viff.test.bedoza.util import BeDOZaTestCase, skip_if_missing_packages
+from viff.test.bedoza.util import TestShareGenerator
+
 
 # Ok to use non-secure random generator in tests.
 #from viff.util import rand
@@ -301,7 +302,7 @@
         paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))
 
         share_random = Random(random.getrandbits(128))
-        gen = ShareGenerator(Zp, runtime, share_random, paillier, u_bound, alpha)
+        gen = TestShareGenerator(Zp, runtime, share_random, paillier, u_bound, alpha)
         shares = gen.generate_random_shares(7)
         expected_result = [9,16,7,12,3,5,6]
         results = []
--- a/viff/test/bedoza/util.py	Thu Sep 30 10:28:09 2010 +0200
+++ b/viff/test/bedoza/util.py	Thu Sep 30 10:59:37 2010 +0200
@@ -1,8 +1,13 @@
+
+from viff.config import generate_configs
 
 from viff.test.util import RuntimeTestCase
-from viff.config import generate_configs
 
 from viff.bedoza.bedoza import BeDOZaRuntime
+from viff.bedoza.util import _convolute
+from viff.bedoza.add_macs import add_macs
+from viff.bedoza.shares import PartialShare, PartialShareContents
+
 
 # HACK: The paillier keys that are available as standard in VIFF tests
 # are not suited for use with pypaillier. Hence, we use NaClPaillier
@@ -64,3 +69,76 @@
     if missing:
         for test_case in test_cases:
             test_case.skip =  "Skipped due to missing packages: %s" % missing
+
+
+class TestPartialShareGenerator(object):
+    """Class for quick generation of partial shares with no
+    security. Suited only for use when partial shares are needed as
+    input to a test.
+    """
+
+    def __init__(self, Zp, runtime, random, paillier):
+        self.paillier = paillier
+        self.Zp = Zp
+        self.runtime = runtime
+        self.random = random
+
+    def generate_share(self, value):
+        self.runtime.increment_pc()
+        
+        # TODO: Exclusive?
+        r = [self.Zp(self.random.randint(0, self.Zp.modulus - 1))
+             for _ in range(self.runtime.num_players - 1)]
+        if self.runtime.id == 1:
+            share = value - sum(r)
+        else:
+            share = r[self.runtime.id - 2]
+        enc_share = self.paillier.encrypt(share.value)
+        enc_shares = _convolute(self.runtime, enc_share)
+        def create_partial_share(enc_shares, share):
+            return PartialShare(self.runtime, self.Zp, share, enc_shares)
+        self.runtime.schedule_callback(enc_shares, create_partial_share, share)
+        return enc_shares
+
+    def generate_random_shares(self, n):
+        self.runtime.increment_pc()
+        N_squared_list = [self.runtime.players[player_id].pubkey['n_square']
+                          for player_id in self.runtime.players]
+        shares = [PartialShare(self.runtime, self.Zp) for _ in xrange(n)]
+        for inx in xrange(n):
+            r = self.random.randint(0, self.Zp.modulus - 1)
+            ri = self.Zp(r)
+            enc_share = self.paillier.encrypt(ri.value)
+            enc_shares = _convolute(self.runtime, enc_share)
+            def create_partial_share(enc_shares, ri, s, N_squared_list):
+                s.callback(PartialShareContents(ri, enc_shares,
+                                                N_squared_list))
+            self.runtime.schedule_callback(enc_shares,
+                                           create_partial_share,
+                                           ri,
+                                           shares[inx],
+                                           N_squared_list)
+        return shares
+
+class TestShareGenerator(TestPartialShareGenerator):
+    """Class for quick generation of shares with no security. Suited
+    only for use when shares are needed as input to a test.
+    """
+
+    def __init__(self, Zp, runtime, random, paillier, u_bound, alpha):
+        self.u_bound = u_bound
+        self.alpha = alpha
+        TestPartialShareGenerator.__init__(self, Zp, runtime, random, paillier)
+
+    def generate_share(self, value):
+        self.runtime.increment_pc()
+        partial_share = TestPartialShareGenerator.generate_share(self, value)
+        full_share = add_macs(self.runtime, self.Zp, self.u_bound, self.alpha,
+                             self.random, self.paillier, [partial_share])
+        return full_share[0]
+    
+    def generate_random_shares(self, n):
+        self.runtime.increment_pc()
+        partial_shares = TestPartialShareGenerator.generate_random_shares(self, n)
+        return add_macs(self.runtime, self.Zp, self.u_bound, self.alpha,
+                        self.random, self.paillier, partial_shares)