viff

changeset 1566:4ec457693fe8

BeDOZa: Added tests of test setup. The tests for the share generators are currently just a copies of the real share generator tests. But as the real share generators and their tests will change into their final shapes, the tests added here will ensure that shares generated for test purposes have the right properties.
author Thomas P Jakobsen <tpj@cs.au.dk>
date Thu, 30 Sep 2010 11:16:19 +0200
parents 1df43db7d7cf
children 3bf0533ed32c
files viff/test/bedoza/test_test_setup.py
diffstat 1 files changed, 169 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/viff/test/bedoza/test_test_setup.py	Thu Sep 30 11:16:19 2010 +0200
     1.3 @@ -0,0 +1,169 @@
     1.4 +# Copyright 2010 VIFF Development Team.
     1.5 +#
     1.6 +# This file is part of VIFF, the Virtual Ideal Functionality Framework.
     1.7 +#
     1.8 +# VIFF is free software: you can redistribute it and/or modify it
     1.9 +# under the terms of the GNU Lesser General Public License (LGPL) as
    1.10 +# published by the Free Software Foundation, either version 3 of the
    1.11 +# License, or (at your option) any later version.
    1.12 +#
    1.13 +# VIFF is distributed in the hope that it will be useful, but WITHOUT
    1.14 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.15 +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
    1.16 +# Public License for more details.
    1.17 +#
    1.18 +# You should have received a copy of the GNU Lesser General Public
    1.19 +# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
    1.20 +
    1.21 +
    1.22 +# We don't need secure random numbers for test purposes.
    1.23 +from random import Random
    1.24 +
    1.25 +from viff.field import GF
    1.26 +from viff.runtime import gather_shares
    1.27 +
    1.28 +from viff.bedoza.bedoza_triple import ModifiedPaillier
    1.29 +from viff.bedoza.util import _convolute, _convolute_gf_elm
    1.30 +
    1.31 +from viff.test.util import protocol
    1.32 +from viff.test.bedoza.util import BeDOZaTestCase, skip_if_missing_packages
    1.33 +from viff.test.bedoza.util import TestShareGenerator, TestPartialShareGenerator
    1.34 +
    1.35 +
    1.36 +class TestPartialShareGeneratorTest(BeDOZaTestCase):
    1.37 + 
    1.38 +    def _partial_share(self, random, runtime, Zp, val, paillier=None):
    1.39 +        if not paillier:
    1.40 +            paillier_random = Random(random.getrandbits(128))
    1.41 +            paillier = ModifiedPaillier(runtime, paillier_random)
    1.42 +        share_random = Random(random.getrandbits(128))
    1.43 +        gen = TestPartialShareGenerator(Zp, runtime, share_random, paillier)
    1.44 +        return gen.generate_share(Zp(val))
    1.45 +
    1.46 +    def _partial_random_shares(self, random, runtime, Zp, n, paillier=None):
    1.47 +        if not paillier:
    1.48 +            paillier_random = Random(random.getrandbits(128))
    1.49 +            paillier = ModifiedPaillier(runtime, paillier_random)
    1.50 +        share_random = Random(random.getrandbits(128))
    1.51 +        gen = TestPartialShareGenerator(Zp, runtime, share_random, paillier)
    1.52 +        return gen.generate_random_shares(n)
    1.53 +
    1.54 +    @protocol
    1.55 +    def test_shares_have_correct_type(self, runtime):
    1.56 +        Zp = GF(23)
    1.57 +        share = self._partial_share(Random(23499), runtime, Zp, 7)
    1.58 +        def test(share):
    1.59 +            self.assertEquals(Zp, share.value.field)
    1.60 +        runtime.schedule_callback(share, test)
    1.61 +        return share
    1.62 + 
    1.63 +    @protocol
    1.64 +    def test_shares_are_additive(self, runtime):
    1.65 +        secret = 7
    1.66 +        share = self._partial_share(Random(34993), runtime, GF(23), secret)
    1.67 +        def convolute(share):
    1.68 +            values = _convolute_gf_elm(runtime, share.value)
    1.69 +            def test_sum(vals):
    1.70 +                self.assertEquals(secret, sum(vals))
    1.71 +            runtime.schedule_callback(values, test_sum)
    1.72 +        runtime.schedule_callback(share, convolute)
    1.73 +        return share
    1.74 +
    1.75 +    @protocol
    1.76 +    def test_encrypted_shares_decrypt_correctly(self, runtime):
    1.77 +        random = Random(3423993)
    1.78 +        modulus = 17
    1.79 +        secret = 7
    1.80 +        paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))
    1.81 +        share = self._partial_share(Random(random.getrandbits(128)), runtime,
    1.82 +                                    GF(modulus), secret, paillier=paillier)
    1.83 +        def decrypt(share):
    1.84 +            decrypted_share = paillier.decrypt(share.enc_shares[runtime.id - 1])
    1.85 +            decrypted_shares = _convolute(runtime, decrypted_share)
    1.86 +            def test_sum(vals):
    1.87 +                self.assertEquals(secret, sum(vals) % modulus)
    1.88 +            runtime.schedule_callback(decrypted_shares, test_sum)
    1.89 +        runtime.schedule_callback(share, decrypt)
    1.90 +        return share
    1.91 +
    1.92 +    @protocol
    1.93 +    def test_random_shares_have_correct_type(self, runtime):
    1.94 +        Zp = GF(23)
    1.95 +        shares = self._partial_random_shares(Random(23499), runtime, Zp, 7)
    1.96 +        for share in shares:
    1.97 +            def test(share):
    1.98 +                self.assertEquals(Zp, share.value.field)
    1.99 +            runtime.schedule_callback(share, test)
   1.100 +            
   1.101 +        return shares
   1.102 + 
   1.103 +    @protocol
   1.104 +    def test_encrypted_random_shares_decrypt_correctly(self, runtime):
   1.105 +        random = Random(3423993)
   1.106 +        modulus = 17
   1.107 +        Zp = GF(modulus)
   1.108 +        paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))
   1.109 +        shares = self._partial_random_shares(random, runtime, Zp, 7, paillier=paillier)
   1.110 +        expected_result = [9,16,7,12,3,5,6]
   1.111 +        for inx, share in enumerate(shares):
   1.112 +            def decrypt(share, expected_result):
   1.113 +                decrypted_share = paillier.decrypt(share.enc_shares[runtime.id - 1])
   1.114 +                decrypted_shares = _convolute(runtime, decrypted_share)
   1.115 +                def test_sum(vals, expected_result):
   1.116 +                    v = Zp(sum(vals))
   1.117 +                    self.assertEquals(expected_result, v)
   1.118 +                runtime.schedule_callback(decrypted_shares, test_sum, expected_result)
   1.119 +            runtime.schedule_callback(share, decrypt, expected_result[inx])
   1.120 +            
   1.121 +        return shares
   1.122 +
   1.123 +class TestShareGeneratorTest(BeDOZaTestCase):
   1.124 +
   1.125 +    @protocol
   1.126 +    def test_encrypted_real_share_open_correctly(self, runtime):
   1.127 +        random = Random(3423993)
   1.128 +        modulus = 17
   1.129 +        Zp = GF(modulus)
   1.130 +        bits_in_p = 5
   1.131 +        u_bound = 2**(4 * bits_in_p)
   1.132 +        alpha = 15
   1.133 +        
   1.134 +        paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))
   1.135 +
   1.136 +        share_random = Random(random.getrandbits(128))
   1.137 +        gen = TestShareGenerator(Zp, runtime, share_random, paillier, u_bound, alpha)
   1.138 +        share = gen.generate_share(7)
   1.139 +        def check(v):
   1.140 +            self.assertEquals(7, v)
   1.141 +        r = runtime.open(share)
   1.142 +        runtime.schedule_callback(r, check)
   1.143 +        return r
   1.144 +
   1.145 +    @protocol
   1.146 +    def test_encrypted_random_real_shares_open_correctly(self, runtime):
   1.147 +        random = Random(3423993)
   1.148 +        modulus = 17
   1.149 +        Zp = GF(modulus)
   1.150 +        bits_in_p = 5
   1.151 +        u_bound = 2**(4 * bits_in_p)
   1.152 +        alpha = 15
   1.153 +        
   1.154 +        paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128)))
   1.155 +
   1.156 +        share_random = Random(random.getrandbits(128))
   1.157 +        gen = TestShareGenerator(Zp, runtime, share_random, paillier, u_bound, alpha)
   1.158 +        shares = gen.generate_random_shares(7)
   1.159 +        expected_result = [9,16,7,12,3,5,6]
   1.160 +        results = []
   1.161 +        for inx, share in enumerate(shares):
   1.162 +            def check(v, expected_result):
   1.163 +                self.assertEquals(expected_result, v)
   1.164 +            r = runtime.open(share)
   1.165 +            results.append(r)
   1.166 +            runtime.schedule_callback(r, check, expected_result[inx])
   1.167 +        return gather_shares(results)
   1.168 +
   1.169 +
   1.170 +skip_if_missing_packages(
   1.171 +    TestPartialShareGeneratorTest,
   1.172 +    TestShareGeneratorTest)