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

Fix: zero-bin is now able again to accesses evm_arithmetization for circuit versions #310

Merged
merged 12 commits into from
Aug 15, 2024
28 changes: 21 additions & 7 deletions zero_bin/common/src/prover_state/persistence.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::env;
use std::{
fmt::{Debug, Display},
fs::{self, OpenOptions},
io::Write,
path::Path,
};

use alloy::hex;
use directories::ProjectDirs;
use evm_arithmetization::cpu::kernel::aggregator::KERNEL;
use once_cell::sync::Lazy;
use plonky2::util::serialization::{
Buffer, DefaultGateSerializer, DefaultGeneratorSerializer, IoError,
};
Expand All @@ -23,6 +25,18 @@ const VERIFIER_STATE_FILE_PREFIX: &str = "verifier_state";
const ZK_EVM_CACHE_DIR_NAME: &str = "zk_evm_circuit_cache";
const ZK_EVM_CACHE_DIR_ENV: &str = "ZK_EVM_CACHE_DIR";

/// We version serialized circuits by the kernel hash they were serialized with,
/// but we really only need a few of the starting hex nibbles to reliably
/// differentiate.
const KERNEL_HASH_PREFIX: usize = 8;

/// When we serialize/deserialize circuits, we rely on the hash of the plonky
/// kernel to determine if the circuit is compatible with our current binary. If
/// the kernel hash of the circuit that we are loading in from disk differs,
/// then using these circuits would cause failures during proof generation
pub static CIRCUIT_VERSION: Lazy<String> =
Lazy::new(|| hex::encode(KERNEL.hash())[..KERNEL_HASH_PREFIX].to_string());

fn get_serializers() -> (
DefaultGateSerializer,
DefaultGeneratorSerializer<Config, SIZE>,
Expand Down Expand Up @@ -80,8 +94,8 @@ pub(crate) trait DiskResource {
if std::fs::metadata(&circuits_dir).is_err() {
std::fs::create_dir_all(&circuits_dir).map_err(|err| {
DiskResourceError::IoError::<Self::Error>(std::io::Error::other(format!(
"Could not create circuits folder with error \"{:?}\"",
err
"Could not create circuits folder at {} (err: {})",
err, circuits_dir
)))
})?;
}
Expand Down Expand Up @@ -111,7 +125,7 @@ impl DiskResource for BaseProverResource {
"{}/{}_base_{}_{}",
circuit_dir(),
PROVER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
*CIRCUIT_VERSION,
p.get_configuration_digest()
)
}
Expand Down Expand Up @@ -147,7 +161,7 @@ impl DiskResource for MonolithicProverResource {
"{}/{}_monolithic_{}_{}",
circuit_dir(),
PROVER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
*CIRCUIT_VERSION,
p.get_configuration_digest()
)
}
Expand Down Expand Up @@ -182,7 +196,7 @@ impl DiskResource for RecursiveCircuitResource {
"{}/{}_{}_{}_{}",
circuit_dir(),
PROVER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
*CIRCUIT_VERSION,
circuit_type.as_short_str(),
size
)
Expand Down Expand Up @@ -226,7 +240,7 @@ impl DiskResource for VerifierResource {
"{}/{}_{}_{}",
circuit_dir(),
VERIFIER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
*CIRCUIT_VERSION,
p.get_configuration_digest()
)
}
Expand Down
6 changes: 3 additions & 3 deletions zero_bin/common/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub fn print_version(
evm_arithmetization_package_version: &str,
evm_arithmetization_kernel_version: &str,
rustc_commit_hash: &str,
rustc_timestamp: &str,
) {
println!(
"evm_arithmetization Package Version: {}\nBuild Commit Hash: {}\nBuild Timestamp: {}",
evm_arithmetization_package_version, rustc_commit_hash, rustc_timestamp
"evm_arithmetization Kernel Version: {}\nBuild Commit Hash: {}\nBuild Timestamp: {}",
evm_arithmetization_kernel_version, rustc_commit_hash, rustc_timestamp
)
}
17 changes: 2 additions & 15 deletions zero_bin/leader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use ops::register;
use paladin::runtime::Runtime;
use proof_gen::proof_types::GeneratedBlockProof;
use tracing::{info, warn};
use zero_bin_common::version;
use zero_bin_common::{
block_interval::BlockInterval, prover_state::persistence::set_circuit_cache_dir_env_if_not_set,
};
use zero_bin_common::{prover_state::persistence::CIRCUIT_VERSION, version};

use crate::client::{client_main, ProofParams};

Expand All @@ -23,8 +23,6 @@ mod http;
mod init;
mod stdio;

const EVM_ARITHMETIZATION_PKG_VER: &str = "EVM_ARITHMETIZATION_PKG_VER";

fn get_previous_proof(path: Option<PathBuf>) -> Result<Option<GeneratedBlockProof>> {
if path.is_none() {
return Ok(None);
Expand All @@ -43,22 +41,11 @@ async fn main() -> Result<()> {
set_circuit_cache_dir_env_if_not_set()?;
init::tracing();

if env::var_os(EVM_ARITHMETIZATION_PKG_VER).is_none() {
// Safety:
// - we're early enough in main that nothing else should race
unsafe {
env::set_var(
EVM_ARITHMETIZATION_PKG_VER,
env!("EVM_ARITHMETIZATION_PKG_VER"),
);
}
}

let args: Vec<String> = env::args().collect();

if args.contains(&"--version".to_string()) {
version::print_version(
env!("EVM_ARITHMETIZATION_PKG_VER"),
CIRCUIT_VERSION.as_str(),
env!("VERGEN_RUSTC_COMMIT_HASH"),
env!("VERGEN_BUILD_TIMESTAMP"),
);
Expand Down
4 changes: 2 additions & 2 deletions zero_bin/rpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use rpc::provider::CachedProvider;
use rpc::{retry::build_http_retry_provider, RpcType};
use tracing_subscriber::{prelude::*, EnvFilter};
use url::Url;
use zero_bin_common::block_interval::BlockInterval;
use zero_bin_common::version;
use zero_bin_common::{block_interval::BlockInterval, prover_state::persistence::CIRCUIT_VERSION};

#[derive(Parser)]
pub enum Cli {
Expand Down Expand Up @@ -82,7 +82,7 @@ async fn main() -> anyhow::Result<()> {
let args: Vec<String> = env::args().collect();
if args.contains(&"--version".to_string()) {
version::print_version(
env!("EVM_ARITHMETIZATION_PKG_VER"),
CIRCUIT_VERSION.as_str(),
env!("VERGEN_RUSTC_COMMIT_HASH"),
env!("VERGEN_BUILD_TIMESTAMP"),
);
Expand Down
3 changes: 0 additions & 3 deletions zero_bin/tools/prove_rpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ fi
# Force the working directory to always be the `tools/` directory.
TOOLS_DIR=$(dirname $(realpath "$0"))

# Set the environment variable to let the binary know that we're running in the project workspace.
export CARGO_WORKSPACE_DIR="${TOOLS_DIR}/../"

PROOF_OUTPUT_DIR="${TOOLS_DIR}/proofs"
OUT_LOG_PATH="${PROOF_OUTPUT_DIR}/b$1_$2.log"
ALWAYS_WRITE_LOGS=0 # Change this to `1` if you always want logs to be written.
Expand Down
3 changes: 0 additions & 3 deletions zero_bin/tools/prove_stdio.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ PROOFS_JSON_PATH="${TOOLS_DIR}/proofs.json"
VERIFY_OUT_PATH="${TOOLS_DIR}/verify.out"
TEST_OUT_PATH="${TOOLS_DIR}/test.out"

# Set the environment variable to let the binary know that we're running in the project workspace.
export CARGO_WORKSPACE_DIR="${TOOLS_DIR}/../"

# Configured Rayon and Tokio with rough defaults
export RAYON_NUM_THREADS=$num_procs
export TOKIO_WORKER_THREADS=$num_procs
Expand Down
7 changes: 5 additions & 2 deletions zero_bin/verifier/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use dotenvy::dotenv;
use proof_gen::proof_types::GeneratedBlockProof;
use serde_json::Deserializer;
use tracing::info;
use zero_bin_common::{prover_state::persistence::set_circuit_cache_dir_env_if_not_set, version};
use zero_bin_common::{
prover_state::persistence::{set_circuit_cache_dir_env_if_not_set, CIRCUIT_VERSION},
version,
};

mod cli;
mod init;
Expand All @@ -20,7 +23,7 @@ fn main() -> Result<()> {
let args: Vec<String> = env::args().collect();
if args.contains(&"--version".to_string()) {
version::print_version(
env!("EVM_ARITHMETIZATION_PKG_VER"),
CIRCUIT_VERSION.as_str(),
env!("VERGEN_RUSTC_COMMIT_HASH"),
env!("VERGEN_BUILD_TIMESTAMP"),
);
Expand Down
5 changes: 3 additions & 2 deletions zero_bin/worker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use dotenvy::dotenv;
use ops::register;
use paladin::runtime::WorkerRuntime;
use zero_bin_common::prover_state::{
cli::CliProverStateConfig, persistence::set_circuit_cache_dir_env_if_not_set,
cli::CliProverStateConfig,
persistence::{set_circuit_cache_dir_env_if_not_set, CIRCUIT_VERSION},
};
use zero_bin_common::version;

Expand All @@ -31,7 +32,7 @@ async fn main() -> Result<()> {
let args: Vec<String> = env::args().collect();
if args.contains(&"--version".to_string()) {
version::print_version(
env!("EVM_ARITHMETIZATION_PKG_VER"),
CIRCUIT_VERSION.as_str(),
env!("VERGEN_RUSTC_COMMIT_HASH"),
env!("VERGEN_BUILD_TIMESTAMP"),
);
Expand Down
Loading