viff

changeset 1078:395273e73571

Optimized byte_sub by initializing A only once.
author Marcel Keller <mkeller@cs.au.dk>
date Mon, 12 Jan 2009 19:22:37 +0100
parents 3be465a9f016
children 8d946965e46f
files viff/aes.py
diffstat 1 files changed, 11 insertions(+), 10 deletions(-) [+]
line diff
     1.1 --- a/viff/aes.py	Mon Jan 12 15:26:47 2009 +0100
     1.2 +++ b/viff/aes.py	Mon Jan 12 19:22:37 2009 +0100
     1.3 @@ -67,6 +67,16 @@
     1.4          self.rounds = max(self.n_k, self.n_b) + 6
     1.5          self.runtime = runtime
     1.6  
     1.7 +    # matrix for byte_sub
     1.8 +    A = Matrix([[1,0,0,0,1,1,1,1],
     1.9 +                [1,1,0,0,0,1,1,1],
    1.10 +                [1,1,1,0,0,0,1,1],
    1.11 +                [1,1,1,1,0,0,0,1],
    1.12 +                [1,1,1,1,1,0,0,0],
    1.13 +                [0,1,1,1,1,1,0,0],
    1.14 +                [0,0,1,1,1,1,1,0],
    1.15 +                [0,0,0,1,1,1,1,1]])
    1.16 +
    1.17      def byte_sub(self, state):
    1.18          """ByteSub operation of Rijndael.
    1.19  
    1.20 @@ -107,17 +117,8 @@
    1.21  
    1.22                  bits = bit_decompose(inverted_byte)
    1.23  
    1.24 -                A = Matrix([[1,0,0,0,1,1,1,1],
    1.25 -                            [1,1,0,0,0,1,1,1],
    1.26 -                            [1,1,1,0,0,0,1,1],
    1.27 -                            [1,1,1,1,0,0,0,1],
    1.28 -                            [1,1,1,1,1,0,0,0],
    1.29 -                            [0,1,1,1,1,1,0,0],
    1.30 -                            [0,0,1,1,1,1,1,0],
    1.31 -                            [0,0,0,1,1,1,1,1]])
    1.32 -
    1.33                  # caution: order is lsb first
    1.34 -                vector = A * Matrix(zip(bits)) + Matrix(zip([1,1,0,0,0,1,1,0]))
    1.35 +                vector = AES.A * Matrix(zip(bits)) + Matrix(zip([1,1,0,0,0,1,1,0]))
    1.36                  bits = zip(*vector.rows)[0]
    1.37  
    1.38                  row[i] = reduce(lambda x,y: x + y,