Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpaulino authored Oct 23, 2023
1 parent 696f1f6 commit aa63a65
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 258 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bellpepper = { workspace = true }
bellpepper-core = { workspace = true }
bincode = { workspace = true }
blstrs = { workspace = true }
bytecount = "=0.6.4"
camino = { workspace = true }
clap = { workspace = true, features = ["derive"] }
config = "0.13.3"
Expand Down
39 changes: 25 additions & 14 deletions src/cli/commitment.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::field::LurkField;
use crate::z_store::ZStore;
use crate::{ptr::Ptr, store::Store};
use crate::{
field::LurkField,
lem::{
pointers::{Ptr, ZPtr},
store::Store,
},
};

use super::{
field_data::{dump, HasFieldModulus},
paths::commitment_path,
zstore::{populate_z_store, ZStore},
};

/// Holds data for commitments.
Expand All @@ -17,7 +23,7 @@ use super::{
#[derive(Serialize, Deserialize)]
pub(crate) struct Commitment<F: LurkField> {
pub(crate) hash: F,
pub(crate) zstore: ZStore<F>,
pub(crate) z_store: ZStore<F>,
}

impl<F: LurkField> HasFieldModulus for Commitment<F> {
Expand All @@ -28,21 +34,26 @@ 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> {
let comm_ptr = match secret {
Some(secret) => store.hide(secret, payload),
None => store.commit(payload),
};
let mut zstore = Some(ZStore::<F>::default());
let hash = *store.get_z_expr(&comm_ptr, &mut zstore)?.0.value();
let zstore = zstore.unwrap();
Ok(Self { hash, zstore })
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())?;
z_store.add_comm(hash, secret, z_payload);
Ok(Self { hash, z_store })
}

#[inline]
pub(crate) fn open(&self) -> Result<&(F, ZPtr<F>)> {
self.z_store
.open(self.hash)
.ok_or_else(|| anyhow!("Couldn't open commitment"))
}
}

impl<F: LurkField + Serialize> Commitment<F> {
#[inline]
pub(crate) fn persist(self) -> Result<()> {
let hash_str = &self.hash.hex_digits();
dump(self, commitment_path(hash_str))
dump(self, &commitment_path(hash_str))
}
}
4 changes: 2 additions & 2 deletions src/cli/field_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ pub(crate) fn de<T: DeserializeOwned + HasFieldModulus>(bytes: &[u8]) -> Result<
Ok(data)
}

pub(crate) fn dump<T: Serialize + HasFieldModulus>(t: T, path: Utf8PathBuf) -> Result<()> {
pub(crate) fn dump<T: Serialize + HasFieldModulus>(t: T, path: &Utf8PathBuf) -> Result<()> {
Ok(std::fs::write(path, ser(t)?)?)
}

pub(crate) fn load<T: DeserializeOwned + HasFieldModulus>(path: Utf8PathBuf) -> Result<T> {
pub(crate) fn load<T: DeserializeOwned + HasFieldModulus>(path: &Utf8PathBuf) -> Result<T> {
de(&std::fs::read(path)?)
}

Expand Down
30 changes: 14 additions & 16 deletions src/cli/lurk_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
coprocessor::Coprocessor,
eval::lang::{Coproc, Lang},
field::LurkField,
lem::pointers::ZPtr,
proof::{
nova::{self, CurveCycleEquipped, G1, G2},
supernova::C2,
Expand All @@ -21,17 +22,14 @@ use crate::{
instance::{Instance, Kind},
public_params,
},
z_ptr::{ZContPtr, ZExprPtr},
z_store::ZStore,
};

use crate::cli::{
field_data::{dump, load},
use super::{
field_data::{dump, load, HasFieldModulus},
paths::{proof_meta_path, proof_path, public_params_dir},
zstore::ZStore,
};

use super::field_data::HasFieldModulus;

/// Carries extra information to help with visualization, experiments etc.
///
/// Note: the `ZStore` in this struct only has enough data to recover the meaning
Expand All @@ -41,13 +39,13 @@ use super::field_data::HasFieldModulus;
#[derive(Serialize, Deserialize)]
pub(crate) struct LurkProofMeta<F: LurkField> {
pub(crate) iterations: usize,
pub(crate) expr: ZExprPtr<F>,
pub(crate) env: ZExprPtr<F>,
pub(crate) cont: ZContPtr<F>,
pub(crate) expr_out: ZExprPtr<F>,
pub(crate) env_out: ZExprPtr<F>,
pub(crate) cont_out: ZContPtr<F>,
pub(crate) zstore: ZStore<F>,
pub(crate) expr: ZPtr<F>,
pub(crate) env: ZPtr<F>,
pub(crate) cont: ZPtr<F>,
pub(crate) expr_out: ZPtr<F>,
pub(crate) env_out: ZPtr<F>,
pub(crate) cont_out: ZPtr<F>,
pub(crate) z_store: ZStore<F>,
}

impl<F: LurkField> HasFieldModulus for LurkProofMeta<F> {
Expand Down Expand Up @@ -93,7 +91,7 @@ where
impl<F: LurkField + Serialize> LurkProofMeta<F> {
#[inline]
pub(crate) fn persist(self, proof_key: &str) -> Result<()> {
dump(self, proof_meta_path(proof_key))
dump(self, &proof_meta_path(proof_key))
}
}

Expand All @@ -107,7 +105,7 @@ where
{
#[inline]
pub(crate) fn persist(self, proof_key: &str) -> Result<()> {
dump(self, proof_path(proof_key))
dump(self, &proof_path(proof_key))
}
}

Expand All @@ -125,7 +123,7 @@ where
<F as CurveCycleEquipped>::CK2: Sync + Send,
{
pub(crate) fn verify_proof(proof_key: &str) -> Result<()> {
let lurk_proof: LurkProof<'_, F, Coproc<F>, M> = load(proof_path(proof_key))?;
let lurk_proof: LurkProof<'_, F, Coproc<F>, M> = load(&proof_path(proof_key))?;
if lurk_proof.verify()? {
println!("✓ Proof \"{proof_key}\" verified");
} else {
Expand Down
21 changes: 9 additions & 12 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod field_data;
mod lurk_proof;
pub mod paths;
pub mod repl;
mod zstore;

use anyhow::{bail, Context, Result};
use camino::Utf8PathBuf;
Expand All @@ -20,21 +21,19 @@ use std::{
};

use crate::{
circuit::MultiFrame,
eval::lang::Coproc,
field::{LanguageField, LurkField},
lem::{multiframe::MultiFrame, store::Store},
public_parameters::instance::Metadata,
store::Store,
z_data::{from_z_data, ZData},
z_store::ZStore,
};

use crate::cli::{
paths::set_lurk_dirs,
repl::{validate_non_zero, Repl},
zstore::ZStore,
};

use self::{backend::Backend, paths::public_params_dir};
use self::{backend::Backend, field_data::load, paths::public_params_dir};

const DEFAULT_LIMIT: usize = 100_000_000;
const DEFAULT_RC: usize = 10;
Expand Down Expand Up @@ -341,15 +340,13 @@ pub fn get_config(config_path: &Option<Utf8PathBuf>) -> Result<HashMap<String, S
}

fn get_store<F: LurkField + for<'a> serde::de::Deserialize<'a>>(
zstore_path: &Option<Utf8PathBuf>,
z_store_path: &Option<Utf8PathBuf>,
) -> Result<Store<F>> {
match zstore_path {
match z_store_path {
None => Ok(Store::default()),
Some(zstore_path) => {
let bytes = fs::read(zstore_path)?;
let zdata = ZData::from_bytes(&bytes)?;
let zstore: ZStore<F> = from_z_data(&zdata)?;
Ok(zstore.to_store())
Some(z_store_path) => {
let z_store: ZStore<F> = load(z_store_path)?;
z_store.to_store()
}
}
}
Expand Down
Loading

0 comments on commit aa63a65

Please sign in to comment.