This repository implements Elgmal, a partial homomorphic encryption scheme originally described by Taher Elgamal in 1985. This implementation includes the original version of Elgamal, which is multiplicative homomorphic, as well as slightly modified versions called Exponential Elgamal, which is multiplicative homomorphic over the cipher space, but additive homomorphic over the plain text space, and a version implementing Elgamal on elliptic curves.
Install the package npm i o1js-elgamal
Import the ElGamal finite field class as well as the cipher class.
import { Cipher, ElGamalFF } from 'o1js-elgamal';
Generate a secret and public key. The secret key is used to decrypt, whereas the public key is used to encrypt a message.
let { pk, sk } = ElGamalFF.generateKeys();
Encrypt two messages using the public key.
let c1 = ElGamalFF.encrypt(Field(5), pk)
let c2 = ElGamalFF.encrypt(Field(5), pk)
Demonstrate the multiplicative homomorphism over the plain text space by multiplying the ciphers.
let c3 = c1.mul(c2)
Decrypt the cipher c3
using the secret key.
let plain = ElGamalFF.decrypt(c3, sk);
Check that the decrypted text equals the expected output of 5 * 5 = 25
.
plain.assertEquals(Field(5).mul(5));
Take a look at at the example which uses ElGamal within a Mina smart contract.
TBD
npm install
npm run build
npm run test
node build/src/examples/elgamalff.js