Skip to content

Commit

Permalink
optional logs on extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Sep 10, 2024
1 parent 9a48860 commit a10b097
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
61 changes: 41 additions & 20 deletions src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Data {
value_type: ValueType,
}

fn extract_string(data: Data, circuit_buffer: &mut String) {
fn extract_string(data: Data, circuit_buffer: &mut String, debug: bool) {
*circuit_buffer += "template ExtractStringValue(DATA_BYTES, MAX_STACK_HEIGHT, ";
for (i, key) in data.keys.iter().enumerate() {
match key {
Expand Down Expand Up @@ -78,12 +78,18 @@ fn extract_string(data: Data, circuit_buffer: &mut String) {
}

*circuit_buffer += r#"
log("value_starting_index", value_starting_index[DATA_BYTES-2]);
value <== SelectSubArray(DATA_BYTES, maxValueLen)(data, value_starting_index[DATA_BYTES-2]+1, maxValueLen);
value <== SelectSubArray(DATA_BYTES, maxValueLen)(data, value_starting_index[DATA_BYTES-2]+1, maxValueLen);"#;

if debug {
*circuit_buffer += r#"
log("value_starting_index", value_starting_index[DATA_BYTES-2]);
for (var i=0 ; i<maxValueLen; i++) {
log("value[",i,"]=", value[i]);
}"#;
}

*circuit_buffer += r#"
}
"#;
}
Expand Down Expand Up @@ -160,6 +166,7 @@ fn extract_number(data: Data, circuit_buffer: &mut String) {
fn parse_json_request(
data: Data,
output_filename: String,
debug: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let mut circuit_buffer = String::new();
circuit_buffer += PRAGMA;
Expand Down Expand Up @@ -293,15 +300,21 @@ fn parse_json_request(
signal is_value_match[DATA_BYTES];
is_value_match[0] <== 0;
signal value_mask[DATA_BYTES];
for(var data_idx = 1; data_idx < DATA_BYTES; data_idx++) {
for(var data_idx = 1; data_idx < DATA_BYTES; data_idx++) {"#;

if debug {
circuit_buffer += r#"
// Debugging
for(var i = 0; i<MAX_STACK_HEIGHT; i++) {
log("State[", data_idx-1, "].stack[", i,"] ", "= [",State[data_idx-1].next_stack[i][0], "][", State[data_idx-1].next_stack[i][1],"]" );
}
log("State[", data_idx-1, "].byte", "= ", data[data_idx-1]);
log("State[", data_idx-1, "].parsing_string", "= ", State[data_idx-1].next_parsing_string);
log("State[", data_idx-1, "].parsing_number", "= ", State[data_idx-1].next_parsing_number);
"#;
}

circuit_buffer += r#"
State[data_idx] = StateUpdate(MAX_STACK_HEIGHT);
State[data_idx].byte <== data[data_idx];
State[data_idx].stack <== State[data_idx - 1].next_stack;
Expand All @@ -318,7 +331,6 @@ fn parse_json_request(
// check if inside key or not
parsing_key[data_idx-1] <== InsideKey(MAX_STACK_HEIGHT)(State[data_idx].stack, State[data_idx].parsing_string, State[data_idx].parsing_number);
// log("parsing key:", parsing_key[data_idx]);
"#;

Expand Down Expand Up @@ -366,16 +378,20 @@ fn parse_json_request(
}

// optional debug logs
circuit_buffer += " // log(\"parsing value:\", ";
for (i, key) in data.keys.iter().enumerate() {
match key {
Key::String(_) => {
circuit_buffer += &format!("parsing_object{}_value[data_idx-1], ", i + 1)
if debug {
circuit_buffer += " // log(\"parsing value:\", ";
for (i, key) in data.keys.iter().enumerate() {
match key {
Key::String(_) => {
circuit_buffer += &format!("parsing_object{}_value[data_idx-1], ", i + 1)
}
Key::Num(_) => {
circuit_buffer += &format!("parsing_array{}[data_idx-1], ", i + 1)
}
}
Key::Num(_) => circuit_buffer += &format!("parsing_array{}[data_idx-1], ", i + 1),
}
circuit_buffer += "parsing_value[data_idx-1]);\n\n";
}
circuit_buffer += "parsing_value[data_idx-1]);\n\n";
}

let mut num_objects = 0;
Expand Down Expand Up @@ -404,7 +420,9 @@ fn parse_json_request(
circuit_buffer += &format!(" is_key{}_match[data_idx-1] <== KeyMatchAtDepth(DATA_BYTES, MAX_STACK_HEIGHT, keyLen{}, depth{})(data, key{}, r, data_idx-1, parsing_key[data_idx-1], State[data_idx].stack);\n", i+1, i+1, i+1, i+1);
circuit_buffer += &format!(" is_next_pair_at_depth{}[data_idx-1] <== NextKVPairAtDepth(MAX_STACK_HEIGHT, depth{})(State[data_idx].stack, data[data_idx-1]);\n", i+1, i+1);
circuit_buffer += &format!(" is_key{}_match_for_value[data_idx] <== Mux1()([is_key{}_match_for_value[data_idx-1] * (1-is_next_pair_at_depth{}[data_idx-1]), is_key{}_match[data_idx-1] * (1-is_next_pair_at_depth{}[data_idx-1])], is_key{}_match[data_idx-1]);\n", i+1, i+1, i+1, i+1, i+1, i+1);
circuit_buffer += &format!(" // log(\"is_key{}_match_for_value\", is_key{}_match_for_value[data_idx]);\n\n", i + 1, i + 1);
if debug {
circuit_buffer += &format!(" // log(\"is_key{}_match_for_value\", is_key{}_match_for_value[data_idx]);\n\n", i + 1, i + 1);
}
}
Key::Num(_) => (),
}
Expand Down Expand Up @@ -434,23 +452,26 @@ fn parse_json_request(

// debugging and output bytes
{
circuit_buffer += r#" // log("is_value_match", is_value_match[data_idx]);
circuit_buffer += r#"
// mask[i] = data[i] * parsing_value[i] * is_key_match_for_value[i]
value_mask[data_idx-1] <== data[data_idx-1] * parsing_value[data_idx-1];
mask[data_idx-1] <== value_mask[data_idx-1] * is_value_match[data_idx];
log("mask", mask[data_idx-1]);
log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
}"#;

// Debugging
// Debugging
if debug {
circuit_buffer += r#"
for(var i = 0; i < MAX_STACK_HEIGHT; i++) {
log("State[", DATA_BYTES-1, "].stack[", i,"] ", "= [",State[DATA_BYTES -1].next_stack[i][0], "][", State[DATA_BYTES - 1].next_stack[i][1],"]" );
}
log("State[", DATA_BYTES-1, "].parsing_string", "= ", State[DATA_BYTES-1].next_parsing_string);
log("State[", DATA_BYTES-1, "].parsing_number", "= ", State[DATA_BYTES-1].next_parsing_number);
log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
"#;
}

circuit_buffer += r#"
// signal value_starting_index[DATA_BYTES];
signal is_zero_mask[DATA_BYTES];
signal is_prev_starting_index[DATA_BYTES];
Expand All @@ -468,7 +489,7 @@ fn parse_json_request(
}

match data.value_type {
ValueType::String => extract_string(data, &mut circuit_buffer),
ValueType::String => extract_string(data, &mut circuit_buffer, debug),
ValueType::Number => extract_number(data, &mut circuit_buffer),
}

Expand All @@ -493,7 +514,7 @@ pub fn extractor(args: ExtractorArgs) -> Result<(), Box<dyn std::error::Error>>
let data = std::fs::read(&args.template)?;
let json_data: Data = serde_json::from_slice(&data)?;

parse_json_request(json_data, args.output_filename)?;
parse_json_request(json_data, args.output_filename, args.debug)?;

Ok(())
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct ExtractorArgs {
#[arg(long, default_value = "extractor")]
output_filename: String,

#[arg(long, default_value = "false")]
#[arg(long, action = clap::ArgAction::SetTrue)]
debug: bool,
}

Expand All @@ -68,7 +68,7 @@ pub struct HttpLockArgs {
#[arg(long, default_value = "extractor")]
output_filename: String,

#[arg(long, default_value = "false")]
#[arg(long, action = clap::ArgAction::SetTrue)]
debug: bool,
}

Expand Down

0 comments on commit a10b097

Please sign in to comment.