changeset 776:577c1e069d06

Populate tables with GF256 objects.
author Martin Geisler <mg@daimi.au.dk>
date Fri, 23 May 2008 10:10:52 +0200
parents 99c56b32fd54
children 25b3944dc403
files viff/field.py
diffstat 1 files changed, 40 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/viff/field.py	Fri May 23 10:03:36 2008 +0200
+++ b/viff/field.py	Fri May 23 10:10:52 2008 +0200
@@ -93,42 +93,6 @@
 #: Maps *(x,y)* to *xy*. See `_generate_tables`.
 _mul_table = {}
 
-def _generate_tables():
-    """Generate tables with logarithms, antilogarithms (exponentials)
-    and inverses.
-
-    This updates the `_log_table`, `_exp_table`, and `_inv_table`
-    fields. The generator used is ``0x03``.
-
-    Code adapted from http://www.samiam.org/galois.html.
-    """
-    a = 1
-    for c in range(255):
-        a &= 0xff
-        _exp_table[c] = a
-        d = a & 0x80
-        a <<= 1
-        if d == 0x80:
-            a ^= 0x1b
-        a ^= _exp_table[c]
-        _log_table[_exp_table[c]] = c
-    _exp_table[255] = _exp_table[0]
-    _log_table[0] = 0
-
-    for x in range(256):
-        for y in range(256):
-            if x == 0 or y == 0:
-                z = 0
-            else:
-                log_product = (_log_table[x] + _log_table[y]) % 255
-                z = _exp_table[log_product]
-            _mul_table[(x,y)] = z
-
-    #_inv_table[0] = 0
-    for c in range(1, 256):
-        _inv_table[c] = _exp_table[255 - _log_table[c]]
-
-_generate_tables()
 
 # The class name is slightly wrong since the class instances cannot be
 # said to be represent a field. Instead they represent instances of
@@ -206,7 +170,7 @@
             return NotImplemented
         if isinstance(other, GF256):
             other = other.value
-        return GF256(_mul_table[(self.value, other)])
+        return _mul_table[(self.value, other)]
 
 
     #: Multiply this and another GF256 element (reflected argument version).
@@ -245,7 +209,7 @@
         """
         if self.value == 0:
             raise ZeroDivisionError("Cannot invert zero")
-        return GF256(_inv_table[self.value])
+        return _inv_table[self.value]
 
     def __repr__(self):
         return "[%d]" % self.value
@@ -297,6 +261,44 @@
 GF256.field = GF256
 
 
+def _generate_tables():
+    """Generate tables with logarithms, antilogarithms (exponentials)
+    and inverses.
+
+    This updates the `_log_table`, `_exp_table`, and `_inv_table`
+    fields. The generator used is ``0x03``.
+
+    Code adapted from http://www.samiam.org/galois.html.
+    """
+    a = 1
+    for c in range(255):
+        a &= 0xff
+        _exp_table[c] = a
+        d = a & 0x80
+        a <<= 1
+        if d == 0x80:
+            a ^= 0x1b
+        a ^= _exp_table[c]
+        _log_table[_exp_table[c]] = c
+    _exp_table[255] = _exp_table[0]
+    _log_table[0] = 0
+
+    for x in range(256):
+        for y in range(256):
+            if x == 0 or y == 0:
+                z = 0
+            else:
+                log_product = (_log_table[x] + _log_table[y]) % 255
+                z = _exp_table[log_product]
+            _mul_table[(x,y)] = GF256(z)
+
+    #_inv_table[0] = 0
+    for c in range(1, 256):
+        _inv_table[c] = GF256(_exp_table[255 - _log_table[c]])
+
+_generate_tables()
+
+
 #: Cached fields.
 #:
 #: Calls to GF with identical modulus must return the same class