Skip to content

Commit

Permalink
add http build docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lonerapier committed Sep 13, 2024
1 parent 0d5f010 commit 2c783cd
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 24 deletions.
59 changes: 49 additions & 10 deletions docs/pabuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ pabuild witness extractor json --input-file examples/json/test/value_string.json
To extract reponse from HTTP, a lockfile need to be given with start line (method, status, version) and headers to be matched. Example can be found in [examples/http/lockfile](../examples/http/lockfile/).

```sh
pabuild witness extractor http --input-file examples/http/get_response.http --lockfile examples/http/lockfile/response.lock.json --circuit-name http-get-response
pabuild witness extractor http --input-file examples/http/get_response.http --lockfile examples/http/lockfile/response.lock.json --circuit-name get-response
```

## Codegen

### JSON Extraction
JSON extractor circuit is generated using rust to handle arbitrary keys and array indices.
Extractor circuit is generated using rust to handle arbitrary keys and array indices.

Run:
```sh
Expand Down Expand Up @@ -86,17 +84,14 @@ Takes 3 input arguments:
- `circuit-name`: circuit filename to save. Located in [circuits/main](../circuits/main/). Prefixed with `json_`
- `debug`: Optional circuit debug logs.

### JSON Extraction

To test an end-to-end JSON extraction proof:
- Run codegen to generate circuits. Replace `value_string` with `circuit-name`.
```sh
pabuild codegen json --circuit-name value_string --input-file examples/json/test/value_string.json --lockfile examples/json/lockfile/value_string.json -d
```

- Compile circom circuit using
```sh
circom ./circuits/main/value_string.circom --r1cs --wasm -l node_modules -o build/value_string/
```

- codegen adds circuit config to [circuits.json](../circuits.json) for circomkit support. Compile circuits using `npx circomkit compile value_string`

- Generate witness:
Expand Down Expand Up @@ -139,4 +134,48 @@ To test an end-to-end JSON extraction proof:

### HTTP Locking and Extraction

TODO
To test an end-to-end HTTP response extraction proof:
- Run codegen to generate circuits. Replace `get-response` with `circuit-name`.
```sh
pabuild codegen http --circuit-name get-response --input-file examples/http/get_response.http --lockfile examples/http/lockfile/response.lock.json -d
```

- codegen adds circuit config to [circuits.json](../circuits.json) for circomkit support. Compile circuits using `npx circomkit compile get-response`

- Generate witness:
```sh
node build/get-response/get-response_js/generate_witness.js build/get-response/get-response_js/get-response.wasm inputs/get-response/inputs.json build/get-response/witness.wtns
```
or generate using circomkit:
```bash
npx circomkit witness get-response inputs
```

- create trusted setup, circomkit downloads the required trusted setup file. Download manually, if using `snarkjs`:
```bash
npx circomkit setup get-response
# OR
snarkjs groth16 setup build/get-response/get-response.r1cs ptau/powersOfTau28_hez_final_16.ptau build/get-response/groth16_pkey.zkey

snarkjs zkey contribute build/get-response/groth16_pkey.zkey build/get-response/groth16_pkey_1.zkey --name="random" -v

snarkjs zkey beacon build/get-response/groth16_pkey_1.zkey build/get-response/groth16_pkey_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2"

snarkjs zkey verify build/get-response/get-response.r1cs ptau/powersOfTau28_hez_final_16.ptau build/get-response/groth16_pkey_final.zkey

snarkjs zkey export verificationkey build/get-response/groth16_pkey_final.zkey build/get-response/groth16_vkey.json
```

- create proof:
```bash
npx circomkit prove get-response inputs
# OR
snarkjs groth16 prove build/get-response/groth16_pkey_final.zkey build/get-response/witness.wtns build/get-response/groth16_proof.json inputs/get-response/inputs.json
```

- verify proof:
```bash
npx circomkit verify value_string value_string
# OR
snarkjs groth16 verify build/get-response/groth16_vkey.json inputs/get-response/inputs.json build/get-response/groth16_proof.json
```
8 changes: 4 additions & 4 deletions src/codegen/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Serialize for Request {
map.serialize_entry("version", self.version.as_bytes())?;

for (i, (key, value)) in self.headers.iter().enumerate() {
map.serialize_entry(&format!("key{}", i + 1), key.as_bytes())?;
map.serialize_entry(&format!("header{}", i + 1), key.as_bytes())?;
map.serialize_entry(&format!("value{}", i + 1), value.as_bytes())?;
}
map.end()
Expand All @@ -90,8 +90,8 @@ impl Serialize for Response {
map.serialize_entry("message", self.message.as_bytes())?;

for (i, (key, value)) in self.headers.iter().enumerate() {
map.serialize_entry(&format!("key{}", i + 1), key)?;
map.serialize_entry(&format!("value{}", i + 1), value)?;
map.serialize_entry(&format!("header{}", i + 1), key.as_bytes())?;
map.serialize_entry(&format!("value{}", i + 1), value.as_bytes())?;
}
map.end()
}
Expand Down Expand Up @@ -573,7 +573,7 @@ fn build_circuit_config(
}

Ok(CircomkitCircuitConfig {
file: codegen_filename,
file: format!("main/{}", codegen_filename),
template: circuit_template_name,
params,
})
Expand Down
37 changes: 27 additions & 10 deletions src/codegen/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ impl Lockfile {
}
}

fn extract_string(data: &Lockfile, circuit_buffer: &mut String, debug: bool) {
*circuit_buffer += "template ExtractStringValue(DATA_BYTES, MAX_STACK_HEIGHT, ";
fn extract_string(
config: &CircomkitCircuitConfig,
data: &Lockfile,
circuit_buffer: &mut String,
debug: bool,
) {
*circuit_buffer += &format!(
"template {}(DATA_BYTES, MAX_STACK_HEIGHT, ",
config.template
);
for (i, key) in data.keys.iter().enumerate() {
match key {
Key::String(_) => *circuit_buffer += &format!("keyLen{}, depth{}, ", i + 1, i + 1),
Expand Down Expand Up @@ -111,8 +119,16 @@ fn extract_string(data: &Lockfile, circuit_buffer: &mut String, debug: bool) {
"#;
}

fn extract_number(data: &Lockfile, circuit_buffer: &mut String, debug: bool) {
*circuit_buffer += "template ExtractNumValue(DATA_BYTES, MAX_STACK_HEIGHT, ";
fn extract_number(
config: &CircomkitCircuitConfig,
data: &Lockfile,
circuit_buffer: &mut String,
debug: bool,
) {
*circuit_buffer += &format!(
"template {}(DATA_BYTES, MAX_STACK_HEIGHT, ",
config.template
);
for (i, key) in data.keys.iter().enumerate() {
match key {
Key::String(_) => *circuit_buffer += &format!("keyLen{}, depth{}, ", i + 1, i + 1),
Expand Down Expand Up @@ -187,6 +203,7 @@ fn extract_number(data: &Lockfile, circuit_buffer: &mut String, debug: bool) {
}

fn build_json_circuit(
config: &CircomkitCircuitConfig,
data: &Lockfile,
output_filename: &String,
debug: bool,
Expand Down Expand Up @@ -518,8 +535,8 @@ fn build_json_circuit(
}

match data.value_type {
ValueType::String => extract_string(data, &mut circuit_buffer, debug),
ValueType::Number => extract_number(data, &mut circuit_buffer, debug),
ValueType::String => extract_string(config, data, &mut circuit_buffer, debug),
ValueType::Number => extract_number(config, data, &mut circuit_buffer, debug),
}

// write circuits to file
Expand Down Expand Up @@ -563,7 +580,7 @@ pub fn json_max_stack_height(input: &[u8]) -> usize {
pub fn build_circuit_config(
args: &ExtractorArgs,
lockfile: &Lockfile,
codegen_filename: String,
codegen_filename: &str,
) -> Result<CircomkitCircuitConfig, Box<dyn std::error::Error>> {
let input = fs::read(args.input_file.clone())?;

Expand Down Expand Up @@ -630,11 +647,11 @@ pub fn build_circuit_config(
pub fn json_circuit(args: ExtractorArgs) -> Result<(), Box<dyn std::error::Error>> {
let lockfile: Lockfile = serde_json::from_slice(&std::fs::read(&args.lockfile)?)?;

let codegen_filename = format!("json_{}", args.circuit_name);
let circuit_filename = format!("json_{}", args.circuit_name);

build_json_circuit(&lockfile, &codegen_filename, args.debug)?;
let config = build_circuit_config(&args, &lockfile, &circuit_filename)?;

let config = build_circuit_config(&args, &lockfile, codegen_filename)?;
build_json_circuit(&config, &lockfile, &circuit_filename, args.debug)?;

write_config(args.circuit_name, &config)?;

Expand Down

0 comments on commit 2c783cd

Please sign in to comment.