Minimal pure-Python implementation of Paillier's additively homomorphic cryptosystem.
This library is available as a package on PyPI:
python -m pip install pailliers
The library can be imported in the usual manner:
from pailliers import *
This library supports the creation of secret
keys, derivation of public
keys from secret
keys, encryption of integers into ciphertexts using public keys via encrypt
, and decryption of ciphertexts into integers using secret
keys via decrypt
:
>>> secret_key = secret(2048)
>>> public_key = public(secret_key)
>>> c = encrypt(public_key, 123)
>>> int(decrypt(secret_key, c))
123
The encrypt
function returns instances of the cipher
class (which is derived from the built-in int
type) that represent ciphertexts. Because the cipher
class includes definitions of special methods (such as __add__
and __mul__
) corresponding to Python's built-in addition and multiplication operators, these operators can be used to add two ciphertexts and to multiply a ciphertext by an integer scalar:
>>> c = encrypt(public_key, 123)
>>> d = encrypt(public_key, 456)
>>> r = (c * 2) + d
>>> int(decrypt(secret_key, r))
702
Other special methods make it possible to use a single variable to accumulate iteratively (via __iadd__
and __imul__
) and to use the built-in sum
function (via __radd__
):
>>> b = 0
>>> b += encrypt(public_key, 1)
>>> b += encrypt(public_key, 2)
>>> b += encrypt(public_key, 3)
>>> b *= 2
>>> b = sum([b, b, b])
>>> int(decrypt(secret_key, b))
36
Addition will only work on two or more instances of the cipher
class. To facilitate the use of cipher
instances that do not all maintain internal copies of the same public key (e.g., in cases where memory constraints are an issue or ciphertexts are stored/communicated separately from key information), the add
and mul
functions are also provided. The public key must be supplied explicitly to these functions:
>>> c = int(encrypt(public_key, 123))
>>> d = int(encrypt(public_key, 456))
>>> r = mul(public_key, cipher(c), 2)
>>> s = add(public_key, r, cipher(d))
>>> int(decrypt(secret_key, s))
702
An alternative to the above is to instantiate cipher
instances using explicit ciphertext values and the public key used to encrypt them:
>>> c = int(encrypt(public_key, 123))
>>> d = int(encrypt(public_key, 456))
>>> c = cipher(c, public_key)
>>> d = cipher(d, public_key)
>>> s = (2 * c) + d
>>> int(decrypt(secret_key, s))
702
All installation and development dependencies are fully specified in pyproject.toml
. The project.optional-dependencies
object is used to specify optional requirements for various development tasks. This makes it possible to specify additional options (such as docs
, lint
, and so on) when performing installation using pip:
python -m pip install ".[docs,lint]"
The documentation can be generated automatically from the source files using Sphinx:
python -m pip install ".[docs]"
cd docs
sphinx-apidoc -f -E --templatedir=_templates -o _source .. && make html
All unit tests are executed and their coverage is measured when using pytest (see the pyproject.toml
file for configuration details):
python -m pip install ".[test]"
python -m pytest
Alternatively, all unit tests are included in the module itself and can be executed using doctest:
python src/pailliers/pailliers.py -v
Style conventions are enforced using Pylint:
python -m pip install ".[lint]"
python -m pylint src/pailliers
In order to contribute to the source code, open an issue or submit a pull request on the GitHub page for this library.
The version number format for this library and the changes to the library associated with version number increments conform with Semantic Versioning 2.0.0.
This library can be published as a package on PyPI via the GitHub Actions workflow found in .github/workflows/build-publish-sign-release.yml
that follows the recommendations found in the Python Packaging User Guide.
Ensure that the correct version number appears in pyproject.toml
, and that any links in this README document to the Read the Docs documentation of this package (or its dependencies) have appropriate version numbers. Also ensure that the Read the Docs project for this library has an automation rule that activates and sets as the default all tagged versions.
To publish the package, create and push a tag for the version being published (replacing ?.?.?
with the version number):
git tag ?.?.?
git push origin ?.?.?