Skip to content

Commit

Permalink
chores: fix let-else usage
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Oct 18, 2023
1 parent 2b73cf0 commit 94f7b28
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 65 deletions.
123 changes: 61 additions & 62 deletions src/gadgets/lookup.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! This module implements lookup gadget for applications built with Nova.
use std::cmp::max;
use std::collections::btree_map::Iter;
use std::collections::BTreeMap;
use std::collections::btree_map::Values;
use std::collections::BTreeMap;

use bellpepper_core::{num::AllocatedNum, ConstraintSystem, LinearCombination, SynthesisError};
use std::cmp::Ord;
Expand Down Expand Up @@ -66,37 +66,35 @@ impl<G: Group> LookupTrace<G> {
self.cursor,
self.expected_rw_trace.len()
);
let RWTrace::Read(expected_addr, expected_read_value, expected_read_counter) =
self.expected_rw_trace[self.cursor]
else {
Err(SynthesisError::AssignmentMissing)?
};

if let Some(key) = addr.get_value() {
assert!(
key == expected_addr,
"read address {:?} mismatch with expected {:?}",
key,
expected_addr
);
}
let read_value =
if let RWTrace::Read(expected_addr, expected_read_value, expected_read_counter) =
self.expected_rw_trace[self.cursor]
{
if let Some(key) = addr.get_value() {
assert!(
key == expected_addr,
"read address {:?} mismatch with expected {:?}",
key,
expected_addr
);
}
let read_value =
AllocatedNum::alloc(cs.namespace(|| "read_value"), || Ok(expected_read_value))?;
let read_counter = AllocatedNum::alloc(cs.namespace(|| "read_counter"), || {
Ok(expected_read_counter)
})?;
self
.rw_trace_allocated_num
.push(RWTrace::Read::<AllocatedNum<G::Scalar>>(
addr.clone(),
read_value.clone(),
read_counter,
)); // append read trace
AllocatedNum::alloc(cs.namespace(|| "read_value"), || Ok(expected_read_value))?;
let read_counter = AllocatedNum::alloc(cs.namespace(|| "read_counter"), || {
Ok(expected_read_counter)
})?;
self
.rw_trace_allocated_num
.push(RWTrace::Read::<AllocatedNum<G::Scalar>>(
addr.clone(),
read_value.clone(),
read_counter,
)); // append read trace

self.cursor += 1;
Ok(read_value)
} else {
Err(SynthesisError::AssignmentMissing)
};
read_value
self.cursor += 1;
Ok(read_value)
}

/// write value to lookup table
Expand All @@ -115,44 +113,44 @@ impl<G: Group> LookupTrace<G> {
self.cursor,
self.expected_rw_trace.len()
);
let result = if let RWTrace::Write(
let RWTrace::Write(
expected_addr,
expected_read_value,
expected_write_value,
expected_read_counter,
) = self.expected_rw_trace[self.cursor]
{
if let Some((addr, value)) = addr.get_value().zip(value.get_value()) {
assert!(
addr == expected_addr,
"write address {:?} mismatch with expected {:?}",
addr,
expected_addr
);
assert!(
value == expected_write_value,
"write value {:?} mismatch with expected {:?}",
value,
expected_write_value
);
}
let expected_read_value =
AllocatedNum::alloc(cs.namespace(|| "read_value"), || Ok(expected_read_value))?;
let expected_read_counter = AllocatedNum::alloc(cs.namespace(|| "read_counter"), || {
Ok(expected_read_counter)
})?;
self.rw_trace_allocated_num.push(RWTrace::Write(
addr.clone(),
expected_read_value,
value.clone(),
expected_read_counter,
)); // append write trace
self.cursor += 1;
Ok(())
} else {
Err(SynthesisError::AssignmentMissing)
else {
Err(SynthesisError::AssignmentMissing)?
};
result

if let Some((addr, value)) = addr.get_value().zip(value.get_value()) {
assert!(
addr == expected_addr,
"write address {:?} mismatch with expected {:?}",
addr,
expected_addr
);
assert!(
value == expected_write_value,
"write value {:?} mismatch with expected {:?}",
value,
expected_write_value
);
}

let expected_read_value =
AllocatedNum::alloc(cs.namespace(|| "read_value"), || Ok(expected_read_value))?;
let expected_read_counter = AllocatedNum::alloc(cs.namespace(|| "read_counter"), || {
Ok(expected_read_counter)
})?;
self.rw_trace_allocated_num.push(RWTrace::Write(
addr.clone(),
expected_read_value,
value.clone(),
expected_read_counter,
)); // append write trace
self.cursor += 1;
Ok(())
}

/// commit rw_trace to lookup
Expand Down Expand Up @@ -543,6 +541,7 @@ impl<F: PrimeField> Lookup<F> {
self.map_aux.len()
}

/// table values
pub fn values(&self) -> Values<'_, F, (F, F)> {
self.map_aux.values()
}
Expand Down
5 changes: 2 additions & 3 deletions src/spartan/lookupsnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ where
.into_iter()
.map(|(_, (value, _))| *value)
.collect();
let (final_values, final_counters): (Vec<_>, Vec<_>) =
final_table.values().copied().unzip();
let (final_values, final_counters): (Vec<_>, Vec<_>) = final_table.values().copied().unzip();
let comm_init_value = pk.comm_init_value;
let (comm_final_value, comm_final_counter) = rayon::join(
|| G::CE::commit(ck, &final_values),
Expand Down Expand Up @@ -272,7 +271,7 @@ where
let eval_right_vec = final_claims[2].clone();
let eval_output_vec = final_claims[3].clone();

let eval_vec = vec![
let eval_vec = [
eval_left_vec.clone(),
eval_right_vec.clone(),
eval_output_vec.clone(),
Expand Down

0 comments on commit 94f7b28

Please sign in to comment.