This project implements ElGamal homomorphic encryption in Solidity, supporting two homomorphic schemes:
-
ElGamal Multiplicative Homomorphism (
ElGamalMultiplicative.sol)- Supports homomorphic multiplication and division
- Supports scalar addition and subtraction
- Uses modular exponentiation and modular inversion
- Allows secure computations on encrypted values
-
ElGamal Additive Homomorphism (
ElGamalAdditive.sol)- Supports homomorphic addition and subtraction
- Supports scalar multiplication and division
- Uses modular exponentiation and modular inversion
- Allows secure additive operations on encrypted values
Each contract enables privacy-preserving computations while ensuring correctness through modular arithmetic.
- Homomorphic Multiplication:
C_1^scalar, C_2^scalar - Homomorphic Division:
C_1^(1/scalar), C_2^(1/scalar) - Scalar Addition:
C_2 * g^scalar - Scalar Subtraction:
C_2 / g^scalar
- Homomorphic Addition:
C_2 * C_2' - Homomorphic Subtraction:
C_2 / C_2' - Scalar Multiplication:
C_1^scalar, C_2^scalar - Scalar Division:
(C_1^scalar * C_2)^(1/d) mod p
Located in contracts/ElGamalMultiplicative.sol, this contract enables multiplicative homomorphic operations.
| Function | Description |
|---|---|
storeEncryptedValue(uint256 c1, uint256 c2) |
Stores encrypted values |
getEncryptedBalance(address user) |
Retrieves the encrypted balance |
homomorphicMultiplication(address user, uint256 scalar) |
Computes C_1^scalar, C_2^scalar |
homomorphicDivision(address user, uint256 scalar) |
Computes C_1^(1/scalar), C_2^(1/scalar) |
scalarAddition(address user, uint256 scalar) |
Adds g^scalar to C_2 |
scalarSubtraction(address user, uint256 scalar) |
Subtracts g^scalar from C_2 |
Located in contracts/ElGamalAdditive.sol, this contract enables additive homomorphic operations.
| Function | Description |
|---|---|
storeEncryptedValue(uint256 c1, uint256 c2) |
Stores encrypted values |
getEncryptedBalance(address user) |
Retrieves the encrypted balance |
homomorphicAddition(address user1, address user2) |
Computes C_2 * C_2' |
homomorphicSubtraction(address user1, address user2) |
Computes C_2 / C_2' |
scalarMultiply(address user1, uint256 multiplier, address user2) |
Computes C_1^scalar * C_2 |
scalarDivide(address user1, uint256 multiplier, address user2, uint256 divisor) |
Computes (C_1^scalar * C_2)^(1/d) mod p |
We use Hardhat Ignition for automated contract deployment.
Ensure you have Node.js, Hardhat, and dependencies installed:
npm installnpx hardhat compileTo deploy ElGamal Multiplicative:
npx hardhat ignition deploy ElGamalMultiplicative.ts --network localhostTo deploy ElGamal Additive:
npx hardhat ignition deploy ElGamalAdditive.ts --network localhostWe use TypeScript test files to validate both contracts.
npx hardhat test test/ElGamalMultiplicative.test.tsnpx hardhat test test/ElGamalAdditive.test.tsTo encrypt a value m:
uint256 r = 42; // Random value
uint256 c1 = modExp(g, r, p);
uint256 c2 = (m * modExp(h, r, p)) % p;To multiply an encrypted value by a scalar k:
homomorphicMultiplication(userAddress, k);To add two encrypted values:
homomorphicAddition(user1, user2);To divide an encrypted value by a scalar d:
scalarDivide(user1, multiplier, user2, divisor);- โ
Ensures modular safety using modular exponentiation (
modExp). - โ
Uses modular inverse (
modInverse) to prevent errors in division. - โ Ensures correctness using TypeScript tests before deployment.
Pull requests are welcome! If you find bugs or have feature requests, please open an issue.
This project is MIT-licensed.
โ
Two Homomorphic Encryption Contracts: Additive & Multiplicative
โ
Complete Deployment & Test Suite
โ
Modular Arithmetic for Secure Computation
โ
Future Support for Zero-Knowledge Proofs & FHE
๐ Run npx hardhat test and deploy homomorphic encryption on Ethereum today!