Warning: do not use this in production as it has not been audited.
This Typescript library implements ElGamal encryption, decryption, and
re-randomization on the BabyJub curve. It also provides
circom
circuits for decryption and
re-randomization.
This was written for future implementation of MACI anonymization.
Clone this repository, install dependencies, and build the source code:
git clone git@github.com:weijiekoh/elgamal-babyjub.git &&
cd elgamal-babyjub &&
npm i &&
npm run build
Run tests:
npm run test
encodeToMessage = (original: BigInt): Message
This function converts an arbitrary value within the BabyJub finite field into
a BabyJub curve point and an xIncrement
value. It generates a random curve
point within the BabyJub subgroup and computes the difference between its
x-value and the plaintext.
encrypt = (plaintext: BigInt, pubKey: PubKey, randomVal?: BigInt): ElGamalCiphertext
This function encrypts a single BigInt
plaintext into a ciphertext. Only the
owner of the private key associated with pubKey
can decrypt it.
decrypt = (privKey: PrivKey, ciphertext: ElGamalCiphertext): BigInt
Decrypts a cipertext into the original BigInt
.
rerandomize = (pubKey: PubKey, ciphertext: ElGamalCiphertext, randomVal: BigInt = genRandomSalt()): ElGamalCiphertext
Re-randomizes a ciphertext such that its value changes but can be decrypted to the same plaintext.
The randomVal
should be specified if one wishes to use the
ElGamalReRandomize
circuit described below.
Input signals:
c1[2]
: The x and y-coordinates of thec1
value of the ciphertextc2[2]
: The x and y-coordinates of thec2
value of the ciphertextxIncrement
: The x-increment value of the ciphertextprivKey
(private): The private key
Output signals:
out
: The original value
Input signals:
c1[2]
: The x and y-coordinates of thec1
value of the ciphertextc2[2]
: The x and y-coordinates of thec2
value of the ciphertextrandomVal
: A random value. It must be the same as the one passed to the abovererandomize()
function for both the circuit and Typescript function to output the same rerandomized ciphertext.pubKey
: The public key originally used to encrypt the ciphertext
Output signals:
d1[2]
: The x and y-coordinates of thed1
value of the rerandomized ciphertextd2[2]
: The x and y-coordinates of thed2
value of the rerandomized ciphertext