elgamal.elgamal_base module

Base implementation of the Asymmetric Encryption Scheme known as ElGamal, which is used for the multiplicative and additive variants of the scheme.

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

Bases: AsymmetricEncryptionScheme[Tuple[ElGamalPublicKey, ElGamalSecretKey], int, int, Tuple[int, int], CiphText, ElGamalPublicKey, ElGamalSecretKey], RandomizedEncryptionScheme[Tuple[ElGamalPublicKey, ElGamalSecretKey], int, int, Tuple[int, int], CiphText, Tuple[int, int]]

ElGamal Encryption Scheme. This is an AsymmetricEncryptionScheme, with a public and secret key. This is also a RandomizedEncryptionScheme, thus having internal randomness generation and allowing for the use of precomputed randomness.

class SerializedElGamalBase[source]

Bases: TypedDict

pubkey: ElGamalPublicKey
scheme_id: int
seckey: ElGamalSecretKey
__eq__(other)[source]

Compare this ElGamalBase scheme with another object to determine (in)equality.

Parameters:

other (object) – Object to compare this ElGamalBase scheme with.

Return type:

bool

Returns:

Boolean representation of (in)equality of both objects.

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

Construct a new ElGamal encryption scheme.

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.

static generate_key_material(bits)[source]

Method to generate key material (ElGamalPublicKey and ElGamalSecretKey), consisting of a safe prime $p$ so $Z_p^*$ is a cyclic group of order $p-1$, a generator $g$ of this group and a random value $x in {1, …, p - 2}$, used to calculate $h = g^x$.

Parameters:

bits (int) – Bit length of prime field size $p$.

Raises:

ValueError – When the given number of bits is not positive.

Return type:

Tuple[ElGamalPublicKey, ElGamalSecretKey]

Returns:

Tuple with first the public key and then the secret key.

classmethod id_from_arguments(public_key)[source]

Method that turns the arguments for the constructor into an identifier. This identifier is used to find constructor calls that would result in identical schemes.

Parameters:

public_key (ElGamalPublicKey) – ElGamalPublicKey of the ElGamalBase instance

Return type:

int

Returns:

Identifier of the ElGamalBase instance

serialize(*, destination=None, **_kwargs)[source]

Serialization function for ElGamalBase schemes, which will be passed to the communication module. The sharing of the secret key depends on the attribute share_secret_key.

Parameters:
  • destination (HTTPClient | list[HTTPClient] | None) – HTTPClient representing where the message will go if applicable, can also be a list of clients in case of a broadcast message.

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

Raises:

SerializationError – When communication library is not installed.

Return type:

SerializedElGamalBase

Returns:

serialized version of this ElGamalBase scheme.

serialize_with_secret_key()[source]

Serialization function for ElGamalBase schemes, that does include the secret key.

Raises:

SerializationError – When communication library is not installed.

Return type:

SerializedElGamalBase

Returns:

serialized version of this ElGamalBase scheme.

serialize_without_secret_key()[source]

Serialization function for ElGamalBase schemes, that does not include the secret key.

Raises:

SerializationError – When communication library is not installed.

Return type:

SerializedElGamalBase

Returns:

serialized version of this ElGamalBase scheme (without the secret key).

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

Bases: RandomizableCiphertext[Tuple[ElGamalPublicKey, ElGamalSecretKey], int, int, Tuple[int, int], Tuple[int, int]]

Ciphertext for the ElGamal encryption scheme. This ciphertext is rerandomizable and supports homomorphic operations.

class SerializedElGamalBaseCipherText[source]

Bases: TypedDict

scheme: ElGamalBase[Any]
value: tuple[int, int]
__eq__(other)[source]

Compare this ElGamalBaseCipherText with another object to determine (in)equality.

Parameters:

other (object) – Object to compare this ElGamalBaseCipherText with.

Raises:

TypeError – If other object is not of the same type as this ElGamalBaseCipherText.

Return type:

bool

Returns:

Boolean representation of (in)equality of both objects.

__init__(raw_value, scheme, fresh=False)[source]

Construct a new ElGamalCipherText.

Parameters:
  • raw_value (Tuple[int, int]) – Ciphertext pair $(c_1, c_2) in mathbb{Z}^*_p$.

  • scheme (ElGamalBase[Any]) – ElGamal scheme that is used to encrypt this ciphertext.

  • fresh (bool) – Indicates whether fresh randomness is already applied to the raw_value.

Raises:

TypeError – If the given scheme is not an ElGamal scheme.

apply_randomness(randomization_value)[source]

Rerandomize this ciphertext $(c_0, c_1)$ using the given random value pair $g^r, h^r$ by taking $(c_0 g^r, c_1 h^r) mod p$.

Parameters:

randomization_value (tuple[int, int]) – Random value used for rerandomization.

Return type:

None

scheme: ElGamalBase[Any]
serialize(**_kwargs)[source]

Serialization function for ElGamalBase ciphertexts, which will be passed to the communication module.

If the ciphertext is not fresh, it is randomized before serialization. After serialization, it is always marked as not fresh for security reasons.

Parameters:

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

Raises:

SerializationError – When communication library is not installed.

Return type:

SerializedElGamalBaseCipherText

Returns:

serialized version of this ElGamalBaseCiphertext.

class elgamal.elgamal_base.ElGamalPublicKey(p, g, h)[source]

Bases: PublicKey

PublicKey $(p, g, h)$ for the ElGamal encryption scheme, such that $p$ is prime and preferably $p-1$ contains a large prime factor (e.g. $p$ is safe), $g in mathbb{Z}^*_p$ such that $g$ generates $mathbb{Z}^*_p$, and $h = g^x$ for some secret key value $x$.

Constructor arguments: :type p: int :param p: Modulus of the cyclic group used. :type g: int :param g: Generator of the cyclic group used. :type h: int :param h: Public key value.

__str__()[source]

Give string representation of this ElGamalPublicKey.

Return type:

str

Returns:

String representation of public key prepended by (p, g, h) =

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

Deserialization function for public keys, which will be passed to the communication module.

Parameters:
  • obj (dict[str, Any]) – serialized version of a ElGamalPublicKey.

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

Raises:

SerializationError – When communication library is not installed.

Return type:

ElGamalPublicKey

Returns:

Deserialized ElGamalPublicKey from the given dict.

g: int
h: int
p: int
serialize(**_kwargs)[source]

Serialization function for public keys, which will be passed to the communication module.

Parameters:

**_kwargs (Any) – optional extra keyword arguments

Raises:

SerializationError – When communication library is not installed.

Return type:

dict[str, Any]

Returns:

serialized version of this ElGamalPublicKey.

class elgamal.elgamal_base.ElGamalSecretKey(p, g, x)[source]

Bases: SecretKey

SecretKey $(p, g, x)$ for the ElGamal encryption scheme, such that $p$ is prime and preferably $p-1$ contains a large prime factor (e.g. $p$ is safe), $g in mathbb{Z}^*_p$ such that $g$ generates $mathbb{Z}^*_p$, and $x$ is a uniformly random integer between $1$ and $p-1$

Constructor arguments: :type p: int :param p: Modulus of the cyclic group used. :type g: int :param g: Generator of the cyclic group used. :type x: int :param x: Secret key value.

__str__()[source]

Give string representation of this ElGamalSecretKey.

Return type:

str

Returns:

String representation of secret key prepended by (p, g, x) =

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

Deserialization function for secret keys, which will be passed to the communication module

Parameters:
  • obj (dict[str, Any]) – serialized version of a ElGamalSecretKey.

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

Raises:

SerializationError – When communication library is not installed.

Return type:

ElGamalSecretKey

Returns:

Deserialized ElGamalSecretKey from the given dict.

g: int
p: int
serialize(**_kwargs)[source]

Serialization function for secret keys, which will be passed to the communication module.

Parameters:

**_kwargs (Any) – optional extra keyword arguments

Raises:

SerializationError – When communication library is not installed.

Return type:

dict[str, Any]

Returns:

serialized version of this ElGamalSecretKey.

x: int
elgamal.elgamal_base.deserialize(elgamal_cls, obj, *, origin=None, **_kwargs)[source]

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

Parameters:
  • elgamal_cls (type[TypeVar(ElGamalType, bound= ElGamalBase[Any])]) – ElGamal or ElGamalAdditive scheme class to deserialize to.

  • obj (SerializedElGamalBase) – Serialized version of a 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:

TypeVar(ElGamalType, bound= ElGamalBase[Any])

Returns:

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

elgamal.elgamal_base.deserialize_ciphertext(elgamalbaseciphertext_cls, obj, **_kwargs)[source]

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

Parameters:
  • elgamalbaseciphertext_cls (type[TypeVar(ElGamalBaseCipherTextType, bound= ElGamalBaseCipherText)]) – ElGamalBaseCipherText class to deserialize to.

  • obj (SerializedElGamalBaseCipherText) – serialized version of a ElGamalBaseCipherText.

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

Raises:

SerializationError – When communication library is not installed.

Return type:

TypeVar(ElGamalBaseCipherTextType, bound= ElGamalBaseCipherText)

Returns:

Deserialized ElGamalBaseCipherText from the given dict.