templates.encryption_scheme module

Generic classes used for creating an encryption scheme.

class templates.encryption_scheme.Ciphertext(raw_value, scheme)[source]

Bases: Generic[KM, PT, RP, CV]

Object that contains the ciphertext value for a particular EncryptionScheme. Also includes functionality for arithmetic operations on ciphertexts.

__add__(other)[source]

Add other to the underlying plaintext of this ciphertext.

Parameters:

other (Union[TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any]), TypeVar(PT)]) – Plaintext value or other ciphertext. If Plaintext value we add the plaintext value to the underlying ciphertext. If ciphertext we add the both underlying ciphertexts.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

Addition of other to this ciphertext.

__init__(raw_value, scheme)[source]

Constructs a Ciphertext with the given ciphertext value encrypted using the specified scheme.

Parameters:
  • raw_value (TypeVar(CV)) – value of the ciphertext

  • scheme (EncryptionScheme[TypeVar(KM), TypeVar(PT), TypeVar(RP), TypeVar(CV), TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])]) – encryption scheme used for creating this ciphertext

__mul__(other)[source]

Multiply other with the underlying plaintext of this ciphertext.

Parameters:

other (Union[TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any]), TypeVar(PT)]) – Plaintext value or other ciphertext. If Plaintext value we multiply the plaintext value with the underlying ciphertext. If ciphertext we multiply the both underlying ciphertexts.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

Multiplication of other with this ciphertext.

__neg__()[source]

Negate the underlying plaintext of this ciphertext.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

Negated ciphertext.

__pow__(power)[source]

Exponentiate underlying plaintext of this ciphertext with the given exponent.

Parameters:

power (int) – Exponent to which the underlying plaintext should be exponentiated.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

Exponentiation of ciphertext to the given power.

__radd__(other)[source]

Add other to the underlying plaintext of this ciphertext.

Parameters:

other (Union[TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any]), TypeVar(PT)]) – Plaintext value or other ciphertext. If Plaintext value we add the plaintext value to the underlying ciphertext. If ciphertext we add the both underlying ciphertexts.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

Addition of other to this ciphertext.

__rmul__(other)[source]

Multiply other with the underlying plaintext of this ciphertext.

Parameters:

other (Union[TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any]), TypeVar(PT)]) – Plaintext value or other ciphertext. If Plaintext value we multiply the plaintext value with the underlying ciphertext. If ciphertext we multiply the both underlying ciphertexts.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

Multiplication of other with this ciphertext.

__rsub__(other)[source]

Subtract other from the underlying plaintext of this ciphertext.

Parameters:

other (Union[TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any]), TypeVar(PT)]) – Plaintext value or other ciphertext. If Plaintext value we subtract the plaintext value from the underlying ciphertext. If ciphertext we subtract the both underlying ciphertexts.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

Subtraction of other from this ciphertext.

__sub__(other)[source]

Subtract other from the underlying plaintext of this ciphertext.

Parameters:

other (Union[TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any]), TypeVar(PT)]) – Plaintext value or other ciphertext. If Plaintext value we subtract the plaintext value from the underlying ciphertext. If ciphertext we subtract the both underlying ciphertexts.

Raises:

TypeError – When the other object has an unsupported type for subtraction from this ciphertext.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

Subtraction of other from this ciphertext.

property value: CV

Raw value of the ciphertext.

Returns:

Value of the ciphertext

class templates.encryption_scheme.EncodedPlaintext(value, scheme)[source]

Bases: Generic[RP]

Class that contains the encoding of a plaintext for a particular scheme.

__eq__(other)[source]

Compare equality of two ciphertexts.

Parameters:

other (object) – The other object to compare with.

Raises:

TypeError – When other object has the incorrect type.

Return type:

bool

Returns:

Boolean value representing (in)equality of self and other.

__init__(value, scheme)[source]

Constructs an EncodedPlaintext with the given value and encoding as specified in the scheme.

Parameters:
  • value (TypeVar(RP)) – value of the plaintext after encoding

  • scheme (EncryptionScheme[TypeVar(KM), TypeVar(PT), TypeVar(RP), TypeVar(CV), TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])]) – encryption scheme that specifies the used encoding

Raises:

TypeError – When scheme has the incorrect type.

class templates.encryption_scheme.EncryptionScheme(*args, **kwargs)[source]

Bases: ABC, Generic[KM, PT, RP, CV, CT]

Abstract base class to define generic EncryptionScheme functionality. Can be used for all kinds of encryption scheme (e.g. Asymmetric, Symmetric).

Most easily constructed using the from_security_parameter method.

All abstract methods should be implemented by subclasses.

__init__(*args, **kwargs)[source]

Construct a new EncryptionScheme.

add(ciphertext_1, ciphertext_2)[source]

Add the underlying plaintext value of ciphertext_1 with the (underlying) plaintext value of ciphertext_2. Where ciphertext_2 can either be another ciphertext or a plaintext, depending on the scheme.

Parameters:
  • ciphertext_1 (TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])) – First Ciphertext of which the underlying plaintext is added.

  • ciphertext_2 (Union[TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any]), TypeVar(PT)]) – Either a second Ciphertext of which the underlying plaintext is multiplied with the first, or a Plaintext that is added with the underlying plaintext of the first Ciphertext.

Raises:

NotImplementedError – Raised when addition is not supported by this scheme.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

A Ciphertext containing the encryption of the addition of both values.

abstract decode(encoded_plaintext)[source]

Decode an EncodedPlaintext using the specified encoding scheme.

Parameters:

encoded_plaintext (EncodedPlaintext[TypeVar(RP)]) – Plaintext to be decoded.

Return type:

TypeVar(PT)

Returns:

Decoded Plaintext value

decrypt(ciphertext, apply_encoding=True)[source]

Decrypts the input ciphertext.

Parameters:
  • ciphertext (TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])) – Ciphertext to be decrypted.

  • apply_encoding (bool) – Boolean indicating whether the decrypted ciphertext is decoded before it is returned. Defaults to True.

Return type:

TypeVar(PT)

Returns:

Plaintext decrypted value.

abstract encode(plaintext)[source]

Encode a supported Plaintext using the specified encoding scheme.

Parameters:

plaintext (TypeVar(PT)) – Plaintext to be encoded.

Return type:

EncodedPlaintext[TypeVar(RP)]

Returns:

EncodedPlaintext object containing the encoded value.

encrypt(plaintext, apply_encoding=True)[source]

Encrypts the entered (encoded) Plaintext. Also encodes the Plaintext when this is required.

Parameters:
  • plaintext (Union[TypeVar(PT), EncodedPlaintext[TypeVar(RP)]]) – Plaintext or EncodedPlaintext to be encrypted.

  • apply_encoding (bool) – Boolean indicating whether a non-encoded plaintext should be encoded. If False, the plaintext is encrypted in raw form. Defaults to True.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

Ciphertext object containing the encrypted value of the plaintext.

abstract classmethod from_security_parameter(*args, **kwargs)[source]

Generate a new EncryptionScheme from a security parameter.

Parameters:
  • args (Any) – Security parameter(s) and optional extra arguments for the EncryptionScheme constructor.

  • kwargs (Any) – Security parameter(s) and optional extra arguments for the EncryptionScheme constructor.

Return type:

TypeVar(ES, bound= EncryptionScheme[Any, Any, Any, Any, Any])

Returns:

A new EncryptionScheme.

abstract static generate_key_material(*args, **kwargs)[source]

Method to generate key material (format depending on the type of scheme) for this scheme.

Parameters:
  • args (Any) – Required arguments to generate said key material.

  • kwargs (Any) – Required arguments to generate said key material.

Return type:

TypeVar(KM)

classmethod get_instance(*args, **kwargs)[source]

Alternative to the constructor function to obtain a quasi-singleton object. This function can be called whenever a scheme needs to be initiated and ensures that identical calls will return a reference to the same object.

Parameters:
  • args (Any) – regular arguments that would normally go into the constructor

  • kwargs (Any) – regular keyword arguments that would normally go into the constructor

Return type:

TypeVar(ES, bound= EncryptionScheme[Any, Any, Any, Any, Any])

Returns:

Either a newly instantiated scheme or a reference to an already existing scheme

classmethod id_from_arguments(*args, **kwargs)[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:
  • args (Any) – regular arguments

  • kwargs (Any) – regular keyword arguments

Return type:

int

Returns:

identifier of type int

mul(ciphertext_1, ciphertext_2)[source]

Multiply the underlying plaintext value of ciphertext_1 with the (underlying) plaintext value of ciphertext_2. Where ciphertext_2 can either be another ciphertext or a plaintext, depending on the scheme.

Parameters:
  • ciphertext_1 (TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])) – First Ciphertext of which the underlying plaintext is multiplied.

  • ciphertext_2 (Union[TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any]), TypeVar(PT)]) – Either a second Ciphertext of which the underlying plaintext is multiplied with the first, or a Plaintext that is multiplied with the underlying plaintext of the first Ciphertext.

Raises:

NotImplementedError – Raised when multiplication is not supported by this scheme.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

A Ciphertext containing the encryption of the product of both values.

neg(ciphertext)[source]

Negate the underlying plaintext of this ciphertext. I.e. if the original plaintext of this ciphertext was 5 this method returns the ciphertext that has -5 as underlying plaintext.

Parameters:

ciphertext (TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])) – Ciphertext of which the underlying plaintext should be negated.

Raises:

NotImplementedError – Raised when negation is not supported by this scheme.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

Ciphertext object corresponding to the negated plaintext.

pow(ciphertext, power)[source]

Raise the underlying plaintext value of ciph with the exponent power.

Parameters:
  • ciphertext (TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])) – Ciphertext containing the plaintext base.

  • power (int) – Exponent to which the base should be raised.

Raises:

NotImplementedError – Raised when exponentiation is not supported by this scheme.

Return type:

TypeVar(CT, bound= Ciphertext[Any, Any, Any, Any])

Returns:

Ciphertext containing the value of the underlying plaintext of ciph raised to the given power.

class templates.encryption_scheme.SupportsNeg(*args, **kwargs)[source]

Bases: Protocol[TCov]

An ABC with one abstract method __neg__.