use std::{
path::{Path, PathBuf},
sync::Arc,
};
use crate::{
OwnershipProver, OwnershipVerifier,
artifacts::{ZkArtifactError, ZkArtifactKind, ZkArtifactSource},
proof::{
CircomGroth16Material, load_nullifier_material_from_paths, load_query_material_from_paths,
},
};
use crate::ownership_proof::{load_ownership_prover_from_path, load_ownership_verifier_from_path};
#[derive(Debug, Default, Clone)]
pub struct FileSystemZkArtifacts {
query_zkey_path: Option<PathBuf>,
query_graph_path: Option<PathBuf>,
nullifier_zkey_path: Option<PathBuf>,
nullifier_graph_path: Option<PathBuf>,
ownership_prover_path: Option<PathBuf>,
ownership_verifier_path: Option<PathBuf>,
}
impl FileSystemZkArtifacts {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_query_paths(
mut self,
zkey_path: impl Into<PathBuf>,
graph_path: impl Into<PathBuf>,
) -> Self {
self.query_zkey_path = Some(zkey_path.into());
self.query_graph_path = Some(graph_path.into());
self
}
#[must_use]
pub fn with_nullifier_paths(
mut self,
zkey_path: impl Into<PathBuf>,
graph_path: impl Into<PathBuf>,
) -> Self {
self.nullifier_zkey_path = Some(zkey_path.into());
self.nullifier_graph_path = Some(graph_path.into());
self
}
#[must_use]
pub fn with_ownership_paths(
mut self,
prover_path: impl Into<PathBuf>,
verifier_path: impl Into<PathBuf>,
) -> Self {
self.ownership_prover_path = Some(prover_path.into());
self.ownership_verifier_path = Some(verifier_path.into());
self
}
#[must_use]
pub fn with_query_zkey_path(mut self, path: impl Into<PathBuf>) -> Self {
self.query_zkey_path = Some(path.into());
self
}
#[must_use]
pub fn with_query_graph_path(mut self, path: impl Into<PathBuf>) -> Self {
self.query_graph_path = Some(path.into());
self
}
#[must_use]
pub fn with_nullifier_zkey_path(mut self, path: impl Into<PathBuf>) -> Self {
self.nullifier_zkey_path = Some(path.into());
self
}
#[must_use]
pub fn with_nullifier_graph_path(mut self, path: impl Into<PathBuf>) -> Self {
self.nullifier_graph_path = Some(path.into());
self
}
#[must_use]
pub fn with_ownership_prover_path(mut self, path: impl Into<PathBuf>) -> Self {
self.ownership_prover_path = Some(path.into());
self
}
#[must_use]
pub fn with_ownership_verifier_path(mut self, path: impl Into<PathBuf>) -> Self {
self.ownership_verifier_path = Some(path.into());
self
}
}
impl ZkArtifactSource for FileSystemZkArtifacts {
fn query_material(&self) -> Result<Arc<CircomGroth16Material>, ZkArtifactError> {
let kind = ZkArtifactKind::QueryMaterial;
load_query_material_from_paths(
required_path(&self.query_zkey_path, kind, "query zkey path not set")?,
required_path(
&self.query_graph_path,
kind,
"query witness graph path not set",
)?,
)
.map(Arc::new)
.map_err(|e| ZkArtifactError::load(kind, e))
}
fn nullifier_material(&self) -> Result<Arc<CircomGroth16Material>, ZkArtifactError> {
let kind = ZkArtifactKind::NullifierMaterial;
load_nullifier_material_from_paths(
required_path(
&self.nullifier_zkey_path,
kind,
"nullifier zkey path not set",
)?,
required_path(
&self.nullifier_graph_path,
kind,
"nullifier witness graph path not set",
)?,
)
.map(Arc::new)
.map_err(|e| ZkArtifactError::load(kind, e))
}
fn ownership_prover(&self) -> Result<OwnershipProver, ZkArtifactError> {
let kind = ZkArtifactKind::OwnershipProver;
load_ownership_prover_from_path(required_path(
&self.ownership_prover_path,
kind,
"ownership prover path not set",
)?)
.map_err(|e| ZkArtifactError::load(kind, e))
}
fn ownership_verifier(&self) -> Result<OwnershipVerifier, ZkArtifactError> {
let kind = ZkArtifactKind::OwnershipVerifier;
load_ownership_verifier_from_path(required_path(
&self.ownership_verifier_path,
kind,
"ownership verifier path not set",
)?)
.map_err(|e| ZkArtifactError::load(kind, e))
}
}
fn required_path<'a>(
path: &'a Option<PathBuf>,
kind: ZkArtifactKind,
detail: &str,
) -> Result<&'a Path, ZkArtifactError> {
path.as_deref().ok_or_else(|| ZkArtifactError::NotProvided {
kind,
detail: Some(detail.to_owned()),
})
}