# HG changeset patch
# User Janus Dam Nielsen <[email protected]>
# Date 1240830411 -7200
# Node ID 4397d64715c625445b0170daf5dfed39ac769219
# Parent  bdc53480bc8ce7d8074a11d157e74c77c773b9a7
Added methods to GF fields for getting the values represented as signed and 
unsigned integers.

diff --git a/viff/field.py b/viff/field.py
--- a/viff/field.py
+++ b/viff/field.py
@@ -505,6 +505,19 @@
             """Extract a bit (index is counted from zero)."""
             return (self.value >> index) & 1
 
+        def signed(self):
+            """ 
+            if x > floor(p/2) then subtract p to obtain negative integer
+            """
+            if self.value > ((self.modulus-1)/2):
+                return self.value - self.modulus
+            else:
+                return self.value
+
+        def unsigned(self):
+            """Returns a unsigned representation of the value"""
+            return self.value
+
         def __repr__(self):
             return "{%d}" % self.value
             #return "GFElement(%d)" % self.value
@@ -514,7 +527,7 @@
 
             This is simply the value enclosed in curly braces.
             """
-            return "{%d}" % self.value
+            return "{%d}" % self.unsigned()
 
         def __eq__(self, other):
             """Equality test."""
diff --git a/viff/test/test_signed_field.py b/viff/test/test_signed_field.py
new file mode 100644
--- /dev/null
+++ b/viff/test/test_signed_field.py
@@ -0,0 +1,91 @@
+#!/usr/bin/python
+# -*- coding: utf-8
+#
+# Copyright 2009 (c) Partisia Market Design.
+#
+# Author: Janus Dam Nielsen
+# Review: need review
+#
+
+# Import system packages.
+import sys
+
+# Import VIFF packages.
+from viff.field import GF
+from viff.comparison import Toft05Runtime
+from viff.test.util import RuntimeTestCase, protocol
+
+p = 30916444023318367583
+Zp = GF(p)
+
+class SignedVsUnsignedTest(RuntimeTestCase):
+
+    runtime_class = Toft05Runtime
+
+    def test_zero_minus_one_signed(self):
+        x = Zp(0)
+        y = Zp(1)
+        z = x - y
+        self.assertEquals(z.signed(), -1)       
+
+    def test_zero_minus_one_unsigned(self):
+        x = Zp(0)
+        y = Zp(1)
+        z = x - y
+        self.assertEquals(z.unsigned(), p-1)       
+
+    def test_maxint_plus_42_signed(self):
+        x = Zp(p)
+        y = Zp(42)
+        z = x + y
+        self.assertEquals(z.signed(), 42)
+
+    @protocol
+    def test_little_subtracted_big_signed(self, runtime):
+        x = Zp(14)
+        y = Zp(42)
+        z = x - y
+        self.assertEquals(z.signed(), -28)       
+
+    @protocol
+    def test_little_subtracted_big_unsigned(self, runtime):
+        x = Zp(14)
+        y = Zp(42)
+        z = x - y
+        self.assertEquals(z.unsigned(), p-28)       
+
+    @protocol
+    def test_big_subtracted_little_signed(self, runtime):
+        x = Zp(42)
+        y = Zp(14)
+        z = x - y
+        self.assertEquals(z.signed(), 28)       
+
+    @protocol
+    def test_big_subtracted_little_unsigned(self, runtime):
+        x = Zp(42)
+        y = Zp(14)
+        z = x - y
+        self.assertEquals(z.unsigned(), 28)       
+
+    @protocol
+    def test_little_add_big_signed(self, runtime):
+        x = Zp(1)
+        y = Zp(p)
+        z = x + y
+        self.assertEquals(z.signed(), 1)
+
+    @protocol
+    def test_little_add_big_unsigned(self, runtime):
+        x = Zp(1)
+        y = Zp(p)
+        z = x + y
+        self.assertEquals(z.unsigned(), 1)
+
+    @protocol
+    def test_maxint_signed(self, runtime):
+        phalf = (p-1)/2
+        x = Zp(phalf)
+        y = Zp(1)
+        z = x + y 
+        self.assertEquals(z.signed(), -phalf)
_______________________________________________
viff-patches mailing list
[email protected]
http://lists.viff.dk/listinfo.cgi/viff-patches-viff.dk

Reply via email to