## viff

### changeset 1114:10b86919c433

Merge with Tord.
author Martin Geisler Sat, 07 Mar 2009 20:55:08 +0100 c73d989810fb dea34098e8fb eb4dfa98b439 2 files changed, 54 insertions(+), 0 deletions(-) [+]
line diff
```     1.1 --- a/viff/field.py	Thu Feb 26 21:15:25 2009 +0100
1.2 +++ b/viff/field.py	Sat Mar 07 20:55:08 2009 +0100
1.3 @@ -32,6 +32,7 @@
1.4
1.5  >>> x = Zp(10)
1.6  >>> y = Zp(15)
1.7 +>>> z = Zp(1)
1.8
1.9  Addition and subtraction (with modulo reduction):
1.10
1.11 @@ -40,6 +41,15 @@
1.12  >>> x - y
1.13  {14}
1.14
1.15 +Bitwise xor for field elements:
1.16 +
1.17 +>>> z ^ z
1.18 +{0}
1.19 +>>> z ^ 0
1.20 +{1}
1.21 +>>> 1 ^ z
1.22 +{0}
1.23 +
1.24  Exponentiation:
1.25
1.26  >>> x**3
1.27 @@ -69,6 +79,7 @@
1.28
1.29
1.30  from gmpy import mpz
1.31 +from math import log, ceil
1.32
1.33
1.34  class FieldElement(object):
1.35 @@ -84,6 +95,25 @@
1.36
1.37      __long__ = __int__
1.38
1.39 +    def split(self):
1.40 +        """Splits self into bit array LSB first.
1.41 +
1.42 +        >>> Zp = GF(29)
1.43 +        >>> Zp(3).split()
1.44 +        [{1}, {1}, {0}, {0}, {0}]
1.45 +        >>> Zp(28).split()
1.46 +        [{0}, {0}, {1}, {1}, {1}]
1.47 +        >>> GF256(8).split()
1.48 +        [[0], [0], [0], [1], [0], [0], [0], [0]]
1.49 +        """
1.50 +        length = int(ceil(log(self.modulus,2)))
1.51 +        result = [0] * length
1.52 +        temp = self.value
1.53 +        for i in range(length):
1.54 +            result[i] = self.field(temp % 2)
1.55 +            temp = temp // 2
1.56 +        return result
1.57 +
1.58  #: Inversion table.
1.59  #:
1.60  #: Maps a value *x* to *x^-1*. See `_generate_tables`.
1.61 @@ -377,6 +407,20 @@
1.62              """Subtraction (reflected argument version)."""
1.63              return GFElement(other - self.value)
1.64
1.65 +        def __xor__(self, other):
1.66 +            """Xor for bitvalues."""
1.67 +            if not isinstance(other, (GFElement, int, long)):
1.68 +                return NotImplemented
1.69 +            try:
1.70 +                assert self.field is other.field, "Fields must be identical"
1.71 +                return GFElement(self.value ^ other.value)
1.72 +            except AttributeError:
1.73 +                return GFElement(self.value ^ other)
1.74 +
1.75 +        def __rxor__(self, other):
1.76 +            """Xor for bitvalues (reflected argument version)."""
1.77 +            return GFElement(other ^ self.value)
1.78 +
1.79          def __mul__(self, other):
1.80              """Multiplication."""
1.81              if not isinstance(other, (GFElement, int, long)):
```
```     2.1 --- a/viff/util.py	Thu Feb 26 21:15:25 2009 +0100
2.2 +++ b/viff/util.py	Sat Mar 07 20:55:08 2009 +0100
2.3 @@ -388,6 +388,16 @@
2.4                           for key, value in usage.iteritems()])
2.5          _last_memory_usage = usage
2.6
2.7 +def if_then(cond, a, b):
2.8 +    """If then else operator works both for integers and for shares.
2.9 +
2.10 +    >>> if_then(0, 3, 6)
2.11 +    6
2.12 +    >>> if_then(1, 3, 6)
2.13 +    3
2.14 +    """
2.15 +    return b + cond * (a - b)
2.16 +
2.17  if __name__ == "__main__":
2.18      import doctest    #pragma NO COVER
2.19      doctest.testmod() #pragma NO COVER
```