|
| 1 | +use std::{fs, path::Path}; |
| 2 | + |
1 | 3 | use anyhow::Result; |
| 4 | +use binius_core::constraint_system::{ValueVec, ValuesData}; |
2 | 5 | use binius_frontend::{compiler::CircuitBuilder, stat::CircuitStat}; |
| 6 | +use binius_utils::serialization::SerializeBytes; |
3 | 7 | use clap::{Arg, Args, Command, FromArgMatches, Subcommand}; |
4 | 8 |
|
5 | 9 | use crate::{ExampleCircuit, prove_verify, setup}; |
6 | 10 |
|
| 11 | +/// Serialize a value implementing `SerializeBytes` and write it to the given path. |
| 12 | +fn write_serialized<T: SerializeBytes>(value: &T, path: &str) -> Result<()> { |
| 13 | + if let Some(parent) = Path::new(path).parent() |
| 14 | + && !parent.as_os_str().is_empty() |
| 15 | + { |
| 16 | + fs::create_dir_all(parent)?; |
| 17 | + } |
| 18 | + let mut buf: Vec<u8> = Vec::new(); |
| 19 | + value.serialize(&mut buf)?; |
| 20 | + fs::write(path, &buf)?; |
| 21 | + Ok(()) |
| 22 | +} |
| 23 | + |
7 | 24 | /// A CLI builder for circuit examples that handles all command-line parsing and execution. |
8 | 25 | /// |
9 | 26 | /// This provides a clean API for circuit examples where developers only need to: |
@@ -74,6 +91,27 @@ enum Commands { |
74 | 91 | #[command(flatten)] |
75 | 92 | params: CommandArgs, |
76 | 93 | }, |
| 94 | + |
| 95 | + /// Save constraint system, public witness, and non-public data to files if paths are provided |
| 96 | + Save { |
| 97 | + /// Output path for the constraint system binary |
| 98 | + #[arg(long = "cs-path")] |
| 99 | + cs_path: Option<String>, |
| 100 | + |
| 101 | + /// Output path for the public witness binary |
| 102 | + #[arg(long = "pub-witness-path")] |
| 103 | + pub_witness_path: Option<String>, |
| 104 | + |
| 105 | + /// Output path for the non-public data (witness + internal) binary |
| 106 | + #[arg(long = "non-pub-data-path")] |
| 107 | + non_pub_data_path: Option<String>, |
| 108 | + |
| 109 | + #[command(flatten)] |
| 110 | + params: CommandArgs, |
| 111 | + |
| 112 | + #[command(flatten)] |
| 113 | + instance: CommandArgs, |
| 114 | + }, |
77 | 115 | } |
78 | 116 |
|
79 | 117 | /// Wrapper for dynamic command arguments |
@@ -102,13 +140,15 @@ where |
102 | 140 | let composition_cmd = Self::build_composition_subcommand(); |
103 | 141 | let check_snapshot_cmd = Self::build_check_snapshot_subcommand(); |
104 | 142 | let bless_snapshot_cmd = Self::build_bless_snapshot_subcommand(); |
| 143 | + let save_cmd = Self::build_save_subcommand(); |
105 | 144 |
|
106 | 145 | let command = command |
107 | 146 | .subcommand(prove_cmd) |
108 | 147 | .subcommand(stat_cmd) |
109 | 148 | .subcommand(composition_cmd) |
110 | 149 | .subcommand(check_snapshot_cmd) |
111 | | - .subcommand(bless_snapshot_cmd); |
| 150 | + .subcommand(bless_snapshot_cmd) |
| 151 | + .subcommand(save_cmd); |
112 | 152 |
|
113 | 153 | // Also add top-level args for default prove behavior |
114 | 154 | let command = command.arg( |
@@ -171,6 +211,34 @@ where |
171 | 211 | E::Params::augment_args(cmd) |
172 | 212 | } |
173 | 213 |
|
| 214 | + fn build_save_subcommand() -> Command { |
| 215 | + let mut cmd = Command::new("save").about( |
| 216 | + "Save constraint system, public witness, and non-public data to files if paths are provided", |
| 217 | + ); |
| 218 | + cmd = cmd |
| 219 | + .arg( |
| 220 | + Arg::new("cs_path") |
| 221 | + .long("cs-path") |
| 222 | + .value_name("PATH") |
| 223 | + .help("Output path for the constraint system binary"), |
| 224 | + ) |
| 225 | + .arg( |
| 226 | + Arg::new("pub_witness_path") |
| 227 | + .long("pub-witness-path") |
| 228 | + .value_name("PATH") |
| 229 | + .help("Output path for the public witness binary"), |
| 230 | + ) |
| 231 | + .arg( |
| 232 | + Arg::new("non_pub_data_path") |
| 233 | + .long("non-pub-data-path") |
| 234 | + .value_name("PATH") |
| 235 | + .help("Output path for the non-public data (witness + internal) binary"), |
| 236 | + ); |
| 237 | + cmd = E::Params::augment_args(cmd); |
| 238 | + cmd = E::Instance::augment_args(cmd); |
| 239 | + cmd |
| 240 | + } |
| 241 | + |
174 | 242 | /// Set the about/description text for the command. |
175 | 243 | /// |
176 | 244 | /// This appears in the help output. |
@@ -212,6 +280,7 @@ where |
212 | 280 | Some(("bless-snapshot", sub_matches)) => { |
213 | 281 | Self::run_bless_snapshot_impl(sub_matches.clone(), circuit_name) |
214 | 282 | } |
| 283 | + Some(("save", sub_matches)) => Self::run_save(sub_matches.clone()), |
215 | 284 | Some((cmd, _)) => anyhow::bail!("Unknown subcommand: {}", cmd), |
216 | 285 | None => { |
217 | 286 | // No subcommand - default to prove behavior for backward compatibility |
@@ -319,6 +388,51 @@ where |
319 | 388 | Ok(()) |
320 | 389 | } |
321 | 390 |
|
| 391 | + fn run_save(matches: clap::ArgMatches) -> Result<()> { |
| 392 | + // Extract optional output paths |
| 393 | + let cs_path = matches.get_one::<String>("cs_path").cloned(); |
| 394 | + let pub_witness_path = matches.get_one::<String>("pub_witness_path").cloned(); |
| 395 | + let non_pub_data_path = matches.get_one::<String>("non_pub_data_path").cloned(); |
| 396 | + |
| 397 | + // If nothing to save, exit early |
| 398 | + if cs_path.is_none() && pub_witness_path.is_none() && non_pub_data_path.is_none() { |
| 399 | + tracing::info!("No output paths provided; nothing to save"); |
| 400 | + return Ok(()); |
| 401 | + } |
| 402 | + |
| 403 | + // Parse Params and Instance |
| 404 | + let params = E::Params::from_arg_matches(&matches)?; |
| 405 | + let instance = E::Instance::from_arg_matches(&matches)?; |
| 406 | + |
| 407 | + // Build circuit |
| 408 | + let mut builder = CircuitBuilder::new(); |
| 409 | + let example = E::build(params, &mut builder)?; |
| 410 | + let circuit = builder.build(); |
| 411 | + |
| 412 | + // Generate witness |
| 413 | + let mut filler = circuit.new_witness_filler(); |
| 414 | + example.populate_witness(instance, &mut filler)?; |
| 415 | + circuit.populate_wire_witness(&mut filler)?; |
| 416 | + let witness: ValueVec = filler.into_value_vec(); |
| 417 | + |
| 418 | + // Conditionally write artifacts |
| 419 | + if let Some(path) = cs_path.as_deref() { |
| 420 | + write_serialized(circuit.constraint_system(), path)?; |
| 421 | + } |
| 422 | + |
| 423 | + if let Some(path) = pub_witness_path.as_deref() { |
| 424 | + let data = ValuesData::from(witness.public()); |
| 425 | + write_serialized(&data, path)?; |
| 426 | + } |
| 427 | + |
| 428 | + if let Some(path) = non_pub_data_path.as_deref() { |
| 429 | + let data = ValuesData::from(witness.non_public()); |
| 430 | + write_serialized(&data, path)?; |
| 431 | + } |
| 432 | + |
| 433 | + Ok(()) |
| 434 | + } |
| 435 | + |
322 | 436 | /// Parse arguments and run the circuit example. |
323 | 437 | /// |
324 | 438 | /// This orchestrates the entire flow: |
|
0 commit comments