changeset 1080:d0c7ced69d2d

Added option to control benchmarking to AES encryption.
author Marcel Keller <mkeller@cs.au.dk>
date Wed, 14 Jan 2009 17:39:31 +0100
parents 8d946965e46f
children c1b3ced5bf05
files viff/aes.py
diffstat 1 files changed, 17 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/viff/aes.py	Tue Jan 13 19:00:40 2009 +0100
+++ b/viff/aes.py	Wed Jan 14 17:39:31 2009 +0100
@@ -212,7 +212,7 @@
                     "or of shares thereof."
             return input
 
-    def encrypt(self, cleartext, key):
+    def encrypt(self, cleartext, key, benchmark=False):
         """Rijndael encryption.
 
         Cleartext and key should be either a string or a list of bytes 
@@ -227,20 +227,23 @@
         state = [cleartext[i::4] for i in xrange(4)]
         key = [key[4*i:4*i+4] for i in xrange(self.n_k)]
 
-        import time
-        start = time.time()
+        if (benchmark):
+            import time
+            start = time.time()
 
-        def progress(x, i):
-            print "Round %2d: %f" % (i, time.time() - start)
-            return x
+            def progress(x, i):
+                print "Round %2d: %f" % (i, time.time() - start)
+                return x
 
         expanded_key = self.key_expansion(key)
 
-        print "Key expansion preparation: %f" % (time.time() - start)
+        if (benchmark):
+            print "Key expansion preparation: %f" % (time.time() - start)
 
         self.add_round_key(state, expanded_key[0:self.n_b])
 
-        state[0][0].addCallback(progress, 0)
+        if (benchmark):
+            state[0][0].addCallback(progress, 0)
 
         for i in xrange(1, self.rounds):
             self.byte_sub(state)
@@ -248,14 +251,16 @@
             self.mix_column(state)
             self.add_round_key(state, expanded_key[i*self.n_b:(i+1)*self.n_b])
 
-            state[0][0].addCallback(progress, i)
-            print "Round %d preparation: %f" % (i, time.time() - start)
+            if (benchmark):
+                state[0][0].addCallback(progress, i)
+                print "Round %2d preparation: %f" % (i, time.time() - start)
 
         self.byte_sub(state)
         self.shift_row(state)
         self.add_round_key(state, expanded_key[self.rounds*self.n_b:])
 
-        state[0][0].addCallback(progress, self.rounds)
-        print "Preparation: %f" % (time.time() - start)
+        if (benchmark):
+            state[0][0].addCallback(progress, self.rounds)
+            print "Preparation: %f" % (time.time() - start)
 
         return [byte for word in zip(*state) for byte in word]