Introduction
Pascal Paillier presented his public-key cryptosystem based on composite-degree residuosity at EUROCRYPT 1999. The Paillier scheme is additively homomorphic: multiplying two ciphertexts under the same public key produces a ciphertext that decrypts to the sum of the two plaintexts. This property launched a generation of privacy-preserving protocols including e-voting, cloud data aggregation, and secure multi-party computation.
For Java learners, Paillier is the cleanest example of an asymmetric encryption scheme whose plaintext space is a different algebraic structure than the ciphertext space. The plaintext lives in Z_N, but the ciphertext lives in Z_{N^2}^*. This separation is exactly what makes the homomorphic property possible. The whole construction requires only BigInteger modular exponentiation, the Carmichael function, and a clever L-function.
What is the Paillier Cryptosystem?
Paillier is a public-key probabilistic encryption scheme in the residuosity family. Like RSA it uses a composite modulus N = p * q, but unlike RSA the ciphertext is computed modulo N^2 instead of N. The public key is (N, g) where g is a generator of Z_{N^2}^* of a specific form, and the private key is the factorization (p, q) or equivalently the Carmichael function lambda = lcm(p-1, q-1).
Paillier is additively homomorphic: E(m1, r1) * E(m2, r2) mod N^2 = E(m1 + m2, r1 * r2). It also supports scalar multiplication: E(m, r)^k mod N^2 = E(k * m, r^k). These two properties together make Paillier the workhorse of cryptographic voting (each ballot is an encryption of 0 or 1, and the tally is the homomorphic sum) and of secure aggregation in federated learning and IoT telemetry.
How Paillier Works - Step by Step
Step 1: Key generation. Pick two large primes p, q. Compute N = p * q, N^2 = N * N, and lambda = lcm(p-1, q-1). Choose g = N + 1, which is a generator of the subgroup of Z_{N^2}^* of order N (this is the canonical shortcut; the original paper allows any generator). Compute the helper mu = L(g^lambda mod N^2)^(-1) mod N, where L(u) = (u - 1) / N. Public key is (N, g); private key is (lambda, mu).
Step 2: Encryption. To encrypt m in Z_N, pick a random r coprime to N. Compute c = g^m * r^N mod N^2. With g = N + 1 this simplifies to c = (1 + N)^m * r^N mod N^2 = (1 + m*N) * r^N mod N^2 by the binomial theorem, which is faster to compute but produces the same result.
Step 3: Decryption. Compute u = c^lambda mod N^2, then m = L(u) * mu mod N. The L-function extracts the N-component of u, and mu rescales it back to the plaintext space. The decryption works because c^lambda = (g^m * r^N)^lambda = g^(m*lambda) * r^(N*lambda) = g^(m*lambda) * 1 mod N^2 (since r^(N*lambda) = 1 by Euler-Carmichael), and L(g^(m*lambda)) = m * lambda mod N, so multiplying by mu = lambda^(-1) mod N recovers m.
Step 4: Homomorphic addition. If c1 = E(m1, r1) and c2 = E(m2, r2), then c1 * c2 mod N^2 = g^(m1+m2) * (r1*r2)^N mod N^2 = E(m1+m2 mod N, r1*r2 mod N). For scalar multiplication, c^k mod N^2 = g^(k*m) * (r^k)^N = E(k*m mod N, r^k).
Java Program for Paillier Homomorphic Encryption
import java.math.BigInteger;
import java.security.SecureRandom;
public class PaillierCryptosystemDemo {
// Demo with small primes p=3, q=5. In production use 1024-bit primes.
private static final BigInteger P = BigInteger.valueOf(3);
private static final BigInteger Q = BigInteger.valueOf(5);
private static final BigInteger N = P.multiply(Q); // 15
private static final BigInteger NSQ = N.multiply(N); // 225
private static final BigInteger G = N.add(BigInteger.ONE); // 16
private static final BigInteger LAMBDA = lcm(
P.subtract(BigInteger.ONE), Q.subtract(BigInteger.ONE)); // 4
private static final BigInteger MU = L(G.modPow(LAMBDA, NSQ))
.modInverse(N); // 4
private static BigInteger lcm(BigInteger a, BigInteger b) {
return a.multiply(b).divide(a.gcd(b));
}
// L(u) = (u - 1) / N
private static BigInteger L(BigInteger u) {
return u.subtract(BigInteger.ONE).divide(N);
}
// c = g^m * r^N mod N^2
public static BigInteger encrypt(BigInteger m, BigInteger r) {
return G.modPow(m, NSQ).multiply(r.modPow(N, NSQ)).mod(NSQ);
}
// m = L(c^lambda mod N^2) * mu mod N
public static BigInteger decrypt(BigInteger c) {
BigInteger u = c.modPow(LAMBDA, NSQ);
return L(u).multiply(MU).mod(N);
}
public static void main(String[] args) {
System.out.println("Setup: p=" + P + " q=" + Q
+ " N=" + N + " g=" + G);
System.out.println("lambda=" + LAMBDA + " mu=" + MU);
// Encrypt m1=2 with r1=7
BigInteger m1 = BigInteger.valueOf(2);
BigInteger r1 = BigInteger.valueOf(7);
BigInteger c1 = encrypt(m1, r1);
System.out.println("Encrypted m=" + m1 + " with r=" + r1
+ " -> c=" + c1);
System.out.println("Decrypted c -> " + decrypt(c1));
// Encrypt m2=3 with r2=11
BigInteger m2 = BigInteger.valueOf(3);
BigInteger r2 = BigInteger.valueOf(11);
BigInteger c2 = encrypt(m2, r2);
System.out.println("Encrypted m=" + m2 + " with r=" + r2
+ " -> c=" + c2);
System.out.println("Decrypted c -> " + decrypt(c2));
// Additive homomorphism: c1 * c2 mod N^2 = E(m1 + m2)
BigInteger cSum = c1.multiply(c2).mod(NSQ);
System.out.println("c1 * c2 mod N^2 = " + cSum);
System.out.println("Decrypted homomorphic sum -> "
+ decrypt(cSum));
// Scalar multiplication: c1^k mod N^2 = E(k * m1)
int k = 4;
BigInteger cMul = c1.modPow(BigInteger.valueOf(k), NSQ);
System.out.println("c1^" + k + " mod N^2 = " + cMul);
System.out.println("Decrypted homomorphic mul -> "
+ decrypt(cMul));
}
}
Code Explanation - Line by Line
The constants P, Q, N, NSQ, G encode the demo group parameters. We use the small primes p=3 and q=5 so that the demo runs in microseconds; production deployments use 1024- or 2048-bit primes. G is set to N + 1 = 16, which is the canonical generator choice that satisfies g^(N*lambda) = 1 mod N^2 and the discrete log of g is exactly N modulo lambda.
LAMBDA is the Carmichael function lcm(p-1, q-1) = lcm(2, 4) = 4. MU is the modular inverse of L(g^lambda mod N^2), the helper that rescales the L-function output back to the plaintext space. The L function itself extracts the N-component of a value u: L(u) = (u - 1) / N, which works because the encryption produces values of the form 1 + m*N mod N^2.
The encrypt method computes g^m * r^N mod N^2 in two modular exponentiations and one multiply. The random r must be coprime to N, otherwise the scheme is broken; in production we loop until gcd(r, N) = 1. The decrypt method computes c^lambda mod N^2 (which kills the r^N term by Fermat-Euler) and then applies L and multiplies by mu to recover m.
The main method demonstrates four things: encrypt and decrypt a single message, verify decryption is correct; encrypt a second message and decrypt it; multiply the two ciphertexts modulo N^2 to get the homomorphic sum; exponentiate the first ciphertext by k to get the homomorphic scalar product. The expected results are c1=58, c2=71, c1*c2 decrypts to 5 (sum of 2 and 3), and c1^4 decrypts to 8 (four times 2).
Sample Input and Output
Setup: p=3 q=5 N=15 g=16 lambda=4 mu=4 Encrypted m=2 with r=7 -> c=58 Decrypted c -> 2 Encrypted m=3 with r=11 -> c=71 Decrypted c -> 3 c1 * c2 mod N^2 = 68 Decrypted homomorphic sum -> 5 c1^4 mod N^2 = 121 Decrypted homomorphic mul -> 8
The output confirms the two homomorphic properties: the product of ciphertexts decrypts to the sum (5 = 2 + 3), and the fourth power of a ciphertext decrypts to four times the plaintext (8 = 4 * 2).
Common Mistakes to Avoid
1. Using r that is not coprime to N. The proof that c^lambda = g^(m*lambda) mod N^2 relies on r^(N*lambda) = 1 mod N^2, which requires r coprime to N. If r shares a factor with N, decryption returns garbage. Always test gcd(r, N) = 1 before using r.
2. Forgetting that plaintexts wrap around mod N. Encrypting m = 16 and m = 1 with N = 15 produces ciphertexts that decrypt to the same value (1) because 16 = 1 mod 15. If your application requires messages larger than N, split them into blocks or use a larger modulus.
3. Reusing the same r for two encryptions. If r is reused across encryptions of m1 and m2, an attacker who learns one plaintext can recover the other by dividing out the known r^N component. Always sample r freshly per encryption using SecureRandom.
4. Computing mod N instead of mod N^2 for ciphertexts. Paillier ciphertexts live in Z_{N^2}^*. Working mod N silently truncates the encryption and breaks both decryption and the homomorphic property. Always reduce ciphertext operations mod NSQ.
Real-World Applications
Paillier is the standard cryptosystem for end-to-end verifiable electronic voting. The Helios voting system, used by several universities and the IETF, encrypts each ballot choice with Paillier, posts the ciphertext on a public bulletin board, and uses the homomorphic property to compute the encrypted tally without ever decrypting individual ballots. A mixnet or zero-knowledge proof then proves the tally is correct.
In cloud computing, Microsoft's Epidemic Privacy Preserving Data Mining and several smart-meter aggregation schemes use Paillier to compute aggregate statistics over encrypted client data without the cloud learning individual readings. Each client encrypts its reading; the cloud multiplies ciphertexts to obtain an encryption of the sum; a designated decryptor with the secret key reveals only the aggregate.
In federated learning, Google's Secure Aggregation protocol and several privacy-preserving mean-estimation systems use Paillier to securely average client gradients. Each client encrypts its gradient contribution; the server homomorphically sums them; the result is decrypted only after a quorum of clients collaboratively contributes decryption shares.
Several privacy-preserving cryptocurrencies and ZK-proof systems use Paillier for commitments and verifiable encryption. The ZK-proof protocol of Camenisch-Shoup, used in IBM Idemix for anonymous credentials, combines Paillier with zero-knowledge proofs of knowledge to prove properties of encrypted values without decrypting them.
Conclusion
Paillier's cryptosystem combines probabilistic public-key encryption with a clean additive homomorphic property in a single primitive, making it the de-facto choice for encrypted aggregation. Our Java program demonstrates encrypt, decrypt, homomorphic addition, and homomorphic scalar multiplication. Next study the Damgard-Jurik generalization to higher powers N^s, threshold Paillier decryption, and fully homomorphic encryption (BGV and CKKS) for circuits of arbitrary depth.
Comments
Comments
Post a Comment