Discussion:
[PATCH] [mq]: Signed_and_unsigned_representation
Janus Dam Nielsen
2009-04-29 07:49:30 UTC
Permalink
# HG changeset patch
# User Janus Dam Nielsen <***@alexandra.dk>
# Date 1240990761 -7200
# Node ID a0d0dd74a4f18097713d56e773978d8328e5562e
# Parent 05bcb213270c49803801f20eac701f806776f295
[mq]: Signed_and_unsigned_representation

diff --git a/viff/field.py b/viff/field.py
--- a/viff/field.py
+++ b/viff/field.py
@@ -505,6 +505,20 @@
"""Extract a bit (index is counted from zero)."""
return (self.value >> index) & 1

+ def signed(self):
+ """Return a signed integer representation of the value.
+
+ 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):
+ """Return a unsigned representation of the value"""
+ return self.value
+
def __repr__(self):
return "{%d}" % self.value
#return "GFElement(%d)" % self.value
@@ -514,7 +528,7 @@

This is simply the value enclosed in curly braces.
"""
- return "{%d}" % self.value
+ return "{%d}" % self.unsigned()

def __eq__(self, other):
"""Equality test."""
@@ -626,3 +640,4 @@
if __name__ == "__main__":
import doctest #pragma NO COVER
doctest.testmod() #pragma NO COVER
+
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 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright 2007, 2008, 2009 VIFF Development Team.
+#
+# This file is part of VIFF, the Virtual Ideal Functionality Framework.
+#
+# VIFF is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License (LGPL) as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# VIFF is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+# Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
+
+# Import system packages.
+import sys
+from unittest import TestCase
+
+# Import VIFF packages.
+from viff.field import GF
+
+p = 30916444023318367583
+Zp = GF(p)
+
+class SignedVsUnsignedTest(TestCase):
+
+ 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)
+
+ def test_little_subtracted_big_signed(self):
+ x = Zp(14)
+ y = Zp(42)
+ z = x - y
+ self.assertEquals(z.signed(), -28)
+
+ def test_little_subtracted_big_unsigned(self):
+ x = Zp(14)
+ y = Zp(42)
+ z = x - y
+ self.assertEquals(z.unsigned(), p-28)
+
+ def test_big_subtracted_little_signed(self):
+ x = Zp(42)
+ y = Zp(14)
+ z = x - y
+ self.assertEquals(z.signed(), 28)
+
+ def test_big_subtracted_little_unsigned(self):
+ x = Zp(42)
+ y = Zp(14)
+ z = x - y
+ self.assertEquals(z.unsigned(), 28)
+
+ def test_little_add_big_signed(self):
+ x = Zp(1)
+ y = Zp(p)
+ z = x + y
+ self.assertEquals(z.signed(), 1)
+
+ def test_little_add_big_unsigned(self):
+ x = Zp(1)
+ y = Zp(p)
+ z = x + y
+ self.assertEquals(z.unsigned(), 1)
+
+ def test_maxint_signed(self):
+ phalf = (p-1)/2
+ x = Zp(phalf)
+ y = Zp(1)
+ z = x + y
+ self.assertEquals(z.signed(), -phalf)
Martin Geisler
2009-04-29 08:05:47 UTC
Permalink
Post by Janus Dam Nielsen
# HG changeset patch
# Date 1240990761 -7200
# Node ID a0d0dd74a4f18097713d56e773978d8328e5562e
# Parent 05bcb213270c49803801f20eac701f806776f295
[mq]: Signed_and_unsigned_representation
Great, I've fixed the commit message and pushed this as revision
38e4007378df.
Post by Janus Dam Nielsen
@@ -0,0 +1,91 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright 2007, 2008, 2009 VIFF Development Team.
Fixed the copyright years and the unnecessary coding line too.
--
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
Janus Dam Nielsen
2009-04-29 08:13:42 UTC
Permalink
Great!

You mentioned that you had some considerations about the implementation?

--
Janus Dam Nielsen

R&D Scientist
Alexandra Instituttet
Post by Martin Geisler
Post by Janus Dam Nielsen
# HG changeset patch
# Date 1240990761 -7200
# Node ID a0d0dd74a4f18097713d56e773978d8328e5562e
# Parent 05bcb213270c49803801f20eac701f806776f295
[mq]: Signed_and_unsigned_representation
Great, I've fixed the commit message and pushed this as revision
38e4007378df.
Post by Janus Dam Nielsen
@@ -0,0 +1,91 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright 2007, 2008, 2009 VIFF Development Team.
Fixed the copyright years and the unnecessary coding line too.
--
Martin Geisler
VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
Martin Geisler
2009-04-29 08:12:13 UTC
Permalink
Post by Janus Dam Nielsen
Great!
You mentioned that you had some considerations about the implementation?
Eh, I did? I don't remember right now...
--
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
Janus Dam Nielsen
2009-04-29 08:18:48 UTC
Permalink
In your mail from yesterday you mention that "had some comments on the
I use GF field elements to represent positive as well as negative
integers, and in some situations I want to convert the field elements
to integers, e.g. to display them to a user.
I like the idea, but had some comments on the implementation. Please
update the two patches and resend them.
--
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.






--
Janus Dam Nielsen

R&D Scientist
Alexandra Instituttet
Post by Janus Dam Nielsen
Great!
You mentioned that you had some considerations about the
implementation?
Eh, I did? I don't remember right now...
--
Martin Geisler
VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
Martin Geisler
2009-04-29 22:23:01 UTC
Permalink
Post by Janus Dam Nielsen
In your mail from yesterday you mention that "had some comments on the
I use GF field elements to represent positive as well as negative
integers, and in some situations I want to convert the field elements
to integers, e.g. to display them to a user.
I like the idea, but had some comments on the implementation. Please
update the two patches and resend them.
Ah, right -- that was just the comments I put inline in the review and
which you fixed nicely :-)
--
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
Loading...