viff

changeset 1126:d3b49f2c8203

Created a simpel example which demonstrates the basic features of VIFF to beginners
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Tue, 10 Mar 2009 13:24:29 +0100
parents 88d6a083a513
children c4222f1a07cd
files apps/beginner.py
diffstat 1 files changed, 114 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/apps/beginner.py	Tue Mar 10 13:24:29 2009 +0100
     1.3 @@ -0,0 +1,114 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +# Copyright 2007, 2008, 2009 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 +# This file contains a simpel example of a VIFF program, which illustrates
    1.24 +# the basic features of VIFF. How to input values from the command line,
    1.25 +# from individual players in the program, add, multiply, and output values
    1.26 +# to all or some of the players.
    1.27 +
    1.28 +# Inorder to run the program follow the three steps:
    1.29 +#
    1.30 +# Generate player configurations in the viff/apps directory using:
    1.31 +#   python generate-config-files.py localhost:4001 localhost:4002 localhost:4003
    1.32 +#
    1.33 +# Generate ssl certificates in the viff/apps directory using:
    1.34 +#   python generate-certificates.py
    1.35 +#
    1.36 +# Run the program using three different shells using the command:
    1.37 +#   python a-simpel-viff-program.py player-x.ini 79
    1.38 +# where x is replaced by the player number
    1.39 +
    1.40 +# Some useful imports.
    1.41 +import sys
    1.42 +
    1.43 +from twisted.internet import reactor
    1.44 +
    1.45 +from viff.field import GF
    1.46 +from viff.runtime import create_runtime
    1.47 +from viff.config import load_config
    1.48 +from viff.util import dprint, find_prime
    1.49 +
    1.50 +# Load the configuration from the player configuration files.
    1.51 +id, players = load_config(sys.argv[1])
    1.52 +
    1.53 +# Initialize the field we do arithmetic over.
    1.54 +Zp = GF(find_prime(2**64))
    1.55 +
    1.56 +# Read input from the commandline.
    1.57 +input = int(sys.argv[2])
    1.58 +
    1.59 +print "I am player %d and will input %s" % (id, input)
    1.60 +
    1.61 +
    1.62 +def protocol(runtime):
    1.63 +    print "-" * 64
    1.64 +    print "Program started"
    1.65 +    print
    1.66 +
    1.67 +    # Each of the players [1, 2, 3] shares his input -- resulting in
    1.68 +    # three shares. The a share is the input from P1, b is from P2,
    1.69 +    # and c is from P3.
    1.70 +    a, b, c = runtime.input([1, 2, 3], Zp, input)
    1.71 +
    1.72 +    # It is possible to make the players do different things by
    1.73 +    # branching on the players ID. In this case only P1 inputs a
    1.74 +    # number. The other two players must still participate in order to
    1.75 +    # get the hold of their share.
    1.76 +    if runtime.id == 1:
    1.77 +        s1 = runtime.input([1], Zp, 42)
    1.78 +    else:
    1.79 +        s1 = runtime.input([1], Zp, None)
    1.80 +
    1.81 +    # Secure arithmetic works like normal.
    1.82 +    a = b + c
    1.83 +    b = c * s1
    1.84 +
    1.85 +    # Outputting shares convert them from secret shared form into
    1.86 +    # cleartext. By default every player receives the cleartext.
    1.87 +    a = runtime.output(a)
    1.88 +    b = runtime.output(b)
    1.89 +    c = runtime.output(c)
    1.90 +    # Output s1 to player 2. The other players will receive None.
    1.91 +    s1 = runtime.output(s1, [2])
    1.92 +
    1.93 +    dprint("### Output a to all: %s ###", a)
    1.94 +    dprint("### Output b to all: %s ###", b)
    1.95 +    dprint("### Output c to all: %s ###", c)
    1.96 +
    1.97 +    # We only print the value of s1 for player 2, 
    1.98 +    # since only player 2 has the value of s1.
    1.99 +    if runtime.id == 2:
   1.100 +        dprint("### opened s1: %s ###", s1)
   1.101 +    
   1.102 +    # We wait for the evaluation of deferred a, b, c.
   1.103 +    runtime.wait_for(a, b, c)
   1.104 +
   1.105 +def errorHandler(failure):
   1.106 +    print "Error: %s" % failure
   1.107 +
   1.108 +
   1.109 +# Create a runtime
   1.110 +pre_runtime = create_runtime(id, players, 1)
   1.111 +pre_runtime.addCallback(protocol)
   1.112 +# This error handler will enable debugging by capturing
   1.113 +# any exceptions and print them on Standard Out.
   1.114 +pre_runtime.addErrback(errorHandler)
   1.115 +
   1.116 +print "#### Starting reactor ###"
   1.117 +reactor.run()