Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark prover/verifier perf and scalability #4

Merged
merged 4 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

# Change to the package directory
cd packages/aggsig_checker || exit 1

# Create temporary directory
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT

# Build once
make build

# Arrays to store results
declare -a events_arr
declare -a prover_times
declare -a verifier_times
declare -a proof_sizes
declare -a total_steps

# Run benchmarks
row=0
for n in $(seq 5 5 50); do
export NUM_EVENTS=$n
export OUTPUT_DIR=$TMP_DIR

make events
make args
make execute

# Capture prove output and print to stderr before processing
raw_output=$(make prove 2>&1)
echo "$raw_output" >&2

# Remove ANSI color codes
prove_output=$(echo "$raw_output" | sed 's/\x1b\[[0-9;]*m//g')

# Check if verification was successful
if ! echo "$prove_output" | grep -q "Proof verified successfully"; then
# If verification failed, store dashes and exit
events_arr[$row]=$n
prover_times[$row]="-"
verifier_times[$row]="-"
proof_sizes[$row]="-"
total_steps[$row]="-"
break
fi

# Extract timings using grep and sed
prover_time=$(echo "$prove_output" | grep "run:prove_cairo: stwo_cairo_prover::cairo_air: close" | sed -E 's/.*time.busy=([0-9.]+[ms]+).*/\1/')
verifier_time=$(echo "$prove_output" | grep "run:verify_cairo: stwo_cairo_prover::cairo_air: close" | sed -E 's/.*time.busy=([0-9.]+[ms]+).*/\1/')

# Extract total steps
steps=$(echo "$prove_output" | grep "Total steps:" | sed -E 's/.*Total steps: ([0-9]+).*/\1/')

# Get proof size in MB (using bc for floating point arithmetic)
size_bytes=$(stat -f %z "target/proof.json")
proof_size=$(echo "scale=2; $size_bytes / 1048576" | bc)

# Store results
events_arr[$row]=$n
prover_times[$row]=$prover_time
verifier_times[$row]=$verifier_time
proof_sizes[$row]="${proof_size}MB"
total_steps[$row]=$steps

((row++))
done

# Print the complete table at the end
printf "\n\n" # Add some space after the logs
printf "+------------+-------------+---------------+-------------+-------------+\n"
printf "| num events | prover time | verifier time | proof size | total steps |\n"
printf "+------------+-------------+---------------+-------------+-------------+\n"

for ((i=0; i<${#events_arr[@]}; i++)); do
printf "| %10d | %11s | %13s | %11s | %11s |\n" \
"${events_arr[$i]}" \
"${prover_times[$i]}" \
"${verifier_times[$i]}" \
"${proof_sizes[$i]}" \
"${total_steps[$i]}"
done

printf "+------------+-------------+---------------+-------------+-------------+\n"
28 changes: 20 additions & 8 deletions packages/aggsig_checker/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
NUM_EVENTS ?= 10
OUTPUT_DIR ?= tests/data

install-stwo:
RUSTFLAGS="-C target-cpu=native -C opt-level=3" \
cargo install \
--git https://github.com/starkware-libs/stwo-cairo \
--git https://github.com/m-kus/stwo-cairo \
--rev b32175604169e6fc0a7a3d35cf50d7e37865be5a \
adapted_stwo

install-requirements:
Expand All @@ -11,29 +15,37 @@ install:
$(MAKE) install-stwo
$(MAKE) install-requirements

build:
scarb --profile proving build --target-kinds executable

events:
python scripts/gen_events.py 10 > tests/data/sample_events.json
python scripts/gen_events.py $(NUM_EVENTS) > $(OUTPUT_DIR)/events.json

args:
python scripts/gen_args.py --file tests/data/sample_events.json --target cairo-run > tests/data/sample_args.json
python scripts/gen_args.py --file tests/data/sample_events.json --target execute > tests/data/sample_exec_args.json
python scripts/gen_args.py --file $(OUTPUT_DIR)/events.json --target execute > $(OUTPUT_DIR)/args.json
python scripts/gen_args.py --file $(OUTPUT_DIR)/events.json --target cairo-run > $(OUTPUT_DIR)/args_test.json

execute:
rm -rf target/execute \
&& scarb --profile proving execute --arguments-file tests/data/sample_exec_args.json --print-program-output

run:
scarb cairo-run --arguments-file tests/data/sample_args.json --print-resource-usage
&& scarb --profile proving execute \
--no-build \
--arguments-file $(OUTPUT_DIR)/args.json \
--print-program-output

prove:
adapted_stwo \
--priv_json target/execute/aggsig_checker/air_private_input.json \
--pub_json target/execute/aggsig_checker/air_public_input.json \
--params_json prover_params.json \
--proof_path target/proof.json \
--verify

e2e:
$(MAKE) events
$(MAKE) args
$(MAKE) build
$(MAKE) execute
$(MAKE) prove

burn:
scarb burn --arguments-file tests/data/args.json --output-file target/graph.svg --open-in-browser
12 changes: 9 additions & 3 deletions packages/aggsig_checker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ make install
```
## Usage

Build the Cairo program:

```sh
make build
```

Generate execution trace for a sample arguments file:

```sh
Expand Down Expand Up @@ -61,13 +67,13 @@ In order to run the Cairo program either in "dev" or in "prove" mode, you need t
Use the `gen_args.py` script to generate arguments for the program.

```sh
python scripts/gen_args.py --file tests/data/sample_events.json --target cairo-run
python scripts/gen_args.py --file tests/data/sample_events.json --target execute
```

The following command will generate arguments for the program given the events file and print them to the stdout.
Note that the `cairo-run` target is used for the "dev" mode, while the `execute` target is used for the "prove" mode.
Note that the `cairo-run` target is used for the backwards compatibility with `scarb cairo-run`, while the `execute` target is used for the `scarb execute` command.

The following command will generate arguments for the program given the events file and save them to `tests/data/sample_args.json` and `tests/data/sample_exec_args.json` respectively:
The following command will generate arguments for the program given the events file and save them to `tests/data/args.json`:

```sh
make args
Expand Down
7 changes: 5 additions & 2 deletions packages/aggsig_checker/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ cairo_execute = "2.10.0-rc.1"
[executable]

[lib]
casm = true

[profile.profiling]
unstable-add-statements-functions-debug-info = true
unstable-add-statements-code-locations-debug-info = true

[profile.proving]
inherits = "release"
Expand All @@ -22,4 +25,4 @@ enable-gas = false
cairo_test = "2.10.0-rc.1"

[scripts]
test = "scarb cairo-test && scarb cairo-run --arguments-file tests/data/sample_args.json"
test = "scarb cairo-test && scarb cairo-run --arguments-file tests/data/args_test.json"
11 changes: 11 additions & 0 deletions packages/aggsig_checker/prover_params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"channel_hash": "blake2s",
"pcs_config": {
"pow_bits": 26,
"fri_config": {
"log_last_layer_degree_bound": 0,
"log_blowup_factor": 1,
"n_queries": 70
}
}
}
2 changes: 0 additions & 2 deletions packages/aggsig_checker/src/nip01.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const TWO_POW_96: u128 = 0x1000000000000000000000000;
/// * `u256` - `sha256(tag) || sha256(tag) || bytes(rx) || bytes(px) || m` as u256
/// where tag = "BIP0340/challenge".
pub fn hash_challenge(rx: u256, px: u256, m: u256) -> u256 {
println!("m: {m}");

let mut input: Array<u32> = array![
// sha256(tag)
0x7bb52d7a,
Expand Down
Loading
Loading