Threshold ElGamal encryption implementation in TypeScript
threshold-elgamal / Exports
Ƭ EncryptedMessage: Object
Name | Type |
---|---|
c1 |
bigint |
c2 |
bigint |
Ƭ Parameters: Object
Name | Type |
---|---|
generator |
bigint |
prime |
bigint |
privateKey |
bigint |
publicKey |
bigint |
▸ combineDecryptionShares(decryptionShares
, prime?
): bigint
Combines partial decryptions from multiple parties into a single decryption factor.
Name | Type | Description |
---|---|---|
decryptionShares |
bigint [] |
An array of partial decryption results. |
prime |
bigint |
The prime modulus used in the ElGamal system. Defaults to the 2048-bit group prime. |
bigint
The combined decryption factor.
▸ combinePublicKeys(publicKeys
, prime?
): bigint
Combines multiple public keys into a single public key.
Name | Type | Description |
---|---|---|
publicKeys |
bigint [] |
An array of public keys to combine. |
prime |
bigint |
The prime modulus used in the ElGamal system. |
bigint
The combined public key.
▸ createDecryptionShare(encryptedMessage
, privateKey
, prime?
): bigint
Performs a partial decryption on a ciphertext using an individual’s private key share.
Name | Type | Description |
---|---|---|
encryptedMessage |
EncryptedMessage |
The encrypted secret. |
privateKey |
bigint |
The private key share of the decrypting party. |
prime |
bigint |
The prime modulus used in the ElGamal system. Defaults to the 2048-bit group prime. |
bigint
The result of the partial decryption.
▸ decrypt(encryptedMessage
, prime
, privateKey
): number
Decrypts an ElGamal encrypted secret.
Name | Type | Description |
---|---|---|
encryptedMessage |
EncryptedMessage |
- |
prime |
bigint |
The prime number used in the encryption system. |
privateKey |
bigint |
The private key used for decryption. |
number
The decrypted secret as an integer.
▸ deserializeEncryptedMessage(message
): EncryptedMessage
Deserializes an object containing string representations of an encrypted message’s components
back into an EncryptedMessage
with bigint components. This is useful for reconstructing
encrypted messages from their stringified forms, such as when retrieving them from JSON data.
Name | Type | Description |
---|---|---|
message |
Object |
An object containing the c1 and c2 components of the message as strings. |
message.c1 |
string |
- |
message.c2 |
string |
- |
The deserialized encrypted message with c1
and c2
as bigints.
Example
// An example serialized message
const serializedMessage = { c1: "1234567890123456789012345678901234567890", c2: "0987654321098765432109876543210987654321" };
const encryptedMessage = deserializeEncryptedMessage(serializedMessage);
console.log(encryptedMessage); // Output: { c1: 1234567890123456789012345678901234567890n, c2: 0987654321098765432109876543210987654321n }
▸ encrypt(secret
, publicKey
, prime?
, generator?
): EncryptedMessage
Encrypts a secret using ElGamal encryption.
Name | Type | Description |
---|---|---|
secret |
number |
The secret to be encrypted. |
publicKey |
bigint |
The public key used for encryption. |
prime |
bigint |
The prime number used in the encryption system. Defaults to the 2048-bit group’s prime. |
generator |
bigint |
The generator used in the encryption system. Defaults to the 2048-bit group’s generator. |
The encrypted secret, consisting of two BigIntegers (c1 and c2).
▸ generateKeyShares(n
, threshold
, primeBits?
): { privateKey
: bigint
; publicKey
: bigint
}[]
Generates key shares for a threshold ElGamal cryptosystem.
Name | Type | Default value | Description |
---|---|---|---|
n |
number |
undefined |
The total number of key shares. |
threshold |
number |
undefined |
The minimum number of key shares required for decryption. |
primeBits |
2048 | 3072 | 4096 |
2048 |
The bit length of the prime modulus (default: 2048). |
{ privateKey
: bigint
; publicKey
: bigint
}[]
An array of key shares, each containing a private and public key share.
▸ generateKeys(index
, threshold
, primeBits?
): Object
Generates a single key share for a participant in a threshold ElGamal cryptosystem.
Name | Type | Default value | Description |
---|---|---|---|
index |
number |
undefined |
The unique index of the participant (starting from 1). |
threshold |
number |
undefined |
The minimum number of key shares required for decryption. |
primeBits |
2048 | 3072 | 4096 |
2048 |
The bit length of the prime modulus (default: 2048). |
Object
The key share containing a private and public key share for the participant.
Name | Type |
---|---|
privateKey |
bigint |
publicKey |
bigint |
▸ generateParameters(primeBits?
): Parameters
Generates the parameters for the ElGamal encryption, including the prime, generator, and key pair (public and private keys).
Name | Type | Default value | Description |
---|---|---|---|
primeBits |
2048 | 3072 | 4096 |
2048 |
The bit length for the prime number. Supports 2048, 3072, or 4096 bits. |
The generated parameters including the prime, generator, publicKey, and privateKey.
▸ getGroup(primeBits?
): Object
Retrieves the group parameters for a given prime bit length.
Name | Type | Default value | Description |
---|---|---|---|
primeBits |
2048 | 3072 | 4096 |
2048 |
The bit length of the prime modulus (2048, 3072, or 4096). |
Object
The group parameters including prime and generator.
Name | Type |
---|---|
generator |
bigint |
prime |
bigint |
▸ getRandomBigIntegerInRange(min
, max
): bigint
Generates a random bigint within a specified range.
Name | Type | Description |
---|---|---|
min |
bigint |
The minimum value (inclusive). |
max |
bigint |
The maximum value (exclusive). |
bigint
A random bigint within the specified range.
▸ multiplyEncryptedValues(value1
, value2
, prime?
): EncryptedMessage
Performs homomorphic multiplication on two encrypted values, allowing for encrypted arithmetic operations.
Name | Type | Description |
---|---|---|
value1 |
EncryptedMessage |
The first encrypted value. |
value2 |
EncryptedMessage |
The second encrypted value. |
prime |
bigint |
The prime modulus used in the encryption system. Defaults to the 2048-bit group prime. |
The result of the multiplication, as a new encrypted message.
▸ serializeEncryptedMessage(message
): Object
Serializes an encrypted message into an object with string representations of its components. This function is useful for converting the bigint components of an encrypted message into strings, making them easier to store or transmit as JSON, for instance.
Name | Type | Description |
---|---|---|
message |
EncryptedMessage |
The encrypted message to be serialized. It should have two bigint properties: c1 and c2 . |
Object
An object containing the c1
and c2
components of the message as strings.
Name | Type |
---|---|
c1 |
string |
c2 |
string |
Example
// An example encrypted message
const encryptedMessage = { c1: BigInt('1234567890123456789012345678901234567890'), c2: BigInt('0987654321098765432109876543210987654321') };
const serializedMessage = serializeEncryptedMessage(encryptedMessage);
console.log(serializedMessage); // Output: { c1: "1234567890123456789012345678901234567890", c2: "0987654321098765432109876543210987654321" }
▸ thresholdDecrypt(encryptedMessage
, combinedDecryptionShares
, prime?
): number
Decrypts an encrypted secret using the combined partial decryptions in a threshold ElGamal scheme.
Name | Type | Description |
---|---|---|
encryptedMessage |
Object |
The encrypted secret components. |
encryptedMessage.c1 |
bigint |
- |
encryptedMessage.c2 |
bigint |
- |
combinedDecryptionShares |
bigint |
The combined partial decryptions from all parties. |
prime |
bigint |
The prime modulus used in the ElGamal system. Defaults to the 2048-bit group prime. |
number
The decrypted secret, assuming it was small enough to be directly encrypted.