Skip to content

Commit

Permalink
Add Benchmark.mdx doc
Browse files Browse the repository at this point in the history
Summary:

Add Benchmark.md up to 13-th Fibonacci  number.
  • Loading branch information
duc-nx committed Aug 15, 2024
1 parent 15bfc3f commit 9eea9a0
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/pages/zkvm/_meta.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cli-quick-start": "CLI Quick Start",
"sdk-quick-start": "SDK Quick Start",
"configuring-prover": "Configuring the Prover"
"configuring-prover": "Configuring the Prover",
"sdk-benchmarking": "SDK Benchmarking"
}
161 changes: 161 additions & 0 deletions docs/pages/zkvm/sdk-benchmarking.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
---
title: "Nexus zkVM SDK Benchmark"
lang: en-US
description: "Benchmark your Nexus project using the SDK"
image: "/nexus-head.png"
---

import Image from "next/image";

import { Steps } from "nextra/components";

## Reproducible Proving and Verification

<Steps>

This example demonstrates how to use the Nexus SDK to generate proving and verification keys, compile a guest program, prove its execution, and verify the proof. The example uses the `guest` package as an example guest program.

The `src/main.rs` at host side is as follows:

```rust
use nexus_sdk::{
compile::CompileOpts,
nova::seq::{Generate, Nova, PP},
Local, Prover, Verifiable,
};

const PACKAGE: &str = "guest";

fn generate_pp_and_compile() -> (PP, Nova<Local>) {
let pp: PP = PP::generate().expect("failed to generate parameters");

let mut opts = CompileOpts::new(PACKAGE);
opts.set_memlimit(8); // use an 8mb memory

let prover: Nova<Local> = Nova::compile(&opts).expect("failed to compile guest program");

(pp, prover)
}

fn prove_execution(pp: &PP, prover: Nova<Local>) -> nexus_sdk::nova::seq::Proof {
let proof: nexus_sdk::nova::seq::Proof = prover.prove(pp).expect("failed to prove program");

proof
}

fn verify_execution(pp: &PP, proof: &nexus_sdk::nova::seq::Proof) {
proof.verify(pp).expect("failed to verify proof");
}

fn main() {
use std::time::Instant;

let start = Instant::now();
let (pp, prover) = generate_pp_and_compile();
let duration = start.elapsed();
println!(
"Time taken to generate PP and compile: {:.2} seconds",
duration.as_secs_f64()
);

let start = Instant::now();
let proof = prove_execution(&pp, prover);
let duration = start.elapsed();
println!(
"Time taken to prove execution: {:.2} seconds",
duration.as_secs_f64()
);

let start = Instant::now();
verify_execution(&pp, &proof);
let duration = start.elapsed();
println!(
"Time taken to verify execution: {:.2} seconds",
duration.as_secs_f64()
);
}
```

The `src/guest/src/main.rs` at guest side is as follows:

```rust
#![no_std]
#![no_main]

#[nexus_rt::profile]
fn fibonacci(n: u32) -> u32 {
fib(n)
}

fn fib(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fib(n - 1) + fib(n - 2),
}
}



#[nexus_rt::main]
fn main() {
let n = 13;
assert_eq!(233, fibonacci(n));
}
```

## Build flags

```rust
RUSTFLAGS='-C target-cpu=apple-m1' cargo build --release --package cargo-nexus --bin cargo-nexus
```

## Fibbonacci benchmark

The results below are obtained in 2024-08 on a MacBook Air M1 with 16 Gb of RAM.

### Cycles count

Run command at `src/guest/` directory: `cargo-nexus nexus run 2>/dev/null`


| n-th Fibonacci | Fibonacci (cycles) | % of total | Cycle count overhead | Total program cycles |
|----------------|--------------------------|------------|----------------------|----------------------|
| 1 | 132 | 42% | 178 | 310 |
| 1 | 237 | 57% | 178 | 415 |
| 2 | 349 | 66% | 178 | 527 |
| 3 | 566 | 76% | 178 | 744 |
| 5 | 895 | 83% | 178 | 1073 |
| 8 | 1441 | 89% | 178 | 1619 |
| 13 | 2316 | 92% | 178 | 2494 |
| 21 | 3737 | 95% | 178 | 3915 |
| 34 | 6033 | 97% | 178 | 6211 |
| 55 | 9750 | 98% | 178 | 9928 |
| 89 | 15763 | 98% | 178 | 15941 |
| 144 | 25493 | 99% | 178 | 25671 |
| 233 | 41236 | 99% | 178 | 41414 |



### Execution time

Prove command at the root directory: `cargo run --release 2>/dev/null`

| n-th Fibonacci | Generate PP and Compile (seconds) | Prove (seconds) | Verify (seconds) | Total Time (seconds)|
|----------------|-----------------------------------|-----------------|------------------|---------------------|
| 1 | 20.94 | 1.27 | 0.79 | 25.00 |
| 1 | 27.88 | 4.21 | 1.39 | 34.00 |
| 2 | 28.82 | 10.40 | 1.70 | 42.00 |
| 3 | 26.22 | 14.67 | 1.51 | 43.00 |
| 5 | 25.03 | 21.55 | 1.64 | 49.00 |
| 8 | 24.99 | 33.51 | 1.63 | 61.00 |
| 13 | 24.99 | 77.45 | 2.03 | 105.00 |
| 21 | 22.62 | 96.93 | 1.74 | 123.00 |
| 34 | 25.10 | 145.54 | 1.72 | 207.00 |
| 55 | 25.13 | 235.26 | 1.73 | 263.00 |
| 89 | 24.83 | 384.73 | 1.71 | 412.00 |
| 144 | 25.28 | 629.38 | 1.88 | 658.00 |
| 233 | 25.22 | 1028.26 | 1.78 | 1056.00 |



0 comments on commit 9eea9a0

Please sign in to comment.