Skip to content
Closed
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
6 changes: 5 additions & 1 deletion .github/workflows/prove.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@ jobs:

- name: Build SP1 program
run: |
cd program
cd program/operations
~/.sp1/bin/cargo-prove prove build

cd -
cd program/slashings_reset
~/.sp1/bin/cargo-prove prove build
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"rust-analyzer.linkedProjects": [
"program/operations/Cargo.toml",
"program/slashings_reset/Cargo.toml",
"script/Cargo.toml"
],
"rust-analyzer.check.overrideCommand": [
Expand Down
30 changes: 21 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[workspace]
members = ["lib", "program/operations", "script"]
members = ["lib", "program/operations", "program/slashings_reset", "script"]
resolver = "2"

[workspace.dependencies]
ethereum_ssz = "0.9"
tracing = "0.1.40"
ream-consensus = { git = "https://github.com/ReamLabs/ream", package = "ream-consensus" }
ream-consensus = { git = "https://github.com/syjn99/ream", package = "ream-consensus", rev = "f5327406270b374b6caeaed05fc1549c5a21dd53" }
ream-merkle = { git = "https://github.com/syjn99/ream", package = "ream-merkle", rev = "f5327406270b374b6caeaed05fc1549c5a21dd53" }

[patch.crates-io]
ethereum_hashing = { git = "https://github.com/ReamLabs/ethereum_hashing" }
sha2 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", tag = "patch-sha2-0.10.8-sp1-4.0.0" }
Binary file added post.ssz_snappy
Binary file not shown.
Binary file added pre.ssz_snappy
Binary file not shown.
12 changes: 12 additions & 0 deletions program/slashings_reset/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
version = "0.1.0"
name = "ream-slashings-reset"
edition = "2024"

[dependencies]
# SP1 dependencies
sp1-zkvm = "4.0.0"
sp1-derive = "4.0.0"

# Ream dependencies
ream-consensus = { workspace = true }
42 changes: 42 additions & 0 deletions program/slashings_reset/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// These two lines are necessary for the program to properly compile.
//
// Under the hood, we wrap your main function with some extra code so that it behaves properly
// inside the zkVM.
#![no_main]
sp1_zkvm::entrypoint!(main);

use ream_consensus::view::PartialBeaconStateBuilder;

#[sp1_derive::cycle_tracker]
pub fn main() {
// Read an input to the program.
//
// Behind the scenes, this compiles down to a custom system call which handles reading inputs
// from the prover.

println!("cycle-tracker-report-start: read-builder");
let builder: PartialBeaconStateBuilder = sp1_zkvm::io::read();
println!("cycle-tracker-report-end: read-builder");

println!("cycle-tracker-report-start: build-partial-beacon-state");
let mut partial_beacon_state = builder
.build()
.expect("Failed to build partial beacon state");
println!("cycle-tracker-report-end: build-partial-beacon-state");

// Main logic of the program.
// State transition of the beacon state.

println!("cycle-tracker-report-start: process-slashings-reset");
partial_beacon_state
.process_slashings_reset()
.expect("Failed to process slashings reset");
println!("cycle-tracker-report-end: process-slashings-reset");

// Commit to the public values of the program. The final proof will have a commitment to all the
// bytes that were committed to.

println!("cycle-tracker-report-start: commit");
sp1_zkvm::io::commit(&partial_beacon_state);
println!("cycle-tracker-report-end: commit");
}
3 changes: 3 additions & 0 deletions script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "src/bin/main.rs"

[dependencies]
sp1-sdk = "4.0.0"
bincode = "1.3.3"
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
serde = { version = "1.0.200", default-features = false, features = ["derive"] }
derive_more = { version = "2.0.1", features = ["full"] }
Expand All @@ -20,9 +21,11 @@ dotenv = "0.15.0"

# Ethereum dependencies
ethereum_ssz = { workspace = true }
tree_hash = "0.10"

# Ream dependencies
ream-consensus = { workspace = true }
ream-merkle = { workspace = true }
ream-lib = { path = "../lib" }

[build-dependencies]
Expand Down
3 changes: 2 additions & 1 deletion script/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use sp1_build::build_program;

fn main() {
build_program("../program/operations")
build_program("../program/operations");
build_program("../program/slashings_reset");
}
80 changes: 76 additions & 4 deletions script/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
use clap::Parser;
use sp1_sdk::{ProverClient, SP1Stdin, include_elf};
use ssz::{Decode, Encode};
use tracing::{error, info};

use ream_consensus::electra::{beacon_block::SignedBeaconBlock, beacon_state::BeaconState};
use tree_hash::TreeHash;

use ream_consensus::{
constants::{
BEACON_STATE_MERKLE_DEPTH, BEACON_STATE_SLASHINGS_GENERALIZED_INDEX,
BEACON_STATE_SLASHINGS_INDEX, BEACON_STATE_SLOT_INDEX,
},
electra::beacon_state::BeaconState,
view::{PartialBeaconState, PartialBeaconStateBuilder, SlashingsView},
};
use ream_lib::{file::read_file, input::OperationInput};

mod cli;
use cli::operation::OperationName;

/// The ELF (executable and linkable format) file for the Succinct RISC-V zkVM.
pub const OPERATIONS_ELF: &[u8] = include_elf!("ream-operations");
pub const SLASHINGS_RESET_ELF: &[u8] = include_elf!("ream-slashings-reset");

/// The arguments for the command.
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -45,6 +53,9 @@ struct Args {
#[clap(long, requires = "replay")]
state_path: Option<String>,

#[clap(long, requires = "replay")]
expected_post_path: Option<String>,

#[clap(long, requires = "replay")]
block_path: Option<String>,
}
Expand Down Expand Up @@ -75,7 +86,68 @@ fn main() {

if args.replay {
info!("Executing with replay test...");
todo!();

let mut pre_state: BeaconState = read_file(&std::path::PathBuf::from(
args.state_path.unwrap_or_default(),
));
let expected_post: BeaconState = read_file(&std::path::PathBuf::from(
args.expected_post_path.unwrap_or_default(),
));
let root = pre_state.tree_hash_root();

let all_leaves = pre_state.merkle_leaves();
let tree = ream_merkle::merkle_tree(&all_leaves, BEACON_STATE_MERKLE_DEPTH)
.expect("Failed to create merkle tree");

let target_indices = vec![BEACON_STATE_SLOT_INDEX, BEACON_STATE_SLASHINGS_INDEX];
let multiproof =
ream_merkle::multiproof::Multiproof::generate::<BEACON_STATE_MERKLE_DEPTH>(
&tree,
&target_indices,
)
.expect("Failed to generate multiproof");

let builder = PartialBeaconStateBuilder::from_root(root)
.with_multiproof(multiproof)
.with_slot(pre_state.slot)
.with_slashings(&pre_state.slashings);

// Setup the prover client.
let client = ProverClient::from_env();

// Setup the inputs.
let mut stdin = SP1Stdin::new();

stdin.write(&builder);

// Execute the program
let (output, report) = client.execute(SLASHINGS_RESET_ELF, &stdin).run().unwrap();
info!("Program executed successfully.");

// Decode the output
let result: PartialBeaconState = bincode::deserialize(output.as_slice()).unwrap();

for &mutated in result.dirty.iter() {
match mutated {
BEACON_STATE_SLASHINGS_GENERALIZED_INDEX => {
pre_state.slashings = result.slashings().unwrap().clone();
}
_ => {
panic!("Unexpected mutated index: {}", mutated);
}
}
}

assert_eq!(expected_post.tree_hash_root(), pre_state.tree_hash_root());

// Record the number of cycles executed.
info!("----- Cycle Tracker -----");
info!("Number of cycles: {}", report.total_instruction_count());
info!("Number of syscall count: {}", report.total_syscall_count());
for (key, value) in report.cycle_tracker.iter() {
info!("{}: {}", key, value);
}
info!("----- Cycle Tracker End -----");
}

if args.ef_test {
Expand Down
Loading