viff

changeset 1552:00e4f1acc454

BeDOZa: Restructured modified paillier encryption.
author Thomas P Jakobsen <tpj@cs.au.dk>
date Mon, 27 Sep 2010 11:00:54 +0200
parents 38793a845e3f
children 3105692a258f
files viff/bedoza/modified_paillier.py
diffstat 1 files changed, 28 insertions(+), 24 deletions(-) [+]
line diff
     1.1 --- a/viff/bedoza/modified_paillier.py	Mon Sep 27 10:31:49 2010 +0200
     1.2 +++ b/viff/bedoza/modified_paillier.py	Mon Sep 27 11:00:54 2010 +0200
     1.3 @@ -47,20 +47,24 @@
     1.4              return y
     1.5          else:
     1.6              return y - n
     1.7 -     
     1.8 -    def _verify_input(self, value, player_id):
     1.9 +
    1.10 +
    1.11 +    def encrypt_r(self, value, player_id=None, random_elm=None):
    1.12 +        """Encryption of the given value.
    1.13 +        
    1.14 +        If player_id is given, encrypts using public key of that
    1.15 +        player, otherwise just use public key of the player itself.
    1.16 +        
    1.17 +        The encryption requires some randomness in the form of an
    1.18 +        element in Zn*. If random_elm is given, it is used as random
    1.19 +        element. Otherwise, a random element is generated using the
    1.20 +        pseudo-random generator given when the ModifiedPaillier object
    1.21 +        was constructed.
    1.22 +        """
    1.23 +        # TODO: Assert that random_elm is None eller in Zn*.
    1.24          assert isinstance(value, int) or isinstance(value, long), \
    1.25              "paillier: encrypts only integers and longs, got %s" % \
    1.26                  value.__class__
    1.27 -
    1.28 -    def encrypt_with_randomness(self, value, randomness, player_id=None):
    1.29 -        """Encrypt using public key of player player_id using the
    1.30 -        given randomness.
    1.31 -
    1.32 -        Defaults to own public key.
    1.33 -
    1.34 -        """
    1.35 -        self._verify_input(value, player_id)
    1.36          if not player_id:
    1.37              player_id = self.runtime.id
    1.38          n = self.runtime.players[player_id].pubkey['n']
    1.39 @@ -69,23 +73,23 @@
    1.40          assert min <= value <= max, \
    1.41              "paillier: plaintext %d outside legal range [-(n-1)/2 " \
    1.42              "; (n-1)/2] = [%d ; %d]"  % (value, min, max)
    1.43 +        # TODO: This is not correct. Since n=pq, Zn* is only a subset
    1.44 +        # of Zn \ {0}.
    1.45 +        if random_elm == None:
    1.46 +            random_elm = self.random.randint(1, long(n))
    1.47          pubkey = self.runtime.players[player_id].pubkey
    1.48 -        return randomness, pypaillier.encrypt_r(self._f(value, n), randomness, pubkey) 
    1.49 +        return random_elm, pypaillier.encrypt_r(
    1.50 +            self._f(value, n), random_elm, pubkey)
    1.51  
    1.52 -    def encrypt_r(self, value, player_id=None):
    1.53 -       """As encrypt_with_randomness, but generates its own randomness."""
    1.54 -       self._verify_input(value, player_id)
    1.55 -       if not player_id:
    1.56 -           player_id = self.runtime.id
    1.57 -       n = self.runtime.players[player_id].pubkey['n']
    1.58 -       randomness = self.random.randint(1, long(n))
    1.59 -       return self.encrypt_with_randomness(value, randomness, player_id=player_id)
    1.60  
    1.61 +    def encrypt(self, value, player_id=None, random_elm=None):
    1.62 +        """Encryption of the given value.
    1.63  
    1.64 -    def encrypt(self, value, player_id=None):
    1.65 -        """As encrypt_r, but doesn't return randomness used, only
    1.66 -        encrypted value."""
    1.67 -        return self.encrypt_r(value, player_id=player_id)[1]
    1.68 +        As encrypt_r, but doesn't return randomness used, only
    1.69 +        encrypted value.
    1.70 +        """
    1.71 +        return self.encrypt_r(value, player_id=player_id,
    1.72 +                              random_elm=random_elm)[1]
    1.73  
    1.74  
    1.75      def decrypt(self, enc_value):