viff

changeset 921:dab7f77c5fc8

Added a new application showing how to use the equality library.
author Sigurd Meldgaard <stm@daimi.au.dk>
date Mon, 08 Sep 2008 18:31:14 +0200
parents 53fde6bf9230
children 71428d0f7888
files apps/equality.py
diffstat 1 files changed, 106 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/apps/equality.py	Mon Sep 08 18:31:14 2008 +0200
     1.3 @@ -0,0 +1,106 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +# Copyright 2008 VIFF Development Team.
     1.7 +#
     1.8 +# This file is part of VIFF, the Virtual Ideal Functionality Framework.
     1.9 +#
    1.10 +# VIFF is free software: you can redistribute it and/or modify it
    1.11 +# under the terms of the GNU Lesser General Public License (LGPL) as
    1.12 +# published by the Free Software Foundation, either version 3 of the
    1.13 +# License, or (at your option) any later version.
    1.14 +#
    1.15 +# VIFF is distributed in the hope that it will be useful, but WITHOUT
    1.16 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.17 +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
    1.18 +# Public License for more details.
    1.19 +#
    1.20 +# You should have received a copy of the GNU Lesser General Public
    1.21 +# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
    1.22 +#
    1.23 +
    1.24 +"""This program is a very simple example of a VIFF program which shows
    1.25 +the secret equality testing of two numbers.
    1.26 +
    1.27 +The program can be run as follows for each player (min 3) where 24 is
    1.28 +the number we would like to compare:
    1.29 +
    1.30 +$ python equality.py player-X.ini -n 24
    1.31 +
    1.32 +Only the numbers of player 1 and player 2 are actually compared, but
    1.33 +more players are necessary for the security.
    1.34 +"""
    1.35 +
    1.36 +from optparse import OptionParser
    1.37 +from twisted.internet import reactor
    1.38 +
    1.39 +from viff.field import GF
    1.40 +from viff.runtime import Runtime, create_runtime, make_runtime_class
    1.41 +from viff.config import load_config
    1.42 +from viff.util import find_prime
    1.43 +from viff.equality import ProbabilisticEqualityMixin
    1.44 +
    1.45 +
    1.46 +class Protocol:
    1.47 +
    1.48 +    def __init__(self, runtime):
    1.49 +        print "Connected."
    1.50 +        self.rt = runtime
    1.51 +
    1.52 +        # Is our player id among the two first?
    1.53 +        if runtime.id <= 2:
    1.54 +            print "My number: %d." % options.number
    1.55 +            # Players 1 and two are doing a sharing over the field ZP
    1.56 +            # our input is options number
    1.57 +            (x, y) = runtime.shamir_share([1, 2], Zp, options.number)
    1.58 +        else:
    1.59 +            print "I do not have a number."
    1.60 +            (x, y) = runtime.shamir_share([1, 2], Zp, None)
    1.61 +
    1.62 +        # Do the secret computation
    1.63 +        result = (x == y)
    1.64 +
    1.65 +        # Now open the result so that we can see it
    1.66 +        result = runtime.open(result)
    1.67 +
    1.68 +        def finish(eq):
    1.69 +            print
    1.70 +            if eq:
    1.71 +                print "The two numbers where equal!"
    1.72 +            else:
    1.73 +                print "The two numbers where different! (with high probability)"
    1.74 +            print
    1.75 +
    1.76 +        # When the values for the opening arrive, we can call the
    1.77 +        # finish function, followed by the shutdown method.
    1.78 +        result.addCallback(finish)
    1.79 +        result.addCallback(lambda _: runtime.shutdown())
    1.80 +
    1.81 +
    1.82 +# Parse command line arguments.
    1.83 +parser = OptionParser(usage=__doc__)
    1.84 +
    1.85 +parser.add_option("--modulus",
    1.86 +                 help="lower limit for modulus (can be an expression)")
    1.87 +parser.add_option("-n", "--number", type="int",
    1.88 +                 help="number to compare")
    1.89 +
    1.90 +parser.set_defaults(modulus=2**65, number=None)
    1.91 +
    1.92 +Runtime.add_options(parser)
    1.93 +
    1.94 +options, args = parser.parse_args()
    1.95 +
    1.96 +if len(args) == 0:
    1.97 +    parser.error("you must specify a config file")
    1.98 +
    1.99 +Zp = GF(find_prime(options.modulus, blum=True))
   1.100 +
   1.101 +# Load configuration file.
   1.102 +id, players = load_config(args[0])
   1.103 +
   1.104 +runtime_class = make_runtime_class(mixins=[ProbabilisticEqualityMixin])
   1.105 +pre_runtime = create_runtime(id, players, 1, options, runtime_class)
   1.106 +pre_runtime.addCallback(Protocol)
   1.107 +
   1.108 +# Start the Twisted event loop.
   1.109 +reactor.run()