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

Implement persistent commitments #543

Merged
merged 20 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ rand_xorshift = "0.3.0"
rayon = "1.7.0"
rustyline-derive = "0.8.0"
serde = { workspace = true, features = ["derive"] }
serde_bytes = "0.11.12"
serde_json = { workspace = true }
serde_repr = "0.1.10"
tap = "1.0.1"
Expand Down
20 changes: 13 additions & 7 deletions src/cli/commitment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use lurk::{field::LurkField, z_ptr::ZExprPtr, z_store::ZStore};
use serde::{Deserialize, Serialize};

use super::field_data::HasFieldModulus;

/// Holds data for commitments.
///
/// **Warning**: holds private data. The `ZStore` contains the secret used to
Expand All @@ -11,14 +13,19 @@ pub struct Commitment<F: LurkField> {
pub(crate) zstore: ZStore<F>,
}

impl<F: LurkField> HasFieldModulus for Commitment<F> {
fn field_modulus() -> String {
F::MODULUS.to_owned()
}
}

#[cfg(not(target_arch = "wasm32"))]
mod cli {
mod non_wasm {
use anyhow::Result;
use lurk::{field::LurkField, ptr::Ptr, store::Store, z_store::ZStore};
use serde::Serialize;
use std::{fs::File, io::BufWriter};

use crate::cli::{field_data::FieldData, paths::cli::commitment_path};
use crate::cli::{field_data::FieldData, paths::non_wasm::commitment_path};

use super::Commitment;

Expand All @@ -35,10 +42,9 @@ mod cli {
}

impl<F: LurkField + Serialize> Commitment<F> {
pub fn persist(&self, hash: &str) -> Result<()> {
let fd = &FieldData::wrap::<F, Commitment<F>>(self)?;
bincode::serialize_into(BufWriter::new(&File::create(commitment_path(hash))?), fd)?;
Ok(())
#[inline]
pub fn persist(self, hash: &str) -> Result<()> {
FieldData::dump(self, commitment_path(hash))
}
}
}
86 changes: 65 additions & 21 deletions src/cli/field_data.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,74 @@
use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use anyhow::Result;

use lurk::field::{LanguageField, LurkField};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

/// A wrapper for data whose deserialization depends on a certain LurkField
#[derive(Serialize, Deserialize)]
pub struct FieldData {
pub(crate) field: LanguageField,
data: Vec<u8>,
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct FieldData<T>(T);

pub(crate) trait HasFieldModulus {
fn field_modulus() -> String;
}

impl<'de, T: DeserializeOwned + HasFieldModulus> Deserialize<'de> for FieldData<T> {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct Labeled {
label: String,
#[serde(with = "serde_bytes")]
bytes: Vec<u8>,
}
let level1_struct = Labeled::deserialize(deserializer)?;
if level1_struct.label != T::field_modulus() {
return Err(serde::de::Error::custom("Field mismatch"));
};
let t: T =
bincode::deserialize(&level1_struct.bytes[..]).map_err(serde::de::Error::custom)?;
Ok(FieldData(t))
}
}

#[allow(dead_code)]
impl FieldData {
#[inline]
pub fn wrap<F: LurkField, T: Serialize>(t: &T) -> Result<Self> {
Ok(Self {
field: F::FIELD,
data: bincode::serialize(t)?,
})
impl<T: Serialize + HasFieldModulus> Serialize for FieldData<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
#[derive(Serialize)]
struct Labeled {
label: String,
#[serde(with = "serde_bytes")]
bytes: Vec<u8>,
}
let bytes = bincode::serialize(&self.0).map_err(serde::ser::Error::custom)?;
let labeled = Labeled {
label: T::field_modulus(),
bytes,
};
labeled.serialize(serializer)
}
}

arthurpaulino marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(not(target_arch = "wasm32"))]
pub mod non_wasm {
use super::{FieldData, HasFieldModulus};
use anyhow::Result;
use serde::{de::DeserializeOwned, Serialize};
use std::path::PathBuf;

impl<T: Serialize + HasFieldModulus> FieldData<T> {
pub fn dump(t: T, path: PathBuf) -> Result<()> {
let bytes = bincode::serialize(&FieldData(t))?;
Ok(std::fs::write(path, bytes)?)
}
}

#[inline]
pub fn extract<'a, F: LurkField, T: Deserialize<'a>>(&'a self) -> Result<T> {
if self.field != F::FIELD {
bail!("Invalid field: {}. Expected {}", &self.field, &F::FIELD)
impl<T: DeserializeOwned + HasFieldModulus> FieldData<T> {
pub fn load(path: PathBuf) -> Result<T> {
let bytes = std::fs::read(path)?;
let fd: FieldData<T> = bincode::deserialize(&bytes)?;
Ok(fd.0)
}
Ok(bincode::deserialize(&self.data)?)
}
}
76 changes: 40 additions & 36 deletions src/cli/lurk_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ use serde::{Deserialize, Serialize};

use lurk::{
coprocessor::Coprocessor,
eval::{
lang::{Coproc, Lang},
Status,
},
eval::lang::{Coproc, Lang},
field::LurkField,
proof::nova,
z_ptr::ZExprPtr,
z_ptr::{ZContPtr, ZExprPtr},
z_store::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 @@ -20,16 +19,21 @@ use lurk::{
#[derive(Serialize, Deserialize)]
pub struct LurkProofMeta<F: LurkField> {
pub(crate) iterations: usize,
pub(crate) evaluation_cost: u128,
pub(crate) generation_cost: u128,
pub(crate) compression_cost: u128,
pub(crate) status: Status,
pub(crate) expression: ZExprPtr<F>,
pub(crate) environment: ZExprPtr<F>,
pub(crate) result: ZExprPtr<F>,
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>,
}

impl<F: LurkField> HasFieldModulus for LurkProofMeta<F> {
fn field_modulus() -> String {
F::MODULUS.to_owned()
}
}

type Pallas = pasta_curves::pallas::Scalar; // TODO: generalize this

/// Minimal data structure containing just enough for proof verification
Expand All @@ -48,46 +52,44 @@ where
},
}

impl<'a, F: LurkField> HasFieldModulus for LurkProof<'a, F>
where
Coproc<F>: Coprocessor<Pallas>,
{
fn field_modulus() -> String {
F::MODULUS.to_owned()
}
}

#[cfg(not(target_arch = "wasm32"))]
mod cli {
mod non_wasm {
use crate::cli::{
field_data::FieldData,
paths::cli::{proof_meta_path, proof_path},
paths::non_wasm::{proof_meta_path, proof_path},
};
use anyhow::Result;
use lurk::{
coprocessor::Coprocessor, eval::lang::Coproc, field::LurkField,
public_parameters::public_params,
};
use serde::Serialize;
use std::{fs::File, io::BufReader, io::BufWriter};

use super::{LurkProof, LurkProofMeta, Pallas};

impl<F: LurkField + Serialize> LurkProofMeta<F> {
pub fn persist(&self, id: &str) -> Result<()> {
let fd = &FieldData::wrap::<F, LurkProofMeta<F>>(self)?;
bincode::serialize_into(BufWriter::new(&File::create(proof_meta_path(id))?), fd)?;
Ok(())
#[inline]
pub fn persist(self, id: &str) -> Result<()> {
FieldData::dump(self, proof_meta_path(id))
}
}

impl<'a, F: LurkField + Serialize> LurkProof<'a, F>
where
Coproc<F>: Coprocessor<Pallas>,
{
pub fn persist(&self, id: &str) -> Result<()> {
let fd = &FieldData::wrap::<F, LurkProof<'_, F>>(self)?;
bincode::serialize_into(BufWriter::new(&File::create(proof_path(id))?), fd)?;
Ok(())
}

fn print_verification(proof_id: &str, success: bool) {
if success {
println!("✓ Proof \"{proof_id}\" verified");
} else {
println!("✗ Proof \"{proof_id}\" failed on verification");
}
#[inline]
pub fn persist(self, id: &str) -> Result<()> {
FieldData::dump(self, proof_path(id))
}
}

Expand All @@ -109,11 +111,13 @@ mod cli {
}
}

pub fn verify_proof<F: LurkField>(proof_id: &str) -> Result<()> {
let file = File::open(proof_path(proof_id))?;
let fd: FieldData = bincode::deserialize_from(BufReader::new(file))?;
let lurk_proof = fd.extract::<F, LurkProof<'_, Pallas>>()?;
Self::print_verification(proof_id, lurk_proof.verify()?);
pub fn verify_proof(proof_id: &str) -> Result<()> {
let lurk_proof: LurkProof<'_, Pallas> = FieldData::load(proof_path(proof_id))?;
if lurk_proof.verify()? {
println!("✓ Proof \"{proof_id}\" verified");
} else {
println!("✗ Proof \"{proof_id}\" failed on verification");
}
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ struct VerifyArgs {
/// Parses CLI arguments and continues the program flow accordingly
pub fn parse_and_run() -> Result<()> {
#[cfg(not(target_arch = "wasm32"))]
paths::cli::create_lurk_dirs()?;
paths::non_wasm::create_lurk_dirs()?;

if let Ok(repl_cli) = ReplCli::try_parse() {
repl_cli.run()
Expand All @@ -378,7 +378,7 @@ pub fn parse_and_run() -> Result<()> {
#[cfg(not(target_arch = "wasm32"))]
{
use crate::cli::lurk_proof::LurkProof;
LurkProof::verify_proof::<pallas::Scalar>(&verify_args.proof_id)?;
LurkProof::verify_proof(&verify_args.proof_id)?;
}
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions src/cli/paths.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(not(target_arch = "wasm32"))]
pub mod cli {
pub mod non_wasm {
use anyhow::Result;

use std::{
Expand Down Expand Up @@ -38,8 +38,8 @@ pub mod cli {
lurk_dir().join(Path::new("repl-history"))
}

pub fn commitment_path(hash: &str) -> PathBuf {
commits_dir().join(Path::new(hash))
pub fn commitment_path(name: &str) -> PathBuf {
commits_dir().join(Path::new(name))
}

pub fn proof_path(name: &str) -> PathBuf {
Expand Down
Loading