-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic but generic http parser (#69)
* slight refactor and basic test * decent progress on request parse * feat: very basic HTTP parsing * fix typos, remove logs
- Loading branch information
1 parent
b3bd8da
commit 5d6aac5
Showing
9 changed files
with
186 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 10 additions & 3 deletions
13
circuits/http/parser_request/machine.circom → circuits/http/interpreter.circom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
pragma circom 2.1.9; | ||
|
||
include "language.circom"; | ||
include "../../utils/array.circom"; | ||
|
||
template StateUpdate() { | ||
signal input parsing_start; // Bool flag for if we are in the start line | ||
signal input parsing_header; // Flag + Counter for what header line we are in | ||
signal input parsing_body; | ||
signal input line_status; // Flag that counts up to 4 to read a double CLRF | ||
signal input byte; | ||
|
||
signal output next_parsing_start; | ||
signal output next_parsing_header; | ||
signal output next_parsing_body; | ||
signal output next_line_status; | ||
|
||
component Syntax = Syntax(); | ||
|
||
//---------------------------------------------------------------------------------// | ||
// Check if what we just read is a CR / LF | ||
component readCR = IsEqual(); | ||
readCR.in <== [byte, Syntax.CR]; | ||
component readLF = IsEqual(); | ||
readLF.in <== [byte, Syntax.LF]; | ||
|
||
signal notCRAndLF <== (1 - readCR.out) * (1 - readLF.out); | ||
//---------------------------------------------------------------------------------// | ||
|
||
//---------------------------------------------------------------------------------// | ||
// 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]; | ||
|
||
signal readCRLF <== prevReadCR.out * readLF.out; | ||
signal readCRLFCRLF <== prevReadCRLFCR.out * readLF.out; | ||
//---------------------------------------------------------------------------------// | ||
|
||
//---------------------------------------------------------------------------------// | ||
// Take current state and CRLF info to update state | ||
signal state[3] <== [parsing_start, parsing_header, parsing_body]; | ||
component stateChange = StateChange(); | ||
stateChange.readCRLF <== readCRLF; | ||
stateChange.readCRLFCRLF <== readCRLFCRLF; | ||
stateChange.state <== state; | ||
|
||
component nextState = ArrayAdd(3); | ||
nextState.lhs <== state; | ||
nextState.rhs <== stateChange.out; | ||
//---------------------------------------------------------------------------------// | ||
|
||
next_parsing_start <== nextState.out[0]; | ||
next_parsing_header <== nextState.out[1]; | ||
next_parsing_body <== nextState.out[2]; | ||
next_line_status <== line_status + readCR.out + readCRLF + readCRLFCRLF - line_status * notCRAndLF; | ||
|
||
} | ||
|
||
template StateChange() { | ||
signal input readCRLF; | ||
signal input readCRLFCRLF; | ||
signal input state[3]; | ||
signal output out[3]; | ||
|
||
signal disableParsingStart <== readCRLF * state[0]; | ||
signal disableParsingHeader <== readCRLFCRLF * state[1]; | ||
|
||
out <== [-disableParsingStart, disableParsingStart - disableParsingHeader, disableParsingHeader]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
pragma circom 2.1.9; | ||
|
||
include "../../utils/bytes.circom"; | ||
include "machine.circom"; | ||
|
||
|
||
template Parser(DATA_BYTES) { | ||
signal input data[DATA_BYTES]; | ||
|
||
signal output Method; | ||
|
||
//--------------------------------------------------------------------------------------------// | ||
//-CONSTRAINTS--------------------------------------------------------------------------------// | ||
//--------------------------------------------------------------------------------------------// | ||
component dataASCII = ASCII(DATA_BYTES); | ||
dataASCII.in <== data; | ||
//--------------------------------------------------------------------------------------------// | ||
|
||
// Initialze the parser | ||
component State[DATA_BYTES]; | ||
State[0] = StateUpdate(); | ||
State[0].byte <== data[0]; | ||
State[0].parsing_start <== 1; | ||
State[0].parsing_header <== 0; | ||
State[0].parsing_body <== 0; | ||
State[0].line_status <== 0; | ||
|
||
for(var data_idx = 1; data_idx < DATA_BYTES; data_idx++) { | ||
State[data_idx] = StateUpdate(); | ||
State[data_idx].byte <== data[data_idx]; | ||
State[data_idx].parsing_start <== State[data_idx - 1].next_parsing_start; | ||
State[data_idx].parsing_header <== State[data_idx - 1].next_parsing_header; | ||
State[data_idx].parsing_body <== State[data_idx - 1].next_parsing_body; | ||
State[data_idx].line_status <== State[data_idx - 1].next_line_status; | ||
|
||
// Debugging | ||
log("State[", data_idx, "].parsing_start ", "= ", State[data_idx].parsing_start); | ||
log("State[", data_idx, "].parsing_header", "= ", State[data_idx].parsing_header); | ||
log("State[", data_idx, "].parsing_body ", "= ", State[data_idx].parsing_body); | ||
log("State[", data_idx, "].line_status ", "= ", State[data_idx].line_status); | ||
log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | ||
} | ||
|
||
// Debugging | ||
log("State[", DATA_BYTES, "].parsing_start ", "= ", State[DATA_BYTES-1].next_parsing_start); | ||
log("State[", DATA_BYTES, "].parsing_header", "= ", State[DATA_BYTES-1].next_parsing_header); | ||
log("State[", DATA_BYTES, "].parsing_body ", "= ", State[DATA_BYTES-1].next_parsing_body); | ||
log("State[", DATA_BYTES, "].line_status ", "= ", State[DATA_BYTES-1].next_line_status); | ||
log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { circomkit, WitnessTester, generateDescription } from "../common"; | ||
|
||
describe("HTTP :: Interpreter", async () => { | ||
describe("YieldMethod", async () => { | ||
let circuit: WitnessTester<["bytes"], ["MethodTag"]>; | ||
|
||
function generatePassCase(input: any, expected: any, depth: number, desc: string) { | ||
const description = generateDescription(input); | ||
|
||
it(`(valid) witness: ${description} ${desc}`, async () => { | ||
circuit = await circomkit.WitnessTester(`YieldMethod`, { | ||
file: "circuits/http/interpreter", | ||
template: "YieldMethod", | ||
params: [4], | ||
}); | ||
console.log("#constraints:", await circuit.getConstraintCount()); | ||
|
||
await circuit.expectPass(input, expected); | ||
}); | ||
} | ||
|
||
// The string `"GET "` | ||
generatePassCase({ bytes: [71, 69, 84, 32] }, { MethodTag: 1 }, 0, ""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
GET /objectserver/restapi/alerts/status HTTP/1.1 | ||
GET /api HTTP/1.1 | ||
Accept: application/json | ||
Authorization: Basic dGVzdHVzZXIwMTpuZXRjb29s | ||
Host: localhost | ||
Connection: keep-alive | ||
Host: localhost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters