viff

changeset 1108:e5aecd62cf67

Splits FieldElement into array of bits LSB first.
author Tord Reistad <tordr@item.ntnu.no>
date Thu, 05 Mar 2009 13:23:42 +0100
parents 88ac6ac5fe70
children 730c30c4d788
files viff/field.py
diffstat 1 files changed, 18 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/viff/field.py	Thu Mar 05 13:00:59 2009 +0100
     1.2 +++ b/viff/field.py	Thu Mar 05 13:23:42 2009 +0100
     1.3 @@ -69,6 +69,7 @@
     1.4  
     1.5  
     1.6  from gmpy import mpz
     1.7 +from math import log, ceil
     1.8  
     1.9  
    1.10  class FieldElement(object):
    1.11 @@ -84,6 +85,23 @@
    1.12  
    1.13      __long__ = __int__
    1.14  
    1.15 +    def split(self):
    1.16 +        """Splits self into bit array LSB first.
    1.17 +
    1.18 +        >>> Zp = GF(29)
    1.19 +        >>> Zp(3).split()
    1.20 +        [1, 1, 0, 0, 0]
    1.21 +        >>> Zp(28).split()
    1.22 +        [0, 0, 1, 1, 1]
    1.23 +        """
    1.24 +        length = int(ceil(log(self.modulus,2)))
    1.25 +        result = [0] * length
    1.26 +        temp = self.value
    1.27 +        for i in range(length):
    1.28 +            result[i] = temp % 2
    1.29 +            temp = temp // 2
    1.30 +        return result
    1.31 +
    1.32  #: Inversion table.
    1.33  #:
    1.34  #: Maps a value *x* to *x^-1*. See `_generate_tables`.