### changeset 1104:f12590662f93

Initialize constants in dedicated function. This works around a false positive reported by pyflakes. It became confused since the alog, log, and mul variables were explicitly deleted from the global scope when they were no longer needed. We now define there variables as local variables in the initialize function.
author Martin Geisler Fri, 20 Feb 2009 09:58:24 +0100 24ba1dae612d 1d5f01ddd720 viff/test/rijndael.py 1 files changed, 131 insertions(+), 141 deletions(-) [+]
line wrap: on
line diff
```--- a/viff/test/rijndael.py	Thu Feb 19 16:16:46 2009 +0100
+++ b/viff/test/rijndael.py	Fri Feb 20 09:58:24 2009 +0100
@@ -36,109 +36,6 @@
# [keysize][block_size]
num_rounds = {16: {16: 10, 24: 12, 32: 14}, 24: {16: 12, 24: 12, 32: 14}, 32: {16: 14, 24: 14, 32: 14}}

-A = [[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],
-     [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]]
-
-# produce log and alog tables, needed for multiplying in the
-# field GF(2^m) (generator = 3)
-alog = [1]
-for i in xrange(255):
-    j = (alog[-1] << 1) ^ alog[-1]
-    if j & 0x100 != 0:
-        j ^= 0x11B
-    alog.append(j)
-
-log = [0] * 256
-for i in xrange(1, 255):
-    log[alog[i]] = i
-
-# multiply two elements of GF(2^m)
-def mul(a, b):
-    if a == 0 or b == 0:
-        return 0
-    return alog[(log[a & 0xFF] + log[b & 0xFF]) % 255]
-
-# substitution box based on F^{-1}(x)
-box = [[0] * 8 for i in xrange(256)]
-box[1][7] = 1
-for i in xrange(2, 256):
-    j = alog[255 - log[i]]
-    for t in xrange(8):
-        box[i][t] = (j >> (7 - t)) & 0x01
-
-B = [0, 1, 1, 0, 0, 0, 1, 1]
-
-# affine transform:  box[i] <- B + A*box[i]
-cox = [[0] * 8 for i in xrange(256)]
-for i in xrange(256):
-    for t in xrange(8):
-        cox[i][t] = B[t]
-        for j in xrange(8):
-            cox[i][t] ^= A[t][j] * box[i][j]
-
-# S-boxes and inverse S-boxes
-S =  [0] * 256
-Si = [0] * 256
-for i in xrange(256):
-    S[i] = cox[i][0] << 7
-    for t in xrange(1, 8):
-        S[i] ^= cox[i][t] << (7-t)
-    Si[S[i] & 0xFF] = i
-
-# T-boxes
-G = [[2, 1, 1, 3],
-    [3, 2, 1, 1],
-    [1, 3, 2, 1],
-    [1, 1, 3, 2]]
-
-AA = [[0] * 8 for i in xrange(4)]
-
-for i in xrange(4):
-    for j in xrange(4):
-        AA[i][j] = G[i][j]
-        AA[i][i+4] = 1
-
-for i in xrange(4):
-    pivot = AA[i][i]
-    if pivot == 0:
-        t = i + 1
-        while AA[t][i] == 0 and t < 4:
-            t += 1
-            assert t != 4, 'G matrix must be invertible'
-            for j in xrange(8):
-                AA[i][j], AA[t][j] = AA[t][j], AA[i][j]
-            pivot = AA[i][i]
-    for j in xrange(8):
-        if AA[i][j] != 0:
-            AA[i][j] = alog[(255 + log[AA[i][j] & 0xFF] - log[pivot & 0xFF]) % 255]
-    for t in xrange(4):
-        if i != t:
-            for j in xrange(i+1, 8):
-                AA[t][j] ^= mul(AA[i][j], AA[t][i])
-            AA[t][i] = 0
-
-iG = [[0] * 4 for i in xrange(4)]
-
-for i in xrange(4):
-    for j in xrange(4):
-        iG[i][j] = AA[i][j + 4]
-
-def mul4(a, bs):
-    if a == 0:
-        return 0
-    r = 0
-    for b in bs:
-        r <<= 8
-        if b != 0:
-            r = r | mul(a, b)
-    return r
-
T1 = []
T2 = []
T3 = []
@@ -152,48 +49,141 @@
U3 = []
U4 = []

-for t in xrange(256):
-    s = S[t]
-    T1.append(mul4(s, G[0]))
-    T2.append(mul4(s, G[1]))
-    T3.append(mul4(s, G[2]))
-    T4.append(mul4(s, G[3]))
+S =  [0] * 256
+Si = [0] * 256

-    s = Si[t]
-    T5.append(mul4(s, iG[0]))
-    T6.append(mul4(s, iG[1]))
-    T7.append(mul4(s, iG[2]))
-    T8.append(mul4(s, iG[3]))
+rcon = [1]

-    U1.append(mul4(t, iG[0]))
-    U2.append(mul4(t, iG[1]))
-    U3.append(mul4(t, iG[2]))
-    U4.append(mul4(t, iG[3]))
+def initialize():
+    """Called once at module import to initialize constants defined above."""

-# round constants
-rcon = [1]
-r = 1
-for t in xrange(1, 30):
-    r = mul(2, r)
-    rcon.append(r)
+    A = [[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],
+         [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]]

-del A
-del AA
-del pivot
-del B
-del G
-del box
-del log
-del alog
-del i
-del j
-del r
-del s
-del t
-del mul
-del mul4
-del cox
-del iG
+    # produce log and alog tables, needed for multiplying in the
+    # field GF(2^m) (generator = 3)
+    alog = [1]
+    for i in xrange(255):
+        j = (alog[-1] << 1) ^ alog[-1]
+        if j & 0x100 != 0:
+            j ^= 0x11B
+        alog.append(j)
+
+    log = [0] * 256
+    for i in xrange(1, 255):
+        log[alog[i]] = i
+
+    # multiply two elements of GF(2^m)
+    def mul(a, b):
+        if a == 0 or b == 0:
+            return 0
+        return alog[(log[a & 0xFF] + log[b & 0xFF]) % 255]
+
+    # substitution box based on F^{-1}(x)
+    box = [[0] * 8 for i in xrange(256)]
+    box[1][7] = 1
+    for i in xrange(2, 256):
+        j = alog[255 - log[i]]
+        for t in xrange(8):
+            box[i][t] = (j >> (7 - t)) & 0x01
+
+    B = [0, 1, 1, 0, 0, 0, 1, 1]
+
+    # affine transform:  box[i] <- B + A*box[i]
+    cox = [[0] * 8 for i in xrange(256)]
+    for i in xrange(256):
+        for t in xrange(8):
+            cox[i][t] = B[t]
+            for j in xrange(8):
+                cox[i][t] ^= A[t][j] * box[i][j]
+
+    # S-boxes and inverse S-boxes
+    for i in xrange(256):
+        S[i] = cox[i][0] << 7
+        for t in xrange(1, 8):
+            S[i] ^= cox[i][t] << (7-t)
+        Si[S[i] & 0xFF] = i
+
+    # T-boxes
+    G = [[2, 1, 1, 3],
+        [3, 2, 1, 1],
+        [1, 3, 2, 1],
+        [1, 1, 3, 2]]
+
+    AA = [[0] * 8 for i in xrange(4)]
+
+    for i in xrange(4):
+        for j in xrange(4):
+            AA[i][j] = G[i][j]
+            AA[i][i+4] = 1
+
+    for i in xrange(4):
+        pivot = AA[i][i]
+        if pivot == 0:
+            t = i + 1
+            while AA[t][i] == 0 and t < 4:
+                t += 1
+                assert t != 4, 'G matrix must be invertible'
+                for j in xrange(8):
+                    AA[i][j], AA[t][j] = AA[t][j], AA[i][j]
+                pivot = AA[i][i]
+        for j in xrange(8):
+            if AA[i][j] != 0:
+                AA[i][j] = alog[(255 + log[AA[i][j] & 0xFF] - log[pivot & 0xFF]) % 255]
+        for t in xrange(4):
+            if i != t:
+                for j in xrange(i+1, 8):
+                    AA[t][j] ^= mul(AA[i][j], AA[t][i])
+                AA[t][i] = 0
+
+    iG = [[0] * 4 for i in xrange(4)]
+
+    for i in xrange(4):
+        for j in xrange(4):
+            iG[i][j] = AA[i][j + 4]
+
+    def mul4(a, bs):
+        if a == 0:
+            return 0
+        r = 0
+        for b in bs:
+            r <<= 8
+            if b != 0:
+                r = r | mul(a, b)
+        return r
+
+    for t in xrange(256):
+        s = S[t]
+        T1.append(mul4(s, G[0]))
+        T2.append(mul4(s, G[1]))
+        T3.append(mul4(s, G[2]))
+        T4.append(mul4(s, G[3]))
+
+        s = Si[t]
+        T5.append(mul4(s, iG[0]))
+        T6.append(mul4(s, iG[1]))
+        T7.append(mul4(s, iG[2]))
+        T8.append(mul4(s, iG[3]))
+
+        U1.append(mul4(t, iG[0]))
+        U2.append(mul4(t, iG[1]))
+        U3.append(mul4(t, iG[2]))
+        U4.append(mul4(t, iG[3]))
+
+    # round constants
+    r = 1
+    for t in xrange(1, 30):
+        r = mul(2, r)
+        rcon.append(r)
+
+# initialize constants
+initialize()

class rijndael:
def __init__(self, key, block_size = 16):```