From 9cd505a7b949148ebb0eb12e02badd08d55f8cdb Mon Sep 17 00:00:00 2001 From: Raphael Toledo Date: Thu, 13 Feb 2025 13:17:04 +0000 Subject: [PATCH] Centralized/Proof: Cleaning code --- src/centralized_telescope/proof.rs | 108 ++++++++++++++--------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/src/centralized_telescope/proof.rs b/src/centralized_telescope/proof.rs index 6e493558..ae60faa0 100644 --- a/src/centralized_telescope/proof.rs +++ b/src/centralized_telescope/proof.rs @@ -130,6 +130,7 @@ impl Proof { return false; } } + Proof::proof_hash(params.valid_proof_probability, &round) } @@ -156,35 +157,36 @@ impl Proof { } } + // Initialize shared variables used to check when to stop the threads + let total_step = Arc::new(Mutex::new(0u64)); + let found = Arc::new(Mutex::new(false)); + // Run the DFS algorithm on up to params.search_width different trees - let step = Arc::new(Mutex::new(0u64)); - let proof_found = Arc::new(Mutex::new(false)); let proof_opt = (0..params.search_width) .into_par_iter() .find_map_any(|search_counter| { // If DFS was called more than params.dfs_bound times,or a proof was // already found abort this retru - if check_step_lock(params, step.clone()) || check_proof_lock(proof_found.clone()) { + if check_step(params, total_step.clone()) || check_proof(found.clone()) { return None; } + // Initialize new round - if let Some(r) = Round::new(retry_counter, search_counter, set_size) { + Round::new(retry_counter, search_counter, set_size).and_then(|r| { // Initializing thread dependent steps - let search_step = Arc::new(Mutex::new(0u64)); + let step = Arc::new(Mutex::new(0u64)); - let dfs_result = - Self::dfs(params, &bins, &r, search_step.clone(), proof_found.clone()); + // Call DFS recursion + let dfs_result = Self::dfs(params, &bins, &r, step.clone(), found.clone()); // Updating global number of steps with the ones done in thread - update_step_lock(step.clone(), *search_step.lock().unwrap()); + update_step_lock(total_step.clone(), *step.lock().unwrap()); dfs_result - } else { - None - } + }) }); - let total_steps = step.lock().unwrap(); + let total_steps = total_step.lock().unwrap(); (*total_steps, proof_opt) } @@ -199,41 +201,39 @@ impl Proof { params: &Params, bins: &[Vec], round: &Round, - local_step: Arc>, - proof_found: Arc>, + step: Arc>, + found: Arc>, ) -> Option { - // If current round comprises params.proof_size elements and satisfies - // the proof_hash check, return it cast as a Proof + // If the current round comprises `params.proof_size elements` and + // passes the `proof_hash` check, then update the `found` boolean + // and return the round cast as a `Proof` if round.element_sequence.len() as u64 == params.proof_size { - let proof_opt = - Proof::proof_hash(params.valid_proof_probability, round).then_some(Self { - retry_counter: round.retry_counter, - search_counter: round.search_counter, - element_sequence: round.element_sequence.clone(), - }); - - // If proof is found, upudate proof lock - if proof_opt.is_some() { - update_proof_lock(proof_found.clone()); + let proof_found = Proof::proof_hash(params.valid_proof_probability, round); + if proof_found { + update_proof_lock(found.clone()); } - return proof_opt; + return proof_found.then(|| Self { + retry_counter: round.retry_counter, + search_counter: round.search_counter, + element_sequence: round.element_sequence.clone(), + }); } - // For each element in bin numbered id + + // Otherwise, try to recursively update the current round with each + // element contained in the bin referenced by the round's id bins[round.id as usize].par_iter().find_map_any(|&element| { - // If DFS was called more than params.dfs_bound times,or a - // proof was already found abort this round - if check_proof_lock(proof_found.clone()) { - return None; - } - // Update round with such element - if let Some(r) = Round::update(round, element) { - // Run DFS on updated round, incrementing step - update_step_lock(local_step.clone(), 1u64); - Self::dfs(params, bins, &r, local_step.clone(), proof_found.clone()) - } else { - None - } + // If a proof has already been found, abort + if check_proof(found.clone()) { + return None; + } + + // Otherwise, update the current round with the new element + Round::update(round, element).and_then(|r| { + // Run DFS on updated round, incrementing step + update_step_lock(step.clone(), 1u64); + Self::dfs(params, bins, &r, step.clone(), found.clone()) }) + }) } /// Oracle producing a uniformly random value in [0, set_size[ used for @@ -259,22 +259,22 @@ impl Proof { } } -fn check_step_lock(params: &Params, step_lock: Arc>) -> bool { +fn check_step(params: &Params, step_lock: Arc>) -> bool { *step_lock.lock().unwrap() >= params.dfs_bound } -fn check_proof_lock(proof_lock: Arc>) -> bool { +fn check_proof(proof_lock: Arc>) -> bool { *proof_lock.lock().unwrap() == true - } +} - fn update_step_lock(step_lock: Arc>, to_add: u64) -> () { - let mut step_guard = step_lock.lock().unwrap(); - *step_guard = (*step_guard).wrapping_add(to_add); - drop(step_guard); - } +fn update_step_lock(step_lock: Arc>, to_add: u64) -> () { + let mut step_guard = step_lock.lock().unwrap(); + *step_guard = (*step_guard).wrapping_add(to_add); + drop(step_guard); +} - fn update_proof_lock(proof_found_lock: Arc>) -> () { - let mut proof_guard = proof_found_lock.lock().unwrap(); - *proof_guard = true; - drop(proof_guard); - } +fn update_proof_lock(found_lock: Arc>) -> () { + let mut proof_guard = found_lock.lock().unwrap(); + *proof_guard = true; + drop(proof_guard); +}