2024-11-14 10:31:32 +01:00
# Workflow of the Storage Proof Circuits
WARNING: This is a work-in-progress prototype, and has not received careful code review. This implementation is NOT ready for production use.
This crate guides you through generating the circuit input,
2025-04-10 12:41:32 +02:00
building the circuit,
running the circuits to generate a proof, aggregating multiple proofs, and finally verify the proof.
2024-11-14 10:31:32 +01:00
This crate can be used to:
- Generate circuit input from **fake data** with given params.
2025-04-10 12:41:32 +02:00
- Build the Plonky2 codex storage proof circuits.
2024-11-14 10:31:32 +01:00
- Generate a proof with given proof input in JSON file.
2025-04-10 12:41:32 +02:00
- Aggregate multiple proofs with 2-to-1 tree like aggregation to generate a final proof.
2024-11-14 10:31:32 +01:00
- Verify the proof.
## Code organization
2025-04-10 12:41:32 +02:00
- [`gen_input` ](./src/bin/gen_input.rs ) contains the main function to generate input with the given params as environment variables.
2024-11-14 10:31:32 +01:00
2025-04-10 12:41:32 +02:00
- [`build_circ` ](./src/bin/build_circ.rs ) contains the main function to build the storage proof (Sampling) circuits.
2024-11-14 10:31:32 +01:00
2025-04-10 12:41:32 +02:00
- [`prove` ](./src/bin/prove.rs ) contains the main function to generate a single proof.
2024-11-14 10:31:32 +01:00
2025-04-10 12:41:32 +02:00
- [`aggregate` ](./src/bin/aggregate.rs ) contains the main function to aggregate `k` (default = 4) proofs.
2024-11-14 10:31:32 +01:00
2025-04-10 12:41:32 +02:00
- [`verify` ](./src/bin/verify ) contains the main function to verify the storage proof (Sampling) proof.
2024-11-14 10:31:32 +01:00
## Usage
### Prerequisites
- **Rust Toolchain**: Ensure you have Rust installed. If not, install it from [rustup.rs ](https://rustup.rs/ ).
- **Rust nightly**:: This crate requires the Rust nightly compiler. To install the nightly toolchain, use `rustup` :
```bash
rustup install nightly
```
To ensure that the nightly toolchain is used when building this crate, you can set the override in the project directory:
```bash
rustup override set nightly
```
### Generate Circuit Input
The steps to generate circuit input with **fake data** are the following:
#### Step 1: Setting Up Parameters
2025-03-10 14:49:13 +01:00
Parameters for generating the circuit input can be defined in [`params.sh` ](scripts/params.sh ).
2024-11-14 10:31:32 +01:00
You can customize the test parameters by setting the following environment variables:
```bash
export MAXDEPTH=32 # Maximum depth of the slot tree
export MAXSLOTS=256 # Maximum number of slots
export CELLSIZE=2048 # Cell size in bytes
export BLOCKSIZE=65536 # Block size in bytes
2025-04-10 12:41:32 +02:00
export NSAMPLES=100 # Number of samples to prove
2024-11-14 10:31:32 +01:00
export ENTROPY=1234567 # External randomness
export SEED=12345 # Seed for creating fake data
export NSLOTS=11 # Number of slots in the dataset
export SLOTINDEX=3 # Which slot to prove (0..NSLOTS-1)
export NCELLS=512 # Number of cells in this slot
```
#### Step 2: Run the Script
2025-04-10 12:41:32 +02:00
Once the params are set, you can run the script to generate the circuit input (with fake data).
2024-11-14 10:31:32 +01:00
```bash
2025-03-10 14:49:13 +01:00
sudo bash ./scripts/gen_input.sh
2024-11-14 10:31:32 +01:00
```
### Build the Circuit
To build the circuit and measure the time to build, you can simply run the script:
```bash
2025-03-10 14:49:13 +01:00
sudo bash ./scripts/build_circuit.sh
2024-11-14 10:31:32 +01:00
```
To see the source code of how to build the circuit, see [`build_circ` ](./src/bin/build_circ.rs ).
### Generate the Proof
2025-04-10 12:41:32 +02:00
After generating the circuit input (in a JSON file), you can run the circuits to generate the proofs.
First make sure you have the circuit data and input from previous scripts then follow the steps:
2024-11-14 10:31:32 +01:00
#### Step 1: Setting Up Circuit Parameters
2025-03-10 14:49:13 +01:00
Parameters for the circuit can be defined in [`circ_params.sh` ](scripts/circ_params.sh ).
2024-11-14 10:31:32 +01:00
You can customize the test parameters by setting the following environment variables:
```bash
export MAX_DEPTH=32 # maximum depth of the slot tree
export MAX_LOG2_N_SLOTS=8 # Depth of the dataset tree = ceiling_log2(max_slots)
export BLOCK_TREE_DEPTH=5 # depth of the mini tree (block tree)
export N_FIELD_ELEMS_PER_CELL=272 # number of field elements per cell
2025-04-10 12:41:32 +02:00
export N_SAMPLES=100 # number of samples to prove
2024-11-14 10:31:32 +01:00
```
#### Step 2: Run the Script
Once the params are set, you can run the script to generate the proof.
You can also see the time taken to generate the proof.
```bash
2025-03-10 14:49:13 +01:00
sudo bash ./scripts/prove.sh
2024-11-14 10:31:32 +01:00
```
2025-04-10 12:41:32 +02:00
### Verify the proof
To verify the generated proof, run the following script.
Make sure that you generate the circuit data and proof prior to this.
```bash
sudo bash ./scripts/verify.sh
```
2024-11-14 10:31:32 +01:00
### Build, Prove, and Verify
To automate the whole process, you can run the following script
the script builds the circuit, loads the JSON circuit input, generates the proof, and verifies it.
It also shows the time taken for each step.
2025-03-10 14:49:13 +01:00
Make sure that you generate the circuit input prior to this so that you have the [`JSON input file` ](./input.json ) and set the [`circ_params.sh` ](scripts/circ_params.sh ).
2024-11-14 10:31:32 +01:00
```bash
2025-03-10 14:49:13 +01:00
sudo bash ./scripts/prove_and_verify.sh
2024-11-14 10:31:32 +01:00
```
2025-04-10 12:41:32 +02:00
This script simply runs all previous scripts.
2025-03-10 14:49:13 +01:00
2025-04-10 12:41:32 +02:00
### Aggregate K Proofs
To do this, you can run the following script.
Note that we don't actually generate `K` proofs but rather just clone the single generated proof which is enough for testing.
Make sure a proof is already generated using the script for proving above.
2025-03-10 14:49:13 +01:00
```bash
sudo bash ./scripts/aggregate.sh
```
or the following if you want to specify the number of proofs to be aggregated
```bash
sudo bash ./scripts/aggregate.sh -- "$K_VALUE"
```