viff

changeset 1521:9dcbcf1a3a6d

BeDOZa: Moved BeDOZaShare and PartialShare to their own file.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Fri, 23 Jul 2010 13:17:51 +0200
parents d5e0360a3566
children e45ed7224d20
files viff/bedoza/bedoza.py viff/bedoza/bedoza_triple.py viff/bedoza/shares.py viff/test/test_bedoza_runtime.py viff/test/test_bedoza_triple.py
diffstat 5 files changed, 143 insertions(+), 117 deletions(-) [+]
line diff
     1.1 --- a/viff/bedoza/bedoza.py	Fri Jul 23 13:17:48 2010 +0200
     1.2 +++ b/viff/bedoza/bedoza.py	Fri Jul 23 13:17:51 2010 +0200
     1.3 @@ -27,88 +27,13 @@
     1.4  
     1.5  from viff.hash_broadcast import HashBroadcastMixin
     1.6  
     1.7 +from viff.bedoza.shares import BeDOZaShare
     1.8 +
     1.9  from viff.bedoza.keylist import BeDOZaKeyList
    1.10  from viff.bedoza.maclist import BeDOZaMACList
    1.11  
    1.12  class BeDOZaException(Exception):
    1.13      pass
    1.14 -
    1.15 -class BeDOZaShareContents(object):
    1.16 -
    1.17 -    def __init__(self, value, keyList, macs):
    1.18 -        self.value = value
    1.19 -        self.keyList = keyList
    1.20 -        self.macs = macs
    1.21 -
    1.22 -    def get_value(self):
    1.23 -        return self.value
    1.24 -
    1.25 -    def get_keys(self):
    1.26 -        return self.keyList
    1.27 -
    1.28 -    def get_macs(self):
    1.29 -        return self.macs
    1.30 -
    1.31 -    def get_mac(self, inx):
    1.32 -        return self.macs.get_mac(inx)
    1.33 -
    1.34 -    def __add__(self, other):
    1.35 -        zi = self.value + other.value
    1.36 -        zks = self.keyList + other.keyList
    1.37 -        zms = self.macs + other.macs
    1.38 -        return BeDOZaShareContents(zi, zks, zms)
    1.39 -
    1.40 -    def __sub__(self, other):
    1.41 -        zi = self.value - other.value
    1.42 -        zks = self.keyList - other.keyList
    1.43 -        zms = self.macs - other.macs
    1.44 -        return BeDOZaShareContents(zi, zks, zms)
    1.45 -
    1.46 -    def add_public(self, c, my_id):
    1.47 -        if my_id == 1:
    1.48 -            self.value = self.value + c
    1.49 -        self.keyList.set_key(0, self.keyList.get_key(0) - self.keyList.alpha * c)
    1.50 -        return self
    1.51 -    
    1.52 -    def sub_public(self, c, my_id):
    1.53 -        if my_id == 1:
    1.54 -            self.value = self.value - c
    1.55 -        self.keyList.set_key(0, self.keyList.get_key(0) + self.keyList.alpha * c)
    1.56 -        return self
    1.57 -
    1.58 -    def cmul(self, c):
    1.59 -        zi = c * self.value
    1.60 -        zks = self.keyList.cmul(c)
    1.61 -        zms = self.macs.cmul(c)
    1.62 -        return BeDOZaShareContents(zi, zks, zms)
    1.63 -
    1.64 -    def __str__(self):
    1.65 -        return "(%s, %s, %s)" % (str(self.value), str(self.keyList), str(self.macs))
    1.66 -    
    1.67 -class BeDOZaShare(Share):
    1.68 -    """A share in the BeDOZa runtime.
    1.69 -
    1.70 -    A share in the BeDOZa runtime is a pair ``(x_i, authentication_codes)`` of:
    1.71 -
    1.72 -    - A share of a number, ``x_i``
    1.73 -    - A list of authentication_codes, ``authentication_codes``
    1.74 -
    1.75 -    The :class:`Runtime` operates on shares, represented by this class.
    1.76 -    Shares are asynchronous in the sense that they promise to attain a
    1.77 -    value at some point in the future.
    1.78 -
    1.79 -    Shares overload the arithmetic operations so that ``x = a + b``
    1.80 -    will create a new share *x*, which will eventually contain the
    1.81 -    sum of *a* and *b*. Each share is associated with a
    1.82 -    :class:`Runtime` and the arithmetic operations simply call back to
    1.83 -    that runtime.
    1.84 -    """
    1.85 -
    1.86 -    def __init__(self, runtime, field, value=None, keyList=None, authentication_codes=None):
    1.87 -        if value == None and keyList == None and authentication_codes == None:
    1.88 -            Share.__init__(self, runtime, field)
    1.89 -        else:
    1.90 -            Share.__init__(self, runtime, field, BeDOZaShareContents(value, keyList, authentication_codes))
    1.91          
    1.92  class RandomShareGenerator:
    1.93      """ TODO: This is a dummy implementation, and should be replaced with proper code."""
     2.1 --- a/viff/bedoza/bedoza_triple.py	Fri Jul 23 13:17:48 2010 +0200
     2.2 +++ b/viff/bedoza/bedoza_triple.py	Fri Jul 23 13:17:51 2010 +0200
     2.3 @@ -28,7 +28,7 @@
     2.4  from viff.constants import TEXT
     2.5  from viff.util import rand
     2.6  
     2.7 -from viff.bedoza.bedoza import BeDOZaShare, BeDOZaShareContents
     2.8 +from viff.bedoza.shares import BeDOZaShare, BeDOZaShareContents, PartialShare, PartialShareContents
     2.9  
    2.10  from viff.bedoza.keylist import BeDOZaKeyList
    2.11  from viff.bedoza.maclist import BeDOZaMACList
    2.12 @@ -92,43 +92,6 @@
    2.13                   deserialize=lambda x: gf_elm.field(int(x)))
    2.14  
    2.15  
    2.16 -class PartialShareContents(object):
    2.17 -    """A BeDOZa share without macs, e.g. < a >.
    2.18 -    TODO: BeDOZaShare should extend this class?
    2.19 -    
    2.20 -    TODO: Should the partial share contain the public encrypted shares?
    2.21 -    TODO: It may be wrong to pass encrypted_shares to super constructor; 
    2.22 -      does it mean that the already public values get passed along on the
    2.23 -      network even though all players already posess them?
    2.24 -    """
    2.25 -    def __init__(self, value, enc_shares, N_squared_list):
    2.26 -        self.value = value
    2.27 -        self.enc_shares = enc_shares
    2.28 -        self.N_squared_list = N_squared_list
    2.29 -
    2.30 -    def __str__(self):
    2.31 -        return "PartialShareContents(%d; %s; %s)" % (self.value, self.enc_shares, self.N_squared_list)
    2.32 -
    2.33 -    def __add__(self, other):
    2.34 -        z = self.value + other.value
    2.35 -        z_enc_shares = []
    2.36 -        for x, y, N_squared in zip(self.enc_shares, other.enc_shares, self.N_squared_list):
    2.37 -            z_enc_shares.append((x * y) % N_squared)
    2.38 -        return PartialShareContents(z, z_enc_shares, self.N_squared_list)
    2.39 -
    2.40 -# In VIFF, callbacks get the *contents* of a share as input. Hence, in order
    2.41 -# to get a PartialShare as input to callbacks, we need this extra level of
    2.42 -# wrapper indirection.
    2.43 -class PartialShare(Share):
    2.44 -    def __init__(self, runtime, field, value=None, enc_shares=None):
    2.45 -        if value == None and enc_shares == None:
    2.46 -            Share.__init__(self, runtime, field)
    2.47 -        else:
    2.48 -            N_squared_list = [ runtime.players[player_id].pubkey['n_square'] for player_id in runtime.players.keys()]
    2.49 -            partial_share_contents = PartialShareContents(value, enc_shares, N_squared_list)
    2.50 -            Share.__init__(self, runtime, field, partial_share_contents)
    2.51 -
    2.52 -
    2.53  class PartialShareGenerator:
    2.54  
    2.55      def __init__(self, Zp, runtime, random, paillier):
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/viff/bedoza/shares.py	Fri Jul 23 13:17:51 2010 +0200
     3.3 @@ -0,0 +1,136 @@
     3.4 +# Copyright 2010 VIFF Development Team.
     3.5 +#
     3.6 +# This file is part of VIFF, the Virtual Ideal Functionality Framework.
     3.7 +#
     3.8 +# VIFF is free software: you can redistribute it and/or modify it
     3.9 +# under the terms of the GNU Lesser General Public License (LGPL) as
    3.10 +# published by the Free Software Foundation, either version 3 of the
    3.11 +# License, or (at your option) any later version.
    3.12 +#
    3.13 +# VIFF is distributed in the hope that it will be useful, but WITHOUT
    3.14 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    3.15 +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
    3.16 +# Public License for more details.
    3.17 +#
    3.18 +# You should have received a copy of the GNU Lesser General Public
    3.19 +# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
    3.20 +
    3.21 +from viff.runtime import Share
    3.22 +
    3.23 +from viff.bedoza.keylist import BeDOZaKeyList
    3.24 +from viff.bedoza.maclist import BeDOZaMACList
    3.25 +
    3.26 +class PartialShareContents(object):
    3.27 +    """A BeDOZa share without macs, e.g. < a >.
    3.28 +    TODO: BeDOZaShare should extend this class?
    3.29 +    
    3.30 +    TODO: Should the partial share contain the public encrypted shares?
    3.31 +    TODO: It may be wrong to pass encrypted_shares to super constructor; 
    3.32 +      does it mean that the already public values get passed along on the
    3.33 +      network even though all players already posess them?
    3.34 +    """
    3.35 +    def __init__(self, value, enc_shares, N_squared_list):
    3.36 +        self.value = value
    3.37 +        self.enc_shares = enc_shares
    3.38 +        self.N_squared_list = N_squared_list
    3.39 +
    3.40 +    def __str__(self):
    3.41 +        return "PartialShareContents(%d; %s; %s)" % (self.value, self.enc_shares, self.N_squared_list)
    3.42 +
    3.43 +    def __add__(self, other):
    3.44 +        z = self.value + other.value
    3.45 +        z_enc_shares = []
    3.46 +        for x, y, N_squared in zip(self.enc_shares, other.enc_shares, self.N_squared_list):
    3.47 +            z_enc_shares.append((x * y) % N_squared)
    3.48 +        return PartialShareContents(z, z_enc_shares, self.N_squared_list)
    3.49 +
    3.50 +
    3.51 +# In VIFF, callbacks get the *contents* of a share as input. Hence, in order
    3.52 +# to get a PartialShare as input to callbacks, we need this extra level of
    3.53 +# wrapper indirection.
    3.54 +class PartialShare(Share):
    3.55 +    def __init__(self, runtime, field, value=None, enc_shares=None):
    3.56 +        if value == None and enc_shares == None:
    3.57 +            Share.__init__(self, runtime, field)
    3.58 +        else:
    3.59 +            N_squared_list = [ runtime.players[player_id].pubkey['n_square'] for player_id in runtime.players.keys()]
    3.60 +            partial_share_contents = PartialShareContents(value, enc_shares, N_squared_list)
    3.61 +            Share.__init__(self, runtime, field, partial_share_contents)
    3.62 +
    3.63 +
    3.64 +class BeDOZaShareContents(object):
    3.65 +
    3.66 +    def __init__(self, value, keyList, macs):
    3.67 +        self.value = value
    3.68 +        self.keyList = keyList
    3.69 +        self.macs = macs
    3.70 +
    3.71 +    def get_value(self):
    3.72 +        return self.value
    3.73 +
    3.74 +    def get_keys(self):
    3.75 +        return self.keyList
    3.76 +
    3.77 +    def get_macs(self):
    3.78 +        return self.macs
    3.79 +
    3.80 +    def get_mac(self, inx):
    3.81 +        return self.macs.get_mac(inx)
    3.82 +
    3.83 +    def __add__(self, other):
    3.84 +        zi = self.value + other.value
    3.85 +        zks = self.keyList + other.keyList
    3.86 +        zms = self.macs + other.macs
    3.87 +        return BeDOZaShareContents(zi, zks, zms)
    3.88 +
    3.89 +    def __sub__(self, other):
    3.90 +        zi = self.value - other.value
    3.91 +        zks = self.keyList - other.keyList
    3.92 +        zms = self.macs - other.macs
    3.93 +        return BeDOZaShareContents(zi, zks, zms)
    3.94 +
    3.95 +    def add_public(self, c, my_id):
    3.96 +        if my_id == 1:
    3.97 +            self.value = self.value + c
    3.98 +        self.keyList.set_key(0, self.keyList.get_key(0) - self.keyList.alpha * c)
    3.99 +        return self
   3.100 +    
   3.101 +    def sub_public(self, c, my_id):
   3.102 +        if my_id == 1:
   3.103 +            self.value = self.value - c
   3.104 +        self.keyList.set_key(0, self.keyList.get_key(0) + self.keyList.alpha * c)
   3.105 +        return self
   3.106 +
   3.107 +    def cmul(self, c):
   3.108 +        zi = c * self.value
   3.109 +        zks = self.keyList.cmul(c)
   3.110 +        zms = self.macs.cmul(c)
   3.111 +        return BeDOZaShareContents(zi, zks, zms)
   3.112 +
   3.113 +    def __str__(self):
   3.114 +        return "(%s, %s, %s)" % (str(self.value), str(self.keyList), str(self.macs))
   3.115 +    
   3.116 +class BeDOZaShare(Share):
   3.117 +    """A share in the BeDOZa runtime.
   3.118 +
   3.119 +    A share in the BeDOZa runtime is a pair ``(x_i, authentication_codes)`` of:
   3.120 +
   3.121 +    - A share of a number, ``x_i``
   3.122 +    - A list of authentication_codes, ``authentication_codes``
   3.123 +
   3.124 +    The :class:`Runtime` operates on shares, represented by this class.
   3.125 +    Shares are asynchronous in the sense that they promise to attain a
   3.126 +    value at some point in the future.
   3.127 +
   3.128 +    Shares overload the arithmetic operations so that ``x = a + b``
   3.129 +    will create a new share *x*, which will eventually contain the
   3.130 +    sum of *a* and *b*. Each share is associated with a
   3.131 +    :class:`Runtime` and the arithmetic operations simply call back to
   3.132 +    that runtime.
   3.133 +    """
   3.134 +
   3.135 +    def __init__(self, runtime, field, value=None, keyList=None, authentication_codes=None):
   3.136 +        if value == None and keyList == None and authentication_codes == None:
   3.137 +            Share.__init__(self, runtime, field)
   3.138 +        else:
   3.139 +            Share.__init__(self, runtime, field, BeDOZaShareContents(value, keyList, authentication_codes))
     4.1 --- a/viff/test/test_bedoza_runtime.py	Fri Jul 23 13:17:48 2010 +0200
     4.2 +++ b/viff/test/test_bedoza_runtime.py	Fri Jul 23 13:17:51 2010 +0200
     4.3 @@ -22,7 +22,8 @@
     4.4  from viff.test.util import RuntimeTestCase, protocol
     4.5  from viff.runtime import gather_shares, Share
     4.6  from viff.config import generate_configs
     4.7 -from viff.bedoza.bedoza import BeDOZaRuntime, BeDOZaShare, BeDOZaShareContents
     4.8 +from viff.bedoza.bedoza import BeDOZaRuntime
     4.9 +from viff.bedoza.shares import BeDOZaShare, BeDOZaShareContents
    4.10  from viff.bedoza.keylist import BeDOZaKeyList
    4.11  from viff.bedoza.maclist import BeDOZaMACList
    4.12  from viff.field import FieldElement, GF
     5.1 --- a/viff/test/test_bedoza_triple.py	Fri Jul 23 13:17:48 2010 +0200
     5.2 +++ b/viff/test/test_bedoza_triple.py	Fri Jul 23 13:17:51 2010 +0200
     5.3 @@ -32,7 +32,8 @@
     5.4  
     5.5  from viff.bedoza.bedoza import BeDOZaRuntime, BeDOZaShare
     5.6  from viff.bedoza.keylist import BeDOZaKeyList
     5.7 -from viff.bedoza.bedoza_triple import TripleGenerator, PartialShare, PartialShareContents, ModifiedPaillier, PartialShareGenerator, ShareGenerator
     5.8 +from viff.bedoza.bedoza_triple import TripleGenerator, ModifiedPaillier, PartialShareGenerator, ShareGenerator
     5.9 +from viff.bedoza.shares import PartialShare, PartialShareContents
    5.10  from viff.bedoza.bedoza_triple import _send, _convolute, _convolute_gf_elm, add_macs
    5.11  
    5.12  from viff.field import FieldElement, GF