viff

changeset 1111:dea34098e8fb

Added xor for field elements of type GF.
author Tord Reistad <tordr@item.ntnu.no>
date Fri, 06 Mar 2009 12:03:08 +0100
parents 24d0ffe14c3a
children 10b86919c433
files viff/field.py
diffstat 1 files changed, 24 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/viff/field.py	Thu Mar 05 21:17:10 2009 +0100
     1.2 +++ b/viff/field.py	Fri Mar 06 12:03: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 @@ -397,6 +407,20 @@
    1.28              """Subtraction (reflected argument version)."""
    1.29              return GFElement(other - self.value)
    1.30  
    1.31 +        def __xor__(self, other):
    1.32 +            """Xor for bitvalues."""
    1.33 +            if not isinstance(other, (GFElement, int, long)):
    1.34 +                return NotImplemented
    1.35 +            try:
    1.36 +                assert self.field is other.field, "Fields must be identical"
    1.37 +                return GFElement(self.value ^ other.value)
    1.38 +            except AttributeError:
    1.39 +                return GFElement(self.value ^ other)
    1.40 +
    1.41 +        def __rxor__(self, other):
    1.42 +            """Xor for bitvalues (reflected argument version)."""
    1.43 +            return GFElement(other ^ self.value)
    1.44 +
    1.45          def __mul__(self, other):
    1.46              """Multiplication."""
    1.47              if not isinstance(other, (GFElement, int, long)):