Minimal pure-Python implementation of Paillier's additively homomorphic cryptosystem.
Project description
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 ?.?.?
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70d31fc5e078d30d0808fd1fd8a8003e429f534fc31d8f3164774896a1067adf
|
|
| MD5 |
ce6af85af5a4a9bb223c3682006181dc
|
|
| BLAKE2b-256 |
fa542b6490c92fe330b043c29024827b6b4b289c946e192c652e3945004ddf13
|
Provenance
The following attestation bundles were made for pailliers-0.3.1.tar.gz:
Publisher:
build-publish-sign-release.yml on lapets/pailliers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pailliers-0.3.1.tar.gz -
Subject digest:
70d31fc5e078d30d0808fd1fd8a8003e429f534fc31d8f3164774896a1067adf - Sigstore transparency entry: 774927764
- Sigstore integration time:
-
Permalink:
lapets/pailliers@3b2e21bbc99e27ef66fe4d6177f8037dc6f8f7ef -
Branch / Tag:
refs/tags/0.3.1 - Owner: https://github.com/lapets
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-publish-sign-release.yml@3b2e21bbc99e27ef66fe4d6177f8037dc6f8f7ef -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23bbdb5cfc2ff7caf8b6e8f2a190d435ae85adca2d7f49418dc0e3639c86b3f1
|
|
| MD5 |
91530e33a89c4ebc7ea51683769f84c0
|
|
| BLAKE2b-256 |
e82c6bed36f39808408b6c188812018a71936ada9fa0c92e4add2af2a7436965
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pailliers-0.3.1-py3-none-any.whl -
Subject digest:
23bbdb5cfc2ff7caf8b6e8f2a190d435ae85adca2d7f49418dc0e3639c86b3f1 - Sigstore transparency entry: 774927765
- Sigstore integration time:
-
Permalink:
lapets/pailliers@3b2e21bbc99e27ef66fe4d6177f8037dc6f8f7ef -
Branch / Tag:
refs/tags/0.3.1 - Owner: https://github.com/lapets
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-publish-sign-release.yml@3b2e21bbc99e27ef66fe4d6177f8037dc6f8f7ef -
Trigger Event:
push
-
Statement type: