tinysig module

class tinysig.network.Node(id, shares_db=<factory>, open_db=<factory>, he_public_keys=<factory>)[source]

Bases: object

Represents a node in the network.

id: int

Identifier for the node.

shares_db: Dict[str, int]

Database for holding shares.

open_db: Dict[str, int]

Database for holding open values.

he_public_keys: Dict[int, int]

Dictionary for holding homomorphic encryption public keys.

get_share(label)[source]

Retrieve a share from the ‘shares_db’.

Return type

None

get_open(label)[source]

Retrieve an open value from the ‘open_db’.

Return type

None

set_share(value, label)[source]

Set a share in the ‘shares_db’.

Return type

None

set_open(value, label)[source]

Set an open value in the ‘open_db’.

Return type

None

delete_share(label)[source]

Delete a share from the ‘shares_db’.

Return type

None

delete_open(label)[source]

Delete an open value from the ‘open_db’.

Return type

None

class tinysig.network.Client(id, shares_db=<factory>, open_db=<factory>, he_public_keys=<factory>, he_private_key=0)[source]

Bases: tinysig.network.Node

Represents a client node in the network, inheriting from the ‘Node’ class.

he_private_key: int = 0
class tinysig.network.Network(N, q, h=2, C=1)[source]

Bases: object

Represents a network of nodes and clients.

Manages the interactions and cryptographic operations within the network, including sharing secrets, broadcasting values, and reconstructing shared values.

nodes: List[tinysig.network.Node]

List of nodes in the network.

clients: List[tinysig.network.Client]

List of clients in the network.

q: int

Prime field.

h: int

Multiplicative field generator.

print()[source]

Print a readable representation of the network, including nodes and clients with their databases.

reconstruct_local(type_share, get_label, save_label, party)[source]

Locally reconstruct exponent share (‘exp’) or base (‘base’) shared value.

Return type

None

broadcast(element, label)[source]

Send element to all nodes.

Return type

None

send(type_share, label, party, delete=False)[source]

Send exponent (‘exp’) or base (‘base’) share to party.

Return type

None

share(secret, size, label)[source]

Share secret value with all

Return type

None

reveal(type_share, get_label, save_label, party)[source]

Send exponent (‘exp’) or base (‘base’) share to party.

Return type

None

tinysig.setup.get_generator(q)[source]

Get the generator (primitive root) for a given prime number q.

Parameters

q (int) – A prime number for which the generator is needed.

Returns

The generator (primitive root) for the given prime number.

Return type

int

Example

>>> get_generator(23)
5
class tinysig.setup.DSASetup(p, q, g, h)[source]

Bases: object

Dataclass representing a DSA (Digital Signature Algorithm) setup.

Example

setup = DSASetup.generate_dsa_setup()

p: int

The DSA modulus.

q: int

The order of the subgroup.

g: int

A generator of the subgroup.

h: int

A generator of the field \(\mathbb{Z}_q\).

generate_dsa_setup()[source]

Generate a DSA setup based on system parameters.

class tinysig.setup.ECDSASetup(curve, p=None, q=None, G=None, h=None)[source]

Bases: object

Dataclass representing an ECDSA (Elliptic Curve Digital Signature Algorithm) setup.

Example

setup = ECDSASetup.generate_ecdsa_setup()

curve: str

The name of the elliptic curve.

p: Optional[int] = None

The finite field of the elliptic curve.

q: Optional[int] = None

The order of the elliptic curve group.

G: Optional[Crypto.PublicKey.ECC.EccPoint] = None

A base point on the elliptic curve.

h: Optional[int] = None

A generator of field \(\mathbb{Z}_q\).

generate_ecdsa_setup()[source]

Generate an ECDSA setup for the specified elliptic curve.

Returns

An instance of ECDSASetup with generated parameters.

Return type

ECDSASetup

Raises

ValueError – If the specified curve is not supported.

Example

>>> setup = ECDSASetup(curve='P-256').generate_ecdsa_setup()
static supported_curves()[source]

Get a list of supported elliptic curves.

Returns

A list of supported elliptic curve names.

Return type

List[str]

Example

>>> supported_curves = ECDSASetup.supported_curves()
>>> print(supported_curves)
['P-192', 'P-224', 'P-256', 'P-384', 'P-521']
print_supported_curves()[source]

Print the list of supported elliptic curves.

class tinysig.tecdsa.ThresholdSignature(N, C, setup=None, debug=False)[source]

Bases: tinysig.network.Network

clients: List[tinysig.network.Client]

List of clients in the network.

get_lambda(labels)[source]

Emulates the generation of LAMBDA pairs \(([h^{\gamma}], [\gamma])\) between all nodes.

Parameters

labels (list[str]) – A list of labels for which lambda values will be generated and stored.

Return type

None

Returns

None

rss_protocol(size, label)[source]

Random Secret Sharing (RSS) Protocol.

This function implements a one-round RSS protocol. The goal is to share a random secret value among a group of nodes using a specific label for the shares.

Parameters
  • size (int) – The maximum size of the random secret to be generated and shared.

  • label (str) – A label to identify the shared secrets and their associated operations.

Return type

None

Returns

None

pow_share_protocol(base_type, get_label, save_label)[source]

Compute a power-sharing protocol among a group of nodes.

This function implements a one-round protocol to securely compute \(b^{s}\) where the exponent is a secret shared element between the nodes.

Parameters
  • base_type (str) – The type of base used: ‘exp’, when base to be used is self.h; ‘base’, when the base to be used is self.dsa.g. Note: ‘base’ option can only be use for the DSA setup.

  • get_label (str) – The label to retrieve shares of ‘s’ from nodes.

  • save_label (str) – The label to save the final result to.

Return type

None

Returns

None

ec_pow_share_protocol(get_label, save_label)[source]

Execute an elliptic curve (EC) version of power-sharing protocol.

This function implements a one-round protocol to securely compute \(scalar\cdot G\) where the scalar is a secret shared element between the nodes.

Parameters
  • get_label (str) – The label used to retrieve scalar shares from nodes.

  • save_label (str) – The label used to save the result of the power-sharing protocol.

Returns: None

Return type

None

subtract_exp_shares_local(label_a, label_b, label_r)[source]

Subtract the shares of the exponent of two labels and store the result in another label.

Parameters
  • label_a (str) – The label for the first operand.

  • label_b (str) – The label for the second operand.

  • label_r (str) – The label where the result is stored.

Return type

None

Returns

None

pow_local(label_base, label_exponent, label_result)[source]

Compute the power of a base saved in open database raised to an exponent and store the result.

Parameters
  • label_base (str) – The label for the base.

  • label_exponent (str) – The label for the exponent.

  • label_result (str) – The label for the element where the result is stored.

Return type

None

Returns

None

key_agreement_protocol(label, delete=True)[source]

Perform a key agreement protocol to derive a mask of the secret key and the corresponding public key.

Parameters
  • label (str) – The label of the pair associated with the secret key mask.

  • delete (bool, optional) – Whether to delete intermediate data after the protocol. Defaults to True.

Return type

None

Returns

None

ec_mult_local(label_ec_point, label_scalar, label_result)[source]

Compute the multiplication of a scalar value with an elliptic point curve and store the result.

Parameters
  • label_ec_point (str) – The label for the elliptic curve point.

  • label_scalar (str) – The label for the scalar.

  • label_result (str) – The label for the element where the result is stored.

Return type

None

Returns

None

encrypt_and_delete_exp_sh_local(label, client_id)[source]

Encrypt the share of the exponent element of the LAMBDA pair and delete the original LAMBDA pair.

Parameters
  • label (str) – The label for LAMBDA pair.

  • client_id (int) – Client id.

Return type

None

Returns

None

send_public_key_to_client(label, client)[source]

Nodes send public key to client.

Parameters
  • label (str) – The label for LAMBDA pair.

  • client_id (int) – Client id.

Return type

None

Returns

None

distributed_key_generation_protocol(client_id, label=None)[source]

Execute a distributed key generation protocol for a specific client.

Parameters
  • client_id (int) – The unique identifier for the client.

  • label (str, optional) – A custom label associated with the client. Defaults to None.

Return type

None

Returns

None

compute_r_local(label, client, delete=True)[source]

Compute r.

Parameters
  • label (str) – The label of the r element.

  • client (Client) – A client.

Return type

None

Returns

None

invert_masked_factor_local(label)[source]

Invert a masked factor.

Parameters

label (str) – The label of the masked factor to be inverted.

Return type

None

Returns

None

step_4_encrypt_elements(label_lambda_1, label_lambda_2, labdel_lambda_k_inv, save_label_m, save_label_gap, save_label_lambda_1, save_label_lambda_2, client_id)[source]

Step 4 of the Threshold Signing protocol.

Parameters
  • label (str) – The label of the masked factor to be inverted.

  • label_lambda_1 (str) – The label of lambda 1.

  • label_lambda_2 (str) – The label of lambda 2.

  • labdel_lambda_k_inv (str) – The label of \(k^{-1}\).

  • save_label_m (str) – The label to save encrypted m.

  • save_label_gap (str) – The label to save \(\lambda_{ ext{gap}}\).

  • save_label_lambda_1 (str) – The label to save lambda 1.

  • save_label_lambda_2 (str) – The label to save lambda 2.

  • client_id (int) – int

Return type

None

Returns

None

delete_shares(list)[source]

Delete a set of shares.

Parameters

list (List) – List of shares to delete.

Return type

None

Returns

None

decrypt_and_reconstruct_local(get_label, save_label, client)[source]

Decryption and reconstruction executed by the client.

Parameters
  • get_label (str) – The label of the shares to be dencrypted and reconstructed.

  • save_label (str) – The label used to save the result.

  • client_id (int) – The unique identifier for the client.

Return type

None

Returns

None

ts_prep_protocol(client_id)[source]

Execute the preprocessing phase of the threshold signature protocol for a specific client.

Parameters

client_id (int) – The unique identifier for the client.

Returns

None

Raises
  • TypeError – If the client with the provided ‘client_id’ is not part of the network.

  • KeyError – If the public key is not complete for the specified client.

broadcast_masked_message_digest(message, client)[source]

Broadcasts a masked message digest to the client.

Parameters
  • message (str) – The input message to be hashed and masked.

  • client (Client) – An instance of the client participating in the protocol.

Return type

None

Returns

None

sign_local(client_id, delete=True)[source]

Sign a message locally and optionally delete intermediate shares.

Parameters
  • client_id (int) – The unique identifier of the client.

  • delete (bool, optional) – A flag indicating whether to delete intermediate shares after signing (default is True).

Returns

None

reconstruct_and_verify_sig(message, get_label, client, delete=True)[source]

Reconstructs and verifies a client’s digital signature for a given message.

Parameters
  • message (str) – The input message for which the signature is to be reconstructed and verified.

  • get_label (str) – The label used to retrieve the client’s signature share from the database.

  • client (Client) – An instance of the client for which the signature is reconstructed and verified.

  • delete (bool, optional) – A flag indicating whether to delete intermediate shares after verification (default is True).

Returns

This function doesn’t return a value; it verifies the signature and potentially deletes intermediate shares.

Return type

None

decrypt_reconstruct_unmask_verify_sig_local(message, get_label, client, delete=True)[source]

Reconstructs and verifies a client’s digital signature for a given message.

Parameters
  • message (str) – The input message for which the signature is to be reconstructed and verified.

  • get_label (str) – The label of the shares to be dencrypted and reconstructed.

  • client (Client) – An instance of the client for which the signature is reconstructed and verified.

  • delete (bool, optional) – A flag indicating whether to delete intermediate shares after verification (default is True).

Returns

This function doesn’t return a value; it verifies the signature and potentially deletes intermediate shares.

Return type

None

ts_online_protocol(message, client_id)[source]

Executes the online phase of the threshold signature protocol for a specific client.

Parameters
  • message (str) – The message to be signed by the client.

  • client_id (int) – The unique identifier of the client participating in the protocol.

Return type

None

Returns

None

print_signature(client_id)[source]
Return type

None

retrieve_signature(client_id)[source]
Return type

(<class ‘int’>, <class ‘int’>, <class ‘str’>)

exception tinysig.tecdsa.PublicKeyDisagreement(message)[source]

Bases: Exception

tinysig.utils.add(values, size)[source]

Calculate the sum of a list of integers modulo ‘size’.

Parameters
  • values (list[int]) – A list of integers to be summed.

  • size (int) – The modulo value.

Returns

The sum of the integers in ‘values’ modulo ‘size’.

Return type

int

Examples

>>> add([2, 4, 6], 5)
2
>>> add([3, 7, 10], 4)
0
tinysig.utils.add_ec(points)[source]

Calculate the sum of a list of elliptic curve points.

Parameters

points (list[EccPoint]) – A list of elliptic curve points to be summed.

Returns

The sum of the points.

Return type

EccPoint

tinysig.utils.generate_additive_shares(secret, n, size)[source]

Generates additive secret shares for a given secret value, using modular arithmetic.

Parameters
  • secret (int) – The secret value to be shared.

  • n (int) – The number of shares to generate.

  • size (int) – The modulus value for modular arithmetic.

Returns

A list of additive secret shares.

Return type

List[int]

Example

>>> random.seed(0)
>>> generate_additive_shares(26, 3, 2**5)
[8, 24, 26]
tinysig.utils.multiply(values, size)[source]

Calculate the product of a list of values, taking the modulus ‘size’ at each step of multiplication.

Parameters
  • values (list[int]) – List of integers to be multiplied.

  • size (int) – Modulus value to prevent the result from growing too large.

Returns

The product of the values, computed modulo ‘size’.

Return type

int

Example

>>> multiply([2, 4, 6], 5)
3
tinysig.utils.egcd(a, p)[source]

Calculate the modular multiplicative inverse of ‘a’ modulo ‘p’ using the extended Euclidean algorithm.

Parameters
  • a (int) – Integer for which the modular inverse is calculated.

  • p (int) – Modulus value for the modulo operation.

Returns

Modular multiplicative inverse of ‘a’ modulo ‘p’.

Return type

int

tinysig.utils.hash(message, q)[source]

Computes the hash of the mesage using SHA256.

Parameters
  • message (str) – The message to verify the signature for.

  • q (int) – Field size to embbed digest.

Returns

hash of message.

Return type

m (int)

tinysig.utils.verify_dsa_signature(message, r, s, y, p, q, g)[source]

Verify a Digital Signature Algorithm (DSA) signature.

Parameters
  • message (str) – The message to verify the signature for.

  • r (int) – The ‘r’ component of the DSA signature.

  • s (int) – The ‘s’ component of the DSA signature.

  • y (int) – The public key ‘y’ associated with the signer.

  • p (int) – The prime modulus used in DSA.

  • q (int) – A prime divisor of ‘p’.

  • g (int) – The generator of the subgroup used in DSA.

Returns

If the signature is valid, no exceptions are raised.

Return type

None

Raises

VerifySignatureError – If the signature verification fails due to one of the following reasons: ‘r’ or ‘s’ is greater than or equal to ‘q’; the calculated ‘v’ does not match ‘r’.

tinysig.utils.verify_ecdsa_signature(message, r, s, Y, q, G)[source]

Verify an Elliptic Curve Digital Signature Algorithm (ECDSA) signature.

Parameters
  • message (str) – The message to verify the signature for.

  • r (int) – The ‘r’ component of the DSA signature.

  • s (int) – The ‘s’ component of the DSA signature.

  • Y (EccPoint) – The public key ‘y’ associated with the signer.

  • q (int) – Order of the Elliptic Curve group.

  • G (EccPoint) – The generator of the Elliptic Curve group.

Returns

If the signature is valid, no exceptions are raised.

Return type

None

Raises

VerifySignatureError – If the signature verification fails due to one of the following reasons: ‘r’ or ‘s’ is greater than or equal to ‘q’; the calculated ‘v’ does not match ‘r’.

exception tinysig.utils.VerifySignatureError(message)[source]

Bases: Exception

class tinysig.utils.TestUtils(methodName='runTest')[source]

Bases: unittest.case.TestCase

setUp()[source]

Hook method for setting up the test fixture before exercising it.

test_add()[source]
test_generate_additive_shares()[source]
test_multiply()[source]