diff --git a/src/centralized_telescope/telescope.rs b/src/centralized_telescope/telescope.rs index b0a92952..cdd9c70a 100644 --- a/src/centralized_telescope/telescope.rs +++ b/src/centralized_telescope/telescope.rs @@ -1,6 +1,6 @@ //! Customer facing Telescope structure -use super::params::Params; +use super::params::{Case, Cases, High, Mid, Params, Small}; use super::proof::Proof; use super::round::Round; @@ -56,7 +56,9 @@ impl Telescope { } } - /// Returns a Telescope structure from input and internal parameters, to use for backward compability + /// Returns a Telescope structure from input and internal parameters, to + /// use for backward compability if the parameters verifies the paper + /// bounds, otherwise None /// /// # Arguments /// @@ -84,6 +86,168 @@ impl Telescope { set_size: u64, lower_bound: u64, params: Params, + ) -> Option { + let proof_size_f64 = params.proof_size as f64; + + let bool = match Cases::which(completeness_param, set_size, proof_size_f64 as u64) { + Cases::Small => { + let small = Small::new(completeness_param, set_size, proof_size_f64); + Self::check_from( + soundness_param, + completeness_param, + set_size, + lower_bound, + params, + &small, + ) + } + Cases::Mid => { + let mid = Mid::new(completeness_param, set_size, proof_size_f64); + Self::check_from( + soundness_param, + completeness_param, + set_size, + lower_bound, + params, + &mid, + ) + } + Cases::High => { + let high = High::new(completeness_param, set_size, proof_size_f64); + Self::check_from( + soundness_param, + completeness_param, + set_size, + lower_bound, + params, + &high, + ) + } + }; + + bool.then_some(Telescope { + soundness_param, + completeness_param, + set_size, + lower_bound, + params, + }) + } + + fn check_from( + soundness_param: f64, + completeness_param: f64, + set_size: u64, + lower_bound: u64, + params: Params, + case: &impl Case, + ) -> bool { + fn completeness_error(u: f64, d: u64, q: f64) -> f64 { + (-(q - u * q * q / 2.0) * d as f64).exp() + } + fn soundness_error(np: u64, nf: u64, u: f64, d: u64, q: f64) -> f64 { + (nf as f64 / np as f64).powf(u) * d as f64 * q + } + let mut bool = true; + + // Checks the proof size given is at least as big as one computed from user parameters + let proof_size = params.proof_size as f64; + if Params::proof_size( + soundness_param, + completeness_param, + set_size as f64, + lower_bound as f64, + ) > proof_size + { + bool = false; + } + + // Check that the number of max retries is at least as big as one + // computed from given user parameters + if case.max_retries() > params.max_retries { + bool = false; + } + + // Check that the search width is at least as big as the one computed + // from given user parameters and given proof size + let search_width = case.search_width(proof_size); + if search_width > params.search_width { + bool = false; + }; + + // Check that the valid proof probability is close enough from the one + // computed from the given user and internal parameters + let error = 8f64.recip(); + let valid_proof_probability = case.valid_proof_probability(params.search_width); + // Checking the completness error difference is bounded by the error + if (completeness_error(proof_size, params.search_width, valid_proof_probability) + / completeness_error( proof_size, search_width, params.valid_proof_probability)) + .log2() // Computes log2(2^-l1 / 2^-l2) = (-l1) - (-l2) + .abs() // Computes |l1 - l2| + > error + { + bool = false; + }; + // Checking the soundness error difference is bounded by the error + if (soundness_error( + set_size, + lower_bound, + proof_size, + params.search_width, + params.valid_proof_probability, + ) / soundness_error( + set_size, + lower_bound, + proof_size, + params.search_width, + valid_proof_probability, + )) + .log2()// Computes log2(2^-l1 / 2^-l2) = (-l1) - (-l2) + .abs() // Computes |l1 - l2| + > error + { + bool = false; + } + + // Check that the DFS bound is at least as big as the one given by the user and internal parametesr. + let dfs_bound = case.dfs_bound(proof_size, search_width); + if dfs_bound > params.dfs_bound { + bool = false; + }; + + bool + } + + /// Use with caution. Returns a Telescope structure from input and internal + /// parameters, to use for backward compability without checking the + /// consistency between input and internal parameters. + /// + /// # Arguments + /// + /// * `soundness_param` - the protocol soundness parameter, typically set at 128 + /// * `completeness_param` - the protocol completeness parameter, typically set at 128 + /// * `set_size` - the size of the prover set to lower bound + /// * `lower_bound` - the lower bound to prove + /// * `params` - some centralized Telescope internal parameters + /// + /// # Returns + /// + /// A Telescope structure + /// + /// # Example + /// + /// ``` + /// use alba::centralized_telescope::telescope::Telescope; + /// use alba::centralized_telescope::params::Params; + /// let params = Params {proof_size : 200, max_retries: 128, search_width: 10, valid_proof_probability: 0.001, dfs_bound: 40_000}; + /// let telescope = Telescope::from(128.0, 128.0, 1_000, 750, params); + /// ``` + pub fn from_unsafe( + soundness_param: f64, + completeness_param: f64, + set_size: u64, + lower_bound: u64, + params: Params, ) -> Self { Telescope { soundness_param,