From-scratch Python implementations of two foundational cryptographic primitives. No external dependencies — just math, random, and hashlib.
The Paillier cryptosystem has an additive homomorphic property: multiplying ciphertexts adds plaintexts.
E(m1) * E(m2) mod n^2 = E(m1 + m2)
This means a server can tally encrypted votes without ever seeing a single vote.
python3 paillier_poll.pyWhat it demonstrates:
- Key generation (512-bit)
- Encrypting individual yes/no votes
- Server-side homomorphic tallying (multiply ciphertexts, decrypt the product)
- Semantic security (same plaintext, different ciphertext every time)
- Multi-candidate polls using positional encoding
The Schnorr protocol proves you know a secret key without revealing it.
Given public key y = g^x mod p, prove you know x in three messages:
- Commit: prover sends
t = g^k(random k) - Challenge: verifier sends random
c - Respond: prover sends
s = k - c*x mod q
Verification: g^s * y^c == t. The verifier is convinced, but learns nothing about x.
python3 schnorr_zkp.pyWhat it demonstrates:
- Interactive protocol (5 rounds, all pass)
- A cheating prover (wrong secret) getting caught
- Non-interactive variant via the Fiat-Shamir transform (replace verifier's challenge with a hash)
- Tampered proofs and wrong keys correctly rejected