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 wrap: on
line diff
--- a/viff/field.py	Thu Mar 05 13:00:59 2009 +0100
+++ b/viff/field.py	Thu Mar 05 13:23:42 2009 +0100
@@ -69,6 +69,7 @@
 
 
 from gmpy import mpz
+from math import log, ceil
 
 
 class FieldElement(object):
@@ -84,6 +85,23 @@
 
     __long__ = __int__
 
+    def split(self):
+        """Splits self into bit array LSB first.
+
+        >>> Zp = GF(29)
+        >>> Zp(3).split()
+        [1, 1, 0, 0, 0]
+        >>> Zp(28).split()
+        [0, 0, 1, 1, 1]
+        """
+        length = int(ceil(log(self.modulus,2)))
+        result = [0] * length
+        temp = self.value
+        for i in range(length):
+            result[i] = temp % 2
+            temp = temp // 2
+        return result
+
 #: Inversion table.
 #:
 #: Maps a value *x* to *x^-1*. See `_generate_tables`.