Example #1
0
def deserializ_pubkey(paillier_type, str):
        pubkey = {}
        for k, v in str.items():
            pubkey[k] = long(v)
        if paillier_type == "nacl":
            g1 = pypaillier.encrypt_r(1, 1, pubkey)
            pubkey['fixed_base'] = tripple.init(g1, pubkey['n_square'])            
        return pubkey
Example #2
0
    def encrypt_r(self, value, player_id=None, random_elm=None):
        """Encryption of the given value.
        
        If player_id is given, encrypts using public key of that
        player, otherwise just use public key of the player itself.
        
        The encryption requires some randomness in the form of an
        element in Zn*. If random_elm is given, it is used as random
        element. Otherwise, a random element is generated using the
        pseudo-random generator given when the ModifiedPaillier object
        was constructed.
        """
        assert isinstance(value, int) or isinstance(value, long), \
            "paillier: encrypts only integers and longs, got %s" % \
                value.__class__
        if not player_id:
            player_id = self.runtime.id
        n = self.runtime.players[player_id].pubkey['n']
        min = -(n - 1) / 2
        max = (n - 1) / 2
        assert min <= value <= max, \
            "paillier: plaintext %d outside legal range [-(n-1)/2 " \
            "; (n-1)/2] = [%d ; %d]"  % (value, min, max)

        # Here we verify that random_elm is either None or in Zn*. But
        # for realistical parameters, we can save time by not doing
        # this, since for large n = pq, it is extremely unlikely that
        # a random element in Zn is not also a member of Zn*.
        if random_elm == None:
            while True:
                random_elm = self.random.randint(1, long(n))
                if gcd(random_elm, n) == 1:
                    break
        elif not gcd(random_elm, n) == 1:
            raise Exception("Random element must be an element in Zn*")

        pubkey = self.runtime.players[player_id].pubkey
        return random_elm, pypaillier.encrypt_r(
            self._f(value, n), random_elm, pubkey)