API reference

eclib.elgamal

ElGamal encryption scheme.

This module implements the ElGamal encryption scheme, which is a public-key cryptosystem based on the Decisional Diffie-Hellman (DDH) assumption. It provides functionalities for generating public and secret keys, encryption, decryption, and homomorphic operations (multiplication). It also includes functions for encoding and decoding floating-point data into and from plaintexts.

Classes

  • PublicParameters

  • SecretKey

  • PublicKey

Functions

  • keygen

  • encrypt

  • decrypt

  • mult

  • encode

  • decode

  • enc

  • dec

  • dec_add

class eclib.elgamal.PublicKey(params=None, sk=None)[source]

Represents a public key of the ElGamal encryption scheme.

h

Public key value.

Type:

int

__init__(params=None, sk=None)[source]

Initializes a new PublicKey object.

Parameters:
Return type:

None

Note

If params or sk is None, the public key will be initialized with 0. Otherwise, it will be computed as h = g^s mod p, where g is a generator of a cyclic group used as a plaintext space, s is a secret key value, and p is a prime number representing the modulus of the cyclic group.

class eclib.elgamal.PublicParameters(bit_length=None)[source]

Represents public parameters of the ElGamal encryption scheme.

p

Prime number representing the modulus of a cyclic group used as a plaintext space.

Type:

int

q

Prime number representing the order of the cyclic group.

Type:

int

g

Generator of the cyclic group.

Type:

int

__init__(bit_length=None)[source]

Initializes a new PublicParameters object.

Parameters:

bit_length (int, optional) – Desired bit length for the prime number q.

Return type:

None

Note

If bit_length is None, the public parameters will be initialized with 0. Otherwise, the public parameters will be initialized with a Sophie Germain prime q and a safe prime p such that p = 2q + 1, and a generator g of the cyclic group of order q modulo p.

class eclib.elgamal.SecretKey(params=None)[source]

Represents a secret key of the ElGamal encryption scheme.

s

Secret key value.

Type:

int

__init__(params=None)[source]

Initializes a new SecretKey object.

Parameters:

params (eclib.elgamal.PublicParameters, optional) – Cryptosystem parameters used for computing the secret key.

Return type:

None

Note

If params is None, the secret key will be initialized with 0. Otherwise, it will be initialized as a random number in [1, q - 1], where q is a prime number representing the order of a cyclic group used as a plaintext space.

eclib.elgamal.dec(params, sk, c, scale)[source]

Decrypts and decodes a scalar, vector, or matrix ciphertext c using a secret key sk.

Parameters:
Returns:

Decoded floating-point data of the decrypted plaintext.

Return type:

array_like

eclib.elgamal.dec_add(params, sk, c, scale)[source]

Decrypts and computes the sum of row-wise elements of a scalar, vector, or matrix ciphertext c using a secret key sk.

Parameters:
Returns:

Decoded floating-point data of the sum of the row-wise elements of the decrypted plaintext.

Return type:

array_like

Raises:

ValueError – If the ciphertext is not a scalar, vector, or matrix.

eclib.elgamal.decode(params, m, scale)[source]

Decodes a scalar, vector, or matrix plaintext m into a floating-point data.

Parameters:
Returns:

Decoded floating-point data.

Return type:

array_like

eclib.elgamal.decrypt(params, sk, c)[source]

Decrypts a scalar, vector, or matrix ciphertext c using a secret key sk.

Parameters:
Returns:

Decrypted plaintext.

Return type:

array_like

Raises:

ValueError – If the ciphertext is not a scalar, vector, or matrix.

eclib.elgamal.enc(params, pk, x, scale)[source]

Encodes and encrypts a scalar, vector, or matrix floating-point data x using a public key pk.

Parameters:
Returns:

Ciphertext of the encoded plaintext of the floating-point data.

Return type:

numpy.ndarray

eclib.elgamal.encode(params, x, scale)[source]

Encodes a scalar, vector, or matrix floating-point data x into a plaintext.

Parameters:
  • params (eclib.elgamal.PublicParameters) – Cryptosystem parameters.

  • x (array_like) – Floating-point data to be encoded.

  • scale (float) – Scaling factor.

Returns:

Encoded plaintext.

Return type:

array_like

eclib.elgamal.encrypt(params, pk, m)[source]

Encrypts a scalar, vector, or matrix plaintext m using a public key pk.

Parameters:
Returns:

Ciphertext of the plaintext.

Return type:

numpy.ndarray

Raises:

ValueError – If the plaintext is not a scalar, vector, or matrix.

eclib.elgamal.keygen(bit_length)[source]

Generates public parameters, a public key, and a secret key.

Parameters:

bit_length (int) – Desired bit length of a prime number representing the order of a cyclic group used as a plaintext space.

Returns:

  • params (eclib.elgamal.PublicParameters) – Cryptosystem parameters.

  • pk (eclib.elgamal.PublicKey) – Public key used for encryption.

  • sk (eclib.elgamal.SecretKey) – Secret key used for decryption.

Return type:

tuple[eclib.elgamal.PublicParameters, eclib.elgamal.PublicKey, eclib.elgamal.SecretKey]

eclib.elgamal.mult(params, c1, c2)[source]

Computes a ciphertext of the Hadamard product of two scalar, vector, or matrix plaintexts corresponding to ciphertexts c1 and c2.

Parameters:
  • params (eclib.elgamal.PublicParameters) – Cryptosystem parameters.

  • c1 (numpy.ndarray) – Ciphertext of the first plaintext.

  • c2 (numpy.ndarray) – Ciphertext of the second plaintext.

Returns:

Ciphertext of the Hadamard product of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the ciphertexts are not the following types of appropriate sizes: scalar-scalar, scalar-vector, scalar-matrix, vector-vector, matrix-vector, or matrix-matrix.

eclib.dyn_elgamal

Dynamic-key ElGamal encryption scheme.

This module implements the dynamic-key ElGamal encryption scheme, a variant of the ElGamal encryption scheme, that allows for updating keys and ciphertexts. This is useful for applications where the key pair needs to be updated periodically to enhance security. The module provides functionalities for generating public and secret keys, encryption, decryption, and homomorphic operations (multiplication). It also includes functions for encoding and decoding floating-point data into and from plaintexts.

Classes

  • Token

Functions

  • keygen

  • encrypt

  • decrypt

  • mult

  • encode

  • decode

  • enc

  • dec

  • dec_add

  • update_key

  • update_ct

class eclib.dyn_elgamal.Token(s, h)[source]

Represents a token used for updating keys and ciphertexts in the dynamic-key ElGamal encryption scheme.

s

Previous secret key value.

Type:

int

h

Previous public key value.

Type:

int

eclib.dyn_elgamal.dec(params, sk, c, scale)[source]

This function is the same as eclib.elgamal.dec().

eclib.dyn_elgamal.dec_add(params, sk, c, scale)[source]

This function is the same as eclib.elgamal.dec_add().

eclib.dyn_elgamal.decode(params, m, scale)[source]

This function is the same as eclib.elgamal.decode().

eclib.dyn_elgamal.decrypt(params, sk, c)[source]

This function is the same as eclib.elgamal.decrypt().

eclib.dyn_elgamal.enc(params, pk, x, scale)[source]

This function is the same as eclib.elgamal.enc().

eclib.dyn_elgamal.encode(params, x, scale)[source]

This function is the same as eclib.elgamal.encode().

eclib.dyn_elgamal.encrypt(params, pk, m)[source]

This function is the same as eclib.elgamal.encrypt().

eclib.dyn_elgamal.keygen(bit_length)[source]

This function is the same as eclib.elgamal.keygen().

eclib.dyn_elgamal.mult(params, c1, c2)[source]

This function is the same as eclib.elgamal.mult().

eclib.dyn_elgamal.update_ct(params, c, t)[source]

Updates a scalar, vector, or matrix ciphertext c using a token t.

Parameters:
Returns:

Updated ciphertext.

Return type:

numpy.ndarray

Raises:

ValueError – If the ciphertext is not a scalar, vector, or matrix.

eclib.dyn_elgamal.update_key(params, pk, sk)[source]

Updates a public key pk and secret key sk with a token used for updating ciphertexts.

Parameters:
Returns:

  • pk_updated (eclib.elgamal.PublicKey) – Updated public key.

  • sk_updated (eclib.elgamal.SecretKey) – Updated secret key.

  • t (eclib.dyn_elgamal.Token) – Token used for updating ciphertexts.

Return type:

tuple[eclib.elgamal.PublicKey, eclib.elgamal.SecretKey, eclib.dyn_elgamal.Token]

eclib.paillier

Paillier encryption scheme.

This module implements the Paillier encryption scheme, which is a public-key cryptosystem based on the decisional composite residuosity assumption. It provides functionalities for generating public and secret keys, encryption, decryption, and homomorphic operations (addition and integer multiplication). It also includes functions for encoding and decoding floating-point data into and from plaintexts.

Classes

  • PublicParameters

  • SecretKey

  • PublicKey

Functions

  • keygen

  • encrypt

  • decrypt

  • add

  • elementwise_add

  • int_mult

  • elementwise_int_mult

  • encode

  • decode

  • enc

  • dec

class eclib.paillier.PublicKey(params)[source]

Represents a public key of the Paillier encryption scheme.

g

Public key value computed as n + 1, where n is the product of two semiprime factors used as the modulus of plaintext space.

Type:

int

__init__(params)[source]

Initializes a new PublicKey object.

Parameters:

params (eclib.paillier.PublicParameters) – Cryptosystem parameters used for computing the public key.

class eclib.paillier.PublicParameters(sk)[source]

Represents public parameters of the Paillier encryption scheme.

n

Product of two semiprime factors used as the modulus of plaintext space.

Type:

int

n_square

The square of n used as the modulus of ciphertext space.

Type:

int

__init__(sk)[source]

Initializes a new PublicParameters object.

Parameters:

sk (eclib.paillier.SecretKey) – Secret key used for computing the public parameters.

class eclib.paillier.SecretKey(bit_length)[source]

Represents a secret key of the Paillier encryption scheme.

p

The first prime factor.

Type:

int

q

The second prime factor.

Type:

int

lmd

Value of Euler’s totient function of n = p * q.

Type:

int

mu

The multiplicative inverse of lmd modulo n.

Type:

int

__init__(bit_length)[source]

Initializes a new SecretKey object.

Parameters:

bit_length (int) – Desired bit length of semiprime factors.

eclib.paillier.add(params, c1, c2)[source]

Computes a ciphertext of the addition of two scalar, vector, or matrix plaintexts corresponding to ciphertexts c1 and c2.

Parameters:
  • params (eclib.paillier.PublicParameters) – Cryptosystem parameters.

  • c1 (int or numpy.ndarray) – Ciphertext of the first plaintext.

  • c2 (int or numpy.ndarray) – Ciphertext of the second plaintext.

Returns:

Ciphertext of the addition of the plaintexts.

Return type:

int or numpy.ndarray

Raises:

ValueError – If the ciphertexts are not the following types of appropriate sizes: scalar-scalar, vector-vector, or matrix-matrix.

eclib.paillier.dec(params, sk, c, scale)[source]

Decrypts and decodes a scalar, vector, or matrix ciphertext c using a secret key sk.

Parameters:
Returns:

Decoded floating-point data of the decrypted plaintext.

Return type:

array_like

eclib.paillier.decode(params, m, scale)[source]

Decodes a scalar, vector, or matrix plaintext m into a floating-point data.

Parameters:
Returns:

Decoded floating-point data.

Return type:

array_like

eclib.paillier.decrypt(params, sk, c)[source]

Decrypts a scalar, vector, or matrix ciphertext c using a secret key sk.

Parameters:
Returns:

Decrypted plaintext.

Return type:

array_like

Raises:

ValueError – If the ciphertext is not a scalar, vector, or matrix.

eclib.paillier.elementwise_add(params, c1, c2)[source]

Computes a ciphertext of the elementwise addition of two scalar, vector, or matrix plaintexts corresponding to ciphertexts c1 and c2.

Parameters:
  • params (eclib.paillier.PublicParameters) – Cryptosystem parameters.

  • c1 (int or numpy.ndarray) – Ciphertext of the first plaintext.

  • c2 (int or numpy.ndarray) – Ciphertext of the second plaintext.

Returns:

Ciphertext of the elementwise addition of the plaintexts.

Return type:

int or numpy.ndarray

Raises:

ValueError – If the ciphertexts are not the following types of appropriate sizes: scalar-scalar, vector-vector, matrix-vector, or matrix-matrix.

eclib.paillier.elementwise_int_mult(params, m, c)[source]

Computes a ciphertext of the elementwise product of a scalar, vector, or matrix plaintext m and another scalar, vector, or matrix plaintext corresponding to a ciphertext c.

Parameters:
  • params (eclib.paillier.PublicParameters) – Cryptosystem parameters.

  • m (array_like) – Plaintext to be multiplied.

  • c (int or numpy.ndarray) – Ciphertext of a plaintext to be multiplied.

Returns:

Ciphertext of the elementwise product of the plaintexts.

Return type:

int or numpy.ndarray

Raises:

ValueError – If the plaintext and ciphertext are not the following types of appropriate sizes: scalar-scalar, scalar-vector, scalar-matrix, vector-vector, matrix-vector, or matrix-matrix.

eclib.paillier.enc(params, pk, x, scale)[source]

Encodes and encrypts a scalar, vector, or matrix floating-point data x using a public key pk.

Parameters:
Returns:

Ciphertext of the encoded plaintext of the floating-point data.

Return type:

int or numpy.ndarray

eclib.paillier.encode(params, x, scale)[source]

Encodes a scalar, vector, or matrix floating-point data x into a plaintext.

Parameters:
  • params (eclib.paillier.PublicParameters) – Cryptosystem parameters.

  • x (array_like) – Floating-point data to be encoded.

  • scale (float) – Scaling factor.

Returns:

Encoded plaintext.

Return type:

array_like

eclib.paillier.encrypt(params, pk, m)[source]

Encrypts a scalar, vector, or matrix plaintext m using a public key pk.

Parameters:
Returns:

Ciphertext of the plaintext.

Return type:

int or numpy.ndarray

Raises:

ValueError – If the plaintext is not a scalar, vector, or matrix.

eclib.paillier.int_mult(params, m, c)[source]

Computes a ciphertext of the product of a scalar, vector, or matrix plaintext m and another scalar, vector, or matrix plaintext corresponding to a ciphertext c.

Parameters:
  • params (eclib.paillier.PublicParameters) – Cryptosystem parameters.

  • m (array_like) – Plaintext to be multiplied.

  • c (int or numpy.ndarray) – Ciphertext of a plaintext to be multiplied.

Returns:

Ciphertext of the product of the plaintexts.

Return type:

int or numpy.ndarray

Raises:

ValueError – If the plaintext and ciphertext are not the following types of appropriate sizes: scalar-scalar, scalar-vector, scalar-matrix, vector-vector, matrix-vector, or matrix-matrix.

eclib.paillier.keygen(bit_length)[source]

Generates public parameters, a public key, and a secret key.

Parameters:

bit_length (int) – Desired bit length of semiprime factors whose product is used as the modulus of plaintext space, and the square of the product is used as the modulus of ciphertext space.

Returns:

  • params (eclib.paillier.PublicParameters) – Cryptosystem parameters.

  • pk (eclib.paillier.PublicKey) – Public key used for encryption.

  • sk (eclib.paillier.SecretKey) – Secret key used for decryption.

Return type:

tuple[eclib.paillier.PublicParameters, eclib.paillier.PublicKey, eclib.paillier.SecretKey]

eclib.regev

Regev (LWE) encryption scheme.

This module implements the Regev (LWE) encryption scheme, which is a public-key cryptosystem based on the Learning With Errors (LWE) problem. It provides functionalities for generating public and secret keys, encryption, decryption, and homomorphic operations (addition and integer multiplication). It also includes functions for encoding and decoding floating-point data into and from plaintexts.

Classes

  • PublicParameters

  • SecretKey

  • PublicKey

Functions

  • keygen

  • encrypt

  • decrypt

  • add

  • elementwise_add

  • int_mult

  • elementwise_int_mult

  • encode

  • decode

  • enc

  • dec

class eclib.regev.PublicKey(params, sk)[source]

Represents a public key of the Regev (LWE) encryption scheme.

A

Public key matrix.

Type:

numpy.ndarray

b

Public key vector.

Type:

numpy.ndarray

B

Concatenation of the vector b and the matrix A.

Type:

numpy.ndarray

__init__(params, sk)[source]

Initializes a new PublicKey object.

Parameters:

Note

The public key is a matrix B, which is a concatenation of a m-dimensional row vector b and a n-by-m matrix A. The matrix A is a random matrix of integers modulo q, and the vector b is given by b = s^T A + e^T mod q, where q is the modulus of a ciphertext space, s is the secret key, and e is a m-dimensional error vector sampled from the discrete Gaussian distribution with mean zero and standard deviation sigma.

class eclib.regev.PublicParameters(n, t, q, sigma, m=None)[source]

Represents public parameters of the Regev (LWE) encryption scheme.

n

Dimension of a lattice, which is equal to the dimension of secret key.

Type:

int

t

Modulus of a plaintext space.

Type:

int

q

Modulus of a ciphertext space.

Type:

int

sigma

Standard deviation of the discrete Gaussian distribution with mean zero used as an error distribution.

Type:

float

m

Subdimension of the lattice.

Type:

int

__init__(n, t, q, sigma, m=None)[source]

Initializes a new PublicParameters object.

Parameters:
  • n (int) – Dimension of a lattice, which is equal to the dimension of secret key.

  • t (int) – Modulus of a plaintext space.

  • q (int) – Modulus of a ciphertext space.

  • sigma (float) – Standard deviation of the discrete Gaussian distribution with mean zero used as an error distribution.

  • m (int, optional) – Subdimension of the lattice.

Note

If m is not provided, it is set to 2 * n * ceil(log2(q)).

class eclib.regev.SecretKey(params)[source]

Represents a secret key of the Regev (LWE) encryption scheme.

s

Secret key value.

Type:

numpy.ndarray

__init__(params)[source]

Initializes a new SecretKey object.

Parameters:

params (eclib.regev.PublicParameters) – Cryptosystem parameters.

Note

A secret key is a n-dimensional random vector of integers modulo q, which is the modulus of a ciphertext space.

eclib.regev.add(params, c1, c2)[source]

Computes a ciphertext of the addition of two scalar, vector, or matrix plaintexts corresponding to ciphertexts c1 and c2.

Parameters:
  • params (eclib.regev.PublicParameters) – Cryptosystem parameters.

  • c1 (numpy.ndarray) – Ciphertext of the first plaintext.

  • c2 (numpy.ndarray) – Ciphertext of the second plaintext.

Returns:

Ciphertext of the addition of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the ciphertexts are not the following types of appropriate sizes: scalar-scalar, vector-vector, or matrix-matrix.

eclib.regev.dec(params, sk, c, scale)[source]

Decrypts and decodes a scalar, vector, or matrix ciphertext c using a secret key sk.

Parameters:
Returns:

Decoded floating-point data of the decrypted plaintext.

Return type:

array_like

eclib.regev.decode(params, m, scale)[source]

Decodes a scalar, vector, or matrix plaintext m into a floating-point data.

Parameters:
  • params (eclib.regev.PublicParameters) – Cryptosystem parameters.

  • m (array_like) – Plaintext to be decoded.

  • scale (float) – Scaling factor.

Returns:

Decoded floating-point data.

Return type:

array_like

eclib.regev.decrypt(params, sk, c)[source]

Decrypts a scalar, vector, or matrix ciphertext c using a secret key sk.

Parameters:
Returns:

Decrypted plaintext.

Return type:

array_like

Raises:

ValueError – If the ciphertext is not a scalar, vector, or matrix.

eclib.regev.elementwise_add(params, c1, c2)[source]

Computes a ciphertext of the elementwise addition of two scalar, vector, or matrix plaintexts corresponding to ciphertexts c1 and c2.

Parameters:
  • params (eclib.regev.PublicParameters) – Cryptosystem parameters.

  • c1 (numpy.ndarray) – Ciphertext of the first plaintext.

  • c2 (numpy.ndarray) – Ciphertext of the second plaintext.

Returns:

Ciphertext of the elementwise addition of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the ciphertexts are not the following types of appropriate sizes: scalar-scalar, vector-vector, matrix-vector, or matrix-matrix.

See also

eclib.regev.add

eclib.regev.elementwise_int_mult(params, m, c)[source]

Computes a ciphertext of the elementwise product of a scalar, vector, or matrix plaintext m and another scalar, vector, or matrix plaintext corresponding to a ciphertext c.

Parameters:
  • params (eclib.regev.PublicParameters) – Cryptosystem parameters.

  • m (array_like) – Plaintext to be multiplied.

  • c (numpy.ndarray) – Ciphertext of a plaintext to be multiplied.

Returns:

Ciphertext of the elementwise product of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the plaintext and ciphertext are not the following types of appropriate sizes: scalar-scalar, scalar-vector, scalar-matrix, vector-vector, matrix-vector, or matrix-matrix.

eclib.regev.enc(params, pk, x, scale)[source]

Encodes and encrypts a scalar, vector, or matrix floating-point data x using a public key pk.

Parameters:
Returns:

Ciphertext of the encoded plaintext of the floating-point data.

Return type:

numpy.ndarray

eclib.regev.encode(params, x, scale)[source]

Encodes a scalar, vector, or matrix floating-point data x into a plaintext.

Parameters:
  • params (eclib.regev.PublicParameters) – Cryptosystem parameters.

  • x (array_like) – Floating-point data to be encoded.

  • scale (float) – Scaling factor.

Returns:

Encoded plaintext.

Return type:

array_like

eclib.regev.encrypt(params, pk, m)[source]

Encrypts a scalar, vector, or matrix plaintext m using a public key pk.

Parameters:
Returns:

Ciphertext of the plaintext.

Return type:

numpy.ndarray

Raises:

ValueError – If the plaintext is not a scalar, vector, or matrix.

eclib.regev.int_mult(params, m, c)[source]

Computes a ciphertext of the product of a scalar, vector, or matrix plaintext m and another scalar, vector, or matrix plaintext corresponding to a ciphertext c.

Parameters:
  • params (eclib.regev.PublicParameters) – Cryptosystem parameters.

  • m (array_like) – Plaintext to be multiplied.

  • c (numpy.ndarray) – Ciphertext of a plaintext to be multiplied.

Returns:

Ciphertext of the product of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the plaintext and ciphertext are not the following types of appropriate sizes: scalar-scalar, scalar-vector, scalar-matrix, vector-vector, matrix-vector, or matrix-matrix.

eclib.regev.keygen(n, t, q, sigma, m=None)[source]

Generates public parameters, a public key, and a secret key.

Parameters:
  • n (int) – Dimension of a lattice, which is equal to the dimension of secret key.

  • t (int) – Modulus of a plaintext space.

  • q (int) – Modulus of a ciphertext space.

  • sigma (float) – Standard deviation of the discrete Gaussian distribution with mean zero used as an error distribution.

  • m (int, optional) – Subdimension of the lattice.

Returns:

  • params (eclib.regev.PublicParameters) – Cryptosystem parameters.

  • pk (eclib.regev.PublicKey) – Public key used for encryption.

  • sk (eclib.regev.SecretKey) – Secret key used for decryption.

Note

If m is not provided, it is set to 2 * n * ceil(log2(q)).

eclib.gsw

GSW encryption scheme.

This module implements the GSW encryption scheme, which is a fully homomorphic encryption scheme based on the Learning with Errors (LWE) problem. It provides functionalities for generating public and secret keys, encryption, decryption, and homomorphic operations (addition, multiplication, and integer multiplication). It also includes functions for encoding and decoding floating-point data into and from plaintexts.

Classes

  • PublicParameters

Functions

  • keygen

  • encrypt

  • decrypt

  • add

  • elementwise_add

  • mult

  • elementwise_mult

  • int_mult

  • elementwise_int_mult

  • encode

  • decode

  • enc

  • dec

class eclib.gsw.PublicParameters(n, q, sigma, m=None)[source]

Represents public parameters of the GSW encryption scheme.

n

Dimension of a lattice, which is equal to the dimension of secret key.

Type:

int

q

Modulus of plaintext and ciphertext spaces.

Type:

int

sigma

Standard deviation of the discrete Gaussian distribution with mean zero used as an error distribution.

Type:

float

m

Subdimension of the lattice.

Type:

int

l

Bit length of the modulus.

Type:

int

N

N = (n + 1) * l.

Type:

int

__init__(n, q, sigma, m=None)[source]

Initializes a new PublicParameters object.

Parameters:
  • n (int) – Dimension of a lattice, which is equal to the dimension of secret key.

  • q (int) – Modulus of plaintext and ciphertext spaces.

  • sigma (float) – Standard deviation of the discrete Gaussian distribution with mean zero used as an error distribution.

  • m (int, optional) – Subdimension of the lattice.

Return type:

None

Note

If m is not provided, it is set to 2 * n * ceil(log2(q)).

eclib.gsw.add(params, c1, c2)[source]

Computes a ciphertext of the addition of two scalar, vector, or matrix plaintexts corresponding to ciphertexts c1 and c2.

Parameters:
  • params (eclib.gsw.PublicParameters) – Cryptosystem parameters.

  • c1 (numpy.ndarray) – Ciphertext of the first plaintext.

  • c2 (numpy.ndarray) – Ciphertext of the second plaintext.

Returns:

Ciphertext of the addition of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the ciphertexts are not the following types of appropriate sizes: scalar-scalar, vector-vector, or matrix-matrix.

eclib.gsw.dec(params, sk, c, scale)[source]

Decrypts and decodes a scalar, vector, or matrix ciphertext c using a secret key sk.

Parameters:
Returns:

Decoded floating-point data of the decrypted plaintext.

Return type:

array_like

eclib.gsw.decode(params, m, scale)[source]

Decodes a scalar, vector, or matrix plaintext m into a floating-point data.

Parameters:
  • params (eclib.gsw.PublicParameters) – Cryptosystem parameters.

  • m (array_like) – Plaintext to be decoded.

  • scale (float) – Scaling factor.

Returns:

Decoded floating-point data.

Return type:

array_like

eclib.gsw.decrypt(params, sk, c)[source]

Decrypts a scalar, vector, or matrix ciphertext c using a secret key sk.

Parameters:
Returns:

Decrypted plaintext.

Return type:

array_like

Raises:

ValueError – If the ciphertext is not a scalar, vector, or matrix.

eclib.gsw.elementwise_add(params, c1, c2)[source]

Computes a ciphertext of the elementwise addition of two scalar, vector, or matrix plaintexts corresponding to ciphertexts c1 and c2.

Parameters:
  • params (eclib.gsw.PublicParameters) – Cryptosystem parameters.

  • c1 (numpy.ndarray) – Ciphertext of the first plaintext.

  • c2 (numpy.ndarray) – Ciphertext of the second plaintext.

Returns:

Ciphertext of the elementwise addition of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the ciphertexts are not the following types of appropriate sizes: scalar-scalar, vector-vector, matrix-vector, or matrix-matrix.

See also

eclib.gsw.add

eclib.gsw.elementwise_int_mult(params, m, c)[source]

Computes a ciphertext of the elementwise product of a scalar, vector, or matrix plaintext m and another scalar, vector, or matrix plaintext corresponding to a ciphertext c.

Parameters:
  • params (eclib.gsw.PublicParameters) – Cryptosystem parameters.

  • m (array_like) – Plaintext to be multiplied.

  • c (numpy.ndarray) – Ciphertext of a plaintext to be multiplied.

Returns:

Ciphertext of the elementwise product of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the plaintext and ciphertext are not the following types of appropriate sizes: scalar-scalar, scalar-vector, scalar-matrix, vector-vector, matrix-vector, or matrix-matrix.

eclib.gsw.elementwise_mult(params, c1, c2)[source]

Computes a ciphertext of the elementwise product of two scalar, vector, or matrix plaintexts corresponding to ciphertexts c1 and c2.

Parameters:
  • params (eclib.gsw.PublicParameters) – Cryptosystem parameters.

  • c1 (numpy.ndarray) – Ciphertext of the first plaintext.

  • c2 (numpy.ndarray) – Ciphertext of the second plaintext.

Returns:

Ciphertext of the elementwise product of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the ciphertexts are not the following types of appropriate sizes: scalar-scalar, scalar-vector, scalar-matrix, vector-vector, matrix-vector, or matrix-matrix.

See also

eclib.gsw.mult

eclib.gsw.enc(params, pk, x, scale)[source]

Encodes and encrypts a scalar, vector, or matrix floating-point data x using a public key pk.

Parameters:
Returns:

Ciphertext of the encoded plaintext of the floating-point data.

Return type:

numpy.ndarray

eclib.gsw.encode(params, x, scale)[source]

Encodes a scalar, vector, or matrix floating-point data x into a plaintext.

Parameters:
  • params (eclib.gsw.PublicParameters) – Cryptosystem parameters.

  • x (array_like) – Floating-point data to be encoded.

  • scale (float) – Scaling factor.

Returns:

Encoded plaintext.

Return type:

array_like

eclib.gsw.encrypt(params, pk, m)[source]

Encrypts a scalar, vector, or matrix plaintext m using a public key pk.

Parameters:
Returns:

Ciphertext of the plaintext.

Return type:

numpy.ndarray

Raises:

ValueError – If the plaintext is not a scalar, vector, or matrix.

eclib.gsw.int_mult(params, m, c)[source]

Computes a ciphertext of the product of a scalar, vector, or matrix plaintext m and another scalar, vector, or matrix plaintext corresponding to a ciphertext c.

Parameters:
  • params (eclib.gsw.PublicParameters) – Cryptosystem parameters.

  • m (array_like) – Plaintext to be multiplied.

  • c (numpy.ndarray) – Ciphertext of a plaintext to be multiplied.

Returns:

Ciphertext of the product of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the plaintext and ciphertext are not the following types of appropriate sizes: scalar-scalar, scalar-vector, scalar-matrix, vector-vector, matrix-vector, or matrix-matrix.

eclib.gsw.keygen(n, q, sigma, m=None)[source]

Generates public parameters, a public key, and a secret key.

Parameters:
  • n (int) – Dimension of a lattice, which is equal to the dimension of secret key.

  • q (int) – Modulus of plaintext and ciphertext spaces.

  • sigma (float) – Standard deviation of the discrete Gaussian distribution with mean zero used as an error distribution.

  • m (int, optional) – Subdimension of the lattice.

Returns:

  • params (eclib.gsw.PublicParameters) – Cryptosystem parameters.

  • pk (eclib.regev.PublicKey) – Public key used for encryption.

  • sk (eclib.regev.SecretKey) – Secret key used for decryption.

Note

If m is not provided, it is set to 2 * n * ceil(log2(q)).

eclib.gsw.mult(params, c1, c2)[source]

Computes a ciphertext of the product of two scalar, vector, or matrix plaintexts corresponding to ciphertexts c1 and c2.

Parameters:
  • params (eclib.gsw.PublicParameters) – Cryptosystem parameters.

  • c1 (numpy.ndarray) – Ciphertext of the first plaintext.

  • c2 (numpy.ndarray) – Ciphertext of the second plaintext.

Returns:

Ciphertext of the product of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the ciphertexts are not the following types of appropriate sizes: scalar-scalar, scalar-vector, scalar-matrix, vector-vector, matrix-vector, or matrix-matrix.

eclib.gsw_lwe

GSW-LWE encryption scheme.

This module implements the GSW-LWE encryption scheme, which is a fully homomorphic encryption scheme that combines the GSW and Regev (LWE) cryptosystems. The GSW-LWE encryption scheme supports the outer product of a GSW ciphertext and an LWE ciphertext: GSW x LWE -> LWE. This allows for efficient homomorphic multiplication. The module provides functionalities for generating public and secret keys, encryption, decryption, and homomorphic operations (addition, multiplication, and integer multiplication). It also includes functions for encoding and decoding floating-point data into and from plaintexts.

Classes

  • PublicParameters

Functions

  • keygen

  • encrypt

  • encrypt_gsw

  • decrypt

  • decrypt_gsw

  • add

  • elementwise_add

  • mult

  • elementwise_mult

  • int_mult

  • elementwise_int_mult

  • encode

  • decode

  • enc

  • enc_gsw

  • dec

  • dec_gsw

class eclib.gsw_lwe.PublicParameters(n, t, q, sigma, m=None)[source]

Represents public parameters of the GSW-LWE encryption scheme.

lwe_params

Regev (LWE) cryptosystem parameters.

Type:

eclib.regev.PublicParameters

gsw_params

GSW cryptosystem parameters.

Type:

eclib.gsw.PublicParameters

__init__(n, t, q, sigma, m=None)[source]

Initializes a new PublicParameters object.

Parameters:
  • n (int) – Dimension of a lattice, which is equal to the dimension of secret key.

  • t (int) – Modulus of a plaintext space.

  • q (int) – Modulus of a ciphertext space.

  • sigma (float) – Standard deviation of the discrete Gaussian distribution with mean zero used as an error distribution.

  • m (int, optional) – Subdimension of the lattice.

Note

If m is not provided, it is set to 2 * n * ceil(log2(q)).

eclib.gsw_lwe.add(params, c1, c2)[source]

This function is the same as eclib.regev.add().

eclib.gsw_lwe.dec(params, sk, c, scale)[source]

This function is the same as eclib.regev.dec().

eclib.gsw_lwe.dec_gsw(params, sk, c, scale)[source]

This function is the same as eclib.gsw.dec().

eclib.gsw_lwe.decode(params, m, scale)[source]

This function is the same as eclib.regev.decode().

eclib.gsw_lwe.decrypt(params, sk, c)[source]

This function is the same as eclib.regev.decrypt().

eclib.gsw_lwe.decrypt_gsw(params, sk, c)[source]

This function is the same as eclib.gsw.decrypt().

eclib.gsw_lwe.elementwise_add(params, c1, c2)[source]

This function is the same as eclib.regev.elementwise_add().

eclib.gsw_lwe.elementwise_int_mult(params, m, c)[source]

This function is the same as eclib.regev.elementwise_int_mult().

eclib.gsw_lwe.elementwise_mult(params, c1, c2)[source]

This function is the same as eclib.gsw.elementwise_mult().

eclib.gsw_lwe.enc(params, pk, x, scale)[source]

This function is the same as eclib.regev.enc().

eclib.gsw_lwe.enc_gsw(params, pk, x, scale)[source]

This function is the same as eclib.gsw.enc().

eclib.gsw_lwe.encode(params, x, scale)[source]

This function is the same as eclib.regev.encode().

eclib.gsw_lwe.encrypt(params, pk, m)[source]

This function is the same as eclib.regev.encrypt().

eclib.gsw_lwe.encrypt_gsw(params, pk, m)[source]

This function is the same as eclib.gsw.encrypt().

eclib.gsw_lwe.int_mult(params, m, c)[source]

This function is the same as eclib.regev.int_mult().

eclib.gsw_lwe.keygen(n, t, q, sigma, m=None)[source]

Generates public parameters, a public key, and a secret key.

Parameters:
  • n (int) – Dimension of a lattice, which is equal to the dimension of secret key.

  • t (int) – Modulus of a plaintext space.

  • q (int) – Modulus of a ciphertext space.

  • sigma (float) – Standard deviation of the discrete Gaussian distribution with mean zero used as an error distribution.

  • m (int, optional) – Subdimension of the lattice.

Returns:

  • params (eclib.gsw_lwe.PublicParameters) – Cryptosystem parameters consisting of Regev (LWE) and GSW public parameters.

  • pk (eclib.regev.PublicKey) – Public key used for encryption.

  • sk (eclib.regev.SecretKey) – Secret key used for decryption.

Return type:

tuple[eclib.gsw_lwe.PublicParameters, eclib.regev.PublicKey, eclib.regev.SecretKey]

Note

If m is not provided, it is set to 2 * n * ceil(log2(q)).

See also

eclib.gsw_lwe.PublicParameters, eclib.gsw_lwe.PublicKey, eclib.gsw_lwe.SecretKey

eclib.gsw_lwe.mult(params, c1, c2)[source]

Computes a ciphertext of the product of two scalar, vector, or matrix plaintexts corresponding to ciphertexts c1 and c2.

Parameters:
  • params (eclib.gsw_lwe.PublicParameters) – Cryptosystem parameters.

  • c1 (numpy.ndarray) – Ciphertext of the first plaintext.

  • c2 (numpy.ndarray) – Ciphertext of the second plaintext.

Returns:

Ciphertext of the product of the plaintexts.

Return type:

numpy.ndarray

Raises:

ValueError – If the ciphertexts are not the following types of appropriate sizes: scalar-scalar, scalar-vector, scalar-matrix, vector-vector, matrix-vector, or matrix-matrix.

eclib.system

Control system components.

This module defines classes for a control system, including a plant, sensor, actuator, operator, and controller. The classes represent the components of a control system and provide methods for fundamental operations in control systems. The classes also include methods for encrypted control systems using various encryption schemes.

Classes

  • Plant

  • Sensor

  • Actuator

  • Controller

  • Operator

  • EncryptedController

class eclib.system.Actuator(scheme=None, params=None, pk=None, sk=None, scale_enc=None, scale_dec=None)[source]

Represents an actuator in a control system.

scheme

Encryption scheme used by the actuator.

Type:

str or None

params

Cryptosystem parameters of the encryption scheme.

Type:

public parameters or None

pk

Public key of the encryption scheme.

Type:

public key or None

sk

Secret key of the encryption scheme.

Type:

secret key or None

scale_enc

Scaling factor for encoding and encryption.

Type:

float or None

scale_dec

Scaling factor for decoding and decryption.

Type:

float or None

__init__(scheme=None, params=None, pk=None, sk=None, scale_enc=None, scale_dec=None)[source]

Initialize a new Actuator object.

Parameters:
  • scheme (str, optional) – Encryption scheme used by the actuator.

  • params (public parameters or None, optional) – Cryptosystem parameters of the encryption scheme.

  • pk (public key or None, optional) – Public key of the encryption scheme.

  • sk (secret key or None, optional) – Secret key of the encryption scheme.

  • scale_enc (float, optional) – Scaling factor for encoding and encryption.

  • scale_dec (float, optional) – Scaling factor for decoding and decryption.

Return type:

None

Note

The encryption scheme must be one of the following:

  • “elgamal”

  • “dyn_elgamal”

  • “paillier”

  • “regev”

  • “gsw”

  • “gsw_lwe”

re_enc_state(controller_state)[source]

Re-encrypts an encrypted controller state using the specified encryption scheme.

Parameters:

controller_state (numpy.ndarray) – Encrypted controller state to be re-encrypted.

Returns:

Re-encrypted controller state.

Return type:

numpy.ndarray

Raises:

TypeError – If the encryption scheme, cryptosystem parameters, public key, or scaling factor is invalid.

Note

This method is used for removing the accumulation of scaling factors in the controller state.

set_enc_input(plant, input)[source]

Decrypts and sets an encrypted control input to a plant.

Parameters:

plant (Plant) – Plant whose control input is to be set.

Return type:

None

Raises:

TypeError – If the encryption scheme, cryptosystem parameters, public key, or scaling factor is invalid.

set_input(plant, input)[source]

Set a control input to a plant.

Parameters:
  • plant (Plant) – Plant whose control input is to be set.

  • input (array_like) – Control input.

Return type:

None

Raises:

ValueError – If the dimension of the input is invalid.

class eclib.system.Controller(A, B, C, D, E=None, F=None, x0=None)[source]

Represents a controller in a control system.

A

State matrix.

Type:

numpy.ndarray

B

Input matrix.

Type:

numpy.ndarray

C

Output matrix.

Type:

numpy.ndarray

D

Feedforward matrix.

Type:

numpy.ndarray

E

Reference input matrix.

Type:

numpy.ndarray

F

Reference feedforward matrix.

Type:

numpy.ndarray

state

Current state.

Type:

numpy.ndarray

input

Current input.

Type:

numpy.ndarray

output

Current output.

Type:

numpy.ndarray

reference

Current reference.

Type:

numpy.ndarray

Note

The controller is modeled as a linear time-invariant system with state-space representation:

x(t + 1) = A x(t) + B y(t) + E r(t)

u(t) = C x(t) + D y(t) + F r(t)

where x is the controller state, y is the plant output, u is the plant input, and r is the reference. Note that the matrices A, B, C, and D are not the same as those of a plant in general.

__init__(A, B, C, D, E=None, F=None, x0=None)[source]

Initialize a new Controller object.

Parameters:
  • A (array_like) – State matrix.

  • B (array_like) – Input matrix.

  • C (array_like) – Output matrix.

  • D (array_like) – Feedforward matrix.

  • E (array_like, optional) – Reference input matrix.

  • F (array_like, optional) – Reference feedforward matrix.

  • x0 (array_like, optional) – Initial state.

Raises:

ValueError – If the dimensions of the matrices or state are invalid.

Note

A, B, C, D, E, and F must be nc x nc, nc x l, m x nc, m x l, nc x q, and m x q matrices, respectively, where nc is a controller state dimension, l is a plant output dimension, m is a plant input dimension, and q is a reference dimension.

If the matrices are 1D arrays, they are reshaped to 2D arrays with a single row or column as necessary. If the state is a 2D array with a single column, it is reshaped to a 1D array.

If the matrices E and F are not provided, they are set to zero matrices of the appropriate dimensions.

If the initial state is not provided, it is set to a zero vector of the same dimension as the state matrix A.

get_output(measurement, reference=None)[source]

Updates the state of the controller and computes a control input to a plant based on a measurement and a reference.

Parameters:
  • measurement (array_like) – Measurement output of the plant.

  • reference (array_like, optional) – Reference input to the controller.

Returns:

Control input to the plant.

Return type:

array_like

Raises:

ValueError – If the dimensions of the measurement or reference are invalid.

reset(state=None, input=None, output=None, reference=None)[source]

Resets the state, input, output, and reference of the controller.

Parameters:
  • state (array_like, optional) – New state.

  • input (array_like, optional) – New input.

  • output (array_like, optional) – New output.

  • reference (array_like, optional) – New reference.

Raises:

ValueError – If the dimensions of the new state, input, output, or reference are invalid.

Return type:

None

Note

If the new state, input, output, or reference is not provided, it is set to a zero vector of the appropriate dimension.

class eclib.system.EncryptedController(scheme, params, pk, controller, scale)[source]

Represents an encrypted controller in a control system.

scheme

Encryption scheme used by the controller.

Type:

str

params

Cryptosystem parameters of the encryption scheme.

Type:

public parameters

A

Encrypted state matrix.

Type:

numpy.ndarray

B

Encrypted input matrix.

Type:

numpy.ndarray

C

Encrypted output matrix.

Type:

numpy.ndarray

D

Encrypted feedforward matrix.

Type:

numpy.ndarray

E

Encrypted reference input matrix.

Type:

numpy.ndarray

F

Encrypted reference feedforward matrix.

Type:

numpy.ndarray

state

Encrypted current state.

Type:

numpy.ndarray

input

Encrypted current input.

Type:

numpy.ndarray

output

Encrypted current output.

Type:

numpy.ndarray

reference

Encrypted current reference.

Type:

numpy.ndarray

__init__(scheme, params, pk, controller, scale)[source]

Initialize a new EncryptedController object.

Parameters:
  • scheme (str) – Encryption scheme used by the controller.

  • params (public parameters) – Cryptosystem parameters of the encryption scheme.

  • pk (public key) – Public key of the encryption scheme.

  • controller (Controller) – Controller to be encrypted.

  • scale (float) – Scaling factor.

Raises:

TypeError – If the encryption scheme, cryptosystem parameters, public key, or scaling factor is invalid.

Note

The encryption scheme must be one of the following:

  • “elgamal”

  • “dyn_elgamal”

  • “paillier”

  • “regev”

  • “gsw”

  • “gsw_lwe”

If the encryption scheme is “paillier” or “regev”, the matrices A, B, C, D, E, and F are stored as plaintexts. Otherwise, they are stored as ciphertexts.

If the encryption scheme is “gsw_lwe”, the matrices A, B, C, D, E, and F are stored as GSW ciphertexts, and state, input, output, and reference are stored as Regev (LWE) ciphertexts.

get_enc_output(measurement, reference=None, controller_state=None)[source]

Computes an encrypted state update and an encrypted control input to a plant based on an encrypted measurement and an encrypted reference.

Parameters:
  • measurement (numpy.ndarray) – Encrypted measurement output of the plant.

  • reference (numpy.ndarray, optional) – Encrypted reference input to the controller.

  • controller_state (numpy.ndarray, optional) – Encrypted current controller state.

Returns:

  • controller_state_update (numpy.ndarray) – Encrypted state update of the controller.

  • output (numpy.ndarray) – Encrypted control input to the plant.

Raises:
  • ValueError – If the dimensions of the measurement, reference, or controller state are invalid.

  • TypeError – If the encryption scheme is unsupported.

Return type:

tuple[numpy.ndarray[Any, numpy.dtype[numpy.object_]], numpy.ndarray[Any, numpy.dtype[numpy.object_]]]

Note

If the reference is not provided, the controller is assumed to be of the form

xc(t + 1) = A xc(t) + B y(t)

u(t) = C xc(t) + D y(t)

Otherwise, the controller is assumed to be of the form

xc(t + 1) = A xc(t) + B y(t) + E r(t)

u(t) = C xc(t) + D y(t) + F r(t)

where xc is the controller state, y is the plant output, u is the plant input, and r is the reference.

If the encryption scheme is “elgamal” or “dyn_elgamal”, only the element-wise product of the controller parameters and inputs is computed.

class eclib.system.Operator(scheme=None, params=None, pk=None, scale=None)[source]

Represents an operator in a control system who gives an encrypted reference to an encrypted controller.

scheme

Encryption scheme used by the operator.

Type:

str or None

params

Cryptosystem parameters of the encryption scheme.

Type:

public parameters or None

pk

Public key of the encryption scheme.

Type:

public key or None

scale

Scaling factor.

Type:

float or None

__init__(scheme=None, params=None, pk=None, scale=None)[source]

Initialize a new Operator object.

Parameters:
  • scheme (str, optional) – Encryption scheme used by the operator.

  • params (public parameters or None, optional) – Cryptosystem parameters of the encryption scheme.

  • pk (public key or None, optional) – Public key of the encryption scheme.

  • scale (float, optional) – Scaling factor.

Return type:

None

Note

The encryption scheme must be one of the following:

  • “elgamal”

  • “dyn_elgamal”

  • “paillier”

  • “regev”

  • “gsw”

  • “gsw_lwe”

get_enc_reference(reference)[source]

Encrypts a reference using the specified encryption scheme.

Parameters:

reference (array_like) – Reference to be encrypted.

Returns:

Encrypted reference.

Return type:

int or numpy.ndarray

Raises:

TypeError – If the encryption scheme, cryptosystem parameters, public key, or scaling factor is invalid.

class eclib.system.Plant(A, B, C, D, x0=None)[source]

Represents a plant in a control system.

A

State matrix.

Type:

numpy.ndarray

B

Input matrix.

Type:

numpy.ndarray

C

Output matrix.

Type:

numpy.ndarray

D

Feedforward matrix.

Type:

numpy.ndarray

state

Current state.

Type:

numpy.ndarray

input

Current input.

Type:

numpy.ndarray

output

Current output.

Type:

numpy.ndarray

Note

The plant is modeled as a linear time-invariant system with state-space representation:

x(t + 1) = A x(t) + B u(t)

y(t) = C x(t) + D u(t)

where x is the state, u is the input, and y is the output.

__init__(A, B, C, D, x0=None)[source]

Initialize a new Plant object.

Parameters:
  • A (array_like) – State matrix.

  • B (array_like) – Input matrix.

  • C (array_like) – Output matrix.

  • D (array_like) – Feedforward matrix.

  • x0 (array_like, optional) – Initial state.

Return type:

None

Raises:

ValueError – If the dimensions of the matrices or state are invalid.

Note

A, B, C, and D must be n x n, n x m, l x n, and l x m matrices, respectively, where n is a state dimension, m is an input dimension, and l is an output dimension.

If the matrices are 1D arrays, they are reshaped to 2D arrays with a single row or column as necessary. If the state is a 2D array with a single column, it is reshaped to a 1D array.

If the initial state is not provided, it is set to a zero vector of the same dimension as the state matrix A.

reset(state=None, input=None, output=None)[source]

Resets the state, input, and output of the plant.

Parameters:
  • state (array_like, optional) – New state.

  • input (array_like, optional) – New input.

  • output (array_like, optional) – New output.

Return type:

None

Raises:

ValueError – If the dimensions of the state, input, or output are invalid.

Note

The dimensions of the state, input, and output must match the dimensions of the state matrix A, input matrix B, and output matrix C, respectively.

If the state, input, or output is not provided, it is set to a zero vector of the appropriate dimension.

update()[source]

Updates the state of the plant based on the current state and input.

Return type:

None

class eclib.system.Sensor(scheme=None, params=None, pk=None, scale=None)[source]

Represents a sensor in a control system.

scheme

Encryption scheme used by the sensor.

Type:

str or None

params

Cryptosystem parameters of the encryption scheme.

Type:

public parameters or None

pk

Public key of the encryption scheme.

Type:

public key or None

scale

Scaling factor.

Type:

float or None

__init__(scheme=None, params=None, pk=None, scale=None)[source]

Initialize a new Sensor object.

Parameters:
  • scheme (str, optional) – Encryption scheme used by the sensor.

  • params (public parameters or None, optional) – Cryptosystem parameters of the encryption scheme.

  • pk (public key or None, optional) – Public key of the encryption scheme.

  • scale (float, optional) – Scaling factor.

Return type:

None

Note

The encryption scheme must be one of the following:

  • “elgamal”

  • “dyn_elgamal”

  • “paillier”

  • “regev”

  • “gsw”

  • “gsw_lwe”

get_enc_output(plant)[source]

Gets and encrypts a measurement output of a plant.

Parameters:

plant (Plant) – Plant whose output is to be measured.

Returns:

Encrypted measurement output of the plant.

Return type:

int or numpy.ndarray

Raises:

TypeError – If the encryption scheme, cryptosystem parameters, public key, or scaling factor is invalid.

get_output(plant)[source]

Gets a measurement output of a plant.

Parameters:

plant (Plant) – Plant whose output is to be measured.

Returns:

Measurement output of the plant.

Return type:

array_like

eclib.figure

Figure settings and universal colors.

This module provides a dataclass for universal colors in RGB format and a function for default figure settings for matplotlib.

Classes

  • Colors

Functions

  • setup

class eclib.figure.Colors[source]

Represents universal colors in RGB format.

red
Type:

tuple

yellow
Type:

tuple

green
Type:

tuple

blue
Type:

tuple

sky_blue
Type:

tuple

pink
Type:

tuple

orange
Type:

tuple

purple
Type:

tuple

brown
Type:

tuple

light_pink
Type:

tuple

cream
Type:

tuple

light_yellowgreen
Type:

tuple

light_sky_blue
Type:

tuple

beige
Type:

tuple

light_green
Type:

tuple

light_purple
Type:

tuple

light_gray
Type:

tuple

gray
Type:

tuple

white
Type:

tuple

black
Type:

tuple

References

https://jfly.uni-koeln.de/colorset/

eclib.figure.setup()[source]

Sets up the default figure settings for matplotlib.

Parameters:

None

Return type:

None

eclib.primeutils

Prime number utilities.

This module provides utility functions for generating prime numbers and semiprime factors. The module includes functions for checking if an integer is prime, generating a prime number with a specified bit length, generating a Sophie Germain prime and its corresponding safe prime, and generating a pair of semiprime factors of the same bit length.

Functions

  • is_prime

  • get_prime

  • get_safe_prime

  • get_semiprime_factors

eclib.primeutils.get_prime(bit_length)[source]

Generates a prime number with the specified bit length.

Parameters:

bit_length (int) – Desired bit length of the prime number.

Returns:

Generated prime number.

Return type:

int

eclib.primeutils.get_safe_prime(bit_length)[source]

Generates a Sophie Germain prime and its corresponding safe prime.

Parameters:

bit_length (int) – Desired bit length of the Sophie Germain prime.

Returns:

  • sophie_germain_prime (int) – Sophie Germain prime.

  • safe_prime (int) – Corresponding safe prime.

Return type:

tuple[int, int]

eclib.primeutils.get_semiprime_factors(bit_length)[source]

Generates a pair of semiprime factors of the same bit length.

Parameters:

bit_length (int) – Desired bit length of the semiprime factors.

Returns:

  • p (int) – First semiprime factor.

  • q (int) – Second semiprime factor.

Return type:

tuple[int, int]

eclib.primeutils.is_prime(n, k=50)[source]

Check if an integer n is prime.

Parameters:
  • n (int) – Integer to be checked for primality.

  • k (int, optional) – The number of iterations for the Miller-Rabin primality test.

Returns:

True if n is a prime number, False otherwise.

Return type:

bool

Note

The function uses the Miller-Rabin primality test to check if n is a prime number. The test is probabilistic and has a probability of failure less than 4^(-k). The parameter k determines the accuracy of the test.

eclib.randutils

Random number utilities.

This module provides utility functions for generating random numbers. The module includes functions for generating random integers in a specified range, random integers with a specified number of bits, and random integers sampled from a discrete Gaussian distribution.

Functions

  • get_rand

  • get_rand_bits

  • get_int_gaussian

Note

The module uses the secrets module for generating cryptographically strong random numbers.

eclib.randutils.get_int_gaussian(mean, std, dim=1)[source]

Generate a random integer or a list of random integers sampled from a discrete Gaussian distribution.

Parameters:
  • mean (int) – Mean of the Gaussian distribution.

  • std (float) – Standard deviation of the Gaussian distribution.

  • dim (int, optional) – Dimension of the output.

Returns:

Random integer or list of random integers sampled from the Gaussian distribution.

Return type:

int or list[int]

eclib.randutils.get_rand(min, max)[source]

Generates a random integer in [min, max).

Parameters:
  • min (int) – The minimum value of the range (inclusive).

  • max (int) – The maximum value of the range (exclusive).

Returns:

Generated random integer in [min, max).

Return type:

int

eclib.randutils.get_rand_bits(bit_length)[source]

Generates a random integer with the specified number of bits.

Parameters:

bit_length (int) – Desired bit length of the random integer.

Returns:

Generated random integer of bit_length bits.

Return type:

int