From 128e8bc72a72fa293e65651d5c9ad41a5f61de10 Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Fri, 30 Aug 2024 14:20:46 -0600 Subject: [PATCH 1/6] feat: combined CLI --- Cargo.lock | 22 ++--- Cargo.toml | 8 +- notes.md | 51 ----------- src/bin/witness.rs | 130 --------------------------- src/{bin/codegen.rs => extractor.rs} | 48 ++-------- src/main.rs | 82 +++++++++++++++++ src/witness.rs | 59 ++++++++++++ 7 files changed, 163 insertions(+), 237 deletions(-) delete mode 100644 notes.md delete mode 100644 src/bin/witness.rs rename src/{bin/codegen.rs => extractor.rs} (95%) create mode 100644 src/main.rs create mode 100644 src/witness.rs diff --git a/Cargo.lock b/Cargo.lock index 2479f30..f7d2691 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -53,9 +53,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.14" +version = "4.5.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c937d4061031a6d0c8da4b9a4f98a172fc2976dfb1c19213a9cf7d0d3c837e36" +checksum = "ed6719fffa43d0d87e5fd8caeab59be1554fb028cd30edc88fc4369b17971019" dependencies = [ "clap_builder", "clap_derive", @@ -63,9 +63,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.14" +version = "4.5.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85379ba512b21a328adf887e85f7742d12e96eb31f3ef077df4ffc26b506ffed" +checksum = "216aec2b177652e3846684cbfe25c9964d18ec45234f0f5da5157b207ed1aab6" dependencies = [ "anstream", "anstyle", @@ -147,18 +147,18 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "serde" -version = "1.0.205" +version = "1.0.209" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e33aedb1a7135da52b7c21791455563facbbcc43d0f0f66165b42c21b3dfb150" +checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.205" +version = "1.0.209" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "692d6f5ac90220161d6774db30c662202721e64aed9058d2c394f451261420c1" +checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" dependencies = [ "proc-macro2", "quote", @@ -167,9 +167,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.122" +version = "1.0.127" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "784b6203951c57ff748476b126ccb5e8e2959a5c19e5c617ab1956be3dbc68da" +checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad" dependencies = [ "itoa", "memchr", @@ -280,7 +280,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] -name = "witness" +name = "wpbuild" version = "0.0.0" dependencies = [ "clap", diff --git a/Cargo.toml b/Cargo.toml index 25d2171..dbfe087 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] -name = "witness" +name = "wpbuild" edition = "2021" [dependencies] -serde = { version = "1.0.204", features = ["derive"] } -serde_json = "1.0.120" -clap = { version = "4.5.14", features = ["derive"] } +serde = { version = "1.0.209", features = ["derive"] } +serde_json = "1.0.127" +clap = { version = "4.5.16", features = ["derive"] } diff --git a/notes.md b/notes.md deleted file mode 100644 index ba1c261..0000000 --- a/notes.md +++ /dev/null @@ -1,51 +0,0 @@ -# Notes - -## TODOs -### JSON Types -- [x] Object -- [x] String -- [x] Array -- [x] Number -- [ ] Boolean -- [ ] Null - -Parsing null and bool need to do some kind of look ahead parsing. To handle numbers properly we also probably need that actually since we need to look ahead to where we get white space. -Need to look ahead for `true` and `false` for example to ensure we get a full match, or we fail or something. Lookaehad might be overkill, but yeah. - -#### Numbers -Numbers can have `e` and decimal `.` in them. Riperoni. - -### string escape -shouldn't be too hard, just add one more state variable `escaping` that is only enabled when parsing a string and can only be toggled -- next state will always have to set back to 0. - -This could also allow for parsing unicode - -### Other thoughts - - Pointer may not actually be necessary, because it just points to the first unallocated position in the stac - - How do we know how tall to make the stack? Open braces `{`, open brackets `[` and colons `:` all push onto the stack. - - We might not actually need to push the stack higher with a colon, instead we could push state into the second slot of the stack like we do with commas inside of arrays. - -## Expected Output -> This is old at this point, but we should update it. -``` -Notes: for `test.json` -xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx - POINTER | Read In: | STATE -------------------------------------------------- -State[1] | { | PARSING TO KEY -------------------------------------------------- -State[7] | " | INSIDE KEY -------------------------------------------------- -State[12]| " | NOT INSIDE KEY -------------------------------------------------- -State[13]| : | PARSING TO VALUE -------------------------------------------------- -State[15]| " | INSIDE VALUE -------------------------------------------------- -State[19]| " | NOT INSIDE VALUE -------------------------------------------------- -State[20]| " | COMPLETE WITH KV PARSING -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -State[20].next_tree_depth == 0 | VALID JSON -xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -``` \ No newline at end of file diff --git a/src/bin/witness.rs b/src/bin/witness.rs deleted file mode 100644 index bacabe0..0000000 --- a/src/bin/witness.rs +++ /dev/null @@ -1,130 +0,0 @@ -use clap::{Parser, Subcommand}; -use serde_json::Value; -use std::io::Write; -use std::path::PathBuf; - -#[derive(Parser, Debug)] -#[command(name = "witness")] -struct Args { - #[command(subcommand)] - command: Command, - - /// Output directory (will be created if it doesn't exist) - #[arg(global = true, long, default_value = ".")] - output_dir: PathBuf, - - /// Output filename (will be created if it doesn't exist) - #[arg(global = true, long, default_value = "output.json")] - output_filename: String, -} - -#[derive(Subcommand, Debug)] -enum Command { - Json { - /// Path to the JSON file - #[arg(short, long)] - input_file: PathBuf, - - /// Keys to extract (can be specified multiple times) - #[arg(short, long)] - keys: Vec, - }, - Http { - /// Path to the HTTP request file - #[arg(short, long)] - input_file: PathBuf, - }, -} - -#[derive(serde::Serialize)] -pub struct Witness { - #[serde(flatten)] - keys: serde_json::Map, - data: Vec, -} - -pub fn main() -> Result<(), Box> { - let args = Args::parse(); - - let (data, keys_map) = match &args.command { - Command::Json { input_file, keys } => { - let data = std::fs::read(input_file)?; - let mut keys_map = serde_json::Map::new(); - for (index, key) in keys.iter().enumerate() { - keys_map.insert( - format!("key{}", index + 1), - Value::Array( - key.as_bytes() - .iter() - .map(|x| serde_json::json!(x)) - .collect(), - ), - ); - } - (data, keys_map) - } - Command::Http { input_file } => { - let mut data = std::fs::read(input_file)?; - let mut i = 0; - while i < data.len() { - if data[i] == 10 && (i == 0 || data[i - 1] != 13) { - data.insert(i, 13); - i += 2; - } else { - i += 1; - } - } - let keys_map = serde_json::Map::new(); - (data, keys_map) - } - }; - - let witness = Witness { - keys: keys_map, - data: data.clone(), - }; - - if !args.output_dir.exists() { - std::fs::create_dir_all(&args.output_dir)?; - } - - let output_file = args.output_dir.join(args.output_filename); - let mut file = std::fs::File::create(output_file)?; - file.write_all(serde_json::to_string_pretty(&witness)?.as_bytes())?; - - // Prepare lines to print - let mut lines = Vec::new(); - match &args.command { - Command::Json { keys, .. } => { - lines.push(String::from("Key lengths:")); - for (index, key) in keys.iter().enumerate() { - lines.push(format!("key{} length: {}", index + 1, key.len())); - } - } - Command::Http { .. } => { - lines.push(String::from("HTTP request processed")); - } - } - lines.push(format!("Data length: {}", data.len())); - - // Print the output inside a nicely formatted box - print_boxed_output(lines); - - Ok(()) -} - -fn print_boxed_output(lines: Vec) { - // Determine the maximum length of the lines - let max_length = lines.iter().map(|line| line.len()).max().unwrap_or(0); - - // Characters for the box - let top_border = format!("┌{}┐", "─".repeat(max_length + 2)); - let bottom_border = format!("└{}┘", "─".repeat(max_length + 2)); - - // Print the box with content - println!("{}", top_border); - for line in lines { - println!("│ {:, - value_type: ValueType, -} +use super::*; const PRAGMA: &str = "pragma circom 2.1.9;\n\n"; @@ -500,10 +468,8 @@ fn parse_json_request( Ok(()) } -pub fn main() -> Result<(), Box> { - let args = Args::parse(); - - let data = std::fs::read(&args.json_file)?; +pub fn extractor(args: ExtractorArgs) -> Result<(), Box> { + let data = std::fs::read(&args.template)?; let json_data: Data = serde_json::from_slice(&data)?; parse_json_request(json_data, args.output_filename)?; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1b8a79b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,82 @@ +use clap::{Parser, Subcommand}; +use serde::{Deserialize, Serialize}; +use std::{error::Error, path::PathBuf}; + +pub mod extractor; +pub mod witness; + +#[derive(Parser, Debug)] +#[command(name = "wpbuild")] +pub struct Args { + #[command(subcommand)] + command: Command, +} + +#[derive(Subcommand, Debug)] +pub enum Command { + Witness(WitnessArgs), + Extractor(ExtractorArgs), +} + +#[derive(Parser, Debug)] +pub struct WitnessArgs { + #[command(subcommand)] + subcommand: WitnessSubcommand, + + /// Path to the JSON file + #[arg(global = true, long)] + input_file: PathBuf, + + /// Output directory (will be created if it doesn't exist) + #[arg(global = true, long, default_value = ".")] + output_dir: PathBuf, + + /// Output filename (will be created if it doesn't exist) + #[arg(global = true, long, default_value = "output.json")] + output_filename: String, +} + +#[derive(Subcommand, Debug)] +pub enum WitnessSubcommand { + Json, + Http, +} + +#[derive(Parser, Debug)] +pub struct ExtractorArgs { + /// Path to the JSON file + #[arg(long)] + template: PathBuf, + + /// Output circuit file name + #[arg(long, default_value = "extractor")] + output_filename: String, +} + +#[derive(Debug, Deserialize)] +enum ValueType { + #[serde(rename = "string")] + String, + #[serde(rename = "number")] + Number, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(untagged)] +enum Key { + String(String), + Num(i64), +} + +#[derive(Debug, Deserialize)] +struct Data { + keys: Vec, + value_type: ValueType, +} + +pub fn main() -> Result<(), Box> { + match Args::parse().command { + Command::Extractor(args) => extractor::extractor(args), + Command::Witness(args) => witness::witness(args), + } +} diff --git a/src/witness.rs b/src/witness.rs new file mode 100644 index 0000000..8375627 --- /dev/null +++ b/src/witness.rs @@ -0,0 +1,59 @@ +use super::*; +use std::io::Write; + +#[derive(serde::Serialize)] +pub struct Witness(Vec); + +pub fn witness(args: WitnessArgs) -> Result<(), Box> { + let data = match &args.subcommand { + WitnessSubcommand::Json => std::fs::read(args.input_file)?, + WitnessSubcommand::Http => { + let mut data = std::fs::read(args.input_file)?; + let mut i = 0; + while i < data.len() { + if data[i] == 10 && (i == 0 || data[i - 1] != 13) { + data.insert(i, 13); + i += 2; + } else { + i += 1; + } + } + data + } + }; + + let witness = Witness(data.clone()); + + if !args.output_dir.exists() { + std::fs::create_dir_all(&args.output_dir)?; + } + + let output_file = args.output_dir.join(args.output_filename); + let mut file = std::fs::File::create(output_file)?; + file.write_all(serde_json::to_string_pretty(&witness)?.as_bytes())?; + + // Prepare lines to print + let mut lines = Vec::new(); + lines.push(format!("Data length: {}", data.len())); + + // Print the output inside a nicely formatted box + print_boxed_output(lines); + + Ok(()) +} + +fn print_boxed_output(lines: Vec) { + // Determine the maximum length of the lines + let max_length = lines.iter().map(|line| line.len()).max().unwrap_or(0); + + // Characters for the box + let top_border = format!("┌{}┐", "─".repeat(max_length + 2)); + let bottom_border = format!("└{}┘", "─".repeat(max_length + 2)); + + // Print the box with content + println!("{}", top_border); + for line in lines { + println!("│ {: Date: Fri, 30 Aug 2024 14:45:41 -0600 Subject: [PATCH 2/6] refactor and add example http lock --- .../test/codegen => extractor}/two_keys.json | 0 .../value_array_nested.json | 0 .../value_array_number.json | 0 .../value_array_object.json | 0 .../value_array_string.json | 0 .../codegen => extractor}/value_number.json | 0 .../codegen => extractor}/value_object.json | 0 .../codegen => extractor}/value_string.json | 0 examples/lockfile/test.lock.json | 32 + inputs/search/witness.json | 803 ------------------ 10 files changed, 32 insertions(+), 803 deletions(-) rename examples/{json/test/codegen => extractor}/two_keys.json (100%) rename examples/{json/test/codegen => extractor}/value_array_nested.json (100%) rename examples/{json/test/codegen => extractor}/value_array_number.json (100%) rename examples/{json/test/codegen => extractor}/value_array_object.json (100%) rename examples/{json/test/codegen => extractor}/value_array_string.json (100%) rename examples/{json/test/codegen => extractor}/value_number.json (100%) rename examples/{json/test/codegen => extractor}/value_object.json (100%) rename examples/{json/test/codegen => extractor}/value_string.json (100%) create mode 100644 examples/lockfile/test.lock.json delete mode 100644 inputs/search/witness.json diff --git a/examples/json/test/codegen/two_keys.json b/examples/extractor/two_keys.json similarity index 100% rename from examples/json/test/codegen/two_keys.json rename to examples/extractor/two_keys.json diff --git a/examples/json/test/codegen/value_array_nested.json b/examples/extractor/value_array_nested.json similarity index 100% rename from examples/json/test/codegen/value_array_nested.json rename to examples/extractor/value_array_nested.json diff --git a/examples/json/test/codegen/value_array_number.json b/examples/extractor/value_array_number.json similarity index 100% rename from examples/json/test/codegen/value_array_number.json rename to examples/extractor/value_array_number.json diff --git a/examples/json/test/codegen/value_array_object.json b/examples/extractor/value_array_object.json similarity index 100% rename from examples/json/test/codegen/value_array_object.json rename to examples/extractor/value_array_object.json diff --git a/examples/json/test/codegen/value_array_string.json b/examples/extractor/value_array_string.json similarity index 100% rename from examples/json/test/codegen/value_array_string.json rename to examples/extractor/value_array_string.json diff --git a/examples/json/test/codegen/value_number.json b/examples/extractor/value_number.json similarity index 100% rename from examples/json/test/codegen/value_number.json rename to examples/extractor/value_number.json diff --git a/examples/json/test/codegen/value_object.json b/examples/extractor/value_object.json similarity index 100% rename from examples/json/test/codegen/value_object.json rename to examples/extractor/value_object.json diff --git a/examples/json/test/codegen/value_string.json b/examples/extractor/value_string.json similarity index 100% rename from examples/json/test/codegen/value_string.json rename to examples/extractor/value_string.json diff --git a/examples/lockfile/test.lock.json b/examples/lockfile/test.lock.json new file mode 100644 index 0000000..ccbf5f1 --- /dev/null +++ b/examples/lockfile/test.lock.json @@ -0,0 +1,32 @@ +{ + "request": { + "method": "GET", + "target": "/api", + "version": "HTTP/1.1", + "headers": [ + [ + "Accept", + "application/json" + ], + [ + "Host", + "localhost" + ] + ], + "Host": "localhost" + }, + "response": { + "version": "HTTP/1.1", + "status": "200", + "headers": [ + [ + "Content-Type", + "application/json" + ], + [ + "Content-Length", + 19 + ] + ] + } +} \ No newline at end of file diff --git a/inputs/search/witness.json b/inputs/search/witness.json deleted file mode 100644 index c69c0a8..0000000 --- a/inputs/search/witness.json +++ /dev/null @@ -1,803 +0,0 @@ -{ - "key": [ - 34, - 103, - 108, - 111, - 115, - 115, - 97, - 114, - 121, - 34 - ], - "data": [ - 123, - 10, - 32, - 32, - 32, - 32, - 34, - 103, - 108, - 111, - 115, - 115, - 97, - 114, - 121, - 34, - 58, - 32, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 116, - 105, - 116, - 108, - 101, - 34, - 58, - 32, - 34, - 101, - 120, - 97, - 109, - 112, - 108, - 101, - 32, - 103, - 108, - 111, - 115, - 115, - 97, - 114, - 121, - 34, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 71, - 108, - 111, - 115, - 115, - 68, - 105, - 118, - 34, - 58, - 32, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 116, - 105, - 116, - 108, - 101, - 34, - 58, - 32, - 34, - 83, - 34, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 71, - 108, - 111, - 115, - 115, - 76, - 105, - 115, - 116, - 34, - 58, - 32, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 71, - 108, - 111, - 115, - 115, - 69, - 110, - 116, - 114, - 121, - 34, - 58, - 32, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 73, - 68, - 34, - 58, - 32, - 34, - 83, - 71, - 77, - 76, - 34, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 83, - 111, - 114, - 116, - 65, - 115, - 34, - 58, - 32, - 34, - 83, - 71, - 77, - 76, - 34, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 71, - 108, - 111, - 115, - 115, - 84, - 101, - 114, - 109, - 34, - 58, - 32, - 34, - 83, - 116, - 97, - 110, - 100, - 97, - 114, - 100, - 32, - 71, - 101, - 110, - 101, - 114, - 97, - 108, - 105, - 122, - 101, - 100, - 32, - 77, - 97, - 114, - 107, - 117, - 112, - 32, - 76, - 97, - 110, - 103, - 117, - 97, - 103, - 101, - 34, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 65, - 99, - 114, - 111, - 110, - 121, - 109, - 34, - 58, - 32, - 34, - 83, - 71, - 77, - 76, - 34, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 65, - 98, - 98, - 114, - 101, - 118, - 34, - 58, - 32, - 34, - 73, - 83, - 79, - 32, - 56, - 56, - 55, - 57, - 58, - 49, - 57, - 56, - 54, - 34, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 71, - 108, - 111, - 115, - 115, - 68, - 101, - 102, - 34, - 58, - 32, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 112, - 97, - 114, - 97, - 34, - 58, - 32, - 34, - 65, - 32, - 109, - 101, - 116, - 97, - 45, - 109, - 97, - 114, - 107, - 117, - 112, - 32, - 108, - 97, - 110, - 103, - 117, - 97, - 103, - 101, - 44, - 32, - 117, - 115, - 101, - 100, - 32, - 116, - 111, - 32, - 99, - 114, - 101, - 97, - 116, - 101, - 32, - 109, - 97, - 114, - 107, - 117, - 112, - 32, - 108, - 97, - 110, - 103, - 117, - 97, - 103, - 101, - 115, - 32, - 115, - 117, - 99, - 104, - 32, - 97, - 115, - 32, - 68, - 111, - 99, - 66, - 111, - 111, - 107, - 46, - 34, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 71, - 108, - 111, - 115, - 115, - 83, - 101, - 101, - 65, - 108, - 115, - 111, - 34, - 58, - 32, - 91, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 71, - 77, - 76, - 34, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 88, - 77, - 76, - 34, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 93, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 125, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 34, - 71, - 108, - 111, - 115, - 115, - 83, - 101, - 101, - 34, - 58, - 32, - 34, - 109, - 97, - 114, - 107, - 117, - 112, - 34, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 125, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 125, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 125, - 10, - 32, - 32, - 32, - 32, - 125, - 10, - 125 - ] -} \ No newline at end of file From d0a22c0bdf03140274fa9c2a0754215b122d4c51 Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Fri, 30 Aug 2024 14:55:13 -0600 Subject: [PATCH 3/6] http lock example --- src/extractor.rs | 21 +++++++++++++++++++++ src/http_lock.rs | 34 ++++++++++++++++++++++++++++++++++ src/main.rs | 29 +++++++++++------------------ 3 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 src/http_lock.rs diff --git a/src/extractor.rs b/src/extractor.rs index 2bcac36..7b563cb 100644 --- a/src/extractor.rs +++ b/src/extractor.rs @@ -7,6 +7,27 @@ use super::*; const PRAGMA: &str = "pragma circom 2.1.9;\n\n"; +#[derive(Debug, Deserialize)] +pub enum ValueType { + #[serde(rename = "string")] + String, + #[serde(rename = "number")] + Number, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(untagged)] +pub enum Key { + String(String), + Num(i64), +} + +#[derive(Debug, Deserialize)] +pub struct Data { + keys: Vec, + value_type: ValueType, +} + fn extract_string(data: Data, circuit_buffer: &mut String) { *circuit_buffer += "template ExtractStringValue(DATA_BYTES, MAX_STACK_HEIGHT, "; for (i, key) in data.keys.iter().enumerate() { diff --git a/src/http_lock.rs b/src/http_lock.rs new file mode 100644 index 0000000..7bf2c24 --- /dev/null +++ b/src/http_lock.rs @@ -0,0 +1,34 @@ +use super::*; + +#[derive(Debug, Serialize, Deserialize)] +struct HttpData { + request: Request, + response: Response, +} + +#[derive(Debug, Serialize, Deserialize)] +struct Request { + method: String, + target: String, + version: String, + headers: Vec<(String, String)>, + #[serde(rename = "Host")] + host: String, +} + +#[derive(Debug, Serialize, Deserialize)] +struct Response { + version: String, + status: String, + headers: Vec<(String, serde_json::Value)>, +} + +// TODO: This needs to codegen a circuit now. +pub fn http_lock(args: HttpLockArgs) -> Result<(), Box> { + let data = std::fs::read(&args.lockfile)?; + let http_data: HttpData = serde_json::from_slice(&data)?; + + dbg!(http_data); + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 1b8a79b..2312c5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize}; use std::{error::Error, path::PathBuf}; pub mod extractor; +pub mod http_lock; pub mod witness; #[derive(Parser, Debug)] @@ -16,6 +17,7 @@ pub struct Args { pub enum Command { Witness(WitnessArgs), Extractor(ExtractorArgs), + HttpLock(HttpLockArgs), } #[derive(Parser, Debug)] @@ -53,30 +55,21 @@ pub struct ExtractorArgs { output_filename: String, } -#[derive(Debug, Deserialize)] -enum ValueType { - #[serde(rename = "string")] - String, - #[serde(rename = "number")] - Number, -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(untagged)] -enum Key { - String(String), - Num(i64), -} +#[derive(Parser, Debug)] +pub struct HttpLockArgs { + /// Path to the JSON file + #[arg(long)] + lockfile: PathBuf, -#[derive(Debug, Deserialize)] -struct Data { - keys: Vec, - value_type: ValueType, + /// Output circuit file name + #[arg(long, default_value = "extractor")] + output_filename: String, } pub fn main() -> Result<(), Box> { match Args::parse().command { Command::Extractor(args) => extractor::extractor(args), Command::Witness(args) => witness::witness(args), + Command::HttpLock(args) => http_lock::http_lock(args), } } From ee527ac2c7c43a724104c33e5d82965a96d4cca1 Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Tue, 3 Sep 2024 10:17:30 -0600 Subject: [PATCH 4/6] include `search/witness.json` --- .gitignore | 3 +- inputs/search/witness.json | 803 +++++++++++++++++++++++++++++++++++++ 2 files changed, 805 insertions(+), 1 deletion(-) create mode 100644 inputs/search/witness.json diff --git a/.gitignore b/.gitignore index bbed9c8..faa5f81 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ circuits/test/*.circom circuits/main/* # Rust generated -inputs/**/*.json \ No newline at end of file +inputs/**/*.json +!inputs/search/witness.json \ No newline at end of file diff --git a/inputs/search/witness.json b/inputs/search/witness.json new file mode 100644 index 0000000..c69c0a8 --- /dev/null +++ b/inputs/search/witness.json @@ -0,0 +1,803 @@ +{ + "key": [ + 34, + 103, + 108, + 111, + 115, + 115, + 97, + 114, + 121, + 34 + ], + "data": [ + 123, + 10, + 32, + 32, + 32, + 32, + 34, + 103, + 108, + 111, + 115, + 115, + 97, + 114, + 121, + 34, + 58, + 32, + 123, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 116, + 105, + 116, + 108, + 101, + 34, + 58, + 32, + 34, + 101, + 120, + 97, + 109, + 112, + 108, + 101, + 32, + 103, + 108, + 111, + 115, + 115, + 97, + 114, + 121, + 34, + 44, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 71, + 108, + 111, + 115, + 115, + 68, + 105, + 118, + 34, + 58, + 32, + 123, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 116, + 105, + 116, + 108, + 101, + 34, + 58, + 32, + 34, + 83, + 34, + 44, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 71, + 108, + 111, + 115, + 115, + 76, + 105, + 115, + 116, + 34, + 58, + 32, + 123, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 71, + 108, + 111, + 115, + 115, + 69, + 110, + 116, + 114, + 121, + 34, + 58, + 32, + 123, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 73, + 68, + 34, + 58, + 32, + 34, + 83, + 71, + 77, + 76, + 34, + 44, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 83, + 111, + 114, + 116, + 65, + 115, + 34, + 58, + 32, + 34, + 83, + 71, + 77, + 76, + 34, + 44, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 71, + 108, + 111, + 115, + 115, + 84, + 101, + 114, + 109, + 34, + 58, + 32, + 34, + 83, + 116, + 97, + 110, + 100, + 97, + 114, + 100, + 32, + 71, + 101, + 110, + 101, + 114, + 97, + 108, + 105, + 122, + 101, + 100, + 32, + 77, + 97, + 114, + 107, + 117, + 112, + 32, + 76, + 97, + 110, + 103, + 117, + 97, + 103, + 101, + 34, + 44, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 65, + 99, + 114, + 111, + 110, + 121, + 109, + 34, + 58, + 32, + 34, + 83, + 71, + 77, + 76, + 34, + 44, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 65, + 98, + 98, + 114, + 101, + 118, + 34, + 58, + 32, + 34, + 73, + 83, + 79, + 32, + 56, + 56, + 55, + 57, + 58, + 49, + 57, + 56, + 54, + 34, + 44, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 71, + 108, + 111, + 115, + 115, + 68, + 101, + 102, + 34, + 58, + 32, + 123, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 112, + 97, + 114, + 97, + 34, + 58, + 32, + 34, + 65, + 32, + 109, + 101, + 116, + 97, + 45, + 109, + 97, + 114, + 107, + 117, + 112, + 32, + 108, + 97, + 110, + 103, + 117, + 97, + 103, + 101, + 44, + 32, + 117, + 115, + 101, + 100, + 32, + 116, + 111, + 32, + 99, + 114, + 101, + 97, + 116, + 101, + 32, + 109, + 97, + 114, + 107, + 117, + 112, + 32, + 108, + 97, + 110, + 103, + 117, + 97, + 103, + 101, + 115, + 32, + 115, + 117, + 99, + 104, + 32, + 97, + 115, + 32, + 68, + 111, + 99, + 66, + 111, + 111, + 107, + 46, + 34, + 44, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 71, + 108, + 111, + 115, + 115, + 83, + 101, + 101, + 65, + 108, + 115, + 111, + 34, + 58, + 32, + 91, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 71, + 77, + 76, + 34, + 44, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 88, + 77, + 76, + 34, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 93, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 125, + 44, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 34, + 71, + 108, + 111, + 115, + 115, + 83, + 101, + 101, + 34, + 58, + 32, + 34, + 109, + 97, + 114, + 107, + 117, + 112, + 34, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 125, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 125, + 10, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 32, + 125, + 10, + 32, + 32, + 32, + 32, + 125, + 10, + 125 + ] +} \ No newline at end of file From 492659a09aca85aaaafac3bd5d883e7bf7f3035d Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Tue, 3 Sep 2024 13:41:04 -0600 Subject: [PATCH 5/6] fix tests --- circuits/test/json/extractor/extractor.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/circuits/test/json/extractor/extractor.test.ts b/circuits/test/json/extractor/extractor.test.ts index bc13620..4d47ea7 100644 --- a/circuits/test/json/extractor/extractor.test.ts +++ b/circuits/test/json/extractor/extractor.test.ts @@ -5,9 +5,9 @@ import { spawn } from "child_process"; function executeCodegen(inputFilename: string, outputFilename: string) { return new Promise((resolve, reject) => { - const inputPath = join(__dirname, "..", "..", "..", "..", "examples", "json", "test", "codegen", inputFilename); + const inputPath = join(__dirname, "..", "..", "..", "..", "examples", "extractor", inputFilename); - const codegen = spawn("cargo", ["run", "--bin", "codegen", "--", "--json-file", inputPath, "--output-filename", outputFilename]); + const codegen = spawn("cargo", ["run", "extractor", "--template", inputPath, "--output-filename", outputFilename]); codegen.stdout.on('data', (data) => { console.log(`stdout: ${data}`); From 6dca99118e4a59b8aaf1dd512ab3c26a62f704db Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Tue, 3 Sep 2024 14:39:28 -0600 Subject: [PATCH 6/6] clean up basic lock file --- examples/lockfile/test.lock.json | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/examples/lockfile/test.lock.json b/examples/lockfile/test.lock.json index ccbf5f1..a95f84e 100644 --- a/examples/lockfile/test.lock.json +++ b/examples/lockfile/test.lock.json @@ -4,16 +4,15 @@ "target": "/api", "version": "HTTP/1.1", "headers": [ - [ - "Accept", - "application/json" - ], [ "Host", "localhost" + ], + [ + "Accept", + "application/json" ] - ], - "Host": "localhost" + ] }, "response": { "version": "HTTP/1.1", @@ -22,10 +21,6 @@ [ "Content-Type", "application/json" - ], - [ - "Content-Length", - 19 ] ] }