cargo-risczero 1.1.0

RISC Zero CLI tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright 2024 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{
    hint::black_box,
    path::PathBuf,
    time::{Duration, Instant},
};

use ::serde::Serialize;
use anyhow::Result;
use clap::ValueEnum;
use risc0_zkp::MAX_CYCLES_PO2;
use risc0_zkvm::{
    ApiClient, Asset, AssetRequest, ExecutorEnv, ExecutorEnvBuilder, ProveInfo, ProverOpts,
    ReceiptClaim, ReceiptKind, SegmentInfo, SessionInfo, SuccinctReceipt, VerifierContext,
    RECURSION_PO2,
};
use serde_with::{serde_as, DurationSeconds};
use tabled::{settings::Style, Table, Tabled};

/// `cargo risczero datasheet`
#[derive(clap::Parser)]
#[non_exhaustive]
pub struct Datasheet {
    /// Filter which benchmarks to run.
    #[arg(name = "BENCHMARKS")]
    pub filter: Vec<Benchmark>,

    /// Path to the RISC Zero server.
    #[arg(long, value_name = "PATH", env = "RISC0_SERVER_PATH")]
    pub r0vm: Option<PathBuf>,

    /// Path to the JSON results file.
    #[arg(long, value_name = "PATH")]
    pub json: Option<PathBuf>,

    /// Max size for composite runs.
    #[arg(long, default_value_t = 20, value_parser = parse_po2)]
    pub max_po2: u32,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
#[non_exhaustive]
#[clap(rename_all = "snake_case")]
pub enum Benchmark {
    Execute,
    Rv32im,
    Lift,
    Join,
    Succinct,
    IdentityP254,
}

/// Pre-compiled program that simply loops `count: u32` times (read from stdin).
const LOOP_ELF: &[u8] = include_bytes!("loop.bin");

/// Powers-of-two for cycles, paired with the number of loop iterations used to
/// achieve that many cycles.
const CYCLES_PO2_ITERS: &[(u32, u32)] = &[
    (15, 1),               // 15, 32K
    (16, 1024 * 8),        // 16, 64K
    (17, 1024 * 32),       // 17, 128K
    (18, 1024 * 64),       // 18, 256K
    (19, 1024 * 128),      // 19, 512K
    (20, 1024 * 256),      // 20, 1M
    (21, 1024 * 256 * 3),  // 21, 2M
    (22, 1024 * 256 * 7),  // 22, 4M
    (23, 1024 * 256 * 15), // 23, 8M
    (24, 1024 * 256 * 31), // 24, 16M
];

const MIN_CYCLES_PO2: u32 = CYCLES_PO2_ITERS[0].0;

fn parse_po2(s: &str) -> Result<u32, String> {
    let po2 = s.parse().map_err(|_| format!("`{s}` must be an integer"))?;

    if (MIN_CYCLES_PO2..=MAX_CYCLES_PO2 as u32).contains(&po2) {
        Ok(po2)
    } else {
        Err(format!(
            "po2 must be in range: {MIN_CYCLES_PO2}-{MAX_CYCLES_PO2}"
        ))
    }
}

impl Datasheet {
    /// Returns which benchmarks the user has chosen to run.
    ///
    /// Each benchmark is provided at most once in enum case order.
    fn benchmarks(&self) -> impl Iterator<Item = Benchmark> + '_ {
        Benchmark::value_variants()
            .iter()
            .filter(|benchmark| self.filter.is_empty() || self.filter.contains(benchmark))
            .copied()
    }

    /// Creates a connection to the zkVM server (`r0vm` executable).
    ///
    /// The path is provided by `--r0vm`, `$RISC0_SERVER_PATH`, or searching
    /// `$PATH`.
    fn connect_to_server(&self) -> Result<ApiClient> {
        if let Some(server_path) = &self.r0vm {
            ApiClient::new_sub_process(server_path)
        } else {
            ApiClient::from_env()
        }
    }

    pub fn run(&self) -> Result<()> {
        let client = self.connect_to_server()?;

        // Run warmup prior to proving to ensure GPU kernels are compiled and
        // ready to use.
        println!("warmup");
        _ = black_box(util::prove(
            &client,
            &util::loop_env(0)?,
            LOOP_ELF,
            &ProverOpts::all_po2s().with_receipt_kind(ReceiptKind::Succinct),
        )?);

        let mut data = Vec::new();

        for benchmark in self.benchmarks() {
            match benchmark {
                Benchmark::Rv32im => {
                    for hashfn in ["sha-256", "poseidon2"] {
                        for &(po2, iters) in CYCLES_PO2_ITERS
                            .iter()
                            .filter(|(po2, _)| *po2 <= self.max_po2)
                        {
                            data.push(benches::rv32im(&client, hashfn, iters, po2)?);
                        }
                    }
                }

                Benchmark::Execute => data.push(benches::execute(&client, 128 * 1024)?),
                Benchmark::Succinct => data.push(benches::succinct(&client, 64 * 1024)?),
                Benchmark::Lift => data.push(benches::lift(&client)?),
                Benchmark::Join => data.push(benches::join(&client)?),
                Benchmark::IdentityP254 => data.push(benches::identity_p254(&client)?),
            }
        }

        // Emit results to stdout as pretty table.
        println!("{}", Table::new(&data).with(Style::modern()));

        // Emit results to path as JSON if requested.
        if let Some(json_path) = &self.json {
            let json = serde_json::to_string_pretty(&data)?;

            if let Some(json_dir) = json_path.parent() {
                std::fs::create_dir_all(json_dir)?;
            }

            std::fs::write(json_path, json)?;
        }

        Ok(())
    }
}

// TODO: Emit RAM use.
#[serde_as]
#[derive(Debug, Serialize, Tabled)]
struct BenchmarkData {
    name: &'static str,
    hashfn: String,

    /// Cycles per second.
    #[tabled(display_with = "display::hertz")]
    throughput: f64,

    #[serde_as(as = "DurationSeconds")]
    #[tabled(display_with = "display::duration")]
    duration: Duration,

    /// Either user execution cycle count or the total cycle count.
    ///
    /// When this is the total cycle count, it includes overhead associated with
    /// continuations and padding up to the nearest power of 2.
    #[tabled(display_with = "display::cycles")]
    cycles: u64,

    #[tabled(display_with = "display::bytes")]
    seal: u64,
}

mod util {
    use super::*;

    pub fn execute(client: &ApiClient, env: &ExecutorEnv, elf: &[u8]) -> Result<SessionInfo> {
        execute_with_segments(client, env, elf, |_, _| Ok(()))
    }

    pub fn execute_with_segments<F>(
        client: &ApiClient,
        env: &ExecutorEnv,
        elf: &[u8],
        segment_callback: F,
    ) -> Result<SessionInfo>
    where
        F: FnMut(SegmentInfo, Asset) -> Result<()>,
    {
        let binary = Asset::Inline(elf.to_vec().into());
        let segments_out = AssetRequest::Inline;
        client.execute(env, binary, segments_out, segment_callback)
    }

    pub fn prove(
        client: &ApiClient,
        env: &ExecutorEnv,
        elf: &[u8],
        opts: &ProverOpts,
    ) -> Result<ProveInfo> {
        let image_id = risc0_zkvm::compute_image_id(elf)?;
        let binary = risc0_zkvm::Asset::Inline(elf.to_vec().into());
        let prove_info = client.prove(env, opts, binary)?;

        let ctx = VerifierContext::all_po2s();
        if opts.prove_guest_errors {
            prove_info.receipt.verify_integrity_with_context(&ctx)?;
        } else {
            prove_info.receipt.verify_with_context(&ctx, image_id)?;
        }

        Ok(prove_info)
    }

    pub fn loop_env<'a>(iters: u32) -> Result<ExecutorEnv<'a>> {
        loop_env_builder(iters).build()
    }

    pub fn loop_env_builder<'a>(iters: u32) -> ExecutorEnvBuilder<'a> {
        let mut builder = ExecutorEnvBuilder::default();
        builder.write_slice(&iters.to_le_bytes());
        builder
    }
}

/// Benchmark implementations.
mod benches {
    use risc0_zkvm::InnerReceipt;

    use super::*;

    const RECURSION_CYCLES: u64 = 1 << RECURSION_PO2;

    pub fn execute(client: &ApiClient, iters: u32) -> Result<BenchmarkData> {
        println!("execute: {iters}");

        let env = util::loop_env(iters)?;
        let (session, duration) = try_time(|| util::execute(client, &env, LOOP_ELF))?;

        // NOTE: We use user cycles as the total because there is no proving.
        let cycles = session.cycles();

        Ok(BenchmarkData {
            name: "execute",
            hashfn: "N/A".into(),
            throughput: cycles as f64 / duration.as_secs_f64(),
            duration,
            cycles,
            seal: 0,
        })
    }

    pub fn rv32im(client: &ApiClient, hashfn: &str, iters: u32, po2: u32) -> Result<BenchmarkData> {
        let expected_cycles = 1u64 << po2;
        println!("rv32im ({hashfn}): {expected_cycles}");

        let opts = ProverOpts::all_po2s().with_hashfn(hashfn.to_string());
        let env = util::loop_env(iters)?;
        let (info, duration) = try_time(|| util::prove(client, &env, LOOP_ELF, &opts))?;

        let cycles = info.stats.total_cycles;
        assert_eq!(
            cycles, expected_cycles,
            "Incorrect cycle count for po2={po2}"
        );

        Ok(BenchmarkData {
            name: "rv32im",
            hashfn: hashfn.into(),
            seal: info.receipt.inner.composite()?.seal_size() as u64,
            throughput: cycles as f64 / duration.as_secs_f64(),
            duration,
            cycles,
        })
    }

    pub fn succinct(client: &ApiClient, iters: u32) -> Result<BenchmarkData> {
        println!("succinct: {iters}");

        let opts = ProverOpts::all_po2s().with_receipt_kind(ReceiptKind::Succinct);
        let env = util::loop_env(iters)?;
        let (info, duration) = try_time(|| util::prove(client, &env, LOOP_ELF, &opts))?;

        let cycles = info.stats.total_cycles;

        Ok(BenchmarkData {
            name: "succinct",
            hashfn: opts.hashfn,
            seal: info.receipt.inner.succinct()?.seal_size() as u64,
            throughput: cycles as f64 / duration.as_secs_f64(),
            duration,
            cycles,
        })
    }

    pub fn lift(client: &ApiClient) -> Result<BenchmarkData> {
        println!("lift");

        let env = util::loop_env(0)?;
        let opts = ProverOpts::all_po2s();

        let segment: Asset = {
            let mut segment_count = 0;
            let mut segment = None::<Asset>;

            util::execute_with_segments(client, &env, LOOP_ELF, |_, next_segment| {
                segment_count += 1;
                segment = Some(next_segment);
                Ok(())
            })?;

            assert_eq!(segment_count, 1);
            segment.unwrap()
        };

        let receipt = client.prove_segment(&opts, segment, AssetRequest::Inline)?;

        let (receipt, duration) =
            try_time(|| client.lift(&opts, receipt.try_into()?, AssetRequest::Inline))?;

        let cycles = RECURSION_CYCLES;

        Ok(BenchmarkData {
            name: "lift",
            hashfn: opts.hashfn,
            seal: receipt.seal_size() as u64,
            throughput: cycles as f64 / duration.as_secs_f64(),
            duration,
            cycles,
        })
    }

    pub fn join(client: &ApiClient) -> Result<BenchmarkData> {
        println!("join");

        // NOTE: Using too few iters results in error "segment limit too small
        // for instruction".
        let (po2, iters) = CYCLES_PO2_ITERS[1];

        let opts = ProverOpts::all_po2s();
        let env = util::loop_env_builder(iters)
            .segment_limit_po2(po2 - 1)
            .build()?;

        let prove_segment = |segment| -> Result<SuccinctReceipt<ReceiptClaim>> {
            let receipt = client.prove_segment(&opts, segment, AssetRequest::Inline)?;
            client.lift(&opts, receipt.try_into()?, AssetRequest::Inline)
        };

        let [left_segment, right_segment]: [Asset; 2] = {
            let mut segments = Vec::new();
            util::execute_with_segments(client, &env, LOOP_ELF, |_, segment| {
                segments.push(segment);
                Ok(())
            })?;

            assert!(segments.len() >= 2);
            let mut segments = segments.into_iter();
            [segments.next().unwrap(), segments.next().unwrap()]
        };

        let left_receipt = prove_segment(left_segment)?;
        let right_receipt = prove_segment(right_segment)?;

        let (receipt, duration) = try_time(|| {
            client.join(
                &opts,
                left_receipt.try_into()?,
                right_receipt.try_into()?,
                AssetRequest::Inline,
            )
        })?;

        let cycles = RECURSION_CYCLES;

        Ok(BenchmarkData {
            name: "join",
            hashfn: opts.hashfn,
            throughput: cycles as f64 / duration.as_secs_f64(),
            seal: receipt.seal_size() as u64,
            cycles,
            duration,
        })
    }

    pub fn identity_p254(client: &ApiClient) -> Result<BenchmarkData> {
        println!("identity_p254");

        let opts = ProverOpts::all_po2s().with_receipt_kind(ReceiptKind::Succinct);
        let info = util::prove(client, &util::loop_env(0)?, LOOP_ELF, &opts)?;

        let InnerReceipt::Succinct(receipt) = info.receipt.inner else {
            unreachable!();
        };

        let (receipt, duration) =
            try_time(|| client.identity_p254(&opts, receipt.try_into()?, AssetRequest::Inline))?;

        let cycles = RECURSION_CYCLES;
        let throughput = cycles as f64 / duration.as_secs_f64();

        Ok(BenchmarkData {
            name: "identity_p254",
            hashfn: opts.hashfn,
            seal: receipt.seal_size() as u64,
            throughput,
            cycles,
            duration,
        })
    }

    /// Measures the duration for executing `operation` once.
    fn time<T>(mut operation: impl FnOnce() -> T) -> (T, Duration) {
        // Make inputs opaque to the optimizer.
        operation = black_box(operation);

        let start = Instant::now();
        let mut output = operation();
        let duration = start.elapsed();

        // Make optimizer believe output is used.
        output = black_box(output);

        (output, duration)
    }

    /// Measures the duration for executing a fallible `operation` once.
    fn try_time<T>(operation: impl FnOnce() -> Result<T>) -> Result<(T, Duration)> {
        let (result, duration) = time(operation);
        Ok((result?, duration))
    }
}

/// Utilities to make data human-readable for displaying in a table.
mod display {
    use human_repr::*;

    use super::*;

    pub fn bytes(bytes: &u64) -> String {
        if *bytes == 0 {
            return "N/A".into();
        }
        bytes.human_count_bytes().to_string()
    }

    pub fn cycles(cycles: &u64) -> String {
        cycles.human_count_bare().to_string()
    }

    pub fn duration(duration: &Duration) -> String {
        duration.human_duration().to_string()
    }

    pub fn hertz(hertz: &f64) -> String {
        hertz.human_count("Hz").to_string()
    }
}