changeset 98:856feae68946

Fixed desugaring of secret if statements.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Wed, 08 Jul 2009 13:19:51 +0200
parents 2ac1028c5c1a
children 3e0aadd17eb7
files pysmcl/ideal_functionality.py pysmcl/pretty_print.py pysmcl/secret_ifs.py pysmcl/util.py
diffstat 4 files changed, 74 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/ideal_functionality.py	Mon Jun 08 15:39:32 2009 +0200
+++ b/pysmcl/ideal_functionality.py	Wed Jul 08 13:19:51 2009 +0200
@@ -10,11 +10,11 @@
 import pysmcl.proof_burden
 
 def ideal_functionality(f):
+    print "ideal"
     source_str = inspect.getsource(f)
     source_ast = ast.parse(source_str) # Returns a module
     function_ast = source_ast.body[0] # We want only the function
-    #pretty_print.PrettyPrinter().visit(source_ast)
-    init_statements(function_ast)
+    pysmcl.pretty_print.PrettyPrinter().visit(source_ast)
     # We don't want recursive applications of this decorator
     #function_ast.decorator_list.remove[0]
     print "-"*80
@@ -22,7 +22,8 @@
     pysmcl.secret_annotator.secret_analysis(function_ast)
     t = pysmcl.secret_ifs.TransformIfs().visit(function_ast)
     t.decorator_list.pop()
-    pysmcl.proof_burden.proof_burden(t)
+#     pysmcl.proof_burden.proof_burden(t)
+    pysmcl.pretty_print.PrettyPrinter().visit(t)
     exec compile(ast.Module([t]), "<string>", "exec")
-    #pretty_print.PrettyPrinter().visit(t)
     return eval(function_ast.name)
+
--- a/pysmcl/pretty_print.py	Mon Jun 08 15:39:32 2009 +0200
+++ b/pysmcl/pretty_print.py	Wed Jul 08 13:19:51 2009 +0200
@@ -200,13 +200,24 @@
     elif(isinstance(exp, ast.Str)):
         my_precedence = 100
         r = '"%s"' % exp.s
+    # | Tuple(expr* elts, expr_context ctx)
+    elif(isinstance(exp, ast.Tuple)):
+        my_precedence = 25
+        r = "("+", ".join(["%s" % (expr_string(i)) for i in exp.elts])+")"
+    # | Name(identifier id, expr_context ctx)
+    elif(isinstance(exp, ast.Name)):
+        my_precedence = 1
+        r = exp.id
     else:
+        print exp, isinstance(exp, _ast.Name)
         assert False, "Not implemented of type %s" % type(exp)
 
     if my_precedence <= prec:
         return "(" + r + ")"
     else:
         return r
+
+
     # TODO:
 #             | GeneratorExp(expr elt, comprehension* generators)
 #             | Repr(expr value)
--- a/pysmcl/secret_ifs.py	Mon Jun 08 15:39:32 2009 +0200
+++ b/pysmcl/secret_ifs.py	Wed Jul 08 13:19:51 2009 +0200
@@ -65,31 +65,65 @@
             all_assigned = set(assigned_then.keys() + assigned_else.keys())
             r = []
             # Todo: correct gensyms
-            condname = ast.Name(id="cond%d" % self.cond_counter)
+            condname_id = "cond%d" % self.cond_counter
+
+            condname_store = ast.Name(id=condname_id,
+                                      lineno=node.test.lineno,
+                                      col_offset=node.test.col_offset,
+                                      ctx=ast.Store())
+            condname_load = ast.Name(id=condname_id,
+                                      lineno=node.test.lineno,
+                                      col_offset=node.test.col_offset,
+                                      ctx=ast.Load())
+
             self.cond_counter += 1
-            r.append(ast.Assign(targets=[condname],
-                                value=node.test))
+            r.append(ast.Assign(targets=[condname_store],
+                                value=node.test,
+                                lineno=node.lineno,
+                                col_offset=node.col_offset))
             for i in all_assigned:
-                then_value = assigned_then.get(i, ast.Name(id=i))
-                else_value = assigned_else.get(i, ast.Name(id=i))
-                r.append(ast.Assign(targets=[ast.Name(id=i)], value=
-                                    ast.BinOp(left=
-                                              ast.BinOp(left=condname,
-                                                        op=ast.Mult(),
-                                                        right=then_value),
-                                              op=ast.Add(),
-                                              right=
-                                              ast.BinOp(left=
-                                                        ast.BinOp(left=
-                                                                  ast.Num(1),
-                                                                  op=ast.Sub(),
-                                                                  right=
-                                                                  condname),
-                                                        op=ast.Mult(),
-                                                        right=else_value))))
+                then_value = assigned_then.get(i, ast.Name(id=i, 
+                                                           lineno=node.lineno,
+                                                           col_offset=node.col_offset,
+                                                           ctx=ast.Load()))
+                else_value = assigned_else.get(i, ast.Name(id=i, 
+                                                           lineno=node.lineno,
+                                                           col_offset=node.col_offset,
+                                                           ctx=ast.Load()))
+                r.append(ast.Assign(targets=[ast.Name(id=i,
+                                                      lineno=node.lineno,
+                                                      col_offset=node.col_offset,
+                                                      ctx=ast.Store())],
+                                    value=ast.BinOp(left=
+                                                    ast.BinOp(left=condname_load,
+                                                              op=ast.Mult(lineno=node.lineno,
+                                                                          col_offset=node.col_offset),
+                                                              right=then_value,
+                                                              lineno=node.lineno,
+                                                              col_offset=node.col_offset),
+                                                    op=ast.Add(lineno=node.lineno,
+                                                               col_offset=node.col_offset),
+                                                    right=
+                                                    ast.BinOp(left=
+                                                              ast.BinOp(left=
+                                                                        ast.Num(1,
+                                                                                lineno=node.lineno,
+                                                                                col_offset=node.col_offset),
+                                                                        op=ast.Sub(lineno=node.lineno,
+                                                                                col_offset=node.col_offset),
+                                                                        right=condname_load,
+                                                                        lineno=node.lineno,
+                                                                        col_offset=node.col_offset),
+                                                              op=ast.Mult(lineno=node.lineno,
+                                                                          col_offset=node.col_offset),
+                                                              right=else_value,
+                                                              lineno=node.lineno,
+                                                              col_offset=node.col_offset),
+                                                    lineno=node.lineno,
+                                                    col_offset=node.col_offset),
+                                    lineno=node.lineno,
+                                    col_offset=node.col_offset))
+
             return r # A list of statements will be merged into the list
         else:
             return node
-
-        
-        
--- a/pysmcl/util.py	Mon Jun 08 15:39:32 2009 +0200
+++ b/pysmcl/util.py	Wed Jul 08 13:19:51 2009 +0200
@@ -5,7 +5,7 @@
     """Print the error message.
     *loc* is an ast-node at the given location."""
     loc_tuple = (loc.lineno, loc.col_offset)
-    print ast.dump(loc)
+#    print ast.dump(loc)
     if loc:
         print "at %s" % (loc_tuple, ),
     print msg