Skip to content

Commit

Permalink
write with state
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpaulino committed Aug 6, 2023
1 parent 283dc7f commit c8428bb
Show file tree
Hide file tree
Showing 18 changed files with 238 additions and 175 deletions.
64 changes: 44 additions & 20 deletions clutch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ impl ReplTrait<F, Coproc<F>> for ClutchState<F, Coproc<F>> {
.fetch_sym(&car)
.ok_or(Error::msg("handle_meta fetch symbol"))?;
match format!("{}", s).as_str() {
"call" => self.call(store, rest)?,
"chain" => self.chain(store, rest)?,
"call" => self.call(store, state, rest)?,
"chain" => self.chain(store, state, rest)?,
"lurk.commit" => self.commit(store, rest)?,
"lurk.open" => self.open(store, rest)?,
"proof-in-expr" => self.proof_in_expr(store, rest)?,
"proof-out-expr" => self.proof_out_expr(store, rest)?,
"proof-in-expr" => self.proof_in_expr(store, state, rest)?,
"proof-out-expr" => self.proof_out_expr(store, state, rest)?,
"proof-claim" => self.proof_claim(store, rest)?,
"prove" => self.prove(store, rest)?,
"verify" => self.verify(store, rest)?,
Expand All @@ -227,11 +227,11 @@ impl ReplTrait<F, Coproc<F>> for ClutchState<F, Coproc<F>> {
}
Expression::Comm(_, c) => {
// NOTE: this cannot happen from a text-based REPL, since there is not currrently a literal Comm syntax.
self.apply_comm(store, *c, rest)?
self.apply_comm(store, state, *c, rest)?
}
Expression::Num(c) => {
let comm = store.intern_num(*c);
self.apply_comm(store, comm, rest)?
self.apply_comm(store, state, comm, rest)?
}
_ => return delegate!(),
},
Expand All @@ -244,7 +244,7 @@ impl ReplTrait<F, Coproc<F>> for ClutchState<F, Coproc<F>> {

if let Some(expr) = res {
let mut handle = io::stdout().lock();
expr.fmt(store, &mut handle)?;
expr.fmt(store, state, &mut handle)?;
println!();
};
Ok(())
Expand All @@ -253,9 +253,11 @@ impl ReplTrait<F, Coproc<F>> for ClutchState<F, Coproc<F>> {
fn handle_non_meta(
&mut self,
store: &mut Store<F>,
state: &State,
expr_ptr: Ptr<F>,
) -> Result<(IO<F>, IO<F>, usize)> {
let (input, output, iterations) = self.repl_state.handle_non_meta(store, expr_ptr)?;
let (input, output, iterations) =
self.repl_state.handle_non_meta(store, state, expr_ptr)?;

self.history.push(output);

Expand Down Expand Up @@ -325,18 +327,29 @@ impl ClutchState<F, Coproc<F>> {
fn apply_comm(
&mut self,
store: &mut Store<F>,
state: &State,
comm: Ptr<F>,
rest: Ptr<F>,
) -> Result<Option<Ptr<F>>> {
let call_form = store.cons(comm, rest);
self.apply_comm_aux(store, call_form, false)
self.apply_comm_aux(store, state, call_form, false)
}
fn call(&mut self, store: &mut Store<F>, rest: Ptr<F>) -> Result<Option<Ptr<F>>> {
self.apply_comm_aux(store, rest, false)
fn call(
&mut self,
store: &mut Store<F>,
state: &State,
rest: Ptr<F>,
) -> Result<Option<Ptr<F>>> {
self.apply_comm_aux(store, state, rest, false)
}
fn chain(&mut self, store: &mut Store<F>, rest: Ptr<F>) -> Result<Option<Ptr<F>>> {
fn chain(
&mut self,
store: &mut Store<F>,
state: &State,
rest: Ptr<F>,
) -> Result<Option<Ptr<F>>> {
let (result_expr, new_comm) = self
.apply_comm_aux(store, rest, true)?
.apply_comm_aux(store, state, rest, true)?
.and_then(|cons| store.car_cdr(&cons).ok()) // unpack pair
.ok_or_else(|| {
anyhow!(
Expand Down Expand Up @@ -372,14 +385,15 @@ impl ClutchState<F, Coproc<F>> {

let interned_commitment = store.intern_maybe_opaque_comm(new_commitment.comm);
let mut handle = io::stdout().lock();
interned_commitment.fmt(store, &mut handle)?;
interned_commitment.fmt(store, state, &mut handle)?;
println!();

Ok(Some(result_expr))
}
fn apply_comm_aux(
&mut self,
store: &mut Store<F>,
state: &State,
rest: Ptr<F>,
chain: bool,
) -> Result<Option<Ptr<F>>> {
Expand All @@ -403,8 +417,8 @@ impl ClutchState<F, Coproc<F>> {
};

let claim = Claim::Opening::<F>(Opening {
input: arg.fmt_to_string(store),
output: output.fmt_to_string(store),
input: arg.fmt_to_string(store, state),
output: output.fmt_to_string(store, state),
status: Status::from(cont),
commitment,
new_commitment,
Expand Down Expand Up @@ -459,21 +473,31 @@ impl ClutchState<F, Coproc<F>> {
Ok((commitment, commitment_ptr))
}

fn proof_in_expr(&self, store: &mut Store<F>, rest: Ptr<F>) -> Result<Option<Ptr<F>>> {
fn proof_in_expr(
&self,
store: &mut Store<F>,
state: &State,
rest: Ptr<F>,
) -> Result<Option<Ptr<F>>> {
let proof = self.get_proof(store, rest)?;
let (input, _output) = proof.io(store, &self.lang())?;

let mut handle = io::stdout().lock();
input.expr.fmt(store, &mut handle)?;
input.expr.fmt(store, state, &mut handle)?;
println!();
Ok(None)
}
fn proof_out_expr(&self, store: &mut Store<F>, rest: Ptr<F>) -> Result<Option<Ptr<F>>> {
fn proof_out_expr(
&self,
store: &mut Store<F>,
state: &State,
rest: Ptr<F>,
) -> Result<Option<Ptr<F>>> {
let proof = self.get_proof(store, rest)?;
let (_input, output) = proof.io(store, &self.lang())?;

let mut handle = io::stdout().lock();
output.expr.fmt(store, &mut handle)?;
output.expr.fmt(store, state, &mut handle)?;
println!();
Ok(None)
}
Expand Down
17 changes: 9 additions & 8 deletions fcomm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use lurk::{
proof::nova::{self, NovaProver, PublicParams},
proof::Prover,
ptr::{ContPtr, Ptr},
state::initial_lurk_state,
store::Store,
tag::ExprTag,
writer::Write,
Expand Down Expand Up @@ -412,13 +413,13 @@ impl Evaluation {
};
}

let expr = input.expr.fmt_to_string(s);
let env = input.env.fmt_to_string(s);
let cont = input.cont.fmt_to_string(s);
let expr = input.expr.fmt_to_string(s, &initial_lurk_state());
let env = input.env.fmt_to_string(s, &initial_lurk_state());
let cont = input.cont.fmt_to_string(s, &initial_lurk_state());

let expr_out = maybe_hide!(output.expr.fmt_to_string(s));
let env_out = maybe_hide!(output.env.fmt_to_string(s));
let cont_out = maybe_hide!(output.cont.fmt_to_string(s));
let expr_out = maybe_hide!(output.expr.fmt_to_string(s, &initial_lurk_state()));
let env_out = maybe_hide!(output.env.fmt_to_string(s, &initial_lurk_state()));
let cont_out = maybe_hide!(output.cont.fmt_to_string(s, &initial_lurk_state()));

Self {
expr,
Expand Down Expand Up @@ -753,12 +754,12 @@ impl<'a> Opening<S1> {
(None, public_output.expr)
};

let input_string = input.fmt_to_string(s);
let input_string = input.fmt_to_string(s, &initial_lurk_state());
let status =
<lurk::eval::IO<S1> as Evaluable<S1, Witness<S1>, Coproc<S1>>>::status(&public_output);
let output_string = if status.is_terminal() {
// Only actual output if result is terminal.
output_expr.fmt_to_string(s)
output_expr.fmt_to_string(s, &initial_lurk_state())
} else {
// We don't want to leak any internal information in the case of incomplete computations.
// Provers might want to expose results in the case of explicit errors.
Expand Down
5 changes: 3 additions & 2 deletions fcomm/tests/proof_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use assert_cmd::prelude::*;
use lurk::state::initial_lurk_state;
use predicates::prelude::*;
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -237,12 +238,12 @@ fn test_function_aux(
let mut store = Store::<S1>::default();

let input = store.read(function_input).expect("store read");
let canonical_input = input.fmt_to_string(&store);
let canonical_input = input.fmt_to_string(&store, &initial_lurk_state());

let canonical_output = store
.read(expected_output)
.expect("store read")
.fmt_to_string(&store);
.fmt_to_string(&store, &initial_lurk_state());

assert_eq!(canonical_input, opening.input);
assert_eq!(*expected_output, canonical_output);
Expand Down
7 changes: 4 additions & 3 deletions src/circuit/gadgets/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use bellperson::{
};
use ff::PrimeField;

use crate::hash::IntoHashComponents;
use crate::{
cont::Continuation,
expr::{Expression, Thunk},
field::LurkField,
hash::IntoHashComponents,
hash_witness::{ConsName, ContName},
ptr::{ContPtr, Ptr},
state::initial_lurk_state,
store::Store,
tag::{ExprTag, Tag},
writer::Write,
Expand Down Expand Up @@ -259,7 +260,7 @@ impl<F: LurkField> AllocatedPtr<F> {

pub fn fetch_and_write_str(&self, store: &Store<F>) -> String {
self.ptr(store)
.map(|a| a.fmt_to_string(store))
.map(|a| a.fmt_to_string(store, &initial_lurk_state()))
.unwrap_or_else(|| "<PTR MISSING>".to_string())
}

Expand Down Expand Up @@ -694,7 +695,7 @@ impl<F: LurkField> AllocatedContPtr<F> {

pub fn fetch_and_write_cont_str(&self, store: &Store<F>) -> String {
self.get_cont_ptr(store)
.map(|a| a.fmt_to_string(store))
.map(|a| a.fmt_to_string(store, &initial_lurk_state()))
.unwrap_or_else(|| "no cont ptr".to_string())
}

Expand Down
34 changes: 17 additions & 17 deletions src/cli/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Repl<F> {
);
Repl {
store,
state: State::initial_lurk_state(),
state: State::init_lurk_state(),
env,
lang: Arc::new(Lang::new()),
rc,
Expand Down Expand Up @@ -295,7 +295,7 @@ impl Repl<F> {
commitment.persist()?;
println!(
"Data: {}\nHash: 0x{hash_str}",
payload.fmt_to_string(&self.store)
payload.fmt_to_string(&self.store, &self.state)
);
Ok(())
}
Expand All @@ -322,7 +322,7 @@ impl Repl<F> {
.unwrap();
if print_data {
let data = self.store.fetch_comm(&comm_ptr).unwrap().1;
println!("{}", data.fmt_to_string(&self.store));
println!("{}", data.fmt_to_string(&self.store, &self.state));
} else {
println!("Data is now available");
}
Expand Down Expand Up @@ -431,7 +431,7 @@ impl Repl<F> {

let (new_binding, _) = &self.store.car_cdr(&expanded_io.expr)?;
let (new_name, _) = self.store.car_cdr(new_binding)?;
println!("{}", new_name.fmt_to_string(&self.store));
println!("{}", new_name.fmt_to_string(&self.store, &self.state));
}
"defrec" => {
// Extends env with a recursive binding.
Expand All @@ -456,7 +456,7 @@ impl Repl<F> {
let (new_binding_outer, _) = &self.store.car_cdr(&expanded_io.expr)?;
let (new_binding_inner, _) = &self.store.car_cdr(new_binding_outer)?;
let (new_name, _) = self.store.car_cdr(new_binding_inner)?;
println!("{}", new_name.fmt_to_string(&self.store));
println!("{}", new_name.fmt_to_string(&self.store, &self.state));
}
"load" => {
let first = self.peek1(cmd, args)?;
Expand All @@ -475,7 +475,7 @@ impl Repl<F> {
if first_io.expr.is_nil() {
eprintln!(
"`assert` failed. {} evaluates to nil",
first.fmt_to_string(&self.store)
first.fmt_to_string(&self.store, &self.state)
);
process::exit(1);
}
Expand All @@ -491,10 +491,10 @@ impl Repl<F> {
if !&self.store.ptr_eq(&first_io.expr, &second_io.expr)? {
eprintln!(
"`assert-eq` failed. Expected:\n {} = {}\nGot:\n {} ≠ {}",
first.fmt_to_string(&self.store),
second.fmt_to_string(&self.store),
first_io.expr.fmt_to_string(&self.store),
second_io.expr.fmt_to_string(&self.store)
first.fmt_to_string(&self.store, &self.state),
second.fmt_to_string(&self.store, &self.state),
first_io.expr.fmt_to_string(&self.store, &self.state),
second_io.expr.fmt_to_string(&self.store, &self.state)
);
process::exit(1);
}
Expand All @@ -512,8 +512,8 @@ impl Repl<F> {
if elem != &first_emitted {
eprintln!(
"`assert-emitted` failed at position {i}. Expected {}, but found {}.",
first_emitted.fmt_to_string(&self.store),
elem.fmt_to_string(&self.store),
first_emitted.fmt_to_string(&self.store, &self.state),
elem.fmt_to_string(&self.store, &self.state),
);
process::exit(1);
}
Expand All @@ -525,7 +525,7 @@ impl Repl<F> {
if self.eval_expr(first).is_ok() {
eprintln!(
"`assert-error` failed. {} doesn't result on evaluation error.",
first.fmt_to_string(&self.store)
first.fmt_to_string(&self.store, &self.state)
);
process::exit(1);
}
Expand All @@ -551,7 +551,7 @@ impl Repl<F> {
let Some(secret) = self.store.fetch_num(&first_io.expr) else {
bail!(
"Secret must be a number. Got {}",
first_io.expr.fmt_to_string(&self.store)
first_io.expr.fmt_to_string(&self.store, &self.state)
)
};
self.hide(secret.into_scalar(), second_io.expr)?;
Expand Down Expand Up @@ -592,7 +592,7 @@ impl Repl<F> {
match self.store.fetch_string(&first) {
None => bail!(
"Proof ID {} not parsed as a string",
first.fmt_to_string(&self.store)
first.fmt_to_string(&self.store, &self.state)
),
Some(proof_id) => {
LurkProof::verify_proof(&proof_id)?;
Expand All @@ -613,7 +613,7 @@ impl Repl<F> {
ContTag::Terminal => {
println!(
"[{iterations_display}] => {}",
output.expr.fmt_to_string(&self.store)
output.expr.fmt_to_string(&self.store, &self.state)
)
}
ContTag::Error => {
Expand All @@ -632,7 +632,7 @@ impl Repl<F> {
}
None => bail!(
"Meta command must be a symbol. Found {}",
car.fmt_to_string(&self.store)
car.fmt_to_string(&self.store, &self.state)
),
}
Ok(())
Expand Down
Loading

0 comments on commit c8428bb

Please sign in to comment.