Skip to content

Commit

Permalink
fix(http): remove last machine state
Browse files Browse the repository at this point in the history
  • Loading branch information
lonerapier committed Jan 30, 2025
1 parent b03f3dc commit 717ac4d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
21 changes: 9 additions & 12 deletions circuits/http/verification.circom
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ include "circomlib/circuits/comparators.circom";
include "machine.circom";
include "../utils/hash.circom";

// TODO:
// - add `if body { PD(b[p]) }` to step_ou[0]
template HTTPVerification(DATA_BYTES, MAX_NUMBER_OF_HEADERS, PUBLIC_IO_LENGTH) {
signal input step_in[PUBLIC_IO_LENGTH];
signal output step_out[PUBLIC_IO_LENGTH];

// next_parsing_start, next_parsing_header, next_parsing_field_name, next_parsing_field_value, next_parsing_body, next_line_status, inner_main_digest, body_digest
signal input machine_state[8];
// next_parsing_start, next_parsing_header, next_parsing_field_name, next_parsing_field_value, next_parsing_body, next_line_status, inner_main_digest
signal input machine_state[7];

signal input ciphertext_digest;

// step_in[2] is the combined length of the data
// step_in[2] = ciphertext_digest ** plaintext_ctr
signal ciphertext_digest_pow[DATA_BYTES+1];
ciphertext_digest_pow[0] <== step_in[2];
signal mult_factor[DATA_BYTES];
Expand All @@ -30,7 +28,7 @@ template HTTPVerification(DATA_BYTES, MAX_NUMBER_OF_HEADERS, PUBLIC_IO_LENGTH) {
ciphertext_digest_pow[i+1] <== ciphertext_digest_pow[i] * mult_factor[i];
}
signal pt_digest <== PolynomialDigestWithCounter(DATA_BYTES)(zeroed_data, ciphertext_digest, step_in[2]);
log("inner plaintext_digest: ", pt_digest);
// log("inner plaintext_digest: ", pt_digest);

// Contains digests of start line and all intended headers (up to `MAX_NUMBER_OF_HEADERS`)
signal input main_digests[MAX_NUMBER_OF_HEADERS + 1];
Expand All @@ -41,10 +39,10 @@ template HTTPVerification(DATA_BYTES, MAX_NUMBER_OF_HEADERS, PUBLIC_IO_LENGTH) {

// assertions:
// - check step_in[3] = machine state hash digest
for (var i = 0 ; i < 8 ; i++) {
for (var i = 0 ; i < 7 ; i++) {
log("machine_state[",i,"] = ", machine_state[i]);
}
signal machine_state_digest <== PolynomialDigest(8)(machine_state, ciphertext_digest);
signal machine_state_digest <== PolynomialDigest(7)(machine_state, ciphertext_digest);
log("machine_state_digest: ", machine_state_digest);
step_in[3] === machine_state_digest;
// - check step_in[4] = start line hash digest + all header hash digests
Expand Down Expand Up @@ -130,7 +128,7 @@ template HTTPVerification(DATA_BYTES, MAX_NUMBER_OF_HEADERS, PUBLIC_IO_LENGTH) {
signal body_digest[DATA_BYTES]; // body digest
body_monomials[0] <== pow_accumulation; // (ciphertext_digest ** body_ctr) * State.parsing_body (0 if we are not in the body)
body_ctr[0] <== body_ctr_is_zero * State[0].parsing_body + (1 - body_ctr_is_zero); // checks if we are in the body
log("body_ctr[0] = ", body_ctr[0]);
// log("body_ctr[0] = ", body_ctr[0]);
// Set this to what the previous digest was
body_digest[0] <== body_monomials[0] * zeroed_data[0];
for(var i = 0 ; i < DATA_BYTES - 1 ; i++) {
Expand All @@ -149,15 +147,14 @@ template HTTPVerification(DATA_BYTES, MAX_NUMBER_OF_HEADERS, PUBLIC_IO_LENGTH) {
step_out[1] <== step_in[1];
step_out[2] <== ciphertext_digest_pow[DATA_BYTES];
// pass machine state to next iteration
step_out[3] <== PolynomialDigest(8)(
step_out[3] <== PolynomialDigest(7)(
[State[DATA_BYTES - 1].next_parsing_start,
State[DATA_BYTES - 1].next_parsing_header,
State[DATA_BYTES - 1].next_parsing_field_name,
State[DATA_BYTES - 1].next_parsing_field_value,
State[DATA_BYTES - 1].next_parsing_body,
State[DATA_BYTES - 1].next_line_status,
inner_main_digest[DATA_BYTES],
body_digest[DATA_BYTES - 1]
inner_main_digest[DATA_BYTES]
],
ciphertext_digest
);
Expand Down
14 changes: 7 additions & 7 deletions circuits/test/full/full.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe("Example NIVC Proof", async () => {
assert.deepEqual(plaintext_authentication_step_out[1], modPow(ciphertext_digest, BigInt(http_response_plaintext.length)));

// Run HTTPVerification
let machine_state = Array(8).fill(0);
let machine_state = Array(7).fill(0);
machine_state[0] = 1; // Sets the parsing start to 1

const start_line_digest = PolynomialDigest(http_start_line, ciphertext_digest, BigInt(0));
Expand Down Expand Up @@ -229,7 +229,7 @@ describe("Example NIVC Proof", async () => {
assert.deepEqual(plaintext_authentication2_step_out[0], modAdd(init_nivc_input[0], ptDigest - ciphertext_digest));

// Run HTTPVerification
let machine_state = Array(8).fill(0);
let machine_state = Array(7).fill(0);
machine_state[0] = 1; // Sets the parsing start to 1

const start_line_digest = PolynomialDigest(http_start_line, ciphertext_digest, BigInt(0));
Expand Down Expand Up @@ -356,7 +356,7 @@ describe("Example NIVC Proof", async () => {
let main_digests = Array(MAX_NUMBER_OF_HEADERS + 1).fill(0);
main_digests[0] = start_line_digest;
main_digests[1] = header_0_digest;
let machine_state = Array(8).fill(0);
let machine_state = Array(7).fill(0);
machine_state[0] = 1; // Sets the parsing start to 1

let http_verification1 = await HTTPVerification.compute({
Expand All @@ -371,7 +371,7 @@ describe("Example NIVC Proof", async () => {
assert.deepEqual(http_verification1_step_out[5], BigInt(0)); // all matched
assert.deepEqual(http_verification1_step_out[6], BigInt(0)); // body doesn't start yet

machine_state = [0, 0, 0, 0, 1, 0, 0, 0];
machine_state = [0, 0, 0, 0, 1, 0, 0];
let bodyDigest = PolynomialDigest(http_response2_0_padded, ciphertext_digest, BigInt(0));
console.log("bodyDigest", bodyDigest);

Expand Down Expand Up @@ -489,9 +489,9 @@ describe("Example NIVC Proof", async () => {
assert.deepEqual(plaintextAuthenticationStepOut[0], modAdd(init_nivc_input[0] - requestCiphertextDigest, plaintextDigest));

// Run HTTPVerification
let machineState = Array(8).fill(0);
let machineState = Array(7).fill(0);
machineState[0] = 1; // Sets the parsing start to 1
let machineState2 = [0, 0, 0, 0, 1, 0, 0, BigInt("4597741686932796891854080771126033240062027585521956904102484899697963333774")];
let machineState2 = [0, 0, 0, 0, 1, 0, 0];
let machineStates = [machineState, machineState2];
let prevBodyCounter = BigInt(0);

Expand Down Expand Up @@ -603,7 +603,7 @@ describe("Example NIVC Proof", async () => {
assert.deepEqual(responsePlaintextAuthenticationStepOut[0], modAdd(init_nivc_input[0] - prevResponseCtDigest, responsePlaintextDigest, requestBodyDigest));

// Run response HTTPVerification
let responseMachineState = Array(8).fill(0);
let responseMachineState = Array(7).fill(0);
responseMachineState[0] = 1; // Sets the parsing start to 1
let responseMachineState2 = [0, 0, 0, 0, 1, 0, 0, BigInt("17254457507538573663580074635865728502553125514899317196086652572055478623335")];
let responseMachineState3 = [0, 0, 0, 0, 1, 0, 0, BigInt("17485877200063103672489113686823507448308624682483242290501494756760660668524")];
Expand Down
17 changes: 9 additions & 8 deletions circuits/test/http/verification.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { circomkit, WitnessTester, PolynomialDigest, http_response_plaintext, http_start_line, http_header_0, http_header_1, http_body, modAdd, PUBLIC_IO_VARIABLES, modPow } from "../common";
import { assert } from "chai";
import { poseidon1 } from "poseidon-lite";
import { poseidon1, poseidon2 } from "poseidon-lite";

// HTTP/1.1 200 OK
// content-type: application/json; charset=utf-8
Expand Down Expand Up @@ -32,10 +32,10 @@ describe("HTTP Verification", async () => {
params: [DATA_BYTES, MAX_NUMBER_OF_HEADERS, PUBLIC_IO_VARIABLES]
});
});
const mock_ct_digest = poseidon1([69]);
const mock_ct_digest = poseidon2([69, 420]);

// Used across tests
let machine_state = Array(8).fill(0);
let machine_state = Array(7).fill(0);
machine_state[0] = 1; // Sets the parsing start to 1
let step_in = Array(PUBLIC_IO_VARIABLES).fill(0);
step_in[2] = 1; // ciphertext_digest_pow
Expand Down Expand Up @@ -142,10 +142,11 @@ describe("HTTP Verification: Split", async () => {
});
});
const mock_ct_digest = poseidon1([69]);
console.log("mock_ct_digest: ", mock_ct_digest);
// const mock_ct_digest = BigInt(2); // TODO: only for debugging!!!

// Used across tests
let machine_state = Array(8).fill(0);
let machine_state = Array(7).fill(0);
machine_state[0] = 1; // Sets the parsing start to 1
let step_in = Array(PUBLIC_IO_VARIABLES).fill(0);
step_in[2] = 1; // ciphertext_digest_pow
Expand Down Expand Up @@ -190,7 +191,7 @@ describe("HTTP Verification: Split", async () => {
ciphertext_digest: mock_ct_digest
}, ["step_out"]);

let next_machine_state = [0, 0, 0, 0, 1, 0, 0, BigInt("16202935654388484508586282728137211277659125633297860980774265969941813526293")];
let next_machine_state = [0, 0, 0, 0, 1, 0, 0];
// let next_machine_state = [0, 0, 0, 0, 1, 0, 0, BigInt("140302555479869")]; // TODO: Only for debuggin!
let next_step_in = (http_nivc_compute_0.step_out as bigint[]).slice(0, PUBLIC_IO_VARIABLES);
let http_nivc_compute_1 = await HTTPVerification.compute({
Expand Down Expand Up @@ -219,7 +220,7 @@ describe("HTTP Verification: Split", async () => {
ciphertext_digest: mock_ct_digest
}, ["step_out"]);

let next_machine_state = [0, 0, 0, 0, 1, 0, 0, BigInt("16202935654388484508586282728137211277659125633297860980774265969941813526293")];
let next_machine_state = [0, 0, 0, 0, 1, 0, 0];
// let next_machine_state = [0, 0, 0, 0, 1, 0, 0, BigInt("140302555479869")]; // TODO: Only for debuggin!
let next_step_in = (http_nivc_compute_0.step_out as bigint[]).slice(0, PUBLIC_IO_VARIABLES);
let http_nivc_compute_1 = await HTTPVerification.compute({
Expand Down Expand Up @@ -247,7 +248,7 @@ describe("HTTP Verification: Split", async () => {
ciphertext_digest: mock_ct_digest
}, ["step_out"]);

let next_machine_state = [0, 0, 0, 0, 1, 0, 0, BigInt("16202935654388484508586282728137211277659125633297860980774265969941813526293")];
let next_machine_state = [0, 0, 0, 0, 1, 0, 0];
// let next_machine_state = [0, 0, 0, 0, 1, 0, 0, BigInt("140302555479869")]; // TODO: Only for debuggin!
let next_step_in = (http_nivc_compute_0.step_out as bigint[]).slice(0, PUBLIC_IO_VARIABLES);
let http_nivc_compute_1 = await HTTPVerification.compute({
Expand Down Expand Up @@ -275,7 +276,7 @@ describe("HTTP Verification: Split", async () => {
ciphertext_digest: mock_ct_digest
}, ["step_out"]);

let next_machine_state = [0, 0, 0, 0, 1, 0, 0, BigInt("16202935654388484508586282728137211277659125633297860980774265969941813526293")];
let next_machine_state = [0, 0, 0, 0, 1, 0, 0];
// let next_machine_state = [0, 0, 0, 0, 1, 0, 0, BigInt("140302555479869")]; // TODO: Only for debuggin!
let next_step_in = (http_nivc_compute_0.step_out as bigint[]).slice(0, PUBLIC_IO_VARIABLES);
let http_nivc_compute_1 = await HTTPVerification.compute({
Expand Down

0 comments on commit 717ac4d

Please sign in to comment.