viff

changeset 1445:67def1bd577b

Added tests for the basic parts of the BeDOZa runtime.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Fri, 02 Jul 2010 10:21:50 +0200
parents 0ca61834e8f8
children 161c652c4ae0
files viff/test/test_bedoza_runtime.py
diffstat 1 files changed, 240 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/viff/test/test_bedoza_runtime.py	Fri Jul 02 10:21:50 2010 +0200
     1.3 @@ -0,0 +1,240 @@
     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 +import sys
    1.22 +
    1.23 +from twisted.internet.defer import gatherResults, DeferredList
    1.24 +
    1.25 +from viff.test.util import RuntimeTestCase, protocol
    1.26 +from viff.runtime import gather_shares, Share
    1.27 +from viff.config import generate_configs
    1.28 +from viff.bedoza import BeDOZaRuntime, BeDOZaShare
    1.29 +from viff.field import FieldElement, GF
    1.30 +from viff.util import rand
    1.31 +
    1.32 +class BeDOZaBasicCommandsTest(RuntimeTestCase):
    1.33 +    """Test for basic commands."""
    1.34 +
    1.35 +    # Number of players.
    1.36 +    num_players = 3
    1.37 +
    1.38 +    runtime_class = BeDOZaRuntime
    1.39 +
    1.40 +    @protocol
    1.41 +    def test_secret_share(self, runtime):
    1.42 +        """Test sharing of random numbers."""
    1.43 +
    1.44 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
    1.45 +
    1.46 +        def check((xi, Ks)):
    1.47 +            # Check that we got the expected number of shares.
    1.48 +            self.assert_type(xi, FieldElement)
    1.49 +            self.assert_type(Ks[0], FieldElement)
    1.50 +            self.assert_type(Ks[1], FieldElement)
    1.51 +            self.assert_type(Ks[2], FieldElement)
    1.52 +
    1.53 +        if 1 == runtime.id:
    1.54 +            share = runtime.secret_share([1], self.Zp, 42)
    1.55 +        else:
    1.56 +            share = runtime.secret_share([1], self.Zp)
    1.57 +        share.addCallback(check)
    1.58 +        return share
    1.59 +
    1.60 +    @protocol
    1.61 +    def test_open_secret_share(self, runtime):
    1.62 +        """Test sharing and open of a number."""
    1.63 +
    1.64 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
    1.65 +
    1.66 +        def check(v):
    1.67 +            self.assertEquals(v, 42)
    1.68 +
    1.69 +        if 1 == runtime.id:
    1.70 +            x = runtime.secret_share([1], self.Zp, 42)
    1.71 +        else:
    1.72 +            x = runtime.secret_share([1], self.Zp)
    1.73 +        d = runtime.open(x)
    1.74 +        d.addCallback(check)
    1.75 +        return d
    1.76 +
    1.77 +    @protocol
    1.78 +    def test_random_share(self, runtime):
    1.79 +        """Test creation of a random shared number."""
    1.80 +
    1.81 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
    1.82 +
    1.83 +        def check(v):
    1.84 +            self.assertEquals(True, True)
    1.85 +
    1.86 +        x = runtime.random_share(self.Zp)
    1.87 +        d = runtime.open(x)
    1.88 +        d.addCallback(check)
    1.89 +        return d
    1.90 +
    1.91 +    @protocol
    1.92 +    def test_sum(self, runtime):
    1.93 +        """Test addition of two numbers."""
    1.94 +
    1.95 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
    1.96 +
    1.97 +        x1 = 42
    1.98 +        y1 = 7
    1.99 +
   1.100 +        def check(v):
   1.101 +            self.assertEquals(v, x1 + y1)
   1.102 +
   1.103 +        if 1 == runtime.id:
   1.104 +            x2 = runtime.secret_share([1], self.Zp, x1)
   1.105 +        else:
   1.106 +            x2 = runtime.secret_share([1], self.Zp)
   1.107 +
   1.108 +        if 3 == runtime.id:
   1.109 +            y2 = runtime.secret_share([3], self.Zp, y1)
   1.110 +        else:
   1.111 +            y2 = runtime.secret_share([3], self.Zp)
   1.112 +
   1.113 +        z2 = runtime.add(x2, y2)
   1.114 +        d = runtime.open(z2)
   1.115 +        d.addCallback(check)
   1.116 +        return d
   1.117 +
   1.118 +    @protocol
   1.119 +    def test_sum_plus(self, runtime):
   1.120 +        """Test addition of two numbers."""
   1.121 +
   1.122 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
   1.123 +
   1.124 +        x1 = 42
   1.125 +        y1 = 7
   1.126 +
   1.127 +        def check(v):
   1.128 +            self.assertEquals(v, x1 + y1)
   1.129 +
   1.130 +        if 1 == runtime.id:
   1.131 +            x2 = runtime.secret_share([1], self.Zp, x1)
   1.132 +        else:
   1.133 +            x2 = runtime.secret_share([1], self.Zp)
   1.134 +
   1.135 +        if 3 == runtime.id:
   1.136 +            y2 = runtime.secret_share([3], self.Zp, y1)
   1.137 +        else:
   1.138 +            y2 = runtime.secret_share([3], self.Zp)
   1.139 +
   1.140 +        z2 = x2 + y2
   1.141 +        d = runtime.open(z2)
   1.142 +        d.addCallback(check)
   1.143 +        return d
   1.144 +
   1.145 +    @protocol
   1.146 +    def test_sum_constant(self, runtime):
   1.147 +        """Test addition of two numbers."""
   1.148 +
   1.149 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
   1.150 +
   1.151 +        x1 = 42
   1.152 +        y1 = 7
   1.153 +
   1.154 +        def check(v):
   1.155 +            self.assertEquals(v, x1 + y1)
   1.156 +
   1.157 +        if 1 == runtime.id:
   1.158 +            x2 = runtime.secret_share([1], self.Zp, x1)
   1.159 +        else:
   1.160 +            x2 = runtime.secret_share([1], self.Zp)
   1.161 +
   1.162 +        z2 = x2 + y1
   1.163 +        d = runtime.open(z2)
   1.164 +        d.addCallback(check)
   1.165 +        return d
   1.166 +
   1.167 +    @protocol
   1.168 +    def test_sub(self, runtime):
   1.169 +        """Test subtration of two numbers."""
   1.170 +
   1.171 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
   1.172 +
   1.173 +        x1 = 42
   1.174 +        y1 = 7
   1.175 +
   1.176 +        def check(v):
   1.177 +            self.assertEquals(v, x1 - y1)
   1.178 +
   1.179 +        if 1 == runtime.id:
   1.180 +            x2 = runtime.secret_share([1], self.Zp, x1)
   1.181 +        else:
   1.182 +            x2 = runtime.secret_share([1], self.Zp)
   1.183 +
   1.184 +        if 3 == runtime.id:
   1.185 +            y2 = runtime.secret_share([3], self.Zp, y1)
   1.186 +        else:
   1.187 +            y2 = runtime.secret_share([3], self.Zp)
   1.188 +
   1.189 +        z2 = runtime.sub(x2, y2)
   1.190 +        d = runtime.open(z2)
   1.191 +        d.addCallback(check)
   1.192 +        return d
   1.193 +
   1.194 +    @protocol
   1.195 +    def test_sub_minus(self, runtime):
   1.196 +        """Test subtration of two numbers."""
   1.197 +
   1.198 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
   1.199 +
   1.200 +        x1 = 42
   1.201 +        y1 = 7
   1.202 +
   1.203 +        def check(v):
   1.204 +            self.assertEquals(v, x1 - y1)
   1.205 +
   1.206 +        if 1 == runtime.id:
   1.207 +            x2 = runtime.secret_share([1], self.Zp, x1)
   1.208 +        else:
   1.209 +            x2 = runtime.secret_share([1], self.Zp)
   1.210 +
   1.211 +        if 3 == runtime.id:
   1.212 +            y2 = runtime.secret_share([3], self.Zp, y1)
   1.213 +        else:
   1.214 +            y2 = runtime.secret_share([3], self.Zp)
   1.215 +
   1.216 +        z2 = x2 - y2
   1.217 +        d = runtime.open(z2)
   1.218 +        d.addCallback(check)
   1.219 +        return d
   1.220 +
   1.221 +    @protocol
   1.222 +    def test_sub_constant(self, runtime):
   1.223 +        """Test subtration of two numbers."""
   1.224 +
   1.225 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
   1.226 +
   1.227 +        x1 = 42
   1.228 +        y1 = 7
   1.229 +
   1.230 +        def check(v):
   1.231 +            self.assertEquals(v, x1 - y1)
   1.232 +
   1.233 +        if 1 == runtime.id:
   1.234 +            x2 = runtime.secret_share([1], self.Zp, x1)
   1.235 +        else:
   1.236 +            x2 = runtime.secret_share([1], self.Zp)
   1.237 +
   1.238 +        z2 = x2 - y1
   1.239 +        d = runtime.open(z2)
   1.240 +        d.addCallback(check)
   1.241 +        return d
   1.242 +
   1.243 +BeDOZaBasicCommandsTest.skip = "Skipped because the tested code is not implemented."