viff

changeset 1136:72b7a0717627

Merged.
author Marcel Keller <mkeller@cs.au.dk>
date Tue, 17 Feb 2009 11:21:38 +0100
parents f24861244622 c47c7f119808
children b6d229859b5b
files viff/aes.py
diffstat 1 files changed, 25 insertions(+), 17 deletions(-) [+]
line diff
     1.1 --- a/viff/aes.py	Thu Feb 12 21:18:31 2009 +0100
     1.2 +++ b/viff/aes.py	Tue Feb 17 11:21:38 2009 +0100
     1.3 @@ -21,6 +21,7 @@
     1.4  
     1.5  
     1.6  import time
     1.7 +import operator
     1.8  
     1.9  from viff.field import GF256
    1.10  from viff.runtime import Share, gather_shares
    1.11 @@ -278,7 +279,7 @@
    1.12                      "or of shares thereof."
    1.13              return input
    1.14  
    1.15 -    def encrypt(self, cleartext, key, benchmark=False):
    1.16 +    def encrypt(self, cleartext, key, benchmark=False, prepare_at_once=False):
    1.17          """Rijndael encryption.
    1.18  
    1.19          Cleartext and key should be either a string or a list of bytes 
    1.20 @@ -338,12 +339,13 @@
    1.21              self.mix_column(state)
    1.22              self.add_round_key(state, expanded_key[i*self.n_b:(i+1)*self.n_b])
    1.23  
    1.24 -            get_last(state).addCallback(progress, i, time.time())
    1.25 +            if (not prepare_at_once):
    1.26 +                get_last(state).addCallback(progress, i, time.time())
    1.27  
    1.28 -            if (i < self.rounds - 1):
    1.29 -                get_trigger(state).addCallback(round, state, i + 1)
    1.30 -            else:
    1.31 -                get_trigger(state).addCallback(final_round, state)
    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              prep_progress(i, start_round)
    1.38  
    1.39 @@ -358,25 +360,31 @@
    1.40  
    1.41              get_last(state).addCallback(progress, self.rounds, time.time())
    1.42  
    1.43 -            get_trigger(state).addCallback(finish, state)
    1.44 +            if (benchmark):
    1.45 +                get_trigger(state).addCallback(finish, state)
    1.46 +
    1.47 +            # connect to final result
    1.48 +            for a, b in zip(reduce(operator.add, zip(*state)), result):
    1.49 +                a.addCallback(b.callback)
    1.50  
    1.51              prep_progress(self.rounds, start_round)
    1.52  
    1.53              return _
    1.54  
    1.55          def finish(_, state):
    1.56 -            actual_result = [byte for word in zip(*state) for byte in word]
    1.57 -
    1.58 -            for a, b in zip(actual_result, result):
    1.59 -                a.addCallback(b.callback)
    1.60 -
    1.61 -            if (benchmark):
    1.62 -                print "Total preparation time: %f" % preparation
    1.63 -                print "Total communication time: %f" % communication
    1.64 +            print "Total preparation time: %f" % preparation
    1.65 +            print "Total communication time: %f" % communication
    1.66  
    1.67              return _
    1.68  
    1.69 -        round(None, state, 1)
    1.70 +        result = [Share(self.runtime, GF256) for i in xrange(4 * self.n_b)]
    1.71  
    1.72 -        result = [Share(self.runtime, GF256) for i in xrange(4 * self.n_b)]
    1.73 +        if (prepare_at_once):
    1.74 +            for i in range(1, self.rounds):
    1.75 +                round(None, state, i)
    1.76 +
    1.77 +            final_round(None, state)
    1.78 +        else:
    1.79 +            round(None, state, 1)
    1.80 +
    1.81          return result