From cd43e4a4969835137e47a97cd3a6c932b1e99419 Mon Sep 17 00:00:00 2001 From: byteZorvin Date: Fri, 6 Sep 2024 18:00:30 +0530 Subject: [PATCH 1/3] add clap for args --- Cargo.lock | 193 ++++++++++++++++++++++++++ Cargo.toml | 1 + Makefile | 2 +- examples/dummy_snos_public_input.json | 22 +-- src/main.rs | 60 ++++---- 5 files changed, 242 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eeb63ae..7c47209 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,6 +37,55 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "anyhow" version = "1.0.86" @@ -253,6 +302,52 @@ dependencies = [ "inout", ] +[[package]] +name = "clap" +version = "4.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "clap_lex" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + [[package]] name = "constant_time_eq" version = "0.1.5" @@ -391,6 +486,12 @@ dependencies = [ "serde", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hex" version = "0.4.3" @@ -415,6 +516,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itertools" version = "0.10.5" @@ -675,6 +782,7 @@ dependencies = [ "bincode", "cairo-bootloader", "cairo-vm", + "clap", ] [[package]] @@ -895,6 +1003,12 @@ dependencies = [ "serde", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -1000,6 +1114,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "version_check" version = "0.9.5" @@ -1067,6 +1187,79 @@ version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + [[package]] name = "wyz" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index 66bacbb..9e9ef12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ cairo-vm = { git = "https://github.com/lambdaclass/cairo-vm", rev = "05352b1c678 bincode = { version = "2.0.0-rc.3", default-features = false, features = [ "serde", ] } +clap = { version = "4.5.17", features = ["derive"] } diff --git a/Makefile b/Makefile index 014b043..0cec6e1 100644 --- a/Makefile +++ b/Makefile @@ -80,7 +80,7 @@ run: compile run_bootloader: compile - cargo run -- $(COMPILED_OUTPUT) $(PUBLIC_INPUT) $(PRIVATE_INPUT) $(MEMORY_FILE) $(TRACE_FILE) + cargo run -- -c $(COMPILED_OUTPUT) -u $(PUBLIC_INPUT) -p $(PRIVATE_INPUT) -m $(MEMORY_FILE) -t $(TRACE_FILE) node format.js $(PUBLIC_INPUT) # Generate the proof diff --git a/examples/dummy_snos_public_input.json b/examples/dummy_snos_public_input.json index da6c1e7..24b8203 100644 --- a/examples/dummy_snos_public_input.json +++ b/examples/dummy_snos_public_input.json @@ -8,25 +8,25 @@ "begin_addr": 721, "stop_ptr": 2103 }, - "output": { - "begin_addr": 2103, - "stop_ptr": 2117 - }, - "pedersen": { - "begin_addr": 2117, - "stop_ptr": 2303 - }, "range_check": { "begin_addr": 3653, "stop_ptr": 3661 }, + "program": { + "begin_addr": 1, + "stop_ptr": 5 + }, "ecdsa": { "begin_addr": 4165, "stop_ptr": 4165 }, - "program": { - "begin_addr": 1, - "stop_ptr": 5 + "pedersen": { + "begin_addr": 2117, + "stop_ptr": 2303 + }, + "output": { + "begin_addr": 2103, + "stop_ptr": 2117 } }, "public_memory": [ diff --git a/src/main.rs b/src/main.rs index d334edb..22c232d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,12 +16,12 @@ use cairo_bootloader::{ PackedOutput, SimpleBootloaderInput, TaskSpec, }; use std::{ - env, io::{self, Write}, - // path::PathBuf, path::Path, }; +use clap::Parser; + fn cairo_run_bootloader_in_proof_mode( bootloader_program: &Program, tasks: Vec, @@ -101,31 +101,43 @@ impl FileWriter { } } -fn main() -> Result<(), Box> { - let args: Vec = env::args().collect(); +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + #[arg(short, long)] + compiled_program: String, - if args.len() != 6 { - eprintln!( - "Usage: cargo run --release -- ", - ); - std::process::exit(1); - } + #[arg(short='u', long)] + air_public_input: String, + + #[arg(short='p', long)] + air_private_input: String, + + #[arg(short, long)] + memory_file: String, - for (_, path) in args[1..].iter().enumerate() { + #[arg(short, long)] + trace: String, +} + +fn main() -> Result<(), Box> { + let args = Args::parse(); + + for path in [ + &args.compiled_program, + &args.air_public_input, + &args.air_private_input, + &args.memory_file, + &args.trace, + ] { if !Path::new(path).exists() { eprintln!("Error: File '{}' does not exist", path); - continue; + std::process::exit(1); } } - let compiled_program = args[1].clone(); - let air_public_input = args[2].clone(); - let air_private_input_file = args[3].clone(); - let memory_file = args[4].clone(); - let trace_file = args[5].clone(); - let bootloader_program = load_bootloader()?; - let dummy_snos_program = std::fs::read(compiled_program)?; + let dummy_snos_program = std::fs::read(args.compiled_program)?; let tasks = make_bootloader_tasks(&[&dummy_snos_program], &[])?; let mut runner = cairo_run_bootloader_in_proof_mode(&bootloader_program, tasks)?; @@ -133,22 +145,22 @@ fn main() -> Result<(), Box> { // Air public input { let json = runner.get_air_public_input().unwrap().serialize_json()?; - std::fs::write(air_public_input, json)?; + std::fs::write(args.air_public_input, json)?; } // Air private input { let json = runner .get_air_private_input() - .to_serializable(trace_file.clone(), memory_file.clone()) + .to_serializable(args.trace.clone(), args.memory_file.clone()) .serialize_json()?; // print!("{:?}", json); - std::fs::write(air_private_input_file, json)?; + std::fs::write(args.air_private_input, json)?; } // memory_file { - let memory_file = std::fs::File::create(memory_file)?; + let memory_file = std::fs::File::create(args.memory_file)?; let mut memory_writer = FileWriter::new(io::BufWriter::with_capacity(5 * 1024 * 1024, memory_file)); @@ -159,7 +171,7 @@ fn main() -> Result<(), Box> { // Trace file { let relocated_trace = runner.relocated_trace.clone().unwrap().clone(); - let trace_file = std::fs::File::create(trace_file)?; + let trace_file = std::fs::File::create(args.trace)?; let mut trace_writer = FileWriter::new(io::BufWriter::with_capacity(3 * 1024 * 1024, trace_file)); From 9b7259da721f6165db72732473b3307cc06e7f7f Mon Sep 17 00:00:00 2001 From: byteZorvin Date: Tue, 10 Sep 2024 14:49:55 +0530 Subject: [PATCH 2/3] Make them required --- src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index a421162..b8ab052 100644 --- a/src/main.rs +++ b/src/main.rs @@ -107,19 +107,19 @@ impl FileWriter { #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { - #[arg(short, long)] + #[arg(short, long, required = true)] compiled_program: String, - #[arg(short='u', long)] + #[arg(short='u', long, required = true)] air_public_input: String, - #[arg(short='p', long)] + #[arg(short='p', long, required = true)] air_private_input: String, - #[arg(short, long)] + #[arg(short, long, required = true)] memory_file: String, - #[arg(short, long)] + #[arg(short, long, required = true)] trace: String, } From 1fda30e352baebb8fd029f16ac686a612a25b286 Mon Sep 17 00:00:00 2001 From: byteZorvin Date: Tue, 10 Sep 2024 14:53:05 +0530 Subject: [PATCH 3/3] Test --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index b8ab052..2fc2df0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ fn cairo_run_bootloader_in_proof_mode( let cairo_run_config = CairoRunConfig { entrypoint: "main", trace_enabled: true, - relocate_mem: false, + relocate_mem: true, layout: LayoutName::small, proof_mode: true, secure_run: Some(true),