changeset 103:7f816d773350

Merged with Sigurd
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Fri, 10 Jul 2009 11:22:55 +0200
parents 49d36bd742b0 (current diff) 1f6c095453ae (diff)
children 874e71d7aa8c
files
diffstat 1 files changed, 11 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/examples/millionaires.py	Thu Jul 09 17:15:33 2009 +0200
+++ b/examples/millionaires.py	Fri Jul 10 11:22:55 2009 +0200
@@ -28,54 +28,6 @@
 
 # ===========================================================================
 
-
-# The ugly millionaires program
-def mill1(runtime):
-    print "-" * 64
-    print "Program started"
-    print
-
-    networths = runtime.input(runtime.players.keys(), Zp, input)
-    ids = runtime.input(runtime.players.keys(), Zp, runtime.id)
-    ls = zip(ids, networths)
-
-    def max(ls, client, value):
-        print "Max..."
-        if ls == []:
-            return (client, value)
-        def update(r, ls, id, networth, client, value):
-            print "  update", id, networth
-            if r:
-                print "  true"
-                client = id
-                value = networth
-            return max(ls, client, value)
-        id, networth = ls.pop()
-        d = runtime.output(networth >= value)
-        d.addCallback(update, ls, id, networth, client, value)
-        return d
-
-    d = max(ls, Share(runtime, Zp, Zp(0)), Share(runtime, Zp, Zp(0)))
-    def rest((c,v)):
-        d1 = runtime.open(c)
-        d2 = runtime.open(v)
-        results = gather_shares([d1, d2])
-        def result((c,v)):
-            print "Result", c, v
-        results.addCallback(result)
-    d.addCallback(rest)
-
-    runtime.schedule_callback(d, lambda _: runtime.synchronize())
-    runtime.schedule_callback(d, lambda _: runtime.shutdown())
-
-def field_converter(field, x):
-    def convert(x):
-        return field(x.value)
-    x.addCallback(convert)
-    return x
-
-# ===========================================================================
-
 # The not so nice millionaires program
 def runNotNiceMills(runtime):
     # Input player identity.
@@ -83,7 +35,7 @@
     # Input player networths.
     networths = runtime.input(runtime.players.keys(), Zp, input)
 
-    # Create a list of tuples of identity and corresponding networths. 
+    # Create a list of tuples of identity and corresponding networths.
     ls = zip(ids, networths)
 
     richest, max = millionaries(runtime, ls)
@@ -93,11 +45,11 @@
 def not_nice_millionaries(runtime, ls):
     """Computes the richest millionaire given a list of
     pairs of identity and networth.
-    
+
     Returns a pair of identity and networth of the richest millionaire.
     """
 
-    # Declare temporary variables. 
+    # Declare temporary variables.
     # *richest* contains the identity of the currently richest player.
     # *max* contains the networth of the currently richest player.
     richest = Share(runtime, Zp, Zp(0))
@@ -108,7 +60,7 @@
         b = networth >= current_max
         max     = b * networth + (1 - b) * max
         richest = b * id + (1 - b) * richest
-        
+
     return richest, max
 
 # ===========================================================================
@@ -119,7 +71,7 @@
 def maximum(current_id, current_max, id, networth):
     """Computes the maximum of current_max and networth.
 
-    Returns the pair corresponding to the maximum value of 
+    Returns the pair corresponding to the maximum value of
     current_max and networth.
     """
     identity = current_id
@@ -132,11 +84,11 @@
 def millionaries(runtime, ls):
     """Computes the richest millionaire given a list of
     pairs of identity and networth.
-    
+
     Returns a pair of identity and networth of the richest millionaire.
     """
 
-    # Declare temporary variables. 
+    # Declare temporary variables.
     # *richest* contains the identity of the currently richest player.
     # *max* contains the networth of the currently richest player.
     richest = Share(runtime, Zp, Zp(0))
@@ -164,7 +116,7 @@
 
 def output_to_richest(runtime, richest, max):
     """Everyone learns the identity of the richest, but not his networth.
-    
+
     This corresponds to Figur 4(A) in the paper
     "A Domain-Specific Programming Language for Secure Multiparty Computation"
     by Janus Dam Nielsen and Michael I. Schwartzbach.
@@ -182,7 +134,7 @@
 
 def output_status(runtime, richest, max):
     """Everyone learns their own status, but not the others.
-    
+
     This corresponds to Figur 4(B) in the paper
     "A Domain-Specific Programming Language for Secure Multiparty Computation"
     by Janus Dam Nielsen and Michael I. Schwartzbach.
@@ -194,7 +146,7 @@
 
     secret_ids = runtime.input(runtime.players.keys(), Zp, runtime.id)
     ids = runtime.players.keys()
-    
+
     ls = zip(ids, secret_ids)
 
     for id, secret_id in ls:
@@ -227,7 +179,7 @@
     # Input player networths.
     networths = runtime.input(runtime.players.keys(), Zp, input)
 
-    # Create a list of tuples of identity and corresponding networths. 
+    # Create a list of tuples of identity and corresponding networths.
     ls = zip(ids, networths)
 
     richest, max = millionaries(runtime, ls)