http://hg.viff.dk/viff/rev/10b86919c433
changeset: 1114:10b86919c433
user: Martin Geisler <[email protected]>
date: Sat Mar 07 20:55:08 2009 +0100
summary: Merge with Tord.
diffstat:
2 files changed, 54 insertions(+)
viff/field.py | 44 ++++++++++++++++++++++++++++++++++++++++++++
viff/util.py | 10 ++++++++++
diffs (102 lines):
diff -r c73d989810fb -r 10b86919c433 viff/field.py
--- a/viff/field.py Thu Feb 26 21:15:25 2009 +0100
+++ b/viff/field.py Sat Mar 07 20:55: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
@@ -69,6 +79,7 @@
from gmpy import mpz
+from math import log, ceil
class FieldElement(object):
@@ -84,6 +95,25 @@
__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}]
+ >>> GF256(8).split()
+ [[0], [0], [0], [1], [0], [0], [0], [0]]
+ """
+ length = int(ceil(log(self.modulus,2)))
+ result = [0] * length
+ temp = self.value
+ for i in range(length):
+ result[i] = self.field(temp % 2)
+ temp = temp // 2
+ return result
+
#: Inversion table.
#:
#: Maps a value *x* to *x^-1*. See `_generate_tables`.
@@ -377,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)):
diff -r c73d989810fb -r 10b86919c433 viff/util.py
--- a/viff/util.py Thu Feb 26 21:15:25 2009 +0100
+++ b/viff/util.py Sat Mar 07 20:55:08 2009 +0100
@@ -388,6 +388,16 @@
for key, value in usage.iteritems()])
_last_memory_usage = usage
+def if_then(cond, a, b):
+ """If then else operator works both for integers and for shares.
+
+ >>> if_then(0, 3, 6)
+ 6
+ >>> if_then(1, 3, 6)
+ 3
+ """
+ return b + cond * (a - b)
+
if __name__ == "__main__":
import doctest #pragma NO COVER
doctest.testmod() #pragma NO COVER
_______________________________________________
viff-commits mailing list
[email protected]
http://lists.viff.dk/listinfo.cgi/viff-commits-viff.dk