Skip to content

Commit

Permalink
tests passing
Browse files Browse the repository at this point in the history
but still not properly indexing in arrays
  • Loading branch information
Autoparallel committed Aug 14, 2024
1 parent e0f8893 commit 33fbe23
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 170 deletions.
8 changes: 4 additions & 4 deletions circuits/extract.circom
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ template Extract(DATA_BYTES, MAX_STACK_HEIGHT) {
State[0].byte <== data[0];
State[0].pointer <== 0;
for(var i = 0; i < MAX_STACK_HEIGHT; i++) {
State[0].stack[i] <== 0;
State[0].stack[i] <== [0,0];
}
// State[0].stack <== [0,0,0,0,0,0,0,0,0,0,0,0];
State[0].parsing_string <== 0;
State[0].parsing_number <== 0;

Expand All @@ -37,7 +36,7 @@ template Extract(DATA_BYTES, MAX_STACK_HEIGHT) {
// Debugging
log("State[", data_idx, "].pointer ", "= ", State[data_idx].pointer);
for(var i = 0; i<MAX_STACK_HEIGHT; i++) {
log("State[", data_idx, "].stack[", i,"] ", "= ", State[data_idx].stack[i]);
log("State[", data_idx, "].stack[", i,"] ", "= [",State[data_idx].stack[i][0], "][", State[data_idx].stack[i][1],"]" );
}
log("State[", data_idx, "].parsing_string", "= ", State[data_idx].parsing_string);
log("State[", data_idx, "].parsing_number", "= ", State[data_idx].parsing_number);
Expand All @@ -50,7 +49,8 @@ template Extract(DATA_BYTES, MAX_STACK_HEIGHT) {
// Debugging
log("State[", DATA_BYTES, "].pointer ", "= ", State[DATA_BYTES -1].next_pointer);
for(var i = 0; i < MAX_STACK_HEIGHT; i++) {
log("State[", DATA_BYTES, "].stack[", i,"] ", "= ", State[DATA_BYTES -1 ].next_stack[i]);
log("State[", DATA_BYTES, "].stack[", i,"] ", "= [",State[DATA_BYTES -1].next_stack[i][0], "][", State[DATA_BYTES - 1].next_stack[i][1],"]" );
// log("State[", DATA_BYTES, "].stack[", i,"] ", "= ", State[DATA_BYTES -1 ].next_stack[i]);
}
log("State[", DATA_BYTES, "].parsing_string", "= ", State[DATA_BYTES-1].next_parsing_string);
log("State[", DATA_BYTES, "].parsing_number", "= ", State[DATA_BYTES-1].next_parsing_number);
Expand Down
16 changes: 11 additions & 5 deletions circuits/parser.circom
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ template StateToMask(n) {
out[0] <== (1 - parsing_string) * (1 - parsing_number);

// `stack_val`can change: IF NOT `parsing_string`
out[1] <== (1 - parsing_string) * (1- parsing_number);
out[1] <== (1 - parsing_string);

// `parsing_string` can change:
out[2] <== 1 - 2 * parsing_string;
Expand Down Expand Up @@ -201,14 +201,20 @@ template RewriteStack(n) {
isArray.in[0] <== topOfStack.out[0];
isArray.in[1] <== 2;

log("isArray: ", isArray.out);

component readComma = IsEqual();
readComma.in[0] <== 4;
readComma.in[1] <== stack_val;

signal READ_COMMA_AND_IN_ARRAY <== (1 - readComma.out) + (1 - isArray.out);
log("readComma: ", readComma.out);

signal READ_COMMA_AND_IN_ARRAY <== (1 - readComma.out) + (1 - isArray.out); // POORLY NAMED. THIS IS MORE LIKE XNOR or something.
component isReadCommaAndInArray = IsZero();
isReadCommaAndInArray.in <== READ_COMMA_AND_IN_ARRAY;

signal read_comma_in_array <== readComma.out * isArray.out;

component isPop = IsZero();
isPop.in <== (1 - isReadCommaAndInArray.out) * pushpop + 1;
component isPush = IsZero();
Expand Down Expand Up @@ -263,10 +269,10 @@ template RewriteStack(n) {
first_pop_val[i] <== isPopAt[i] * temp_val[i]; // = isPopAt[i] * (corrected_stack_val * (1 - isDoublePop) - 3 * isDoublePop)

next_stack[i][0] <== stack[i][0] + isPushAt[i] * corrected_stack_val + first_pop_val[i] + second_pop_val[i];
next_stack[i][1] <== prev_indicator[i].out * isReadCommaAndInArray.out;
next_stack[i][1] <== prev_indicator[i].out * read_comma_in_array;

log("next_stack[",i,"][0]: ", next_stack[i][0]);
log("next_stack[",i,"][1]: ", next_stack[i][1]);
log("read_comma_in_array: ", read_comma_in_array);
log("next_stack[", i,"] ", "= [",next_stack[i][0], "][", next_stack[i][1],"]" );
// TODO: Constrain next_stack entries to be 0,1,2,3
}

Expand Down
222 changes: 111 additions & 111 deletions circuits/test/parser/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,117 +35,117 @@ describe("StateUpdate", () => {

});

// //-TEST_1----------------------------------------------------------//
// // init: ZEROS then read `do_nothing` byte
// // expect: ZEROS
// generatePassCase(INITIAL_IN, INITIAL_OUT, ">>>> `NUL` read");

// // TODO: Consider moving to `stack.test.ts`
// //-TEST_2----------------------------------------------------------//
// // init: INIT
// // read: `{`
// // expect: pointer --> 1
// // stack --> [1,0,0,0]
// let read_start_brace = { ...INITIAL_IN };
// read_start_brace.byte = Delimiters.START_BRACE;
// let read_start_brace_out = { ...INITIAL_OUT };
// read_start_brace_out.next_pointer = 1;
// read_start_brace_out.next_stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
// generatePassCase(read_start_brace,
// read_start_brace_out,
// ">>>> `{` read"
// );

// //-TEST_3----------------------------------------------------------//
// // state: INIT
// // read: `}`
// // expect: FAIL (stack underflow)
// let read_end_brace = { ...INITIAL_IN };
// read_end_brace.byte = Delimiters.END_BRACE;
// generateFailCase(read_end_brace,
// ">>>> `}` read --> (stack underflow)"
// );

// //-TEST_4----------------------------------------------------------//
// // state: pointer == 1, stack == [1,0,0,0]
// // read: `"`
// // expect: parsing_string --> 1
// let in_object_find_key = { ...INITIAL_IN };
// in_object_find_key.pointer = 1;
// in_object_find_key.stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
// in_object_find_key.byte = Delimiters.QUOTE;
// let in_object_find_key_out = { ...INITIAL_OUT };
// in_object_find_key_out.next_pointer = 1;
// in_object_find_key_out.next_stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
// in_object_find_key_out.next_parsing_string = 1;
// generatePassCase(in_object_find_key,
// in_object_find_key_out,
// ">>>> `\"` read"
// );

// //-TEST_5----------------------------------------------------------//
// // state: pointer == 1, stack = [1,0,0,0], parsing_string == 1
// // read: ` `
// // expect: NIL
// let in_key = { ...INITIAL_IN };
// in_key.pointer = 1;
// in_key.stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
// in_key.parsing_string = 1;
// in_key.byte = WhiteSpace.SPACE;
// let in_key_out = { ...INITIAL_OUT };
// in_key_out.next_pointer = 1;
// in_key_out.next_stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
// in_key_out.next_parsing_string = 1;
// generatePassCase(in_key, in_key_out, ">>>> ` ` read");

// //-TEST_6----------------------------------------------------------//
// // init: pointer == 1, stack == [1,0,0,0]
// // read: `"`
// // expect: parsing_string --> 0
// //
// let in_key_to_exit = { ...INITIAL_IN };
// in_key_to_exit.pointer = 1;
// in_key_to_exit.stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
// in_key_to_exit.parsing_string = 1
// in_key_to_exit.byte = Delimiters.QUOTE;
// let in_key_to_exit_out = { ...INITIAL_OUT };
// in_key_to_exit_out.next_pointer = 1;
// in_key_to_exit_out.next_stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
// generatePassCase(in_key_to_exit, in_key_to_exit_out, "`\"` read");

// //-TEST_7----------------------------------------------------------//
// // state: pointer == 2, stack == [1,3,0,0]
// // read: `"`
// // expect: parsing_string --> 1
// let in_tree_find_value = { ...INITIAL_IN };
// in_tree_find_value.pointer = 1;
// in_tree_find_value.stack = [[1, 0], [3, 0], [0, 0], [0, 0]];
// in_tree_find_value.byte = Delimiters.QUOTE;
// let in_tree_find_value_out = { ...INITIAL_OUT };
// in_tree_find_value_out.next_pointer = 1;
// in_tree_find_value_out.next_stack = [[1, 0], [3, 0], [0, 0], [0, 0]];
// in_tree_find_value_out.next_parsing_string = 1;
// generatePassCase(in_tree_find_value,
// in_tree_find_value_out,
// ">>>> `\"` read"
// );

// //-TEST_8----------------------------------------------------------//
// // state: pointer == 2, stack == [1,3,0,0], parsing_string == 1
// // read: `"`
// // expect: parsing_string == 0,
// let in_value_to_exit = { ...INITIAL_IN };
// in_value_to_exit.pointer = 2;
// in_value_to_exit.stack = [[1, 0], [3, 0], [0, 0], [0, 0]];
// in_value_to_exit.parsing_string = 1;
// in_value_to_exit.byte = Delimiters.QUOTE;
// let in_value_to_exit_out = { ...INITIAL_OUT };
// in_value_to_exit_out.next_pointer = 2;
// in_value_to_exit_out.next_stack = [[1, 0], [3, 0], [0, 0], [0, 0]];
// generatePassCase(in_value_to_exit,
// in_value_to_exit_out,
// ">>>> `\"` is read"
// );
//-TEST_1----------------------------------------------------------//
// init: ZEROS then read `do_nothing` byte
// expect: ZEROS
generatePassCase(INITIAL_IN, INITIAL_OUT, ">>>> `NUL` read");

// TODO: Consider moving to `stack.test.ts`
//-TEST_2----------------------------------------------------------//
// init: INIT
// read: `{`
// expect: pointer --> 1
// stack --> [1,0,0,0]
let read_start_brace = { ...INITIAL_IN };
read_start_brace.byte = Delimiters.START_BRACE;
let read_start_brace_out = { ...INITIAL_OUT };
read_start_brace_out.next_pointer = 1;
read_start_brace_out.next_stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
generatePassCase(read_start_brace,
read_start_brace_out,
">>>> `{` read"
);

//-TEST_3----------------------------------------------------------//
// state: INIT
// read: `}`
// expect: FAIL (stack underflow)
let read_end_brace = { ...INITIAL_IN };
read_end_brace.byte = Delimiters.END_BRACE;
generateFailCase(read_end_brace,
">>>> `}` read --> (stack underflow)"
);

//-TEST_4----------------------------------------------------------//
// state: pointer == 1, stack == [1,0,0,0]
// read: `"`
// expect: parsing_string --> 1
let in_object_find_key = { ...INITIAL_IN };
in_object_find_key.pointer = 1;
in_object_find_key.stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
in_object_find_key.byte = Delimiters.QUOTE;
let in_object_find_key_out = { ...INITIAL_OUT };
in_object_find_key_out.next_pointer = 1;
in_object_find_key_out.next_stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
in_object_find_key_out.next_parsing_string = 1;
generatePassCase(in_object_find_key,
in_object_find_key_out,
">>>> `\"` read"
);

//-TEST_5----------------------------------------------------------//
// state: pointer == 1, stack = [1,0,0,0], parsing_string == 1
// read: ` `
// expect: NIL
let in_key = { ...INITIAL_IN };
in_key.pointer = 1;
in_key.stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
in_key.parsing_string = 1;
in_key.byte = WhiteSpace.SPACE;
let in_key_out = { ...INITIAL_OUT };
in_key_out.next_pointer = 1;
in_key_out.next_stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
in_key_out.next_parsing_string = 1;
generatePassCase(in_key, in_key_out, ">>>> ` ` read");

//-TEST_6----------------------------------------------------------//
// init: pointer == 1, stack == [1,0,0,0]
// read: `"`
// expect: parsing_string --> 0
//
let in_key_to_exit = { ...INITIAL_IN };
in_key_to_exit.pointer = 1;
in_key_to_exit.stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
in_key_to_exit.parsing_string = 1
in_key_to_exit.byte = Delimiters.QUOTE;
let in_key_to_exit_out = { ...INITIAL_OUT };
in_key_to_exit_out.next_pointer = 1;
in_key_to_exit_out.next_stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
generatePassCase(in_key_to_exit, in_key_to_exit_out, "`\"` read");

//-TEST_7----------------------------------------------------------//
// state: pointer == 2, stack == [1,3,0,0]
// read: `"`
// expect: parsing_string --> 1
let in_tree_find_value = { ...INITIAL_IN };
in_tree_find_value.pointer = 1;
in_tree_find_value.stack = [[1, 0], [3, 0], [0, 0], [0, 0]];
in_tree_find_value.byte = Delimiters.QUOTE;
let in_tree_find_value_out = { ...INITIAL_OUT };
in_tree_find_value_out.next_pointer = 1;
in_tree_find_value_out.next_stack = [[1, 0], [3, 0], [0, 0], [0, 0]];
in_tree_find_value_out.next_parsing_string = 1;
generatePassCase(in_tree_find_value,
in_tree_find_value_out,
">>>> `\"` read"
);

//-TEST_8----------------------------------------------------------//
// state: pointer == 2, stack == [1,3,0,0], parsing_string == 1
// read: `"`
// expect: parsing_string == 0,
let in_value_to_exit = { ...INITIAL_IN };
in_value_to_exit.pointer = 2;
in_value_to_exit.stack = [[1, 0], [3, 0], [0, 0], [0, 0]];
in_value_to_exit.parsing_string = 1;
in_value_to_exit.byte = Delimiters.QUOTE;
let in_value_to_exit_out = { ...INITIAL_OUT };
in_value_to_exit_out.next_pointer = 2;
in_value_to_exit_out.next_stack = [[1, 0], [3, 0], [0, 0], [0, 0]];
generatePassCase(in_value_to_exit,
in_value_to_exit_out,
">>>> `\"` is read"
);

});

Expand Down
Loading

0 comments on commit 33fbe23

Please sign in to comment.