Skip to content

Commit

Permalink
slight refactor and basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Aug 29, 2024
1 parent 2af0bcd commit 0bbd8d7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
34 changes: 34 additions & 0 deletions circuits/http/interpreter.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
pragma circom 2.1.9;

include "parser/language.circom";
include "../utils/array.circom";

/* TODO:
Notes --
- This is a pretty efficient way to simply check what the method used in a request is by checking
the first `DATA_LENGTH` number of bytes.
- Certainly this could be more modular.
*/
template YieldMethod(DATA_LENGTH) {
signal input bytes[DATA_LENGTH];
signal output MethodTag;

component RequestMethod = RequestMethod();
component RequestMethodTag = RequestMethodTag();

component IsGet = IsEqualArray(3);
for(var byte_idx = 0; byte_idx < 3; byte_idx++) {
IsGet.in[0][byte_idx] <== bytes[byte_idx];
IsGet.in[1][byte_idx] <== RequestMethod.GET[byte_idx];
}
signal TagGet <== IsGet.out * RequestMethodTag.GET;

component IsPost = IsEqualArray(4);
for(var byte_idx = 0; byte_idx < 4; byte_idx++) {
IsPost.in[0][byte_idx] <== bytes[byte_idx];
IsPost.in[1][byte_idx] <== RequestMethod.POST[byte_idx];
}
signal TagPost <== IsPost.out * RequestMethodTag.POST;

MethodTag <== TagGet + TagPost;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions circuits/test/http/interpreter.test.ts
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, "");
});
});

0 comments on commit 0bbd8d7

Please sign in to comment.