What is the Paillier cryptosystem?

This post is part of our Privacy-Preserving Data Science, Explained series.

This post introduces the Paillier cryptosystem, which is a partial homomorphic encryption scheme. In a subsequent post we’ll see how this can be used as the basis for a private set intersection protocol.

Homomorphic encryption

Homomorphic encryption is a form of encryption which allows you to perform mathematical or logical operations on the encrypted data. For example, suppose we have two numbers m1m_1 and m2m_2 and we encrypt those numbers using some public key encryption scheme with a public key pubpub and a private key privpriv. We get two ciphertexts c1=Epub(m1)c_1 = E_{pub}(m_1) and c2=Epub(m2)c_2 = E_{pub}(m_2). Normally, encryption aims to make all encrypted numbers indistinguishable from random numbers for anyone who does not have the private key required for decryption.

However with homomorphic encryption, some relationships are preserved. For example, if we have a homomorphic encryption scheme which enables addition, there will be a function addadd which anyone can perform on c1c_1 and c2c_2 such that the result, addpub(c1,c2)add_{pub}(c_1, c_2), will decrypt to the sum of m1m_1 and m2m_2:

Dpriv(addpub(Epub(m1),Epub(m2)))=m1+m2D_{priv}(add_{pub}(E_{pub}(m_1), E_{pub}(m_2))) = m_1 + m_2

Note that the addadd function will not necessarily be literal addition, just whichever function plays the role described above, according to the relevant homomorphic encryption scheme.

There have been partial homomorphic encryption schemes for quite a while, where a limited number of operations can be performed on encrypted data, for example only addition or only multiplication. Fully homomorphic encryption schemes have been developed over the last decade or so, which support arbitrary computations on encrypted data.

The Paillier cryptosystem, invented by Pascal Paillier in 1999, is a partial homomorphic encryption scheme which allows two types of computation:

  • addition of two ciphertexts
  • multiplication of a ciphertext by a plaintext number

Public key encryption scheme

The basic public key encryption scheme has three stages:

  1. generate a public-private key pair
  2. encrypt a number
  3. decrypt a number

Helper functions

  1. gcd(x,y)\mathrm{gcd}(x, y) outputs the greatest common divisor of xx and yy.
  2. lcm(x,y)\mathrm{lcm}(x, y) outputs the least common multiple of xx and yy.

Key generation

Key generation works as follows:

  1. Pick two large prime numbers pp and qq, randomly and independently. Confirm that gcd(pq,(p−1)(q−1))\mathrm{gcd}(pq, (p-1)(q-1)) is 11. If not, start again.
  2. Compute n=pqn = pq.
  3. Define function L(x)=x–1nL(x) = \frac{x – 1}{n}.
  4. Compute λ\lambda as lcm(p−1,q−1)\mathrm{lcm}(p-1, q-1).
  5. Pick a random integer gg in the set Zn2∗\mathbb{Z}^*_{n^2} (integers between 1 and n2n^2).
  6. Calculate the modular multiplicative inverse μ=(L(gλ mod n2))−1 mod n\mu = \left(L(g^\lambda \bmod n^2)\right)^{-1} \bmod n. If μ\mu does not exist, start again from step 1.
  7. The public key is (n,g)(n, g). Use this for encryption.
  8. The private key is λ\lambda. Use this for decryption.

Encryption

Encryption can work for any mm in the range 0≤m<n0 \leq m < n:

  1. Pick a random number rr in the range 0<r<n0 < r < n.
  2. Compute ciphertext c=gm⋅rn mod n2c = g^m \cdot r^n \bmod n^2.

Decryption

Decryption presupposes a ciphertext created by the above encryption process, so that cc is in the range 0<c<n20 < c < n^2:

  1. Compute the plaintext m=L(cλ mod n2)⋅μ mod nm = L(c^\lambda \bmod n^2) \cdot \mu \bmod n.

(Reminder: we can always recalculate μ\mu from λ\lambda and the public key).

Example

Here are some example values if you want to work through the algorithm:

Key generation

  1. Pick p=13p = 13 and q=17q = 17. (They satisfy the condition.)
  2. Compute n=221n = 221.
  3. Compute λ=48\lambda = 48.
  4. Pick g=4886g = 4886.
  5. Compute μ=159\mu = 159. (It exists.)

Encryption

  1. Set m1=123m_1 = 123.
  2. Pick r1=666r_1 = 666.
  3. Compute c1=25889 mod 2212c_1 = 25889 \bmod 221^2.

Decryption

  1. Compute mdecrypted=123 mod 221m_{\text{decrypted}} = 123 \bmod 221. (The same as m1m_1.)

(But beware these numbers are too small to offer any real security and my random values weren’t all that random.)

Homomorphic properties

Let’s take a look at the homomorphic properties of this encryption scheme…

Addition of two ciphertexts

When two ciphertexts are multiplied, the result decrypts to the sum of their plaintexts:

Dpriv(Epub(m1)⋅Epub(m2) mod n2)=m1+m2 mod nD_{priv}(E_{pub}(m_1) \cdot E_{pub}(m_2) \bmod n^2) = m_1 + m_2 \bmod n

Multiplication of a ciphertext by a plaintext

When a ciphertext is raised to the power of a plaintext, the result decrypts to the product of the two plaintexts:

Dpriv(Epub(m1)m2 mod n2)=m1⋅m2 mod nD_{priv}(E_{pub}(m_1)^{m_2} \bmod n^2) = m_1 \cdot m_2 \bmod n

Gotchas

There are a couple of special cases which need to be handled carefully. The first is multiplying by 00. Because any number to the power of 00 is 11, if we multiply a ciphertext by a plaintext 00 using the method above, the result will always be 11, and anyone who sees this “encrypted” value will know that it decrypts to 00. Luckily we can use an alternative method for this case. Because multiplying any number by 00 gives 00, we can just skip the calculations and encrypt a 00 directly using the standard public key encryption scheme. Because of the random number introduced in the encryption step, nobody without the private key will be able to know what the plaintext is.

The other case is multiplying by 11. Because any number xx to the power of 11 is xx, if we multiply a ciphertext by a plaintext 11 using the normal method, the output will be the same as the input. This is less severe than the case with 00 where the encrypted value could be inferred, but still a problem because anybody who is watching the communication between whoever holds the private key and whoever is multiplying numbers will be able to work out that the number was multiplied by 11. The solution is another workaround: instead of multiplying by 11, we perform an equivalent operation: adding 00! We just freshly encrypt a 00 and perform the usual addition procedure to obtain a secure ciphertext.

Example

Here are some example values continuing from the last example:

Homomorphic addition

  1. Set m2=37m_2 = 37.
  2. Pick r2=999r_2 = 999.
  3. Compute c2=30692mod  2212c_2 = 30692 \mod 221^2.
  4. Compute csum=25889⋅30692=39800mod  2212c_{sum} = 25889 \cdot 30692 = 39800 \mod 221^2.
  5. Compute msum=160=123+37=m1+m2mod  221m_{sum} = 160 = 123 + 37 = m_1 + m_2 \mod 221.

Homomorphic multiplication

  1. Set m3=25m_3 = 25.
  2. Compute cproduct=2588925=15723mod  2212c_{product} = 25889^{25} = 15723 \mod 221^2.
  3. Compute mproduct=202=123⋅25=m1⋅m3mod  221m_{product} = 202 = 123 \cdot 25 = m_1 \cdot m_3 \mod 221.

Multiplication by 0

  1. Set mmultiply0=0m_{multiply0} = 0.
  2. Pick rmultiply0=444r_{multiply0} = 444.
  3. Compute cmultiply0=46663mod  2212c_{multiply0} = 46663 \mod 221^2.
  4. Compute mdecrypted=0=123⋅0=m1⋅0mod  221m_{decrypted} = 0 = 123 \cdot 0 = m_1 \cdot 0 \mod 221.

Multiplication by 1

  1. Set mencrypt0=0m_{encrypt0} = 0.
  2. Pick rencrypt0=555r_{encrypt0} = 555.
  3. Compute cencrypt0=653mod  2212c_{encrypt0} = 653 \mod 221^2.
  4. Compute cmultiply1=25889⋅653=6531mod  2212c_{multiply1} = 25889 \cdot 653 = 6531 \mod 221^2.
  5. Compute mmultiply1=123=123⋅1=m1⋅1mod  221m_{multiply1} = 123 = 123 \cdot 1 = m_1 \cdot 1 \mod 221.

Code

I have a learning repository for privacy-related algorithms here. You can have a look there to see how I implemented these functions, and below is some code showing how you can use the library.

WARNING: This library is not recommended for production use. It was written for learning purposes only.

Assuming you have Git and Node.js with npm installed:

git clone https://github.com/willclarktech/privacy-implementations.git
cd privacy-implementations
npm install
npm run build

Now in a JavaScript file or Node.js REPL:

const paillier = require("./build/cryptosystem/paillier");

const keys = paillier.generateKeysSync(); // Slow!

const plaintext1 = 1234567890n;
const plaintext2 = 55555555555n;

const ciphertext1 = paillier.encrypt(keys.pub)(plaintext1);
const ciphertext2 = paillier.encrypt(keys.pub)(plaintext2);

const ciphertextSum = paillier.add(keys.pub)(ciphertext1, ciphertext2);
const plaintextSum = paillier.decrypt(keys)(ciphertextSum); // 56790123445n = plaintext1 + plaintext2

const ciphertextProduct = paillier.multiply(keys.pub)(ciphertext1, plaintext2);
const plaintextProduct = paillier.decrypt(keys)(ciphertextProduct); // 68587104999314128950n = plaintext1 * plaintext2

const ciphertextMultiply0 = paillier.multiply(keys.pub)(ciphertext1, 0n); // != 1n
const ciphertextMultiply1 = paillier.multiply(keys.pub)(ciphertext1, 1n); // != ciphertext1

Summary

In this post we’ve covered the Paillier cryptosystem, looking at how encryption, decryption, addition, and multiplication work. Next time we’ll take a look at how this can be used as a basis for a private set intersection protocol.

Interested? 👀

Sign up to receive an email when new content like this is posted.

Want to write for OpenMined or help update a post?

Let us know!

Continued Reading…
View all posts ->
A frosted glass diamond on a dark reflective floor: a single beam of light enters one side and a fan of many distinct filtered rays emerges from the other
  • Research
How to scale the impact of embedded AI evaluators
Graphic depicting a concept of a proprietary, frontier class AI model
  • News
PySyft used for first double-blind evaluation of a proprietary, frontier-class AI model