viff
view apps/sort.py @ 1575:cfb8e1485006
Updated email address.
| author | Thomas P Jakobsen <tpj@cs.au.dk> |
|---|---|
| date | Wed Dec 15 13:00:00 2010 +0100 (17 months ago) |
| parents | 6cd5ceb87542 |
| children |
line source
1 #!/usr/bin/env python
3 # Copyright 2008 VIFF Development Team.
4 #
5 # This file is part of VIFF, the Virtual Ideal Functionality Framework.
6 #
7 # VIFF is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License (LGPL) as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
11 #
12 # VIFF is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
15 # Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
20 # This example demonstrates how a secure sorting algorithm can be
21 # implemented using VIFF. The algorithm used is bitonic sort which was
22 # chosen in order to maximize the amount of work (comparisons) done in
23 # parallel. Bitonic sort uses 1/2 * log n * (log n + 1) comparator
24 # stages and each stage consists of n/2 parallel comparisons. The
25 # total number of comparisons is thus in O(n log^2 n).
26 #
27 # This is larger than the lower bound of Omega(n log n) for comparison
28 # based sorting algorithms, achieved by mergesort and other well known
29 # algorithms. The problem with mergesort is its merging step: to merge
30 # two sorted arrays of length n/2 one needs up to n comparisons, and
31 # what is worse, the comparisons must be made one after another. The
32 # mergesort does one merge with lists of size n/2, two merges with
33 # lists of size n/4, and so on. This gives a total chain of
34 # comparisons of length n + n/2 + n/4 + ... + 1 = 2n - 1, which is
35 # more than the O(log^2 n) stages needed by the Bitonic Sort.
36 #
37 # TODO: Implement mergesort too to better back this up.
38 #
39 # See this page for more analysis and a Java implementation:
40 #
41 # http://iti.fh-flensburg.de/lang/algorithmen/sortieren/bitonic/bitonicen.htm
42 #
43 # It requires ProgressBar v. 2.2 found on
44 # http://pypi.python.org/pypi/progressbar/
45 #
46 # TODO: Predict the number of comparisons and support preprocessing.
47 #
49 # Give a player configuration file as a command line argument or run
50 # the example with '--help' for help with the command line options.
66 # Parse command line arguments.
94 "Showing the number of finished comparisons/total number"
128 @staticmethod
130 """ Calculate how many comparisons will be made by the bitonic
131 sort
132 >>> predict_sort(8)
133 24
134 >>> predict_sort(10)
135 33
136 """
170 # Make a shallow copy -- the algorithm wont be in-place anyway
171 # since we create lots of new Shares as we go along.
183 # Choose m as the greatest power of 2 less than n.
193 """This is added as a callback to the deferred in le, and
194 therefor must pass on the result of the comparison to
195 where it is actually used."""
201 # TODO: We use this simple xor until
202 # http://tracker.viff.dk/issue60 is fixed.
208 # We must swap array[i] and array[j] when they sort in the
209 # wrong direction, that is, when ascending is True and
210 # array[i] > array[j], or when ascending is False (meaning
211 # descending) and array[i] <= array[j].
212 #
213 # Using array[i] <= array[j] in both cases we see that
214 # this is the exclusive-or:
217 # We now wish to calculate
218 #
219 # ai = b * array[j] + (1-b) * array[i]
220 # aj = b * array[i] + (1-b) * array[j]
221 #
222 # which uses four secure multiplications. We can rewrite
223 # this to use only one secure multiplication:
233 # Run doctests
238 # Load configuration file.
241 # Create a deferred Runtime and ask it to run our protocol when ready.
246 # Start the Twisted event loop.
