elgamal.elgamal_additive module

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

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

Bases: ElGamalBase[ElGamalAdditiveCiphertext]

Construct ElGamalAdditive encryption scheme that is additively homomorphic.

CipherTextConstructor

alias of ElGamalAdditiveCiphertext

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

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

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

  • secret_key (ElGamalSecretKey | None) – Optional Secret Key for this ElGamalAdditive 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.

add(ciphertext_1, ciphertext_2)[source]

Secure addition.

If ciphertext_2 is another ElGamalAdditiveCiphertext, add the underlying plaintext value of ciphertext_1 to the underlying plaintext value of ciphertext_2. If it is a Plaintext, add the plaintext value to 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 (ElGamalAdditiveCiphertext) – ElGamalAdditiveCiphertext of which the underlying plaintext is added.

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

Raises:

AttributeError – When ciphertext does not have the same public key as this ciphertext object.

Return type:

ElGamalAdditiveCiphertext

Returns:

An ElGamalAdditiveCiphertext containing the encryption of the addition.

decode(encoded_plaintext)[source]

Decode an EncodedPlaintext using Pollard’s rho algorithm for discrete logarithms.

Parameters:

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

Raises:

ValueError – When ciphertext cannot be decoded because it is out of range.

Return type:

int

Returns:

Decoded plaintext value.

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

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

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

ElGamalAdditive

Returns:

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

encode(plaintext)[source]

Encode message $m$ (int) as $g^m mod p$ (int).

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.

mul(ciphertext, scalar)[source]

Multiply the underlying plaintext value of this ciphertext with a scalar.

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 (ElGamalAdditiveCiphertext) – ElGamalAdditiveCiphertext of which the underlying plaintext is multiplied.

  • scalar (int) – A scalar with which the plaintext underlying this ciphertext should be multiplied.

Raises:

TypeError – When the scalar is not an integer.

Return type:

ElGamalAdditiveCiphertext

Returns:

ElGamalAdditiveCiphertext containing the encryption of the product.

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 (ElGamalAdditiveCiphertext) – ElGamalAdditiveCiphertext of which the underlying plaintext should be negated.

Return type:

ElGamalAdditiveCiphertext

Returns:

ElGamalAdditiveCiphertext object corresponding to the negated plaintext.

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

Bases: ElGamalBaseCipherText

Ciphertext for the ElGamal encryption scheme that is additively homomorphic.

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

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

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

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

Raises:

SerializationError – When communication library is not installed.

Return type:

ElGamalAdditiveCiphertext

Returns:

Deserialized ElGamalAdditiveCiphertext from the given dict.

scheme: ElGamalAdditive