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 wrap: on
line diff
--- a/viff/aes.py	Mon Jan 12 15:26:47 2009 +0100
+++ b/viff/aes.py	Mon Jan 12 19:22:37 2009 +0100
@@ -67,6 +67,16 @@
         self.rounds = max(self.n_k, self.n_b) + 6
         self.runtime = runtime
 
+    # matrix for byte_sub
+    A = Matrix([[1,0,0,0,1,1,1,1],
+                [1,1,0,0,0,1,1,1],
+                [1,1,1,0,0,0,1,1],
+                [1,1,1,1,0,0,0,1],
+                [1,1,1,1,1,0,0,0],
+                [0,1,1,1,1,1,0,0],
+                [0,0,1,1,1,1,1,0],
+                [0,0,0,1,1,1,1,1]])
+
     def byte_sub(self, state):
         """ByteSub operation of Rijndael.
 
@@ -107,17 +117,8 @@
 
                 bits = bit_decompose(inverted_byte)
 
-                A = Matrix([[1,0,0,0,1,1,1,1],
-                            [1,1,0,0,0,1,1,1],
-                            [1,1,1,0,0,0,1,1],
-                            [1,1,1,1,0,0,0,1],
-                            [1,1,1,1,1,0,0,0],
-                            [0,1,1,1,1,1,0,0],
-                            [0,0,1,1,1,1,1,0],
-                            [0,0,0,1,1,1,1,1]])
-
                 # caution: order is lsb first
-                vector = A * Matrix(zip(bits)) + Matrix(zip([1,1,0,0,0,1,1,0]))
+                vector = AES.A * Matrix(zip(bits)) + Matrix(zip([1,1,0,0,0,1,1,0]))
                 bits = zip(*vector.rows)[0]
 
                 row[i] = reduce(lambda x,y: x + y,