use ark_bn254::Bn254;
use ark_groth16::ProvingKey;
use ark_serialize::CanonicalDeserialize;
use std::path::Path;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ZkeyError {
#[error("File not found: {0}")]
FileNotFound(String),
#[error("Invalid format: {0}")]
InvalidFormat(String),
#[error("Parse error: {0}")]
ParseError(String),
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
}
pub fn load_ark_proving_key(path: &str) -> Result<ProvingKey<Bn254>, ZkeyError> {
if !Path::new(path).exists() {
return Err(ZkeyError::FileNotFound(path.to_string()));
}
let bytes = std::fs::read(path)?;
let pk = ProvingKey::<Bn254>::deserialize_uncompressed(&bytes[..])
.map_err(|e| ZkeyError::ParseError(format!("{:?}", e)))?;
Ok(pk)
}
pub fn has_ark_proving_key(zkey_path: &str) -> bool {
let ark_path = format!("{}.ark", zkey_path);
Path::new(&ark_path).exists()
}
#[allow(unused)]
pub fn parse_snarkjs_zkey(_path: &str) -> Result<ProvingKey<Bn254>, ZkeyError> {
Err(ZkeyError::InvalidFormat(
"Direct .zkey parsing not implemented. Use pre-converted .zkey.ark file.".into()
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_has_ark_key() {
assert!(!has_ark_proving_key("/nonexistent/path"));
}
}