viff

changeset 1086:e954b0a9c939

Improved benchmarking of AES encryption: Time is now splitted in preparation and communication time.
author Marcel Keller <mkeller@cs.au.dk>
date Thu, 22 Jan 2009 19:15:41 +0100
parents 29d85ec50160
children 2e3f751f0667
files viff/aes.py
diffstat 1 files changed, 36 insertions(+), 15 deletions(-) [+]
line diff
     1.1 --- a/viff/aes.py	Tue Jan 20 11:30:27 2009 +0100
     1.2 +++ b/viff/aes.py	Thu Jan 22 19:15:41 2009 +0100
     1.3 @@ -20,6 +20,8 @@
     1.4  __docformat__ = "restructuredtext"
     1.5  
     1.6  
     1.7 +import time
     1.8 +
     1.9  from viff.field import GF256
    1.10  from viff.runtime import Share
    1.11  from viff.matrix import Matrix
    1.12 @@ -254,57 +256,72 @@
    1.13          key = [key[4*i:4*i+4] for i in xrange(self.n_k)]
    1.14  
    1.15          if (benchmark):
    1.16 -            import time
    1.17              start = time.time()
    1.18 +            global preparation, communication
    1.19 +            preparation = 0
    1.20 +            communication = 0
    1.21  
    1.22 -            def progress(x, i):
    1.23 -                print "Round %2d: %f" % (i, time.time() - start)
    1.24 +            def progress(x, i, start_round):
    1.25 +                time_diff = time.time() - start_round
    1.26 +                global communication
    1.27 +                communication += time_diff
    1.28 +                print "Round %2d: %f, %f" % \
    1.29 +                    (i, time_diff, time.time() - start)
    1.30                  return x
    1.31 +
    1.32 +            def prep_progress(i, start_round):
    1.33 +                time_diff = time.time() - start_round
    1.34 +                global preparation
    1.35 +                preparation += time_diff
    1.36 +                print "Round %2d preparation: %f, %f" % \
    1.37 +                    (i, time_diff, time.time() - start)
    1.38          else:
    1.39 -            progress = lambda x, i: x
    1.40 +            progress = lambda x, i, start_round: x
    1.41 +            prep_progress = lambda i, start_round: None
    1.42  
    1.43          expanded_key = self.key_expansion(key)
    1.44  
    1.45 -        if (benchmark):
    1.46 -            print "Key expansion preparation: %f" % (time.time() - start)
    1.47 +        self.add_round_key(state, expanded_key[0:self.n_b])
    1.48  
    1.49 -        self.add_round_key(state, expanded_key[0:self.n_b])
    1.50 +        prep_progress(0, start)
    1.51  
    1.52          def get_trigger(state):
    1.53              return state[3][self.n_b-1]
    1.54  
    1.55 -        state[3][self.n_b-1].addCallback(progress, 0)
    1.56 +        def get_last(state):
    1.57 +            return state[3][self.n_b-1]
    1.58  
    1.59          def round(_, state, i):
    1.60 +            start_round = time.time()
    1.61 +            
    1.62              self.byte_sub(state)
    1.63              self.shift_row(state)
    1.64              self.mix_column(state)
    1.65              self.add_round_key(state, expanded_key[i*self.n_b:(i+1)*self.n_b])
    1.66  
    1.67 -            state[3][self.n_b-1].addCallback(progress, i)
    1.68 +            get_last(state).addCallback(progress, i, time.time())
    1.69  
    1.70              if (i < self.rounds - 1):
    1.71                  get_trigger(state).addCallback(round, state, i + 1)
    1.72              else:
    1.73                  get_trigger(state).addCallback(final_round, state)
    1.74  
    1.75 -            if (benchmark):
    1.76 -                print "Round %2d preparation: %f" % (i, time.time() - start)
    1.77 +            prep_progress(i, start_round)
    1.78  
    1.79              return _
    1.80  
    1.81          def final_round(_, state):
    1.82 +            start_round = time.time()
    1.83 +
    1.84              self.byte_sub(state)
    1.85              self.shift_row(state)
    1.86              self.add_round_key(state, expanded_key[self.rounds*self.n_b:])
    1.87  
    1.88 -            state[3][self.n_b-1].addCallback(progress, self.rounds)
    1.89 +            get_last(state).addCallback(progress, self.rounds, time.time())
    1.90  
    1.91              get_trigger(state).addCallback(finish, state)
    1.92  
    1.93 -            if (benchmark):
    1.94 -                print "Round %2d preparation: %f" % (self.rounds, 
    1.95 -                                                     time.time() - start)
    1.96 +            prep_progress(self.rounds, start_round)
    1.97  
    1.98              return _
    1.99  
   1.100 @@ -314,6 +331,10 @@
   1.101              for a, b in zip(actual_result, result):
   1.102                  a.addCallback(b.callback)
   1.103  
   1.104 +            if (benchmark):
   1.105 +                print "Total preparation time: %f" % preparation
   1.106 +                print "Total communication time: %f" % communication
   1.107 +
   1.108              return _
   1.109  
   1.110          round(None, state, 1)