Skip to content

Commit

Permalink
reverted associated types
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-barrett committed Jan 12, 2024
1 parent 7f25be3 commit 0f5441f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 40 deletions.
21 changes: 9 additions & 12 deletions src/coprocessor/memoset/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub(crate) enum DemoCircuitQuery<F: LurkField> {
}

impl<F: LurkField> Query<F> for DemoQuery<F> {
type CQ = DemoCircuitQuery<F>;

// DemoQuery and Scope depend on each other.
fn eval(&self, s: &Store<F>, scope: &mut Scope<F, Self, LogMemo<F>>) -> Ptr {
match self {
Expand Down Expand Up @@ -95,14 +97,6 @@ impl<F: LurkField> Query<F> for DemoQuery<F> {
}

impl<F: LurkField> CircuitQuery<F> for DemoCircuitQuery<F> {
type Q = DemoQuery<F>;

fn dummy_query_variant(&self, s: &Store<F>) -> Self::Q {
match self {
Self::Factorial(_) => Self::Q::Factorial(s.num(F::ZERO)),
}
}

fn synthesize_eval<CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
Expand Down Expand Up @@ -143,8 +137,7 @@ impl<F: LurkField> CircuitQuery<F> for DemoCircuitQuery<F> {
);

let subquery = {
let symbol =
g.alloc_ptr(cs, &store.intern_symbol(&self.symbol(store)), store);
let symbol = g.alloc_ptr(cs, &self.symbol_ptr(store), store);

let new_num = AllocatedPtr::alloc_tag(
&mut cs.namespace(|| "new_num"),
Expand Down Expand Up @@ -215,10 +208,10 @@ impl<F: LurkField> CircuitQuery<F> for DemoCircuitQuery<F> {
s: &Store<F>,
ptr: &Ptr,
) -> Result<Option<Self>, SynthesisError> {
let query = Self::Q::from_ptr(s, ptr);
let query = DemoQuery::from_ptr(s, ptr);
Ok(if let Some(q) = query {
match q {
Self::Q::Factorial(n) => Some(Self::Factorial(AllocatedPtr::alloc(cs, || {
DemoQuery::Factorial(n) => Some(Self::Factorial(AllocatedPtr::alloc(cs, || {
Ok(s.hash_ptr(&n))
})?)),
_ => unreachable!(),
Expand All @@ -227,6 +220,10 @@ impl<F: LurkField> CircuitQuery<F> for DemoCircuitQuery<F> {
None
})
}

fn symbol(&self) -> Symbol {
todo!()
}
}

#[cfg(test)]
Expand Down
33 changes: 13 additions & 20 deletions src/coprocessor/memoset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,14 @@ impl<F: LurkField, Q: Query<F>> Scope<F, Q, LogMemo<F>> {
(transcript, unique_keys)
}

pub fn synthesize<CS: ConstraintSystem<F>, CQ: CircuitQuery<F, Q = Q>>(
pub fn synthesize<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
g: &mut GlobalAllocator<F>,
s: &Store<F>,
) -> Result<(), SynthesisError> {
self.ensure_transcript_finalized(s);
let mut circuit_scope: CircuitScope<_, CQ, _> =
let mut circuit_scope: CircuitScope<_, Q::CQ, _> =
CircuitScope::from_scope(&mut cs.namespace(|| "transcript"), g, s, self);
circuit_scope.init(cs, g, s);
{
Expand All @@ -360,11 +360,11 @@ impl<F: LurkField, Q: Query<F>> Scope<F, Q, LogMemo<F>> {
}

impl<F: LurkField, CQ: CircuitQuery<F>> CircuitScope<F, CQ, LogMemo<F>> {
pub fn from_scope<CS: ConstraintSystem<F>>(
fn from_scope<CS: ConstraintSystem<F>, Q: Query<F, CQ = CQ>>(
cs: &mut CS,
g: &mut GlobalAllocator<F>,
s: &Store<F>,
scope: &Scope<F, CQ::Q, LogMemo<F>>,
scope: &Scope<F, Q, LogMemo<F>>,
) -> Self {
let queries = scope
.queries
Expand Down Expand Up @@ -498,9 +498,9 @@ impl<F: LurkField, CQ: CircuitQuery<F>> CircuitScope<F, CQ, LogMemo<F>> {
Ok((value, new_acc, new_insertion_transcript))
}

fn synthesize_insert_toplevel_queries<CS: ConstraintSystem<F>>(
fn synthesize_insert_toplevel_queries<CS: ConstraintSystem<F>, Q: Query<F, CQ = CQ>>(
&mut self,
scope: &mut Scope<F, CQ::Q, LogMemo<F>>,
scope: &mut Scope<F, Q, LogMemo<F>>,
cs: &mut CS,
g: &mut GlobalAllocator<F>,
s: &Store<F>,
Expand Down Expand Up @@ -548,9 +548,9 @@ impl<F: LurkField, CQ: CircuitQuery<F>> CircuitScope<F, CQ, LogMemo<F>> {
Ok(())
}

fn synthesize_prove_all_queries<CS: ConstraintSystem<F>>(
fn synthesize_prove_all_queries<CS: ConstraintSystem<F>, Q: Query<F, CQ = CQ>>(
&mut self,
scope: &mut Scope<F, CQ::Q, LogMemo<F>>,
scope: &mut Scope<F, Q, LogMemo<F>>,
cs: &mut CS,
g: &mut GlobalAllocator<F>,
s: &Store<F>,
Expand Down Expand Up @@ -746,18 +746,15 @@ mod test {

use crate::state::State;
use bellpepper_core::{test_cs::TestConstraintSystem, Comparable};
use demo::DemoCircuitQuery;
use demo::DemoQuery;
use expect_test::{expect, Expect};
use pasta_curves::pallas::Scalar as F;
use std::default::Default;

type ScopeCircuitQuery<F> = DemoCircuitQuery<F>;
type ScopeQuery<F> = <ScopeCircuitQuery<F> as CircuitQuery<F>>::Q;

#[test]
fn test_query() {
let s = &Store::<F>::default();
let mut scope: Scope<F, ScopeQuery<F>, LogMemo<F>> = Scope::default();
let mut scope: Scope<F, DemoQuery<F>, LogMemo<F>> = Scope::default();
let state = State::init_lurk_state();

let fact_4 = s.read_with_default_state("(factorial 4)").unwrap();
Expand Down Expand Up @@ -785,9 +782,7 @@ mod test {
let cs = &mut TestConstraintSystem::new();
let g = &mut GlobalAllocator::default();

scope
.synthesize::<_, ScopeCircuitQuery<F>>(cs, g, s)
.unwrap();
scope.synthesize(cs, g, s).unwrap();

println!(
"transcript: {}",
Expand All @@ -810,7 +805,7 @@ mod test {
assert!(cs.is_satisfied());
}
{
let mut scope: Scope<F, ScopeQuery<F>, LogMemo<F>> = Scope::default();
let mut scope: Scope<F, DemoQuery<F>, LogMemo<F>> = Scope::default();
scope.query(s, fact_4);
scope.query(s, fact_3);

Expand All @@ -826,9 +821,7 @@ mod test {
let cs = &mut TestConstraintSystem::new();
let g = &mut GlobalAllocator::default();

scope
.synthesize::<_, ScopeCircuitQuery<F>>(cs, g, s)
.unwrap();
scope.synthesize(cs, g, s).unwrap();

expect_eq(cs.num_constraints(), expect!["11408"]);
expect_eq(cs.aux().len(), expect!["11445"]);
Expand Down
12 changes: 4 additions & 8 deletions src/coprocessor/memoset/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub trait Query<F: LurkField>
where
Self: Sized + Clone,
{
type CQ: CircuitQuery<F>;

fn eval(&self, s: &Store<F>, scope: &mut Scope<F, Self, LogMemo<F>>) -> Ptr;
fn recursive_eval(
&self,
Expand All @@ -33,8 +35,6 @@ pub trait CircuitQuery<F: LurkField>
where
Self: Sized + Clone,
{
type Q: Query<F>;

fn synthesize_eval<CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
Expand All @@ -45,19 +45,15 @@ where
transcript: &CircuitTranscript<F>,
) -> Result<(AllocatedPtr<F>, AllocatedPtr<F>, CircuitTranscript<F>), SynthesisError>;

fn symbol(&self, s: &Store<F>) -> Symbol {
self.dummy_query_variant(s).symbol()
}
fn symbol(&self) -> Symbol;

fn symbol_ptr(&self, s: &Store<F>) -> Ptr {
self.dummy_query_variant(s).symbol_ptr(s)
s.intern_symbol(&self.symbol())
}

fn from_ptr<CS: ConstraintSystem<F>>(
cs: &mut CS,
s: &Store<F>,
ptr: &Ptr,
) -> Result<Option<Self>, SynthesisError>;

fn dummy_query_variant(&self, s: &Store<F>) -> Self::Q;
}

0 comments on commit 0f5441f

Please sign in to comment.