viff

changeset 1222:7fe8f5835b61

Boilerplate code for the implementation of the Orlandi runtime.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Tue, 06 Oct 2009 10:05:24 +0200
parents 53d198cdf14c
children 42d95e56edf6
files viff/orlandi.py viff/test/test_orlandi_runtime.py
diffstat 2 files changed, 102 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/viff/orlandi.py	Tue Oct 06 10:05:24 2009 +0200
     1.3 @@ -0,0 +1,69 @@
     1.4 +# Copyright 2009 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 +from viff.runtime import Runtime, Share, ShareList, gather_shares
    1.22 +from viff.util import rand
    1.23 +
    1.24 +class OrlandiException(Exception):
    1.25 +    pass
    1.26 +
    1.27 +class OrlandiShare(Share):
    1.28 +    """A share in the Orlandi runtime.
    1.29 +
    1.30 +    A share in the Orlandi runtime is a 3-tuple ``(x_i, rho_i, Cr_i)`` of:
    1.31 +    - A share of a number, ``x_i``
    1.32 +    - A tuple of two random numbers, ``rho_i = (rho_i1, rho_i2)``
    1.33 +    - A commitment to the number and the random numbers, ``Cr_i``
    1.34 +
    1.35 +    The :class:`Runtime` operates on shares, represented by this class.
    1.36 +    Shares are asynchronous in the sense that they promise to attain a
    1.37 +    value at some point in the future.
    1.38 +
    1.39 +    Shares overload the arithmetic operations so that ``x = a + b``
    1.40 +    will create a new share *x*, which will eventually contain the
    1.41 +    sum of *a* and *b*. Each share is associated with a
    1.42 +    :class:`Runtime` and the arithmetic operations simply call back to
    1.43 +    that runtime.
    1.44 +    """
    1.45 +
    1.46 +    def __init__(self, runtime, field, value=None, rho=None, commitment=None):
    1.47 +        Share.__init__(self, runtime, field, (value, rho, commitment))
    1.48 +
    1.49 +
    1.50 +class OrlandiRuntime(Runtime):
    1.51 +    """The Orlandi runtime.
    1.52 +
    1.53 +    The runtime is used for sharing values (:meth:`secret_share` or
    1.54 +    :meth:`shift`) into :class:`OrlandiShare` object and opening such
    1.55 +    shares (:meth:`open`) again. Calculations on shares is normally
    1.56 +    done through overloaded arithmetic operations, but it is also
    1.57 +    possible to call :meth:`add`, :meth:`mul`, etc. directly if one
    1.58 +    prefers.
    1.59 +
    1.60 +    Each player in the protocol uses a :class:`Runtime` object. To
    1.61 +    create an instance and connect it correctly with the other
    1.62 +    players, please use the :func:`create_runtime` function instead of
    1.63 +    instantiating a Runtime directly. The :func:`create_runtime`
    1.64 +    function will take care of setting up network connections and
    1.65 +    return a :class:`Deferred` which triggers with the
    1.66 +    :class:`Runtime` object when it is ready.
    1.67 +    """
    1.68 +
    1.69 +    def __init__(self, player, threshold=None, options=None):
    1.70 +        """Initialize runtime."""
    1.71 +        Runtime.__init__(self, player, threshold, options)
    1.72 +        self.threshold = self.num_players - 1
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/viff/test/test_orlandi_runtime.py	Tue Oct 06 10:05:24 2009 +0200
     2.3 @@ -0,0 +1,33 @@
     2.4 +# Copyright 2009 VIFF Development Team.
     2.5 +#
     2.6 +# This file is part of VIFF, the Virtual Ideal Functionality Framework.
     2.7 +#
     2.8 +# VIFF is free software: you can redistribute it and/or modify it
     2.9 +# under the terms of the GNU Lesser General Public License (LGPL) as
    2.10 +# published by the Free Software Foundation, either version 3 of the
    2.11 +# License, or (at your option) any later version.
    2.12 +#
    2.13 +# VIFF is distributed in the hope that it will be useful, but WITHOUT
    2.14 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    2.15 +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
    2.16 +# Public License for more details.
    2.17 +#
    2.18 +# You should have received a copy of the GNU Lesser General Public
    2.19 +# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
    2.20 +
    2.21 +from twisted.internet.defer import gatherResults
    2.22 +
    2.23 +from viff.test.util import RuntimeTestCase, protocol, BinaryOperatorTestCase
    2.24 +from viff.runtime import Share
    2.25 +from viff.orlandi import OrlandiRuntime
    2.26 +
    2.27 +from viff.field import FieldElement
    2.28 +from viff.passive import PassiveRuntime
    2.29 +
    2.30 +class OrlandiBasicCommandsTest(RuntimeTestCase):
    2.31 +    """Test for basic commands."""
    2.32 +
    2.33 +    # Number of players.
    2.34 +    num_players = 3
    2.35 +
    2.36 +    runtime_class = OrlandiRuntime