viff

changeset 1081:e3b5d98cdbda

Compute every round directly after its preparation in AES encryption.
author Marcel Keller <mkeller@cs.au.dk>
date Wed, 14 Jan 2009 18:16:47 +0100
parents 8d946965e46f
children c1b3ced5bf05
files viff/aes.py
diffstat 1 files changed, 38 insertions(+), 10 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 18:16:47 2009 +0100
     1.3 @@ -231,7 +231,7 @@
     1.4          start = time.time()
     1.5  
     1.6          def progress(x, i):
     1.7 -            print "Round %2d: %f" % (i, time.time() - start)
     1.8 +            print "Round %d: %f" % (i, time.time() - start)
     1.9              return x
    1.10  
    1.11          expanded_key = self.key_expansion(key)
    1.12 @@ -240,22 +240,50 @@
    1.13  
    1.14          self.add_round_key(state, expanded_key[0:self.n_b])
    1.15  
    1.16 -        state[0][0].addCallback(progress, 0)
    1.17 +        def get_trigger(state):
    1.18 +            return state[3][self.n_b-1]
    1.19  
    1.20 -        for i in xrange(1, self.rounds):
    1.21 +        state[3][self.n_b-1].addCallback(progress, 0)
    1.22 +
    1.23 +        def round(_, state, i):
    1.24              self.byte_sub(state)
    1.25              self.shift_row(state)
    1.26              self.mix_column(state)
    1.27              self.add_round_key(state, expanded_key[i*self.n_b:(i+1)*self.n_b])
    1.28  
    1.29 -            state[0][0].addCallback(progress, i)
    1.30 +            state[3][self.n_b-1].addCallback(progress, i)
    1.31 +
    1.32 +            if (i < self.rounds - 1):
    1.33 +                get_trigger(state).addCallback(round, state, i + 1)
    1.34 +            else:
    1.35 +                get_trigger(state).addCallback(final_round, state)
    1.36 +
    1.37              print "Round %d preparation: %f" % (i, time.time() - start)
    1.38  
    1.39 -        self.byte_sub(state)
    1.40 -        self.shift_row(state)
    1.41 -        self.add_round_key(state, expanded_key[self.rounds*self.n_b:])
    1.42 +            return _
    1.43  
    1.44 -        state[0][0].addCallback(progress, self.rounds)
    1.45 -        print "Preparation: %f" % (time.time() - start)
    1.46 +        def final_round(_, state):
    1.47 +            self.byte_sub(state)
    1.48 +            self.shift_row(state)
    1.49 +            self.add_round_key(state, expanded_key[self.rounds*self.n_b:])
    1.50  
    1.51 -        return [byte for word in zip(*state) for byte in word]
    1.52 +            state[3][self.n_b-1].addCallback(progress, self.rounds)
    1.53 +
    1.54 +            get_trigger(state).addCallback(finish, state)
    1.55 +
    1.56 +            print "Round %d preparation: %f" % (self.rounds, time.time() - start)
    1.57 +
    1.58 +            return _
    1.59 +
    1.60 +        def finish(_, state):
    1.61 +            actual_result = [byte for word in zip(*state) for byte in word]
    1.62 +
    1.63 +            for a, b in zip(actual_result, result):
    1.64 +                a.addCallback(b.callback)
    1.65 +
    1.66 +            return _
    1.67 +
    1.68 +        round(None, state, 1)
    1.69 +
    1.70 +        result = [Share(self.runtime, GF256) for i in xrange(4 * self.n_b)]
    1.71 +        return result