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 wrap: on
line diff
--- a/viff/bedoza_triple.py	Thu Jul 22 15:53:25 2010 +0200
+++ b/viff/bedoza_triple.py	Fri Jul 23 11:09:42 2010 +0200
@@ -170,12 +170,23 @@
 
 class ShareGenerator(PartialShareGenerator):
 
-    def __init__(self, Zp, runtime, random, paillier):
+    def __init__(self, Zp, runtime, random, paillier, u_bound, alpha):
+        self.u_bound = u_bound
+        self.alpha = alpha
         PartialShareGenerator.__init__(self, Zp, runtime, random, paillier)
 
+    def generate_share(self, value):
+        self.runtime.increment_pc()
+        partial_share = PartialShareGenerator.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 = PartialShareGenerator.generate_random_shares(self, n)
-
+        return add_macs(self.runtime, self.Zp, self.u_bound, self.alpha,
+                        self.random, self.paillier, partial_shares)
 
 class ModifiedPaillier(object):
     """A slight modification of the Paillier cryptosystem.
--- a/viff/test/test_bedoza_triple.py	Thu Jul 22 15:53:25 2010 +0200
+++ b/viff/test/test_bedoza_triple.py	Fri Jul 23 11:09:42 2010 +0200
@@ -31,7 +31,7 @@
 from viff.config import generate_configs
 from viff.bedoza import BeDOZaRuntime, BeDOZaShare, BeDOZaKeyList
 
-from viff.bedoza_triple import TripleGenerator, PartialShare, PartialShareContents, ModifiedPaillier, PartialShareGenerator
+from viff.bedoza_triple import TripleGenerator, PartialShare, PartialShareContents, ModifiedPaillier, PartialShareGenerator, ShareGenerator
 from viff.bedoza_triple import _send, _convolute, _convolute_gf_elm, add_macs
 
 from viff.field import FieldElement, GF
@@ -280,6 +280,53 @@
             
         return shares
 
+class ShareGeneratorTest(BeDOZaTestCase):
+    num_players = 3
+
+    @protocol
+    def test_encrypted_real_share_open_correctly(self, runtime):
+        random = Random(3423993)
+        modulus = 17
+        Zp = GF(modulus)
+        bits_in_p = 5
+        u_bound = 2**(4 * bits_in_p)
+        alpha = 15
+        
+        paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))
+
+        share_random = Random(random.getrandbits(128))
+        gen = ShareGenerator(Zp, runtime, share_random, paillier, u_bound, alpha)
+        share = gen.generate_share(7)
+        def check(v):
+            self.assertEquals(7, v)
+        r = runtime.open(share)
+        runtime.schedule_callback(r, check)
+        return r
+
+    @protocol
+    def test_encrypted_random_real_shares_open_correctly(self, runtime):
+        random = Random(3423993)
+        modulus = 17
+        Zp = GF(modulus)
+        bits_in_p = 5
+        u_bound = 2**(4 * bits_in_p)
+        alpha = 15
+        
+        paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))
+
+        share_random = Random(random.getrandbits(128))
+        gen = ShareGenerator(Zp, runtime, share_random, paillier, u_bound, alpha)
+        shares = gen.generate_random_shares(7)
+        expected_result = [9,16,7,12,3,5,6]
+        results = []
+        for inx, share in enumerate(shares):
+            def check(v, expected_result):
+                self.assertEquals(expected_result, v)
+            r = runtime.open(share)
+            results.append(r)
+            runtime.schedule_callback(r, check, expected_result[inx])
+        return gather_shares(results)
+
 class AddMacsTest(BeDOZaTestCase): 
     num_players = 3