risk_propagation.player module

Configuration of a bank

class risk_propagation.player.Player(name, accounts, transactions, pool, paillier, delta_func=<function Player.<lambda>>, intermediate_results_path=None)[source]

Bases: object

Player class performing steps in protocol

COMPENSATION_FACTOR = 1000000000000
__init__(name, accounts, transactions, pool, paillier, delta_func=<function Player.<lambda>>, intermediate_results_path=None)[source]

Initializes a player instance

Parameters:
  • name (str) – the name of the player

  • accounts (ndarray[tuple[int, ...], dtype[object_]]) – an array of accounts containing an initial risk score per account

  • transactions (ndarray[tuple[int, ...], dtype[object_]] | list[ndarray[tuple[int, ...], dtype[object_]]]) – an array containing arrays of transactions corresponding to periods

  • pool (Pool) – the communication pool to use

  • paillier (DistributedPaillier) – an instance of DistributedPaillier

  • delta_func (Callable[[int], float]) – Callable function which uses the iteration index to determine the delta value (must be between [0 and 1)).

  • intermediate_results_path (Path | None) – the path to the folder where the intermediate results should be stored. If no path is provided, intermediate results are not stored.

property banks: tuple[Bank, ...]

All banks in the protocol

Returns:

all banks in the protocol

async continue_from_stored_results(iterations, intermediate_results)[source]

Resume the protocol from a stored state.

Parameters:
  • iterations (int) – the number of iterations to perform in each period.

  • intermediate_results (bytes) – the bytes containing the intermediate results to start from.

Return type:

None

async decrypt()[source]

Decryption of the risk scores per bank, compensating for the COMPENSATION_FACTOR

Return type:

None

property delta: float

The delta value for the current iteration

Returns:

Float in the range of [0,1)

Raises:

AssertionError – raised when the delta is not within the range [0..1)

deserialize_risk_scores(bytes_object)[source]

Deserialize the risk scores stored at a given location.

Parameters:

bytes_object (bytes) – The bytes from which the risk scores should be deserialized.

Return type:

tuple[dict[str, PaillierCiphertext], int, int]

Returns:

Tuple consisting of a dictionary with accounts and risk scores, the period and the iteration in which the loaded risk scores were stored.

Raises:

WrongDeserializationException – Raised when the public key of the loaded risk scores or the accounts of the loaded risk scores does not match the current context.

encrypt_initial_risk_scores()[source]

Encrypt the initialised risk scores of this player’s accounts

Return type:

None

property intermediate_results: list[bytes]

List of bytes objects of all the intermediate states for this player.

Returns:

list of bytes objects.

Raises:

AttributeError – raised when there are no intermediate states to return.

intermediate_results_from_disk(path)[source]

Read intermediate results from disk.

Parameters:

path (Path) – The path from which the intermediate results should be loaded from disk.

Return type:

bytes

Returns:

Bytes containing the intermediate results

intermediate_results_to_disk(bytes_object, current_period, current_iteration)[source]

Write the intermediate results to disk.

Parameters:
  • bytes_object (bytes) – Bytes representing the current state.

  • current_period (int) – The current period.

  • current_iteration (int) – The current iteration.

Return type:

None

async iteration()[source]

Perform a single iteration

Return type:

None

property other_banks: tuple[Bank, ...]

The other banks in the protocol

Returns:

the other banks in the protocol

async randomize_and_send_updated_risk_scores(all_external_accounts)[source]

Send updated risk score to the other banks. While ensuring that the ciphertexts are randomized only once when it is sent.

A workaround is used to send randomized ciphertexts to each party. The randomization is done once for all parties. In other words all ciphertexts are refreshed if needed at the start of this method, but after that they are not rerandomized (which would happen without this workaround). Rerandomization would happen, since the communication module marks a ciphertext as unfresh when it is sent.

This is a feature when sending randomizable ciphertext to multiple parties over multiple messages.

Example

Let ciphertext x be a fresh ciphertext being sent to two parties A and B. First x is sent to party A. The communication module now sets x as unfresh. The workaround marks x as fresh without randomizing. Now x can be sent to party B.

Parameters:

all_external_accounts (set[str]) – All account ids, where the account has a transaction to another bank.

Return type:

None

property results_path: Path

The path object where the intermediate results are stored.

Returns:

Path object where the results are stored.

Raises:

AttributeError – raised when the results path is not configured for this party.

property risk_scores: dict[str, FixedPoint]

The plaintext risk scores belonging to this player’s bank

Returns:

plaintext dictionary of risk scores

Raises:

AttributeError – raised when risk scores are not available

async run_protocol(iterations, starting_period=0, starting_iteration=0)[source]

Runs the entire protocol

Parameters:
  • iterations (int) – the number of iterations to perform in each period

  • starting_period (int) – the first period that should be run. This is only relevant if the protocol is restarted from intermediate resuls.

  • starting_iteration (int) – the first iteration that should be performed. This is only relevant if the protocol is restarted from intermediate results.

Raises:

IncorrectStartException – Raised if the protocol is incorrectly started.

Return type:

None

serialize_current_risk_scores(current_z, current_iteration)[source]

Serialize the risk scores of all accounts of this player.

Parameters:
  • current_z (int) – The current period z.

  • current_iteration (int) – The current iteration.

Return type:

bytes

Returns:

Bytes storing the risk scores, the current public key, the current period z and the current iteration.

set_current_period(period_z)[source]

Set the period z to be used in the next iteration(s)

Parameters:

period_z (int) – The period z that should be used.

Return type:

None

update_risk_scores()[source]

Updates risk scores of all accounts

Return type:

None