Skip to main content

Minimal pure-Python implementation of Paillier's additively homomorphic cryptosystem.

Project description

Minimal pure-Python implementation of Paillier’s additively homomorphic cryptosystem.

PyPI version and link. Read the Docs documentation status. GitHub Actions status. Coveralls test coverage summary.

Installation and Usage

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 *

Examples

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

Development

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]"

Documentation

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

Testing and Conventions

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

Contributions

In order to contribute to the source code, open an issue or submit a pull request on the GitHub page for this library.

Versioning

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.

Publishing

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 ?.?.?

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pailliers-0.3.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pailliers-0.3.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file pailliers-0.3.1.tar.gz.

File metadata

  • Download URL: pailliers-0.3.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pailliers-0.3.1.tar.gz
Algorithm Hash digest
SHA256 70d31fc5e078d30d0808fd1fd8a8003e429f534fc31d8f3164774896a1067adf
MD5 ce6af85af5a4a9bb223c3682006181dc
BLAKE2b-256 fa542b6490c92fe330b043c29024827b6b4b289c946e192c652e3945004ddf13

See more details on using hashes here.

Provenance

The following attestation bundles were made for pailliers-0.3.1.tar.gz:

Publisher: build-publish-sign-release.yml on lapets/pailliers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pailliers-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pailliers-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pailliers-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 23bbdb5cfc2ff7caf8b6e8f2a190d435ae85adca2d7f49418dc0e3639c86b3f1
MD5 91530e33a89c4ebc7ea51683769f84c0
BLAKE2b-256 e82c6bed36f39808408b6c188812018a71936ada9fa0c92e4add2af2a7436965

See more details on using hashes here.

Provenance

The following attestation bundles were made for pailliers-0.3.1-py3-none-any.whl:

Publisher: build-publish-sign-release.yml on lapets/pailliers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page