viff

changeset 963:4daa42544157

Test case with different thresholds.
author Martin Geisler <mg@daimi.au.dk>
date Mon, 06 Oct 2008 20:57:13 +0200
parents 37e05ffb25af
children 003ca9e1ad90
files viff/test/test_thresholds.py
diffstat 1 files changed, 158 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/viff/test/test_thresholds.py	Mon Oct 06 20:57:13 2008 +0200
     1.3 @@ -0,0 +1,158 @@
     1.4 +# Copyright 2008 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 +"""Test different thresholds."""
    1.22 +
    1.23 +import operator
    1.24 +
    1.25 +from twisted.internet.defer import gatherResults
    1.26 +
    1.27 +from viff.runtime import Share
    1.28 +from viff.comparison import Toft07Runtime
    1.29 +from viff.test.util import RuntimeTestCase, protocol
    1.30 +
    1.31 +
    1.32 +class Tests:
    1.33 +    """Test L{viff.runtime.Runtime} with a given threshold.
    1.34 +
    1.35 +    This mixin class should be used with a L{RuntimeTestCase} which
    1.36 +    specifies a number of players and a threshold. The unit tests here
    1.37 +    have been designed to work with any number of players greater than
    1.38 +    two and any threshold.
    1.39 +    """
    1.40 +
    1.41 +    runtime_class = Toft07Runtime
    1.42 +
    1.43 +    def _test_binop(self, runtime, op):
    1.44 +        a, b = 12345, 34567
    1.45 +        share_a = Share(runtime, self.Zp, self.Zp(a + runtime.id))
    1.46 +        share_b = Share(runtime, self.Zp, self.Zp(b - runtime.id))
    1.47 +
    1.48 +        expected = self.Zp(op(a, b))
    1.49 +        result = op(share_a, share_b)
    1.50 +
    1.51 +        opened = runtime.open(result)
    1.52 +        opened.addCallback(self.assertEquals, expected)
    1.53 +        return opened
    1.54 +
    1.55 +    @protocol
    1.56 +    def test_add(self, runtime):
    1.57 +        """Test addition."""
    1.58 +        return self._test_binop(runtime, operator.add)
    1.59 +
    1.60 +    @protocol
    1.61 +    def test_sub(self, runtime):
    1.62 +        """Test subtraction."""
    1.63 +        return self._test_binop(runtime, operator.sub)
    1.64 +
    1.65 +    @protocol
    1.66 +    def test_mul(self, runtime):
    1.67 +        """Test multiplication."""
    1.68 +        return self._test_binop(runtime, operator.mul)
    1.69 +
    1.70 +    @protocol
    1.71 +    def test_ge(self, runtime):
    1.72 +        """Test greater-than-equal."""
    1.73 +        return self._test_binop(runtime, operator.ge)
    1.74 +
    1.75 +    @protocol
    1.76 +    def test_open(self, runtime):
    1.77 +        """Test opening of a single sharing."""
    1.78 +        share = Share(runtime, self.Zp, self.Zp(117 + runtime.id))
    1.79 +        opened = runtime.open(share)
    1.80 +        opened.addCallback(self.assertEquals, 117)
    1.81 +        return opened
    1.82 +
    1.83 +    @protocol
    1.84 +    def test_shamir_share(self, runtime):
    1.85 +        """Test a Shamir sharing by Player 2."""
    1.86 +        if runtime.id == 2:
    1.87 +            number = 117
    1.88 +        else:
    1.89 +            number = None
    1.90 +        x = runtime.open(runtime.shamir_share([2], self.Zp, number))
    1.91 +        x.addCallback(self.assertEquals, 117)
    1.92 +        return x
    1.93 +
    1.94 +
    1.95 +class Players3Threshold1Test(Tests, RuntimeTestCase):
    1.96 +    num_players = 3
    1.97 +    threshold = 1
    1.98 +
    1.99 +
   1.100 +class Players4Threshold1Test(Tests, RuntimeTestCase):
   1.101 +    num_players = 4
   1.102 +    threshold = 1
   1.103 +
   1.104 +
   1.105 +class Players5Threshold1Test(Tests, RuntimeTestCase):
   1.106 +    num_players = 5
   1.107 +    threshold = 1
   1.108 +
   1.109 +class Players5Threshold2Test(Tests, RuntimeTestCase):
   1.110 +    num_players = 5
   1.111 +    threshold = 2
   1.112 +
   1.113 +
   1.114 +class Players6Threshold1Test(Tests, RuntimeTestCase):
   1.115 +    num_players = 6
   1.116 +    threshold = 1
   1.117 +
   1.118 +class Players6Threshold2Test(Tests, RuntimeTestCase):
   1.119 +    num_players = 6
   1.120 +    threshold = 2
   1.121 +
   1.122 +
   1.123 +class Players7Threshold1Test(Tests, RuntimeTestCase):
   1.124 +    num_players = 7
   1.125 +    threshold = 1
   1.126 +
   1.127 +class Players7Threshold2Test(Tests, RuntimeTestCase):
   1.128 +    num_players = 7
   1.129 +    threshold = 2
   1.130 +
   1.131 +class Players7Threshold3Test(Tests, RuntimeTestCase):
   1.132 +    num_players = 7
   1.133 +    threshold = 3
   1.134 +
   1.135 +class Players8Threshold1Test(Tests, RuntimeTestCase):
   1.136 +    num_players = 8
   1.137 +    threshold = 1
   1.138 +
   1.139 +class Players8Threshold2Test(Tests, RuntimeTestCase):
   1.140 +    num_players = 8
   1.141 +    threshold = 2
   1.142 +
   1.143 +class Players8Threshold3Test(Tests, RuntimeTestCase):
   1.144 +    num_players = 8
   1.145 +    threshold = 3
   1.146 +
   1.147 +class Players9Threshold1Test(Tests, RuntimeTestCase):
   1.148 +    num_players = 9
   1.149 +    threshold = 1
   1.150 +
   1.151 +class Players9Threshold2Test(Tests, RuntimeTestCase):
   1.152 +    num_players = 9
   1.153 +    threshold = 2
   1.154 +
   1.155 +class Players9Threshold3Test(Tests, RuntimeTestCase):
   1.156 +    num_players = 9
   1.157 +    threshold = 3
   1.158 +
   1.159 +class Players9Threshold4Test(Tests, RuntimeTestCase):
   1.160 +    num_players = 9
   1.161 +    threshold = 4