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 b07b4c9 commit 44fef0b
Showing 1 changed file with 62 additions and 62 deletions.
124 changes: 62 additions & 62 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 Down Expand Up @@ -159,35 +160,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
Self::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 @@ -202,82 +204,80 @@ impl Proof {
params: &Params,
bins: &[Vec<Element>],
round: &Round,
local_step: Arc<Mutex<u64>>,
proof_found: Arc<Mutex<bool>>,
step: Arc<Mutex<u64>>,
found: Arc<Mutex<bool>>,
) -> 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 {
return Proof::proof_hash(params.valid_proof_probability, round).then_some({
update_proof_lock(found.clone());
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());
}
return proof_opt;
}
});
}
// 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
/// prehashing S_p
fn bin_hash(set_size: u64, retry_counter: u64, element: Element) -> Option<u64> {
let retry_bytes: [u8; 8] = retry_counter.to_be_bytes();
let mut hasher = Blake2s256::new();
hasher.update(b"Telescope-bin_hash");
hasher.update(retry_bytes);
hasher.update(element);
let digest: Hash = hasher.finalize().into();
let digest: Hash = Blake2s256::new()
.chain_update(b"Telescope-bin_hash")
.chain_update(retry_counter.to_be_bytes())
.chain_update(element)
.finalize()
.into();
sample::sample_uniform(&digest, set_size)
}

/// Oracle defined as Bernoulli(q) returning 1 with probability q and 0
/// otherwise
fn proof_hash(valid_proof_probability: f64, r: &Round) -> bool {
let mut hasher = Blake2s256::new();
hasher.update(b"Telescope-proof_hash");
hasher.update(r.hash);
let digest: Hash = hasher.finalize().into();
let digest: Hash = Blake2s256::new()
.chain_update(b"Telescope-proof_hash")
.chain_update(r.hash)
.finalize()
.into();
sample::sample_bernoulli(&digest, valid_proof_probability)
}
}

fn check_step_lock(params: &Params, step_lock: Arc<Mutex<u64>>) -> bool {
fn check_step(params: &Params, step_lock: Arc<Mutex<u64>>) -> bool {
*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 {
*proof_lock.lock().unwrap() == true
}
}

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) -> () {
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>>) -> () {
let mut proof_guard = found_lock.lock().unwrap();
*proof_guard = true;
drop(proof_guard);
}

0 comments on commit 44fef0b

Please sign in to comment.