elgamal.elgamal module

Base implementation of the Asymmetric Encryption Scheme known as multiplicative ElGamal.

class elgamal.elgamal.ElGamal(public_key, secret_key, share_secret_key=False, debug=False)[source]

Bases: ElGamalBase[ElGamalCipherText]

Construct ElGamal encryption scheme that is multiplicatively homomorphic.

CipherTextConstructor

alias of ElGamalCipherText

__init__(public_key, secret_key, share_secret_key=False, debug=False)[source]

Construct a new multiplicatively homomorphic ElGamal encryption scheme with the given keypair.

Parameters:
  • public_key (ElGamalPublicKey) – Public key for this ElGamal scheme.

  • secret_key (ElGamalSecretKey | None) – Optional Secret Key for this ElGamal scheme (None when unknown).

  • share_secret_key (bool) – Boolean value stating whether or not the secret key should be included in serialization. This should only be set to True if one is really sure of it.

  • debug (bool) – flag to determine whether debug information should be displayed.

decode(encoded_plaintext)[source]

Decode an EncodedPlaintext.

Parameters:

encoded_plaintext (EncodedPlaintext[int]) – Encoded plaintext to be decoded.

Return type:

int

Returns:

Decoded plaintext value.

static deserialize(obj, *, origin=None, **_kwargs)[source]

Deserialization function for ElGamal schemes, which will be passed to the communication module.

Parameters:
  • obj (SerializedElGamalBase) – serialized version of a ElGamal scheme.

  • origin (HTTPClient | None) – HTTPClient representing where the message came from if applicable

  • **_kwargs (Any) – optional extra keyword arguments

Raises:
  • SerializationError – When communication library is not installed.

  • ValueError – When a scheme is sent through ID without any prior communication of the scheme

Return type:

ElGamal

Returns:

Deserialized ElGamal scheme from the given dict. Might not have a secret key when that was not included in the received serialization.

encode(plaintext)[source]

Encode integers as integers.

Parameters:

plaintext (int) – Plaintext to be encoded.

Raises:

ValueError – If the plaintext is outside the supported range of this ElGamal scheme.

Return type:

EncodedPlaintext[int]

Returns:

EncodedPlaintext object containing the encoded value.

is_zero(ciphertext)[source]

Determine if the underlying plaintext of a ciphertext equals 0, without doing a full decryption.

Parameters:

ciphertext (ElGamalCipherText) – ElGamalCipherText object containing the ciphertext to be checked.

Return type:

bool

Returns:

True if plaintext is 0, False otherwise.

mul(ciphertext_1, ciphertext_2)[source]

Secure multiplication.

If ciphertext_2 is another ElGamalCipherText, multiply the underlying plaintext value of ciphertext_1 with the underlying plaintext value of ciphertext_2. If it is a Plaintext, multiply the plaintext value with the underlying value of ciphertext_1.

The resulting ciphertext is fresh only if at least one of the inputs was fresh. Both inputs are marked as non-fresh after the operation.

Parameters:
  • ciphertext_1 (ElGamalCipherText) – First ElGamalCipherText of which the underlying plaintext is multiplied.

  • ciphertext_2 (ElGamalCipherText | int) – Either an ElGamalCipherText of which the underlying plaintext is used for multiplication or a Plaintext that is used for multiplication.

Raises:

AttributeError – When ciphertext_2 does not have the same public key as ciphertext_1 object.

Return type:

ElGamalCipherText

Returns:

An ElGamalCipherText containing the encryption of the multiplication.

neg(ciphertext)[source]

Negate the underlying plaintext of this ciphertext.

The resulting ciphertext is fresh only if the original ciphertext was fresh. The original ciphertext is marked as non-fresh after the operation.

Parameters:

ciphertext (ElGamalCipherText) – ElGamalCipherText of which the underlying plaintext should be negated.

Return type:

ElGamalCipherText

Returns:

ElGamalCiphertext object corresponding to the negated plaintext.

pow(ciphertext, power)[source]

Take the exponentiation the underlying plaintext value of this ciphertext with power as exponent.

The resulting ciphertext is fresh only if the original ciphertext was fresh. The original ciphertext is marked as non-fresh after the operation.

Parameters:
  • ciphertext (ElGamalCipherText) – ElGamalAdditiveCiphertext of which the underlying plaintext is exponentiated.

  • power (int) – An integer exponent with which the plaintext underlying this ciphertext should be exponentiated.

Raises:

TypeError – When the exponent is not an integer.

Return type:

ElGamalCipherText

Returns:

ElGamalAdditiveCiphertext containing the encryption of the exponentiation.

class elgamal.elgamal.ElGamalCipherText(raw_value, scheme, fresh=False)[source]

Bases: ElGamalBaseCipherText

Ciphertext for the ElGamal encryption scheme that is multiplicatively homomorphic.

static deserialize(obj, **_kwargs)[source]

Deserialization function for ElGamal ciphertexts, which will be passed to the communication module.

Parameters:
  • obj (SerializedElGamalBaseCipherText) – serialized version of a ElGamalCipherText.

  • **_kwargs (Any) – Optional extra keyword arguments.

Raises:

SerializationError – When communication library is not installed.

Return type:

ElGamalCipherText

Returns:

Deserialized ElGamalCipherText from the given dict.

is_zero()[source]

Determine if the underlying plaintext of a ciphertext equals 0, without doing a full decryption.

Return type:

bool

Returns:

True if plaintext is 0, False otherwise.

scheme: ElGamal