Skip to content

Commit

Permalink
several minor API improvements, new docstrings, and helper gadgets fo…
Browse files Browse the repository at this point in the history
…r coprocessors authors
  • Loading branch information
arthurpaulino committed Nov 1, 2023
1 parent ba7fc55 commit 472771f
Show file tree
Hide file tree
Showing 15 changed files with 438 additions and 178 deletions.
6 changes: 3 additions & 3 deletions src/cli/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ impl<F: LurkField> HasFieldModulus for Commitment<F> {
}

impl<F: LurkField> Commitment<F> {
pub(crate) fn new(secret: Option<F>, payload: Ptr<F>, store: &Store<F>) -> Result<Self> {
pub(crate) fn new(secret: Option<F>, payload: Ptr<F>, store: &Store<F>) -> Self {

Check warning on line 36 in src/cli/commitment.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/commitment.rs#L36

Added line #L36 was not covered by tests
let secret = secret.unwrap_or(F::NON_HIDING_COMMITMENT_SECRET);
let (hash, z_payload) = store.hide_and_return_z_payload(secret, payload);
let mut z_store = ZStore::<F>::default();
populate_z_store(&mut z_store, &payload, store, &mut HashMap::default())?;
populate_z_store(&mut z_store, &payload, store, &mut HashMap::default());

Check warning on line 40 in src/cli/commitment.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/commitment.rs#L40

Added line #L40 was not covered by tests
z_store.add_comm(hash, secret, z_payload);
Ok(Self { hash, z_store })
Self { hash, z_store }

Check warning on line 42 in src/cli/commitment.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/commitment.rs#L42

Added line #L42 was not covered by tests
}

#[inline]
Expand Down
16 changes: 8 additions & 8 deletions src/cli/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ impl Repl<F> {
let output = &frames[n_frames - 1].output;
let mut z_store = ZStore::<F>::default();
let mut cache = HashMap::default();
let expr = populate_z_store(&mut z_store, &input[0], &self.store, &mut cache)?;
let env = populate_z_store(&mut z_store, &input[1], &self.store, &mut cache)?;
let cont = populate_z_store(&mut z_store, &input[2], &self.store, &mut cache)?;
let expr = populate_z_store(&mut z_store, &input[0], &self.store, &mut cache);
let env = populate_z_store(&mut z_store, &input[1], &self.store, &mut cache);
let cont = populate_z_store(&mut z_store, &input[2], &self.store, &mut cache);

Check warning on line 218 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L216-L218

Added lines #L216 - L218 were not covered by tests
let expr_out =
populate_z_store(&mut z_store, &output[0], &self.store, &mut cache)?;
populate_z_store(&mut z_store, &output[0], &self.store, &mut cache);

Check warning on line 220 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L220

Added line #L220 was not covered by tests
let env_out =
populate_z_store(&mut z_store, &output[1], &self.store, &mut cache)?;
populate_z_store(&mut z_store, &output[1], &self.store, &mut cache);

Check warning on line 222 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L222

Added line #L222 was not covered by tests
let cont_out =
populate_z_store(&mut z_store, &output[2], &self.store, &mut cache)?;
populate_z_store(&mut z_store, &output[2], &self.store, &mut cache);

Check warning on line 224 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L224

Added line #L224 was not covered by tests

let claim = Self::proof_claim(
&self.store,
Expand All @@ -230,7 +230,7 @@ impl Repl<F> {
(cont.parts(), cont_out.parts()),
);

let claim_comm = Commitment::new(None, claim, &self.store)?;
let claim_comm = Commitment::new(None, claim, &self.store);

Check warning on line 233 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L233

Added line #L233 was not covered by tests
let claim_hash = &claim_comm.hash.hex_digits();
let proof_key = &Self::proof_key(&self.backend, &self.rc, claim_hash);
let proof_path = proof_path(proof_key);
Expand Down Expand Up @@ -292,7 +292,7 @@ impl Repl<F> {
}

fn hide(&mut self, secret: F, payload: Ptr<F>) -> Result<()> {
let commitment = Commitment::new(Some(secret), payload, &self.store)?;
let commitment = Commitment::new(Some(secret), payload, &self.store);

Check warning on line 295 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L295

Added line #L295 was not covered by tests
let hash_str = &commitment.hash.hex_digits();
commitment.persist()?;
println!(
Expand Down
82 changes: 44 additions & 38 deletions src/cli/zstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,28 @@ use std::collections::{BTreeMap, HashMap};
use crate::{
field::{FWrap, LurkField},
lem::{
pointers::{Ptr, ZChildren, ZPtr},
pointers::{Ptr, ZPtr},
store::Store,
},
};

use super::field_data::HasFieldModulus;

/// `ZPtrType` holds information about the `Ptr` that originated a certain `ZPtr`.
/// If the `Ptr` was not atomic, `ZPtrType` can refer to its children once they
/// have already been turned into `ZPtr`s.
#[derive(Debug, Serialize, Deserialize)]

Check warning on line 18 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L18

Added line #L18 was not covered by tests
pub(crate) enum ZPtrType<F: LurkField> {
Atom,
Tuple2(ZPtr<F>, ZPtr<F>),
Tuple3(ZPtr<F>, ZPtr<F>, ZPtr<F>),
Tuple4(ZPtr<F>, ZPtr<F>, ZPtr<F>, ZPtr<F>),
}

/// A `ZStore` is a stable IO format for `Store`, without index-based references
#[derive(Debug, Default, Serialize, Deserialize)]
pub(crate) struct ZStore<F: LurkField> {
dag: BTreeMap<ZPtr<F>, ZChildren<F>>,
dag: BTreeMap<ZPtr<F>, ZPtrType<F>>,
comms: BTreeMap<FWrap<F>, (F, ZPtr<F>)>,
}

Expand All @@ -36,7 +48,7 @@ impl<F: LurkField> ZStore<F> {
}

#[inline]
pub(crate) fn get_children(&self, z_ptr: &ZPtr<F>) -> Option<&ZChildren<F>> {
pub(crate) fn get_type(&self, z_ptr: &ZPtr<F>) -> Option<&ZPtrType<F>> {

Check warning on line 51 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L51

Added line #L51 was not covered by tests
self.dag.get(z_ptr)
}

Expand All @@ -59,23 +71,21 @@ pub(crate) fn populate_z_store<F: LurkField>(
ptr: &Ptr<F>,
store: &Store<F>,
cache: &mut HashMap<Ptr<F>, ZPtr<F>>,
) -> Result<ZPtr<F>> {
let mut recurse = |ptr: &Ptr<F>| -> Result<ZPtr<F>> {
) -> ZPtr<F> {
let mut recurse = |ptr: &Ptr<F>| -> ZPtr<F> {

Check warning on line 75 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L74-L75

Added lines #L74 - L75 were not covered by tests
if let Some(z_ptr) = cache.get(ptr) {
Ok(*z_ptr)
*z_ptr

Check warning on line 77 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L77

Added line #L77 was not covered by tests
} else {
let z_ptr = match ptr {
Ptr::Atom(tag, f) => {
let z_ptr = ZPtr::from_parts(*tag, *f);
z_store.dag.insert(z_ptr, ZChildren::Atom);
z_store.dag.insert(z_ptr, ZPtrType::Atom);

Check warning on line 82 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L82

Added line #L82 was not covered by tests
z_ptr
}
Ptr::Tuple2(tag, idx) => {
let Some((a, b)) = store.fetch_2_ptrs(*idx) else {
bail!("Index {idx} not found on tuple2")
};
let a = populate_z_store(z_store, a, store, cache)?;
let b = populate_z_store(z_store, b, store, cache)?;
let (a, b) = store.expect_2_ptrs(*idx);
let a = populate_z_store(z_store, a, store, cache);
let b = populate_z_store(z_store, b, store, cache);

Check warning on line 88 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L86-L88

Added lines #L86 - L88 were not covered by tests
let z_ptr = ZPtr::from_parts(
*tag,
store.poseidon_cache.hash4(&[
Expand All @@ -85,16 +95,14 @@ pub(crate) fn populate_z_store<F: LurkField>(
*b.value(),
]),
);
z_store.dag.insert(z_ptr, ZChildren::Tuple2(a, b));
z_store.dag.insert(z_ptr, ZPtrType::Tuple2(a, b));

Check warning on line 98 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L98

Added line #L98 was not covered by tests
z_ptr
}
Ptr::Tuple3(tag, idx) => {
let Some((a, b, c)) = store.fetch_3_ptrs(*idx) else {
bail!("Index {idx} not found on tuple3")
};
let a = populate_z_store(z_store, a, store, cache)?;
let b = populate_z_store(z_store, b, store, cache)?;
let c = populate_z_store(z_store, c, store, cache)?;
let (a, b, c) = store.expect_3_ptrs(*idx);
let a = populate_z_store(z_store, a, store, cache);
let b = populate_z_store(z_store, b, store, cache);
let c = populate_z_store(z_store, c, store, cache);

Check warning on line 105 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L102-L105

Added lines #L102 - L105 were not covered by tests
let z_ptr = ZPtr::from_parts(
*tag,
store.poseidon_cache.hash6(&[
Expand All @@ -106,17 +114,15 @@ pub(crate) fn populate_z_store<F: LurkField>(
*c.value(),
]),
);
z_store.dag.insert(z_ptr, ZChildren::Tuple3(a, b, c));
z_store.dag.insert(z_ptr, ZPtrType::Tuple3(a, b, c));

Check warning on line 117 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L117

Added line #L117 was not covered by tests
z_ptr
}
Ptr::Tuple4(tag, idx) => {
let Some((a, b, c, d)) = store.fetch_4_ptrs(*idx) else {
bail!("Index {idx} not found on tuple4")
};
let a = populate_z_store(z_store, a, store, cache)?;
let b = populate_z_store(z_store, b, store, cache)?;
let c = populate_z_store(z_store, c, store, cache)?;
let d = populate_z_store(z_store, d, store, cache)?;
let (a, b, c, d) = store.expect_4_ptrs(*idx);
let a = populate_z_store(z_store, a, store, cache);
let b = populate_z_store(z_store, b, store, cache);
let c = populate_z_store(z_store, c, store, cache);
let d = populate_z_store(z_store, d, store, cache);

Check warning on line 125 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L121-L125

Added lines #L121 - L125 were not covered by tests
let z_ptr = ZPtr::from_parts(
*tag,
store.poseidon_cache.hash8(&[
Expand All @@ -130,12 +136,12 @@ pub(crate) fn populate_z_store<F: LurkField>(
*d.value(),
]),
);
z_store.dag.insert(z_ptr, ZChildren::Tuple4(a, b, c, d));
z_store.dag.insert(z_ptr, ZPtrType::Tuple4(a, b, c, d));

Check warning on line 139 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L139

Added line #L139 was not covered by tests
z_ptr
}
};
cache.insert(*ptr, z_ptr);
Ok(z_ptr)
z_ptr

Check warning on line 144 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L144

Added line #L144 was not covered by tests
}
};
recurse(ptr)
Expand All @@ -151,26 +157,26 @@ pub(crate) fn populate_store<F: LurkField>(
if let Some(z_ptr) = cache.get(z_ptr) {
Ok(*z_ptr)
} else {
let ptr = match z_store.get_children(z_ptr) {
None => bail!("Couldn't find ZPtr"),
Some(ZChildren::Atom) => Ptr::Atom(z_ptr.tag(), *z_ptr.value()),
Some(ZChildren::Tuple2(z1, z2)) => {
let ptr = match z_store.get_type(z_ptr) {
None => bail!("Couldn't find ZPtr on ZStore"),
Some(ZPtrType::Atom) => Ptr::Atom(*z_ptr.tag(), *z_ptr.value()),
Some(ZPtrType::Tuple2(z1, z2)) => {

Check warning on line 163 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L160-L163

Added lines #L160 - L163 were not covered by tests
let ptr1 = populate_store(store, z1, z_store, cache)?;
let ptr2 = populate_store(store, z2, z_store, cache)?;
store.intern_2_ptrs_hydrated(z_ptr.tag(), ptr1, ptr2, *z_ptr)
store.intern_2_ptrs_hydrated(*z_ptr.tag(), ptr1, ptr2, *z_ptr)

Check warning on line 166 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L166

Added line #L166 was not covered by tests
}
Some(ZChildren::Tuple3(z1, z2, z3)) => {
Some(ZPtrType::Tuple3(z1, z2, z3)) => {

Check warning on line 168 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L168

Added line #L168 was not covered by tests
let ptr1 = populate_store(store, z1, z_store, cache)?;
let ptr2 = populate_store(store, z2, z_store, cache)?;
let ptr3 = populate_store(store, z3, z_store, cache)?;
store.intern_3_ptrs_hydrated(z_ptr.tag(), ptr1, ptr2, ptr3, *z_ptr)
store.intern_3_ptrs_hydrated(*z_ptr.tag(), ptr1, ptr2, ptr3, *z_ptr)

Check warning on line 172 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L172

Added line #L172 was not covered by tests
}
Some(ZChildren::Tuple4(z1, z2, z3, z4)) => {
Some(ZPtrType::Tuple4(z1, z2, z3, z4)) => {

Check warning on line 174 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L174

Added line #L174 was not covered by tests
let ptr1 = populate_store(store, z1, z_store, cache)?;
let ptr2 = populate_store(store, z2, z_store, cache)?;
let ptr3 = populate_store(store, z3, z_store, cache)?;
let ptr4 = populate_store(store, z4, z_store, cache)?;
store.intern_4_ptrs_hydrated(z_ptr.tag(), ptr1, ptr2, ptr3, ptr4, *z_ptr)
store.intern_4_ptrs_hydrated(*z_ptr.tag(), ptr1, ptr2, ptr3, ptr4, *z_ptr)

Check warning on line 179 in src/cli/zstore.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/zstore.rs#L179

Added line #L179 was not covered by tests
}
};
cache.insert(*z_ptr, ptr);
Expand Down
4 changes: 2 additions & 2 deletions src/coprocessor/circom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub mod non_wasm {
cli::paths::circom_dir,
coprocessor::{CoCircuit, Coprocessor},
field::LurkField,
lem::{pointers::Ptr as LEMPtr, store::Store as LEMStore, Tag},
lem::{pointers::Ptr as LEMPtr, store::Store as LEMStore},
ptr::Ptr,
store::Store,
};
Expand Down Expand Up @@ -150,7 +150,7 @@ Then run `lurk coprocessor --name {name} <{}_FOLDER>` to instantiate a new gadge
})?;
let output = circom_scotia::synthesize(cs, self.config.r1cs.clone(), Some(witness))?;
let num_tag = g
.get_allocated_const(Tag::Expr(crate::tag::ExprTag::Num).to_field())
.get_tag(&crate::tag::ExprTag::Num)

Check warning on line 153 in src/coprocessor/circom.rs

View check run for this annotation

Codecov / codecov/patch

src/coprocessor/circom.rs#L153

Added line #L153 was not covered by tests
.expect("Num tag should have been allocated");
let res = AllocatedPtr::from_parts(num_tag.clone(), output);

Expand Down
Loading

0 comments on commit 472771f

Please sign in to comment.