viff

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 diff
     1.1 --- a/viff/aes.py	Tue Jan 13 19:00:40 2009 +0100
     1.2 +++ b/viff/aes.py	Wed Jan 14 17:39:31 2009 +0100
     1.3 @@ -212,7 +212,7 @@
     1.4                      "or of shares thereof."
     1.5              return input
     1.6  
     1.7 -    def encrypt(self, cleartext, key):
     1.8 +    def encrypt(self, cleartext, key, benchmark=False):
     1.9          """Rijndael encryption.
    1.10  
    1.11          Cleartext and key should be either a string or a list of bytes 
    1.12 @@ -227,20 +227,23 @@
    1.13          state = [cleartext[i::4] for i in xrange(4)]
    1.14          key = [key[4*i:4*i+4] for i in xrange(self.n_k)]
    1.15  
    1.16 -        import time
    1.17 -        start = time.time()
    1.18 +        if (benchmark):
    1.19 +            import time
    1.20 +            start = time.time()
    1.21  
    1.22 -        def progress(x, i):
    1.23 -            print "Round %2d: %f" % (i, time.time() - start)
    1.24 -            return x
    1.25 +            def progress(x, i):
    1.26 +                print "Round %2d: %f" % (i, time.time() - start)
    1.27 +                return x
    1.28  
    1.29          expanded_key = self.key_expansion(key)
    1.30  
    1.31 -        print "Key expansion preparation: %f" % (time.time() - start)
    1.32 +        if (benchmark):
    1.33 +            print "Key expansion preparation: %f" % (time.time() - start)
    1.34  
    1.35          self.add_round_key(state, expanded_key[0:self.n_b])
    1.36  
    1.37 -        state[0][0].addCallback(progress, 0)
    1.38 +        if (benchmark):
    1.39 +            state[0][0].addCallback(progress, 0)
    1.40  
    1.41          for i in xrange(1, self.rounds):
    1.42              self.byte_sub(state)
    1.43 @@ -248,14 +251,16 @@
    1.44              self.mix_column(state)
    1.45              self.add_round_key(state, expanded_key[i*self.n_b:(i+1)*self.n_b])
    1.46  
    1.47 -            state[0][0].addCallback(progress, i)
    1.48 -            print "Round %d preparation: %f" % (i, time.time() - start)
    1.49 +            if (benchmark):
    1.50 +                state[0][0].addCallback(progress, i)
    1.51 +                print "Round %2d preparation: %f" % (i, time.time() - start)
    1.52  
    1.53          self.byte_sub(state)
    1.54          self.shift_row(state)
    1.55          self.add_round_key(state, expanded_key[self.rounds*self.n_b:])
    1.56  
    1.57 -        state[0][0].addCallback(progress, self.rounds)
    1.58 -        print "Preparation: %f" % (time.time() - start)
    1.59 +        if (benchmark):
    1.60 +            state[0][0].addCallback(progress, self.rounds)
    1.61 +            print "Preparation: %f" % (time.time() - start)
    1.62  
    1.63          return [byte for word in zip(*state) for byte in word]