viff

changeset 775:99c56b32fd54

Precompute a multiplication table for GF256.
author Martin Geisler <mg@daimi.au.dk>
date Fri, 23 May 2008 10:03:36 +0200
parents d7d3f6ddb39b
children 577c1e069d06
files viff/field.py
diffstat 1 files changed, 14 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/viff/field.py	Thu May 22 10:12:46 2008 +0200
     1.2 +++ b/viff/field.py	Fri May 23 10:03:36 2008 +0200
     1.3 @@ -88,6 +88,10 @@
     1.4  #: Maps a value *x* to *x^-1*. See `_generate_tables`.
     1.5  _inv_table = {}
     1.6  
     1.7 +#: Multiplication table.
     1.8 +#:
     1.9 +#: Maps *(x,y)* to *xy*. See `_generate_tables`.
    1.10 +_mul_table = {}
    1.11  
    1.12  def _generate_tables():
    1.13      """Generate tables with logarithms, antilogarithms (exponentials)
    1.14 @@ -111,6 +115,15 @@
    1.15      _exp_table[255] = _exp_table[0]
    1.16      _log_table[0] = 0
    1.17  
    1.18 +    for x in range(256):
    1.19 +        for y in range(256):
    1.20 +            if x == 0 or y == 0:
    1.21 +                z = 0
    1.22 +            else:
    1.23 +                log_product = (_log_table[x] + _log_table[y]) % 255
    1.24 +                z = _exp_table[log_product]
    1.25 +            _mul_table[(x,y)] = z
    1.26 +
    1.27      #_inv_table[0] = 0
    1.28      for c in range(1, 256):
    1.29          _inv_table[c] = _exp_table[255 - _log_table[c]]
    1.30 @@ -193,11 +206,7 @@
    1.31              return NotImplemented
    1.32          if isinstance(other, GF256):
    1.33              other = other.value
    1.34 -        if self.value == 0 or other == 0:
    1.35 -            return GF256(0)
    1.36 -        else:
    1.37 -            log_product = (_log_table[self.value] + _log_table[other]) % 255
    1.38 -            return GF256(_exp_table[log_product])
    1.39 +        return GF256(_mul_table[(self.value, other)])
    1.40  
    1.41  
    1.42      #: Multiply this and another GF256 element (reflected argument version).