Skip to content

Commit

Permalink
Merge pull request #2 from Sindri-Labs/klm-introduce-blocking-methods
Browse files Browse the repository at this point in the history
Introduce blocking circuit create & prove
  • Loading branch information
katiemckeon authored Jan 14, 2025
2 parents 3af55af + 5a6571a commit ea66645
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 11 deletions.
46 changes: 41 additions & 5 deletions sindri-rs/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl SindriClient {
///
/// # Examples
///
/// ```
/// ```ignore
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::new(None, None);
Expand Down Expand Up @@ -298,6 +298,20 @@ impl SindriClient {
Ok(circuit_info)
}

/// Blocking version of `create_circuit`.
///
/// This method provides the same functionality as `create_circuit` but can be used
/// in synchronous contexts. It internally creates a runtime to execute the async operation.
pub fn create_circuit_blocking(
&self,
project: String,
tags: Option<Vec<String>>,
meta: Option<HashMap<String, String>>,
) -> Result<CircuitInfoResponse, Box<dyn std::error::Error>> {
let runtime = tokio::runtime::Runtime::new()?;
runtime.block_on(self.create_circuit(project, tags, meta))
}

/// Deletes a circuit by ID.
///
/// # Arguments
Expand Down Expand Up @@ -333,7 +347,7 @@ impl SindriClient {
///
/// # Examples
///
/// ```
/// ```ignore
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::new(None, None);
Expand Down Expand Up @@ -395,7 +409,7 @@ impl SindriClient {
///
/// # Examples
///
/// ```
/// ```ignore
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::new(None, None);
Expand Down Expand Up @@ -437,7 +451,7 @@ impl SindriClient {
///
/// # Examples
///
/// ```
/// ```ignore
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::new(None, None);
Expand Down Expand Up @@ -496,6 +510,28 @@ impl SindriClient {
Ok(proof_info)
}

/// Blocking version of `prove_circuit`.
///
/// This method provides the same functionality as `prove_circuit` but can be used
/// in synchronous contexts. It internally creates a runtime to execute the async operation.
pub fn prove_circuit_blocking(
&self,
circuit_id: &str,
proof_input: impl Into<ProofInput>,
meta: Option<HashMap<String, String>>,
verify: Option<bool>,
prover_implementation: Option<String>,
) -> Result<ProofInfoResponse, Box<dyn std::error::Error>> {
let runtime = tokio::runtime::Runtime::new()?;
runtime.block_on(self.prove_circuit(
circuit_id,
proof_input,
meta,
verify,
prover_implementation,
))
}

/// Deletes a proof by ID.
///
/// # Arguments
Expand Down Expand Up @@ -533,7 +569,7 @@ impl SindriClient {
///
/// # Examples
///
/// ```
/// ```ignore
/// use sindri_rs::client::SindriClient;
///
/// let client = SindriClient::new(None, None);
Expand Down
2 changes: 1 addition & 1 deletion sindri-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//!
//! Generate your first zero-knowledge proof in just a few lines of code:
//!
//! ```rust
//! ```ignore
//! use sindri_rs::client::SindriClient;
//!
//! let client = SindriClient::new(None, None);
Expand Down
48 changes: 43 additions & 5 deletions sindri-rs/tests/projects.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{
collections::{HashMap, HashSet},
fs,
io::Cursor,
};
use std::collections::{HashMap, HashSet};
#[cfg(not(any(feature = "record", feature = "replay")))]
use std::{fs, io::Cursor};

#[cfg(not(any(feature = "record", feature = "replay")))]
use flate2::read::GzDecoder;
#[cfg(not(any(feature = "record", feature = "replay")))]
use tar::Archive;
use tempfile::TempDir;

Expand Down Expand Up @@ -51,6 +51,44 @@ async fn test_create_circuit() {
assert_eq!(circom_info.num_outputs, Some(1));
}

#[test]
fn test_create_circuit_blocking() {
let (_temp_dir, dir_path) = factory::baby_circuit();

let client = SindriClient::new(None, None);

let test_tags = vec!["tag1".to_string(), "tag2".to_string()];
let test_meta = HashMap::from([
("key1".to_string(), "value1".to_string()),
("key2".to_string(), "value2".to_string()),
]);

let result = client
.create_circuit_blocking(
dir_path.to_string_lossy().to_string(),
Some(test_tags.clone()),
Some(test_meta.clone()),
);

assert!(result.is_ok());
let circuit = result.unwrap();

assert_eq!(*circuit.status(), JobStatus::Ready);
assert_eq!(circuit.meta(), &test_meta);
assert_eq!(
circuit.tags().iter().collect::<HashSet<_>>(),
test_tags.iter().collect::<HashSet<_>>()
);

let circom_info = match circuit {
CircuitInfoResponse::Circom(circom_info) => circom_info,
_ => panic!("Circuit is not of Circom type"),
};

assert_eq!(circom_info.proving_scheme, "groth16");
assert_eq!(circom_info.num_outputs, Some(1));
}

#[tokio::test]
async fn test_delete_circuit() {
let (_temp_dir, dir_path) = factory::baby_circuit();
Expand Down
36 changes: 36 additions & 0 deletions sindri-rs/tests/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ async fn test_create_proof_basic() {
assert_eq!(proof.meta, test_meta);
}

#[test]
fn test_create_proof_basic_blocking() {
let (_temp_dir, dir_path) = factory::baby_circuit();

let client = SindriClient::new(None, None);

// Create circuit first (using blocking version for consistency)
let circuit = client
.create_circuit_blocking(
dir_path.to_string_lossy().to_string(),
Some(vec!["prove_basic_blocking".to_string()]),
None,
)
.unwrap();
let circuit_identifier = circuit.id();

let input = r#"{"a": 1, "b": 2}"#;
let test_meta = HashMap::from([
("key1".to_string(), "value1".to_string()),
("key2".to_string(), "value2".to_string()),
]);
let result = client.prove_circuit_blocking(
circuit_identifier,
input,
Some(test_meta.clone()),
None,
None,
);

assert!(result.is_ok());
let proof = result.unwrap();

assert!(!proof.proof_id.is_empty());
assert_eq!(proof.meta, test_meta);
}

#[tokio::test]
async fn test_create_proof_input_modes() {
let (_temp_dir, dir_path) = factory::baby_circuit();
Expand Down

0 comments on commit ea66645

Please sign in to comment.