pailliers
Minimal pure-Python implementation of Paillier’s additively homomorphic cryptosystem.
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 ?.?.?
Metadata
Release files for pailliers 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pailliers-0.3.1.tar.gz | 11.4 kB | With provenance | Details for pailliers-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pailliers-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for pailliers-0.3.1-py3-none-any.whl |
Total release size: 20.7 kB
Release files / pailliers-0.3.1.tar.gz
| Download URL | pailliers-0.3.1.tar.gz |
|---|---|
| Size | 11.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
70d31fc5e078d30d0808fd1fd8a8003e429f534fc31d8f3164774896a1067adf
|
|
BLAKE2b-256 checksum How to use checksums |
fa542b6490c92fe330b043c29024827b6b4b289c946e192c652e3945004ddf13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 22, 2025.
Transparency logRelease files / pailliers-0.3.1-py3-none-any.whl
| Download URL | pailliers-0.3.1-py3-none-any.whl |
|---|---|
| Size | 9.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
23bbdb5cfc2ff7caf8b6e8f2a190d435ae85adca2d7f49418dc0e3639c86b3f1
|
|
BLAKE2b-256 checksum How to use checksums |
e82c6bed36f39808408b6c188812018a71936ada9fa0c92e4add2af2a7436965
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 22, 2025.
Transparency logSecurity for pailliers 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for pailliers-0.3.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 22, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.