From 9b87f8e7339d966e30ac9c3cb29862db768000ff Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Date: Tue, 27 Jun 2023 17:39:31 +0200 Subject: [PATCH] Committee sizes computation (#33) * Added optimal number of committees and sizes function * Remove main * Extract common scope code snippet * Fmt --- carnot/committee_sizes.py | 53 +++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 carnot/committee_sizes.py diff --git a/carnot/committee_sizes.py b/carnot/committee_sizes.py new file mode 100644 index 0000000..5bbc4fe --- /dev/null +++ b/carnot/committee_sizes.py @@ -0,0 +1,53 @@ +import math +from scipy.stats import binom + + +CARNOT_ADVERSARY_THRESHOLD_PER_COMMITTEE: float = 1/3 +CARNOT_NETWORK_ADVERSARY_THRESHOLD: float = 1 / 4 + + +def compute_optimal_number_of_committees_and_committee_size( + number_of_nodes: int, + failure_threshold: float, + adversaries_threshold_per_committee: float, + network_adversary_threshold: float +): + assert failure_threshold > 0 + # N is the number of nodes, delta is the failure prob. which can be tolerated, + # A is the fraction of a committee (typical value is 1/3) and P + # is the fraction of adversarial nodes (typical value is 1/4). + number_of_committees = 1 + committee_size = number_of_nodes + remainder = 0 + current_probability = 0.0 + odd_committee = 0 + while current_probability < failure_threshold: + previous_number_of_committees = number_of_committees + previous_committee_size = committee_size + previous_remainder = remainder + previous_probability = current_probability + odd_committee = odd_committee + 1 + number_of_committees = 2 * odd_committee + 1 + committee_size = number_of_nodes // number_of_committees + remainder = number_of_nodes % number_of_committees + + committee_size_probability = binom.cdf( + math.floor(adversaries_threshold_per_committee * committee_size), + committee_size, + network_adversary_threshold + ) + if 0 < remainder: + committee_size_plus_one_probability = binom.cdf( + math.floor(adversaries_threshold_per_committee * (committee_size + 1)), + committee_size + 1, + network_adversary_threshold + ) + current_probability = ( + 1 - committee_size_probability ** (number_of_committees - remainder) + * committee_size_plus_one_probability ** remainder + ) + else: + current_probability = 1 - committee_size_probability ** number_of_committees + # return number of committees, K_1, committee size, n_1, number of committees + # with size n_1+1, r_1 and prob. of failure, Prob_1. + return previous_number_of_committees, previous_committee_size, previous_remainder, previous_probability diff --git a/requirements.txt b/requirements.txt index 058bb11..4ea0792 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -blspy~=1.0.16 \ No newline at end of file +blspy~=1.0.16 +scipy~=1.10.1 \ No newline at end of file