Skip to content

Commit

Permalink
some review suggestions + renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpaulino committed Aug 24, 2023
1 parent 7f1bf01 commit f89c7f1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
50 changes: 24 additions & 26 deletions src/lem/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,40 +138,38 @@ impl<'a, F: LurkField> MultiFrame<'a, F> {
input: &[AllocatedPtr<F>],
frames: &[Frame<F>],
blank: bool,
) -> Vec<AllocatedPtr<F>> {
) -> Result<Vec<AllocatedPtr<F>>, SynthesisError> {
let global_allocator = &mut GlobalAllocator::default();
let (_, output) = frames
.iter()
.fold((0, input.to_vec()), |(i, input), frame| {
.try_fold((0, input.to_vec()), |(i, input), frame| {
if !blank {
for (alloc_ptr, input) in input.iter().zip(&frame.input) {
let input_zptr = store.hash_ptr(input).expect("Hash did not succeed");
assert_eq!(
alloc_ptr.tag().get_value().expect("Assignment missing"),
input_zptr.tag.to_field(),
);
assert_eq!(
alloc_ptr.hash().get_value().expect("Assignment missing"),
input_zptr.hash,
);
match (alloc_ptr.tag().get_value(), alloc_ptr.hash().get_value()) {
(Some(alloc_ptr_tag), Some(alloc_ptr_hash)) => {
assert_eq!(alloc_ptr_tag, input_zptr.tag.to_field());
assert_eq!(alloc_ptr_hash, input_zptr.hash);
}
_ => return Err(SynthesisError::AssignmentMissing),
}
}
}
let bound_allocations = &mut BoundAllocations::new();
self.func.add_input(&input, bound_allocations);
let output = self
.func
.synthesize_aux(
.synthesize_frame(
&mut cs.namespace(|| format!("step {i}")),
store,
frame,
global_allocator,
bound_allocations,
)
.unwrap();
(i + 1, output)
});

output
Ok((i + 1, output))
})?;
Ok(output)
}
}

Expand All @@ -186,7 +184,7 @@ fn allocate_num<F: LurkField, CS: ConstraintSystem<F>>(
value: F,
) -> Result<AllocatedNum<F>> {
AllocatedNum::alloc(cs.namespace(|| namespace), || Ok(value))
.with_context(|| format!("allocation for '{namespace}' failed"))
.with_context(|| format!("allocation for '{namespace}'"))
}

#[inline]
Expand All @@ -196,7 +194,7 @@ fn allocate_const<F: LurkField, CS: ConstraintSystem<F>>(
value: F,
) -> Result<AllocatedNum<F>> {
allocate_constant(&mut cs.namespace(|| namespace), value)
.with_context(|| format!("allocation for '{namespace}' failed"))
.with_context(|| format!("allocation for '{namespace}'"))
}

impl<F: LurkField> GlobalAllocator<F> {
Expand Down Expand Up @@ -462,7 +460,7 @@ impl Func {
/// each slot and then, as we traverse the function, we add constraints to make
/// sure that the witness satisfies the arithmetic equations for the
/// corresponding slots.
pub fn synthesize_aux<F: LurkField, CS: ConstraintSystem<F>>(
pub fn synthesize_frame<F: LurkField, CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
store: &Store<F>,
Expand Down Expand Up @@ -1056,12 +1054,11 @@ impl Func {
let mut selector = Vec::with_capacity(cases.len() + 2);
let mut branch_slots = Vec::with_capacity(cases.len());
for (tag, block) in cases {
let has_match_bool = match (not_dummy.get_value(), match_tag.get_value()) {
(Some(not_dummy), Some(val)) => {
Some(not_dummy && val == tag.to_field::<F>())
}
_ => None,
};
let has_match_bool = not_dummy.get_value().and_then(|not_dummy| {
match_tag
.get_value()
.map(|val| not_dummy && val == tag.to_field::<F>())
});

let has_match = Boolean::Is(AllocatedBit::alloc(
&mut cs.namespace(|| format!("{tag}.allocated_bit")),
Expand Down Expand Up @@ -1267,7 +1264,8 @@ impl Func {
Ok(preallocated_outputs)
}

pub fn synthesize<F: LurkField, CS: ConstraintSystem<F>>(
/// Helper API for tests
pub fn synthesize_frame_aux<F: LurkField, CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
store: &Store<F>,
Expand All @@ -1276,7 +1274,7 @@ impl Func {
let bound_allocations = &mut BoundAllocations::new();
let global_allocator = &mut GlobalAllocator::default();
self.allocate_input(cs, store, frame, bound_allocations)?;
self.synthesize_aux(cs, store, frame, global_allocator, bound_allocations)?;
self.synthesize_frame(cs, store, frame, global_allocator, bound_allocations)?;
Ok(())
}

Expand Down
4 changes: 3 additions & 1 deletion src/lem/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,9 @@ mod tests {
store.hydrate_z_cache();
for frame in frames.iter() {
let mut cs = TestConstraintSystem::<Fr>::new();
eval_step.synthesize(&mut cs, store, frame).unwrap();
eval_step
.synthesize_frame_aux(&mut cs, store, frame)
.unwrap();
assert!(cs.is_satisfied());
assert_eq!(cs.num_inputs(), NUM_INPUTS);
assert_eq!(cs.aux().len(), NUM_AUX);
Expand Down
2 changes: 1 addition & 1 deletion src/lem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ mod tests {

for frame in frames.clone() {
cs = TestConstraintSystem::<Fr>::new();
func.synthesize(&mut cs, store, &frame).unwrap();
func.synthesize_frame_aux(&mut cs, store, &frame).unwrap();
assert!(cs.is_satisfied());
assert_eq!(computed_num_constraints, cs.num_constraints());
if let Some(cs_prev) = cs_prev {
Expand Down
6 changes: 3 additions & 3 deletions src/proof/nova_lem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,14 @@ impl<'a, F: LurkField> StepCircuit<F> for MultiFrame<'a, F> {
let output_ptrs = match self.frames.as_ref() {
Some(frames) => {
let s = self.store.expect("store missing");
self.synthesize_frames(cs, s, &input, frames, false)
self.synthesize_frames(cs, s, &input, frames, false)?
}
None => {
assert!(self.store.is_none());
let s = self.func.init_store();
let blank_frame = Frame::blank(self.func);
let frames = vec![blank_frame; self.count];
self.synthesize_frames(cs, &s, &input, &frames, true)
self.synthesize_frames(cs, &s, &input, &frames, true)?
}
};

Expand Down Expand Up @@ -572,7 +572,7 @@ pub mod tests {

for frame in frames.iter() {
let mut cs = TestConstraintSystem::<Fr>::new();
func.synthesize(&mut cs, s, frame).unwrap();
func.synthesize_frame_aux(&mut cs, s, frame).unwrap();
assert!(cs.is_satisfied());

if let Some(cs_prev) = cs_prev {
Expand Down

0 comments on commit f89c7f1

Please sign in to comment.