viff

changeset 876:3d13185afed2

Implemented bitonic sort on secret shared values. This is a sorting algorithm designed for many parallel processors and I hope that this will make it perform better than a traditional mergesort or quicksort.
author Martin Geisler <mg@daimi.au.dk>
date Fri, 08 Aug 2008 10:18:10 +0200
parents d897fe37f20e
children f12a36276d56
files apps/sort.py
diffstat 1 files changed, 150 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/apps/sort.py	Fri Aug 08 10:18:10 2008 +0200
     1.3 @@ -0,0 +1,150 @@
     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 +# Give a player configuration file as a command line argument or run
    1.24 +# the example with '--help' for help with the command line options.
    1.25 +
    1.26 +from math import log
    1.27 +from optparse import OptionParser
    1.28 +from twisted.internet import reactor
    1.29 +
    1.30 +from viff.field import GF
    1.31 +from viff.runtime import Runtime, create_runtime, gather_shares
    1.32 +from viff.comparison import Toft07Runtime
    1.33 +from viff.config import load_config
    1.34 +from viff.util import find_prime, rand, dprint
    1.35 +
    1.36 +# Parse command line arguments.
    1.37 +parser = OptionParser()
    1.38 +parser.add_option("--modulus",
    1.39 +                  help="lower limit for modulus (can be an expression)")
    1.40 +parser.add_option("-s", "--size", type="int",
    1.41 +                  help="array size (must be power of 2)")
    1.42 +parser.add_option("-m", "--max", type="int",
    1.43 +                  help="maximum size of array numbers")
    1.44 +parser.set_defaults(modulus=2**65, size=8, max=100)
    1.45 +
    1.46 +Runtime.add_options(parser)
    1.47 +
    1.48 +options, args = parser.parse_args()
    1.49 +
    1.50 +if len(args) == 0:
    1.51 +    parser.error("you must specify a config file")
    1.52 +
    1.53 +log_s = log(options.size, 2)
    1.54 +if int(log_s) != log_s:
    1.55 +    parser.error("the array size must be a power of 2")
    1.56 +
    1.57 +Zp = GF(find_prime(options.modulus, blum=True))
    1.58 +
    1.59 +class Protocol:
    1.60 +
    1.61 +    def __init__(self, runtime):
    1.62 +        self.rt = runtime
    1.63 +
    1.64 +        array = self.make_array()
    1.65 +        sorted = self.sort(array)
    1.66 +
    1.67 +        array = gather_shares(map(runtime.open, array))
    1.68 +        sorted = gather_shares(map(runtime.open, sorted))
    1.69 +
    1.70 +        dprint("Original array: %s", array)
    1.71 +        dprint("Sorted array:   %s", sorted)
    1.72 +
    1.73 +        def finish(_):
    1.74 +            runtime.shutdown()
    1.75 +        sorted.addCallback(finish)
    1.76 +
    1.77 +    def make_array(self):
    1.78 +        array = []
    1.79 +        for i in range(options.size):
    1.80 +            inputter = (i % 3) + 1
    1.81 +            if  inputter == self.rt.id:
    1.82 +                number = rand.randint(1, options.max)
    1.83 +                print "Sharing array[%d] = %s" % (i, number)
    1.84 +            else:
    1.85 +                number = None
    1.86 +            share = self.rt.shamir_share([inputter], Zp, number)
    1.87 +            array.append(share)
    1.88 +        return array
    1.89 +
    1.90 +    def sort(self, array):
    1.91 +        # Make a shallow copy -- the algorithm wont be in-place anyway
    1.92 +        # since we create lots of new Shares as we go along.
    1.93 +        array = array[:]
    1.94 +
    1.95 +        def bitonic_sort(low, n, ascending):
    1.96 +            if n > 1:
    1.97 +                m = n // 2
    1.98 +                bitonic_sort(low, m, ascending=True)
    1.99 +                bitonic_sort(low + m, m, ascending=False)
   1.100 +                bitonic_merge(low, n, ascending)
   1.101 +
   1.102 +        def bitonic_merge(low, n, ascending):
   1.103 +            if n > 1:
   1.104 +                m = n // 2
   1.105 +                for i in range(low, low + m):
   1.106 +                    compare(i, i+m, ascending)
   1.107 +                bitonic_merge(low, m, ascending)
   1.108 +                bitonic_merge(low + m, m, ascending)
   1.109 +
   1.110 +        def compare(i, j, ascending):
   1.111 +
   1.112 +            def xor(a, b):
   1.113 +                # TODO: We use this simple xor until
   1.114 +                # http://tracker.viff.dk/issue60 is fixed.
   1.115 +                return a + b - 2*a*b
   1.116 +
   1.117 +            # We must swap array[i] and array[j] when they sort in the
   1.118 +            # wrong direction, that is, when ascending is True and
   1.119 +            # array[i] > array[j], or when ascending is False (meaning
   1.120 +            # descending) and array[i] <= array[j].
   1.121 +            #
   1.122 +            # Using array[i] <= array[j] in both cases we see that
   1.123 +            # this is the exclusive-or:
   1.124 +            b = xor(ascending, array[i] <= array[j])
   1.125 +
   1.126 +            # We now wish to calculate
   1.127 +            #
   1.128 +            #   ai = b * array[j] + (1-b) * array[i]
   1.129 +            #   aj = b * array[i] + (1-b) * array[j]
   1.130 +            #
   1.131 +            # which uses four secure multiplications. We can rewrite
   1.132 +            # this to use only one secure multiplication:
   1.133 +            ai, aj = array[i], array[j]
   1.134 +            b_ai_aj = b * (ai - aj)
   1.135 +
   1.136 +            array[i] = ai - b_ai_aj
   1.137 +            array[j] = aj + b_ai_aj
   1.138 +
   1.139 +        bitonic_sort(0, len(array), ascending=True)
   1.140 +        return array
   1.141 +
   1.142 +
   1.143 +# Load configuration file.
   1.144 +id, players = load_config(args[0])
   1.145 +
   1.146 +# Create a deferred Runtime and ask it to run our protocol when ready.
   1.147 +pre_runtime = create_runtime(id, players, 1, options, runtime_class=Toft07Runtime)
   1.148 +pre_runtime.addCallback(Protocol)
   1.149 +
   1.150 +# Start the Twisted event loop.
   1.151 +reactor.run()
   1.152 +
   1.153 +