Hello,

Continuing on my previous email about how to code things in VIFF. I hereby present the complete test code.

It seems my calculations are done at the same time that the reactor is closing down so sometimes my code does not return any printed values. This is a mistake, either in my understanding of VIFF or in the framework itself.

See the two runs below.

C:\Documents and Settings\item\Desktop\VIFF\viff-dev\apps>tir_test.py --no-ssl player-3.ini
Seeding random generator with random seed 5522
Not using SSL
Listening on port 9003
Synchronizing shutdown... done.
Closing connections... Comparing  [{6}]  to 0
Result  [{12}]
Result  True
done.
Stopping reactor... done.

C:\Documents and Settings\item\Desktop\VIFF\viff-dev\apps>tir_test.py --no-ssl player-3.ini
Seeding random generator with random seed 3023
Not using SSL
Listening on port 9003
Synchronizing shutdown... done.
Closing connections... done.
Stopping reactor... done.


#!/usr/bin/python

# Copyright 2007, 2008 VIFF Development Team.
#
# This file is part of VIFF, the Virtual Ideal Functionality Framework.
#
# VIFF is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License (LGPL) as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# VIFF is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
# Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.

# This example is a tribute to the original example of secure
# multi-party computation by Yao in 1982. In his example two
# millionaires meet in the street and they want to securely compare
# their fortunes. They want to do so without revealing how much they
# own to each other. This problem would be easy to solve if both
# millionaires trust a common third party, but we want to solve it
# without access to a third party.
#
# In this example the protocol is run between three millionaires and
# uses a protocol for secure integer comparison by Toft from 2005.
#
# Give a player configuration file as a command line argument or run
# the example with '--help' for help with the command line options.

from optparse import OptionParser
from twisted.internet import reactor

from viff.field import GF
from viff.runtime import create_runtime, gather_shares
from viff.comparison import Toft05Runtime, ComparisonReistad08Mixin
from viff.config import load_config
from viff.util import rand

# We start by defining the protocol, it will be started at the bottom
# of the file.

class Protocol:

    def __init__(self, runtime):
        # Save the Runtime for later use
        self.runtime = runtime
        own_id = self.runtime.id

        Zp = GF(43)
        test_vector = [0, 1, 6, 0]
        self.value = test_vector[own_id - 1]
        m1, m2, m3 = runtime.shamir_share([1, 2, 3], Zp, self.value)
        shares = [m1, m2, m3]
        r_as_bits = [m1, m2, m1]
        r = m3
        mask = m3
        x = m3
        result = self.protocol_part_1(r_as_bits, r, mask, x)
        callback_result = result.addCallback(self.print_answer)
        #results.addCallback(lambda _: runtime.synchronize())
        m1.addCallback(lambda _: runtime.shutdown())

        #Create_random_bits
        #Convert_to_test_values
        #Test_values if useable use random bits otherwise find new bits
        #c = r + x
        #Compare (c,r)

    def protocol_part_1(self, r_as_bits, r, mask, x):
        test = self.test_not_all_zero(r_as_bits, mask)
        self.r = r
        self.x = x
        return test.addCallback(self.protocol_part_2)

    def protocol_part_2(self, test):
        if test:
            c = self.r + self.x
            c_open = self.runtime.open(c)
            result = gather_shares([c_open])
            callback_result = result.addCallback(self.print_answer)
            return callback_result
        else:
            print "No value, False"
            return False

    def print_answer(self, value):
        print "Result ", value
        return True


    def test_not_all_zero(self, r_as_bits, mask):
        #reveal (sum(bits))*mask
        temp = sum(r_as_bits[:]) * mask
        temp_open = self.runtime.open(temp)

        def check_result(check):
            print "Comparing ", check, " to 0"
            if check[0].value == 0:
                return False
            return True

        results = gather_shares([temp_open])
        test_results = results.addCallback(check_result)
        return test_results


    #test = self.check_not_all_one(r_as_bits, r)
     #   test.addCallback(self.protcol_part_2)



#    def print_answer(self, value):
#        print "Result ", value
#
#    def tests(self, shares, test):
#        open_shares = []
#        for sh in shares:
#            open_shares.append(self.runtime.open(sh))
#
#            def check_result(check):
#                for ch in check:
#                    print "Comparing ", ch, " to ", test
#                    if ch == test:
#                        return False
#                return True
#            
#        results = gather_shares(open_shares)
#        test_results = results.addCallback(check_result)
#        return test_results

# Parse command line arguments.
parser = OptionParser()
Toft05Runtime.add_options(parser)
options, args = parser.parse_args()

if len(args) == 0:
    parser.error("you must specify a config file")
else:
    id, players = load_config(args[0])

# Create a deferred Runtime and ask it to run our protocol when ready.
pre_runtime = create_runtime(id, players, 1, options, Toft05Runtime)
pre_runtime.addCallback(Protocol)

# Start the Twisted event loop.
reactor.run()
_______________________________________________
viff-devel mailing list (http://viff.dk/)
viff-devel@viff.dk
http://lists.viff.dk/listinfo.cgi/viff-devel-viff.dk

Reply via email to