Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor more hash cache logic from Lurk Alpha #901

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 5 additions & 115 deletions src/circuit/circuit_frame.rs
Original file line number Diff line number Diff line change
@@ -1,119 +1,6 @@
use bellpepper::util_cs::Comparable;
use bellpepper_core::{boolean::Boolean, num::AllocatedNum, ConstraintSystem, SynthesisError};

use crate::{
circuit::gadgets::{data::GlobalAllocations, pointer::AllocatedPtr},
field::LurkField,
};

use super::gadgets::constraints::{self, alloc_equal, enforce_implication};
use crate::circuit::circuit_frame::constraints::boolean_to_num;
use crate::lurk_sym_ptr;
use crate::store::Store;

pub fn destructure_list<F: LurkField, CS: ConstraintSystem<F>>(
cs: &mut CS,
store: &Store<F>,
g: &GlobalAllocations<F>,
n: usize,
list: &AllocatedPtr<F>,
) -> Result<(Vec<AllocatedPtr<F>>, AllocatedNum<F>), SynthesisError> {
let mut elements = Vec::with_capacity(n);

let actual_length = destructure_list_aux(cs, store, g, n, list, &mut elements, &g.false_num)?;

Ok((elements, actual_length))
}

fn destructure_list_aux<F: LurkField, CS: ConstraintSystem<F>>(
cs: &mut CS,
store: &Store<F>,
g: &GlobalAllocations<F>,
n: usize,
list: &AllocatedPtr<F>,
elements: &mut Vec<AllocatedPtr<F>>,
length_so_far: &AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError> {
let is_cons = alloc_equal(&mut cs.namespace(|| "is_cons"), list.tag(), &g.cons_tag)?;
let increment = boolean_to_num(&mut cs.namespace(|| "increment"), &is_cons)?;

let new_length_so_far =
increment.add(&mut cs.namespace(|| "new_length_so_far"), length_so_far)?;

if n == 0 {
return Ok(new_length_so_far.clone());
};

let (element, tail) = car_cdr(
&mut cs.namespace(|| format!("element-{}", n)),
g,
list,
store,
&is_cons,
)?;

elements.push(element);

destructure_list_aux(
&mut cs.namespace(|| format!("tail-{}", n)),
store,
g,
n - 1,
&tail,
elements,
&new_length_so_far,
)
}

/// Returns allocated car and cdr of `maybe_cons` if `not_dummy`. If `maybe_cons` is not a cons and `not_dummy` is true, the circuit will not be satisfied.
pub(crate) fn car_cdr<F: LurkField, CS: ConstraintSystem<F>>(
mut cs: CS,
g: &GlobalAllocations<F>,
maybe_cons: &AllocatedPtr<F>,
store: &Store<F>,
not_dummy: &Boolean,
) -> Result<(AllocatedPtr<F>, AllocatedPtr<F>), SynthesisError> {
let (car, cdr) = if let Some(ptr) = maybe_cons.ptr(store).as_ref() {
if not_dummy.get_value().expect("not_dummy is missing") {
store
.car_cdr(ptr)
.map_err(|_| SynthesisError::AssignmentMissing)?
} else {
let nil_ptr = lurk_sym_ptr!(store, nil);
(nil_ptr, nil_ptr)
}
} else {
let nil_ptr = lurk_sym_ptr!(store, nil);
(nil_ptr, nil_ptr)
};

let allocated_car = AllocatedPtr::alloc_ptr(&mut cs.namespace(|| "car"), store, || Ok(&car))?;
let allocated_cdr = AllocatedPtr::alloc_ptr(&mut cs.namespace(|| "cdr"), store, || Ok(&cdr))?;

let constructed_cons = AllocatedPtr::construct_cons(
&mut cs.namespace(|| "cons"),
g,
store,
&allocated_car,
&allocated_cdr,
)?;

let real_cons = alloc_equal(
&mut cs.namespace(|| "cons is real"),
maybe_cons.hash(),
constructed_cons.hash(),
)?;

// If `maybe_cons` is not a cons, then it is dummy. No check is necessary.
// Otherwise, we must enforce equality of hashes.
enforce_implication(
&mut cs.namespace(|| "is cons implies real cons"),
not_dummy,
&real_cons,
);

Ok((allocated_car, allocated_cdr))
}
use crate::field::LurkField;

/// Prints out the full CS for debugging purposes
#[allow(dead_code)]
Expand Down Expand Up @@ -143,9 +30,12 @@ pub(crate) fn print_cs<F: LurkField, C: Comparable<F>>(this: &C) -> String {
#[cfg(test)]
mod tests {
use super::*;
use crate::circuit::circuit_frame::constraints::popcount_equal;
use crate::circuit::gadgets::constraints::implies_pack;
use crate::circuit::gadgets::constraints::popcount_equal;
use bellpepper_core::boolean::Boolean;
use bellpepper_core::num::AllocatedNum;
use bellpepper_core::test_cs::TestConstraintSystem;
use bellpepper_core::ConstraintSystem;

use pasta_curves::pallas::Scalar as Fr;

Expand Down
1 change: 1 addition & 0 deletions src/circuit/gadgets/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ pub(crate) fn enforce_selector_with_premise<F: PrimeField, CS: ConstraintSystem<
}

/// Enforce `premise` implies `implication`.
#[allow(dead_code)]
pub(crate) fn enforce_implication<CS: ConstraintSystem<F>, F: PrimeField>(
cs: CS,
premise: &Boolean,
Expand Down
4 changes: 3 additions & 1 deletion src/circuit/gadgets/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use neptune::circuit2_witness::poseidon_hash_allocated_witness;

use crate::field::{FWrap, LurkField};
use crate::hash::HashConst;
use crate::hash_witness::{Digest, WitnessBlock};

pub(crate) type WitnessBlock<F> = Vec<F>;
pub(crate) type Digest<F> = F;

type HashCircuitWitnessCache<F> = HashMap<Vec<FWrap<F>>, (Vec<F>, F)>;

Expand Down
3 changes: 1 addition & 2 deletions src/circuit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[macro_use]
pub mod gadgets;

pub mod circuit_frame;
mod circuit_frame;
3 changes: 0 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::hash_witness::ConsName;
use crate::store;

use bellpepper_core::SynthesisError;
Expand All @@ -23,8 +22,6 @@ impl From<store::Error> for ProofError {

#[derive(Error, Debug, Clone)]
pub enum ReductionError {
#[error("car_cdr of named cons {0:?} requires a cons or nil.")]
CarCdrType(ConsName),
#[error("Miscellaneous error: {0}")]
Misc(String),
#[error("Lookup error: {0}")]
Expand Down
15 changes: 1 addition & 14 deletions src/eval/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::field::LurkField;
use crate::hash_witness::{ConsWitness, ContWitness};
use crate::lurk_sym_ptr;
use crate::ptr::{ContPtr, Ptr};
use crate::ptr::Ptr;
use crate::store::Store;
use crate::z_ptr::ZExprPtr;

Expand Down Expand Up @@ -33,18 +32,6 @@ pub struct Frame<T: Copy, W: Copy, F: LurkField, C> {
pub _p: PhantomData<C>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Witness<F: LurkField> {
pub(crate) prethunk_output_expr: Ptr<F>,
pub(crate) prethunk_output_env: Ptr<F>,
pub(crate) prethunk_output_cont: ContPtr<F>,

pub(crate) closure_to_extend: Option<Ptr<F>>,
pub(crate) apply_continuation_cont: Option<ContPtr<F>>,
pub(crate) conses: ConsWitness<F>,
pub(crate) conts: ContWitness<F>,
}

#[inline]
pub fn empty_sym_env<F: LurkField>(store: &Store<F>) -> Ptr<F> {
lurk_sym_ptr!(store, nil)
Expand Down
Loading
Loading