Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani committed Sep 3, 2024
1 parent 656b1db commit 8f8534e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 52 deletions.
38 changes: 25 additions & 13 deletions book/cost-estimator.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Cycle Counts [Cost Estimator]
# Cost Estimator

We provide a convenient CLI tool to estimate the RISC-V cycle counts (and cost) for generating ZKPs for a range of blocks for a given rollup.

Expand Down Expand Up @@ -26,9 +26,9 @@ This command will execute `op-succinct` as if it's in production. First, it will
into smaller ranges optimized along the span batch boundaries. Then it will fetch the required data for generating the ZKP for each of these ranges, and execute the SP1 `span` program. Once each program finishes, it will collect the statistics and output the aggregate statistics
for the entire block range. From this data, you can extrapolate the cycle count to a cost based on the cost per billion cycles.

## Example Block Range
## Example

On OP Sepolia, generating a proof from 15840000 to 15840050 (50 blocks) generates 4 span proofs, takes ~1.8B cycles and takes
On Optimism Sepolia, proving the block range 15840000 to 15840050 (50 blocks) generates 4 span proofs, takes ~1.8B cycles and
~2 minutes to execute.

```bash
Expand All @@ -39,16 +39,28 @@ RUST_LOG=info just cost-estimator 15840000 15840050
+--------------------------------+---------------------------+
| Metric | Value |
+--------------------------------+---------------------------+
| Total Cycles | 1,502,329,547 |
| Block Execution Cycles | 1,009,112,508 |
| Total Blocks | 51 |
| Total Transactions | 202 |
| Cycles per Block | 19,786,519 |
| Cycles per Transaction | 4,995,606 |
| Transactions per Block | 3 |
| Total Gas Used | 52,647,751 |
| Gas Used per Block | 1,032,308 |
| Gas Used per Transaction | 260,632 |
| Batch Start | 16,240,000 |
| Batch End | 16,240,050 |
| Execution Duration (seconds) | 130 |
| Total Instruction Count | 1,776,092,063 |
| Oracle Verify Cycles | 237,150,812 |
| Derivation Cycles | 493,177,851 |
| Block Execution Cycles | 987,885,587 |
| Blob Verification Cycles | 84,995,660 |
| Total SP1 Gas | 2,203,604,618 |
| Number of Blocks | 51 |
| Number of Transactions | 160 |
| Ethereum Gas Used | 43,859,242 |
| Cycles per Block | 74,736,691 |
| Cycles per Transaction | 23,422,603 |
| Transactions per Block | 11 |
| Gas Used per Block | 3,509,360 |
| Gas Used per Transaction | 1,105,066 |
| BN Pair Cycles | 0 |
| BN Add Cycles | 0 |
| BN Mul Cycles | 0 |
| KZG Eval Cycles | 0 |
| EC Recover Cycles | 9,407,847 |
+--------------------------------+---------------------------+
```

Expand Down
13 changes: 7 additions & 6 deletions proposer/succinct/bin/cost_estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,19 @@ fn aggregate_execution_stats(execution_stats: &[ExecutionStats]) -> ExecutionSta
aggregate_stats.nb_blocks += stats.nb_blocks;
aggregate_stats.nb_transactions += stats.nb_transactions;
aggregate_stats.eth_gas_used += stats.eth_gas_used;
aggregate_stats.cycles_per_block += stats.cycles_per_block;
aggregate_stats.cycles_per_transaction += stats.cycles_per_transaction;
aggregate_stats.transactions_per_block += stats.transactions_per_block;
aggregate_stats.gas_used_per_block += stats.gas_used_per_block;
aggregate_stats.gas_used_per_transaction += stats.gas_used_per_transaction;
aggregate_stats.bn_pair_cycles += stats.bn_pair_cycles;
aggregate_stats.bn_add_cycles += stats.bn_add_cycles;
aggregate_stats.bn_mul_cycles += stats.bn_mul_cycles;
aggregate_stats.kzg_eval_cycles += stats.kzg_eval_cycles;
aggregate_stats.ec_recover_cycles += stats.ec_recover_cycles;
}

aggregate_stats.cycles_per_block = aggregate_stats.total_instruction_count / aggregate_stats.nb_blocks;
aggregate_stats.cycles_per_transaction = aggregate_stats.total_instruction_count / aggregate_stats.nb_transactions;
aggregate_stats.transactions_per_block = aggregate_stats.nb_transactions / aggregate_stats.nb_blocks;
aggregate_stats.gas_used_per_block = aggregate_stats.eth_gas_used / aggregate_stats.nb_blocks;
aggregate_stats.gas_used_per_transaction = aggregate_stats.eth_gas_used / aggregate_stats.nb_transactions;

aggregate_stats.batch_start = batch_start;
aggregate_stats.batch_end = batch_end;

Expand Down Expand Up @@ -300,7 +301,7 @@ async fn main() -> Result<()> {
write_execution_stats_to_csv(&execution_stats, l2_chain_id, &args)?;

let aggregate_execution_stats = aggregate_execution_stats(&execution_stats);
println!("Aggregate Execution Stats: {:?}", aggregate_execution_stats);
println!("Aggregate Execution Stats\n: {}", aggregate_execution_stats);

Ok(())
}
41 changes: 8 additions & 33 deletions utils/host/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use sp1_sdk::{CostEstimator, ExecutionReport};

/// Statistics for the multi-block execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ExecutionStats {
pub batch_start: u64,
pub batch_end: u64,
Expand All @@ -32,35 +32,6 @@ pub struct ExecutionStats {
pub ec_recover_cycles: u64,
}

impl Default for ExecutionStats {
fn default() -> Self {
ExecutionStats {
batch_start: 0,
batch_end: 0,
execution_duration_sec: 0,
total_instruction_count: 0,
oracle_verify_instruction_count: 0,
derivation_instruction_count: 0,
block_execution_instruction_count: 0,
blob_verification_instruction_count: 0,
total_sp1_gas: 0,
nb_blocks: 0,
nb_transactions: 0,
eth_gas_used: 0,
cycles_per_block: 0,
cycles_per_transaction: 0,
transactions_per_block: 0,
gas_used_per_block: 0,
gas_used_per_transaction: 0,
bn_pair_cycles: 0,
bn_add_cycles: 0,
bn_mul_cycles: 0,
kzg_eval_cycles: 0,
ec_recover_cycles: 0,
}
}
}

/// Write a statistic to the formatter.
fn write_stat(f: &mut fmt::Formatter<'_>, label: &str, value: u64) -> fmt::Result {
writeln!(f, "| {:<30} | {:>25} |", label, value.to_formatted_string(&Locale::en))
Expand Down Expand Up @@ -132,8 +103,12 @@ pub async fn get_execution_stats(
let kzg_eval_cycles: u64 = *report.cycle_tracker.get("precompile-kzg-eval").unwrap_or(&0);
let ec_recover_cycles: u64 = *report.cycle_tracker.get("precompile-ec-recover").unwrap_or(&0);

let cycles_per_block = block_execution_instruction_count / nb_blocks;
let cycles_per_transaction = block_execution_instruction_count / nb_transactions;
let total_instruction_count = report.total_instruction_count();

// Cycles per block, transaction are computed with respect to the total instruction count.
let cycles_per_block = total_instruction_count / nb_blocks;
let cycles_per_transaction = total_instruction_count / nb_transactions;

let transactions_per_block = nb_transactions / nb_blocks;
let gas_used_per_block = total_gas_used / nb_blocks;
let gas_used_per_transaction = total_gas_used / nb_transactions;
Expand All @@ -142,7 +117,7 @@ pub async fn get_execution_stats(
batch_start: start,
batch_end: end,
execution_duration_sec: execution_duration.as_secs(),
total_instruction_count: report.total_instruction_count(),
total_instruction_count,
derivation_instruction_count,
oracle_verify_instruction_count,
block_execution_instruction_count,
Expand Down

0 comments on commit 8f8534e

Please sign in to comment.