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 wrap: on
line diff
--- a/viff/field.py	Thu Mar 05 21:17:10 2009 +0100
+++ b/viff/field.py	Fri Mar 06 12:03:08 2009 +0100
@@ -32,6 +32,7 @@
 
 >>> x = Zp(10)
 >>> y = Zp(15)
+>>> z = Zp(1)
 
 Addition and subtraction (with modulo reduction):
 
@@ -40,6 +41,15 @@
 >>> x - y
 {14}
 
+Bitwise xor for field elements:
+
+>>> z ^ z
+{0}
+>>> z ^ 0
+{1}
+>>> 1 ^ z
+{0}
+
 Exponentiation:
 
 >>> x**3
@@ -397,6 +407,20 @@
             """Subtraction (reflected argument version)."""
             return GFElement(other - self.value)
 
+        def __xor__(self, other):
+            """Xor for bitvalues."""
+            if not isinstance(other, (GFElement, int, long)):
+                return NotImplemented
+            try:
+                assert self.field is other.field, "Fields must be identical"
+                return GFElement(self.value ^ other.value)
+            except AttributeError:
+                return GFElement(self.value ^ other)
+
+        def __rxor__(self, other):
+            """Xor for bitvalues (reflected argument version)."""
+            return GFElement(other ^ self.value)
+
         def __mul__(self, other):
             """Multiplication."""
             if not isinstance(other, (GFElement, int, long)):