Overview
The twisted ElGamal encryption module provides a homomorphic encryption scheme built on Curve25519. Unlike traditional ElGamal, this implementation encrypts messages directly as Pedersen commitments, enabling compatibility with zero-knowledge proof systems designed for Pedersen commitments.Key Concepts
Message Encoding
Messages are encrypted as scalar elements (in the “exponent”), which means decryption requires solving the discrete logarithm problem. This design choice enables homomorphic properties while maintaining compatibility with proof systems.Ciphertext Structure
A twisted ElGamal ciphertext consists of two components:- Pedersen Commitment: Encodes the encrypted message
- Decryption Handle: Binds the Pedersen opening to a specific public key
Core Types
ElGamalKeypair
A keypair containing both public and secret keys for ElGamal encryption.Methods
Creating KeypairsElGamalPubkey
Public key for ElGamal encryption.Methods
ElGamalSecretKey
Secret key for ElGamal decryption. Instances are zeroized on drop.Methods
ElGamalCiphertext
An ElGamal ciphertext containing a commitment and decrypt handle.Methods
Operator Overloading
ElGamalCiphertext supports arithmetic operations:DecryptHandle
Binds a Pedersen opening to a specific public key.Methods
Usage Examples
Basic Encryption and Decryption
Deterministic Encryption
Homomorphic Addition
Homomorphic Subtraction
Scalar Multiplication
Deriving Keys from Solana Signer
Working with Decrypt Handles
Security Considerations
Constant-Time Operations
Thedecrypt_u32 method is not constant-time and may leak information through timing side channels. Use with caution in security-sensitive contexts.
Key Management
- Secret keys are automatically zeroized on drop
- Never expose secret keys or serialize them insecurely
- Use proper key derivation when deriving from Solana signers
Discrete Log Limitations
Decryption requires solving the discrete log problem, which limits the practical range of encrypted values to 32-bit unsigned integers. Larger values cannot be efficiently decrypted.Algorithm Handle
TheElGamal struct provides low-level algorithm operations:
ElGamalPubkey and ElGamalSecretKey instead of calling these directly.