Skip to content

Commit

Permalink
Centralized/Proof: Cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
rrtoledo committed Feb 13, 2025
1 parent 7513778 commit 9cd505a
Showing 1 changed file with 54 additions and 54 deletions.
108 changes: 54 additions & 54 deletions src/centralized_telescope/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl Proof {
return false;
}
}

Proof::proof_hash(params.valid_proof_probability, &round)
}

Expand All @@ -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)
}

Expand All @@ -199,41 +201,39 @@ impl Proof {
params: &Params,
bins: &[Vec<Element>],
round: &Round,
local_step: Arc<Mutex<u64>>,
proof_found: Arc<Mutex<bool>>,
step: Arc<Mutex<u64>>,

Check warning on line 204 in src/centralized_telescope/proof.rs

View workflow job for this annotation

GitHub Actions / build

this argument is passed by value, but not consumed in the function body
found: Arc<Mutex<bool>>,

Check warning on line 205 in src/centralized_telescope/proof.rs

View workflow job for this annotation

GitHub Actions / build

this argument is passed by value, but not consumed in the function body
) -> Option<Self> {
// 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
Expand All @@ -259,22 +259,22 @@ impl Proof {
}
}

fn check_step_lock(params: &Params, step_lock: Arc<Mutex<u64>>) -> bool {
fn check_step(params: &Params, step_lock: Arc<Mutex<u64>>) -> bool {

Check warning on line 262 in src/centralized_telescope/proof.rs

View workflow job for this annotation

GitHub Actions / build

this argument is passed by value, but not consumed in the function body
*step_lock.lock().unwrap() >= params.dfs_bound
}

fn check_proof_lock(proof_lock: Arc<Mutex<bool>>) -> bool {
fn check_proof(proof_lock: Arc<Mutex<bool>>) -> bool {

Check warning on line 266 in src/centralized_telescope/proof.rs

View workflow job for this annotation

GitHub Actions / build

this argument is passed by value, but not consumed in the function body
*proof_lock.lock().unwrap() == true

Check warning on line 267 in src/centralized_telescope/proof.rs

View workflow job for this annotation

GitHub Actions / build

equality checks against true are unnecessary
}
}

fn update_step_lock(step_lock: Arc<Mutex<u64>>, 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<Mutex<u64>>, to_add: u64) -> () {

Check warning on line 270 in src/centralized_telescope/proof.rs

View workflow job for this annotation

GitHub Actions / build

unneeded unit return type

Check warning on line 270 in src/centralized_telescope/proof.rs

View workflow job for this annotation

GitHub Actions / build

this argument is passed by value, but not consumed in the function body
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<Mutex<bool>>) -> () {
let mut proof_guard = proof_found_lock.lock().unwrap();
*proof_guard = true;
drop(proof_guard);
}
fn update_proof_lock(found_lock: Arc<Mutex<bool>>) -> () {

Check warning on line 276 in src/centralized_telescope/proof.rs

View workflow job for this annotation

GitHub Actions / build

unneeded unit return type

Check warning on line 276 in src/centralized_telescope/proof.rs

View workflow job for this annotation

GitHub Actions / build

this argument is passed by value, but not consumed in the function body
let mut proof_guard = found_lock.lock().unwrap();
*proof_guard = true;
drop(proof_guard);
}

0 comments on commit 9cd505a

Please sign in to comment.