Skip to content

Commit

Permalink
fix: circuit warnings (#93)
Browse files Browse the repository at this point in the history
* remove slice

* remove syntax

* feat: json improvements

* feat(http): remove syntax use

* feat(http): remove hasher substring matching

* fix: delete extractor

* fix: codegen

* feat: remove hashing based substring matching

* fix: spotify test

* fix: tests
  • Loading branch information
lonerapier authored Sep 26, 2024
1 parent 726b6f9 commit 7fa287d
Show file tree
Hide file tree
Showing 20 changed files with 457 additions and 612 deletions.
165 changes: 0 additions & 165 deletions circuits/http/extractor.circom

This file was deleted.

19 changes: 6 additions & 13 deletions circuits/http/interpreter.circom
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ template MethodMatch(dataLen, methodLen) {
signal input data[dataLen];
signal input method[methodLen];

signal input r;
signal input index;

signal isMatch <== SubstringMatchWithIndex(dataLen, methodLen)(data, method, r, index);
signal isMatch <== SubstringMatchWithIndex(dataLen, methodLen)(data, method, index);
isMatch === 1;
}

Expand All @@ -46,24 +45,21 @@ template HeaderFieldNameValueMatch(dataLen, nameLen, valueLen) {
signal input data[dataLen];
signal input headerName[nameLen];
signal input headerValue[valueLen];
signal input r;
signal input index;

component syntax = HttpSyntax();

// signal output value[valueLen];

// is name matches
signal headerNameMatch <== SubstringMatchWithIndex(dataLen, nameLen)(data, headerName, r, index);
signal headerNameMatch <== SubstringMatchWithIndex(dataLen, nameLen)(data, headerName, index);

// next byte to name should be COLON
signal endOfHeaderName <== IndexSelector(dataLen)(data, index + nameLen);
signal isNextByteColon <== IsEqual()([endOfHeaderName, syntax.COLON]);
signal isNextByteColon <== IsEqual()([endOfHeaderName, 58]);

signal headerNameMatchAndNextByteColon <== headerNameMatch * isNextByteColon;

// field-name: SP field-value
signal headerValueMatch <== SubstringMatchWithIndex(dataLen, valueLen)(data, headerValue, r, index + nameLen + 2);
signal headerValueMatch <== SubstringMatchWithIndex(dataLen, valueLen)(data, headerValue, index + nameLen + 2);

// header name matches + header value matches
signal output out <== headerNameMatchAndNextByteColon * headerValueMatch;
Expand All @@ -73,17 +69,14 @@ template HeaderFieldNameValueMatch(dataLen, nameLen, valueLen) {
template HeaderFieldNameMatch(dataLen, nameLen) {
signal input data[dataLen];
signal input headerName[nameLen];
signal input r;
signal input index;

component syntax = HttpSyntax();

// is name matches
signal headerNameMatch <== SubstringMatchWithIndex(dataLen, nameLen)(data, headerName, r, index);
signal headerNameMatch <== SubstringMatchWithIndex(dataLen, nameLen)(data, headerName, index);

// next byte to name should be COLON
signal endOfHeaderName <== IndexSelector(dataLen)(data, index + nameLen);
signal isNextByteColon <== IsEqual()([endOfHeaderName, syntax.COLON]);
signal isNextByteColon <== IsEqual()([endOfHeaderName, 58]);

// header name matches
signal output out;
Expand Down
8 changes: 2 additions & 6 deletions circuits/http/locker.circom
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,12 @@ template LockStartLine(DATA_BYTES, beginningLen, middleLen, finalLen) {
beginningLen === middle_start_counter - 1;

// Check middle is correct by substring match and length check
// TODO: change r
signal middleMatch <== SubstringMatchWithIndex(DATA_BYTES, middleLen)(data, middle, 100, middle_start_counter);
signal middleMatch <== SubstringMatchWithIndex(DATA_BYTES, middleLen)(data, middle, middle_start_counter);
middleMatch === 1;
middleLen === middle_end_counter - middle_start_counter - 1;

// Check final is correct by substring match and length check
// TODO: change r
signal finalMatch <== SubstringMatchWithIndex(DATA_BYTES, finalLen)(data, final, 100, middle_end_counter);
signal finalMatch <== SubstringMatchWithIndex(DATA_BYTES, finalLen)(data, final, middle_end_counter);
finalMatch === 1;
// -2 here for the CRLF
finalLen === final_end_counter - middle_end_counter - 2;
Expand Down Expand Up @@ -153,12 +151,10 @@ template LockHeader(DATA_BYTES, headerNameLen, headerValueLen) {
State[data_idx].parsing_body <== State[data_idx - 1].next_parsing_body;
State[data_idx].line_status <== State[data_idx - 1].next_line_status;

// TODO: change r
headerFieldNameValueMatch[data_idx] = HeaderFieldNameValueMatch(DATA_BYTES, headerNameLen, headerValueLen);
headerFieldNameValueMatch[data_idx].data <== data;
headerFieldNameValueMatch[data_idx].headerName <== header;
headerFieldNameValueMatch[data_idx].headerValue <== value;
headerFieldNameValueMatch[data_idx].r <== 100;
headerFieldNameValueMatch[data_idx].index <== data_idx;
isHeaderFieldNameValueMatch[data_idx] <== isHeaderFieldNameValueMatch[data_idx-1] + headerFieldNameValueMatch[data_idx].out;

Expand Down
23 changes: 10 additions & 13 deletions circuits/http/parser/machine.circom
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ template HttpStateUpdate() {
signal output next_parsing_body;
signal output next_line_status;

component HttpSyntax = HttpSyntax();

//---------------------------------------------------------------------------------//
// check if we read space or colon
// check if we read space: 32 or colon: 58
component readSP = IsEqual();
readSP.in <== [byte, HttpSyntax.SPACE];
readSP.in <== [byte, 32];
component readColon = IsEqual();
readColon.in <== [byte, HttpSyntax.COLON];
readColon.in <== [byte, 58];

// Check if what we just read is a CR / LF
component readCR = IsEqual();
readCR.in <== [byte, HttpSyntax.CR];
readCR.in <== [byte, 13];
component readLF = IsEqual();
readLF.in <== [byte, HttpSyntax.LF];
readLF.in <== [byte, 10];

signal notCRAndLF <== (1 - readCR.out) * (1 - readLF.out);
//---------------------------------------------------------------------------------//
Expand All @@ -41,8 +39,6 @@ template HttpStateUpdate() {
// Check if we had read previously CR / LF or multiple
component prevReadCR = IsEqual();
prevReadCR.in <== [line_status, 1];
component prevReadCRLF = IsEqual();
prevReadCRLF.in <== [line_status, 2];
component prevReadCRLFCR = IsEqual();
prevReadCRLFCR.in <== [line_status, 3];

Expand All @@ -52,7 +48,7 @@ template HttpStateUpdate() {

//---------------------------------------------------------------------------------//
// Take current state and CRLF info to update state
signal state[5] <== [parsing_start, parsing_header, parsing_field_name, parsing_field_value, parsing_body];
signal state[2] <== [parsing_start, parsing_header];
component stateChange = StateChange();
stateChange.readCRLF <== readCRLF;
stateChange.readCRLFCRLF <== readCRLFCRLF;
Expand All @@ -61,7 +57,7 @@ template HttpStateUpdate() {
stateChange.state <== state;

component nextState = ArrayAdd(5);
nextState.lhs <== state;
nextState.lhs <== [state[0], state[1], parsing_field_name, parsing_field_value, parsing_body];
nextState.rhs <== stateChange.out;
//---------------------------------------------------------------------------------//

Expand All @@ -82,7 +78,7 @@ template StateChange() {
signal input readCRLFCRLF;
signal input readSP;
signal input readColon;
signal input state[5];
signal input state[2];
signal output out[5];

// GreaterEqThan(2) because start line can have at most 3 values for request or response
Expand All @@ -95,7 +91,8 @@ template StateChange() {
// enable parsing header on reading CRLF
signal enableParsingHeader <== readCRLF * isParsingStart;
// check if we are parsing header
signal isParsingHeader <== GreaterEqThan(10)([state[1], 1]);
// TODO: correct this 3 (it means we can parse max 2^3 headers)
signal isParsingHeader <== GreaterEqThan(3)([state[1], 1]);
// increment parsing header counter on CRLF and parsing header
signal incrementParsingHeader <== readCRLF * isParsingHeader;
// disable parsing header on reading CRLF-CRLF
Expand Down
Loading

0 comments on commit 7fa287d

Please sign in to comment.