viff

changeset 1172:38e4007378df

Added signed and unsigned methods to field elements.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Wed, 29 Apr 2009 09:39:21 +0200
parents 0e3d8a3d3b15
children e86a007bddbd
files viff/field.py viff/test/test_signed_field.py
diffstat 2 files changed, 106 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/viff/field.py	Thu Apr 23 11:21:37 2009 +0200
     1.2 +++ b/viff/field.py	Wed Apr 29 09:39:21 2009 +0200
     1.3 @@ -505,6 +505,20 @@
     1.4              """Extract a bit (index is counted from zero)."""
     1.5              return (self.value >> index) & 1
     1.6  
     1.7 +        def signed(self):
     1.8 +            """Return a signed integer representation of the value.
     1.9 +
    1.10 +            If x > floor(p/2) then subtract p to obtain negative integer.
    1.11 +            """
    1.12 +            if self.value > ((self.modulus-1)/2):
    1.13 +                return self.value - self.modulus
    1.14 +            else:
    1.15 +                return self.value
    1.16 +
    1.17 +        def unsigned(self):
    1.18 +            """Return a unsigned representation of the value"""
    1.19 +            return self.value
    1.20 +
    1.21          def __repr__(self):
    1.22              return "{%d}" % self.value
    1.23              #return "GFElement(%d)" % self.value
    1.24 @@ -514,7 +528,7 @@
    1.25  
    1.26              This is simply the value enclosed in curly braces.
    1.27              """
    1.28 -            return "{%d}" % self.value
    1.29 +            return "{%d}" % self.unsigned()
    1.30  
    1.31          def __eq__(self, other):
    1.32              """Equality test."""
    1.33 @@ -626,3 +640,4 @@
    1.34  if __name__ == "__main__":
    1.35      import doctest    #pragma NO COVER
    1.36      doctest.testmod() #pragma NO COVER
    1.37 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/viff/test/test_signed_field.py	Wed Apr 29 09:39:21 2009 +0200
     2.3 @@ -0,0 +1,90 @@
     2.4 +# Copyright 2009 VIFF Development Team.
     2.5 +#
     2.6 +# This file is part of VIFF, the Virtual Ideal Functionality Framework.
     2.7 +#
     2.8 +# VIFF is free software: you can redistribute it and/or modify it
     2.9 +# under the terms of the GNU Lesser General Public License (LGPL) as
    2.10 +# published by the Free Software Foundation, either version 3 of the
    2.11 +# License, or (at your option) any later version.
    2.12 +#
    2.13 +# VIFF is distributed in the hope that it will be useful, but WITHOUT
    2.14 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    2.15 +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
    2.16 +# Public License for more details.
    2.17 +#
    2.18 +# You should have received a copy of the GNU Lesser General Public
    2.19 +# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
    2.20 +
    2.21 +# Import system packages.
    2.22 +import sys
    2.23 +
    2.24 +from twisted.trial.unittest import TestCase
    2.25 +
    2.26 +# Import VIFF packages.
    2.27 +from viff.field import GF
    2.28 +
    2.29 +p = 30916444023318367583
    2.30 +Zp = GF(p)
    2.31 +
    2.32 +class SignedVsUnsignedTest(TestCase):
    2.33 +
    2.34 +    def test_zero_minus_one_signed(self):
    2.35 +        x = Zp(0)
    2.36 +        y = Zp(1)
    2.37 +        z = x - y
    2.38 +        self.assertEquals(z.signed(), -1)       
    2.39 +
    2.40 +    def test_zero_minus_one_unsigned(self):
    2.41 +        x = Zp(0)
    2.42 +        y = Zp(1)
    2.43 +        z = x - y
    2.44 +        self.assertEquals(z.unsigned(), p-1)       
    2.45 +
    2.46 +    def test_maxint_plus_42_signed(self):
    2.47 +        x = Zp(p)
    2.48 +        y = Zp(42)
    2.49 +        z = x + y
    2.50 +        self.assertEquals(z.signed(), 42)
    2.51 +
    2.52 +    def test_little_subtracted_big_signed(self):
    2.53 +        x = Zp(14)
    2.54 +        y = Zp(42)
    2.55 +        z = x - y
    2.56 +        self.assertEquals(z.signed(), -28)       
    2.57 +
    2.58 +    def test_little_subtracted_big_unsigned(self):
    2.59 +        x = Zp(14)
    2.60 +        y = Zp(42)
    2.61 +        z = x - y
    2.62 +        self.assertEquals(z.unsigned(), p-28)       
    2.63 +
    2.64 +    def test_big_subtracted_little_signed(self):
    2.65 +        x = Zp(42)
    2.66 +        y = Zp(14)
    2.67 +        z = x - y
    2.68 +        self.assertEquals(z.signed(), 28)       
    2.69 +
    2.70 +    def test_big_subtracted_little_unsigned(self):
    2.71 +        x = Zp(42)
    2.72 +        y = Zp(14)
    2.73 +        z = x - y
    2.74 +        self.assertEquals(z.unsigned(), 28)       
    2.75 +
    2.76 +    def test_little_add_big_signed(self):
    2.77 +        x = Zp(1)
    2.78 +        y = Zp(p)
    2.79 +        z = x + y
    2.80 +        self.assertEquals(z.signed(), 1)
    2.81 +
    2.82 +    def test_little_add_big_unsigned(self):
    2.83 +        x = Zp(1)
    2.84 +        y = Zp(p)
    2.85 +        z = x + y
    2.86 +        self.assertEquals(z.unsigned(), 1)
    2.87 +
    2.88 +    def test_maxint_signed(self):
    2.89 +        phalf = (p-1)/2
    2.90 +        x = Zp(phalf)
    2.91 +        y = Zp(1)
    2.92 +        z = x + y 
    2.93 +        self.assertEquals(z.signed(), -phalf)