Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Sep 10, 2024
1 parent d1c6b92 commit d2ca595
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 112 deletions.
2 changes: 1 addition & 1 deletion circuits/http/extractor.circom
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ template ExtractResponse(DATA_BYTES, maxContentLength) {
signal isPrevStartingIndex[DATA_BYTES];
valueStartingIndex[0] <== 0;
isZeroMask[0] <== IsZero()(dataMask[0]);
for (var i=1 ; i<DATA_BYTES ; i++) {
for (var i=1 ; i < DATA_BYTES; i++) {
isZeroMask[i] <== IsZero()(dataMask[i]);
isPrevStartingIndex[i] <== IsZero()(valueStartingIndex[i-1]);
valueStartingIndex[i] <== valueStartingIndex[i-1] + i * (1-isZeroMask[i]) * isPrevStartingIndex[i];
Expand Down
107 changes: 61 additions & 46 deletions circuits/test/http/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,41 @@ import { join } from "path";
import { spawn } from "child_process";
import { readFileSync } from "fs";

function readLockFile(filename: string): HttpData {
function readLockFile<T>(filename: string): T {
const filePath = join(__dirname, "..", "..", "..", "examples", "lockfile", filename);
const jsonString = readFileSync(filePath, 'utf-8');
const jsonData = JSON.parse(jsonString);
return jsonData;
}

interface HttpData {
request: Request;
response: Response;
function getHeaders(data: Request | Response): [string, string][] {
const headers: [string, string][] = [];
let i = 1;
while (true) {
const nameKey = `headerName${i}`;
const valueKey = `headerValue${i}`;
if (nameKey in data && valueKey in data) {
headers.push([data[nameKey], data[valueKey]]);
i++;
} else {
break;
}
}
return headers;
}

interface Request {
method: string,
target: string,
version: string,
headers: [string, string][],
[key: string]: string,
}

interface Response {
version: string,
status: string,
message: string,
headers: [string, string][],
[key: string]: string,
}


Expand Down Expand Up @@ -54,86 +65,90 @@ function executeCodegen(inputFilename: string, outputFilename: string) {
});
}

describe("HTTP :: Codegen", async () => {
let circuit: WitnessTester<["data", "beginning", "middle", "final", "header1", "value1", "header2", "value2"], []>;
describe("HTTP :: Codegen :: Request", async () => {
let circuit: WitnessTester<["data", "method", "target", "version", "header1", "value1", "header2", "value2"], []>;

it("(valid) get_request:", async () => {
let lockfile = "test.lock";
it("(valid) GET:", async () => {
let lockfile = "request.lock";
let inputfile = "get_request.http";

// generate extractor circuit using codegen
await executeCodegen(`${lockfile}.json`, lockfile);

const lockData = await readLockFile(`${lockfile}.json`);
const lockData = await readLockFile<Request>(`${lockfile}.json`);
console.log("lockData: ", JSON.stringify(lockData));

const input = await readHTTPInputFile(`${inputfile}`).input

const params = [input.length, lockData.request.method.length, lockData.request.target.length, lockData.request.version.length];
lockData.request.headers.forEach(header => {
params.push(header[0].length); // Header name length
params.push(header[1].length); // Header value length
console.log("header: ", header[0]);
console.log("value: ", header[1]);
const headers = getHeaders(lockData);
const params = [input.length, lockData.method.length, lockData.target.length, lockData.version.length];
headers.forEach(header => {
params.push(header[0].length);
params.push(header[1].length);
});


circuit = await circomkit.WitnessTester(`Extract`, {
file: `circuits/main/${lockfile}`,
template: "LockHTTP",
template: "LockHTTPRequest",
params: params,
});
console.log("#constraints:", await circuit.getConstraintCount());

// match circuit output to original JSON value
await circuit.expectPass({
const circuitInput: any = {
data: input,
beginning: toByte(lockData.request.method),
middle: toByte(lockData.request.target),
final: toByte(lockData.request.version),
header1: toByte(lockData.request.headers[0][0]),
value1: toByte(lockData.request.headers[0][1]),
header2: toByte(lockData.request.headers[1][0]),
value2: toByte(lockData.request.headers[1][1])
},
{}
);
method: toByte(lockData.method),
target: toByte(lockData.target),
version: toByte(lockData.version),
};

headers.forEach((header, index) => {
circuitInput[`header${index + 1}`] = toByte(header[0]);
circuitInput[`value${index + 1}`] = toByte(header[1]);
});
await circuit.expectPass(circuitInput, {});
});

it("(invalid) get_request:", async () => {
let lockfile = "test.lock";
it("(invalid) GET:", async () => {
let lockfile = "request.lock";
let inputfile = "get_request.http";

// generate extractor circuit using codegen
await executeCodegen(`${lockfile}.json`, lockfile);

const lockData = await readLockFile(`${lockfile}.json`);
const lockData = await readLockFile<Request>(`${lockfile}.json`);

const input = await readHTTPInputFile(`${inputfile}`).input

const params = [input.length, lockData.request.method.length, lockData.request.target.length, lockData.request.version.length];
lockData.request.headers.forEach(header => {
params.push(header[0].length); // Header name length
params.push(header[1].length); // Header value length
const headers = getHeaders(lockData);
const params = [input.length, lockData.method.length, lockData.target.length, lockData.version.length];
headers.forEach(header => {
params.push(header[0].length);
params.push(header[1].length);
});


circuit = await circomkit.WitnessTester(`Extract`, {
file: `circuits/main/${lockfile}`,
template: "LockHTTP",
template: "LockHTTPRequest",
params: params,
});
console.log("#constraints:", await circuit.getConstraintCount());

await circuit.expectFail({
data: input.slice(0),
beginning: toByte(lockData.request.method),
middle: toByte(lockData.request.target),
final: toByte(lockData.request.version),
header1: toByte(lockData.request.headers[0][0]),
value1: toByte("/aip"),
header2: toByte(lockData.request.headers[1][0]),
value2: toByte(lockData.request.headers[1][1])
const circuitInput: any = {
data: input,
method: toByte(lockData.method),
target: toByte(lockData.target),
version: toByte(lockData.version),
};

headers.forEach((header, index) => {
circuitInput[`header${index + 1}`] = toByte(header[0]);
circuitInput[`value${index + 1}`] = toByte(header[1]);
});

circuitInput.value1 = toByte("/aip");
await circuit.expectFail(circuitInput);
});
});
Loading

0 comments on commit d2ca595

Please sign in to comment.