Homomorphic encryption using the threshold variant of the DamgardJurik cryptosystem.
Project description
DamgardJurik
An implementation of the threshold variant of the DamgardJurik homomorphic encryption cryptosystem.
Table of Contents
 Installation
 Public and Private Keys
 Key Generation
 Encryption and Decryption
 Homomorphic Operations
Installation
Requires Python 3.6+.
pip install damgardjurik
Alternatively, the code can be cloned and installed locally as follows.
git clone https://github.com/cryptovoting/damgardjurik.git
cd damgardjurik
pip install e .
Note that the e
flag will instruct pip to install the package as "editable". That is, when changes are made to any part of the package during development, those changes will immediately be available systemwide on the activated python environment.
All requirements for this package should be added to setup.py
.
Public and Private Keys
In the threshold variant of DamgardJurik implemented in this repository, a key pair consists of single public key along with a private key that has been split into multiple components using Shamir's secret sharing. The public key encrypts messages while the shares of the private key all contribute a portion of the decryption without ever requiring reconstruction of the private key. Thus, trust is distributed among the holders of the private key shares.
In this implementation, the public key is a PublicKey
object with an encrypt function while the private key shares are PrivateKeyShare
objects with a decrypt function that performs a partial decryption using that share of the private key. A PrivateKeyRing
object holds a set of PrivateKeyShare
s and contains a decrypt function that calls each PrivateKeyShare
's decrypt function and combines the results to obtain the final decryption.
Key Generation
To generate a PublicKey
and corresponding PrivateKeyRing
, run the following commands:
from damgard_jurik import keygen public_key, private_key_ring = keygen( n_bits=64, s=1, threshold=3, n_shares=3 )
The parameters to keygen
are as follows:
n_bits
: The number of bits of encryption used in the public key and private key shares.s
: The exponent to which the public key parametern
is raised (wheren = p * q
is the product of twon_bits
bit primesp
andq
.). Plaintexts live in the spaceZ_n^s
.threshold
: The minimum number of private key shares needed to decrypt an encrypted message.n_shares
: The number of private key shares to generate.
Encryption and Decryption
To encrypt an integer m
, run public_key.encrypt(m)
. This will return an EncryptedNumber
containing the encryption of m
.
To decrypt an EncryptedNumber
c
, run private_key_ring.decrypt(c)
. This will return an integer containing the decryption of c
.
For example:
m = 42 c = public_key.encrypt(m) m_prime = private_key_ring.decrypt(c) # m_prime = 42
Additionally, PublicKey
s and PrivateKingRing
s have a convenience method for encrypting and decrypting lists of integers, as shown below.
m_list = [42, 33, 100] c_list = public_key.encrypt(m_list) m_prime_list = private_key_ring.decrypt(c_list) # m_prime_list = [42, 33, 100]
Homomorphic Operations
Due to the additively homomorphic nature of the DamgardJurik cryptosystem, encrypted numbers can be combined in such a way as to obtain an encryption of the sum of the associated plaintexts. Futhermore, encrypted numbers can be combined with unencrypted integers in such a way as to obtain the product of the associated plaintext and the unencrypted integer. For convenience, the +
, 
, *
, and /
operators have been overridden for EncryptedNumbers
to implement these combinations.
For example:
m_1, m_2 = 42, 33 c_1, c_2 = public_key.encrypt(m_1), public_key.encrypt(m_2) c = c_1 + c_2 m_prime = private_key_ring.decrypt(c) # m_prime = 75 = 42 + 33
m, s = 42, 2 c = public_key.encrypt(m) c_prime = c * s m_prime = private_key_ring.decrypt(c_prime) # m_prime = 84 = 42 * 2
Project details
Release history Release notifications
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size  File type  Python version  Upload date  Hashes 

Filename, size damgard_jurik0.0.2py3noneany.whl (11.2 kB)  File type Wheel  Python version py3  Upload date  Hashes View hashes 
Hashes for damgard_jurik0.0.2py3noneany.whl
Algorithm  Hash digest  

SHA256  3fdbf9b7f31de848038ba70401e701a1903724a933241fcc14264b42c166a106 

MD5  a158155ad781ace93d8361c4fadd1f54 

BLAKE2256  fcdb0868e76a8198ea65c3fd88ae8e25ffa9ee429e7d76bbaa62ebc38165e8f7 