Skip to content
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"

members = [
"api",
"zks_get_proof_verifier",
"basic_bootloader",
"basic_system",
"callable_oracles",
Expand Down Expand Up @@ -105,4 +106,3 @@ debug = true
# prover_examples = { path = "../zksync-airbender/circuit_defs/prover_examples" }
# risc_v_simulator = { path = "../zksync-airbender/risc_v_simulator" }
# execution_utils = { path = "../zksync-airbender/execution_utils" }

3 changes: 2 additions & 1 deletion api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ zksync_os_runner = { path = "../zksync_os_runner"}
risc_v_simulator = { workspace = true }
zk_ee = { path = "../zk_ee", default-features = false }
crypto = { path = "../crypto", default-features = false }
basic_system = { path = "../basic_system", default-features = false }
basic_system = { path = "../basic_system", default-features = false, features = ["get-proof"] }
basic_bootloader = { path = "../basic_bootloader", default-features = false }
evm_interpreter = { path = "../evm_interpreter", default-features = false }
ruint = { workspace = true, default-features = false }
alloy = { version = "=1", default-features = false, features = ["eip712", "consensus", "rpc-types", "signer-local", "dyn-abi", "network"] }
zksync_os_interface = { workspace = true }
alloy-sol-types = "1"
zks_get_proof_verifier = { path = "../zks_get_proof_verifier", default-features = false, features = ["serde"] }


[features]
Expand Down
56 changes: 56 additions & 0 deletions api/src/get_proof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! Re-exports and convenience helpers for `zks_getProof` verification.

use crypto::MiniDigest;

pub use zks_get_proof_verifier::{
LeafWithProof, StateCommitmentPreimage, StorageProof, StorageProofType, ZksGetProofHasher,
ZksGetProofResponse, ZksGetProofVerificationError, MAX_32_BYTES, ZERO_32_BYTES,
};

pub use zks_get_proof_verifier::compute_state_commitment as compute_state_commitment_with_hasher;
pub use zks_get_proof_verifier::verify_response as verify_response_with_hasher;

#[derive(Clone, Debug)]
pub struct Blake2sGetProofHasher {
hasher: crypto::blake2s::Blake2s256,
}

impl Blake2sGetProofHasher {
pub fn new() -> Self {
Self {
hasher: crypto::blake2s::Blake2s256::new(),
}
}
}

impl Default for Blake2sGetProofHasher {
fn default() -> Self {
Self::new()
}
}

impl ZksGetProofHasher for Blake2sGetProofHasher {
fn update(&mut self, input: impl AsRef<[u8]>) {
self.hasher.update(input);
}

fn finalize_reset(&mut self) -> [u8; 32] {
self.hasher.finalize_reset()
}
}

pub fn compute_state_commitment(
state_root: &[u8; 32],
preimage: &StateCommitmentPreimage,
) -> [u8; 32] {
let mut hasher = Blake2sGetProofHasher::new();
zks_get_proof_verifier::compute_state_commitment(&mut hasher, state_root, preimage)
}

pub fn verify_response<const N: usize>(
response: &ZksGetProofResponse,
expected_batch_hash: &[u8; 32],
) -> Result<Vec<[u8; 32]>, ZksGetProofVerificationError> {
let mut hasher = Blake2sGetProofHasher::new();
zks_get_proof_verifier::verify_response::<N, _>(response, expected_batch_hash, &mut hasher)
}
1 change: 1 addition & 0 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use forward_system::run::{
};
use oracle_provider::ReadWitnessSource;
use zksync_os_interface::traits::TxListSource;
pub mod get_proof;
pub mod helpers;

/// Runs the batch, and returns the output (that contains gas usage, transaction status etc.).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,9 @@ use crypto::sha3::Keccak256;
use crypto::MiniDigest;
use ruint::aliases::U256;
use zk_ee::common_structs::da_commitment_scheme::DACommitmentScheme;
pub use zk_ee::common_structs::ChainStateCommitment;
use zk_ee::utils::Bytes32;

///
/// Commitment to state that we need to keep between blocks execution:
/// - state commitment(`state_root` and `next_free_slot`)
/// - block number
/// - last 256 block hashes, previous can be "unrolled" from the last, but we commit to 256 for optimization.
/// - last block timestamp, to ensure that block timestamps are not decreasing.
///
/// This commitment(hash of its fields) will be saved on the settlement layer.
/// With proofs, we'll ensure that the values used during block execution correspond to this commitment.
///
#[derive(Debug)]
pub struct ChainStateCommitment {
pub state_root: Bytes32,
pub next_free_slot: u64,
pub block_number: u64,
pub last_256_block_hashes_blake: Bytes32,
pub last_block_timestamp: u64,
}

impl ChainStateCommitment {
///
/// Calculate blake2s hash of chain state commitment.
///
/// We are using proving friendly blake2s because this commitment will be generated and opened during proving,
/// but we don't need to open it on the settlement layer.
///
pub fn hash(&self) -> [u8; 32] {
let mut hasher = crypto::blake2s::Blake2s256::new();
hasher.update(self.state_root.as_u8_ref());
hasher.update(&self.next_free_slot.to_be_bytes());
hasher.update(&self.block_number.to_be_bytes());
hasher.update(self.last_256_block_hashes_blake.as_u8_ref());
hasher.update(&self.last_block_timestamp.to_be_bytes());
hasher.finalize()
}
}

///
/// Except for proving existence of batch(of blocks) that changes state from one to another, we want to open some info about this batch on the settlement layer:
/// - pubdata: to make sure that it's published and state is recoverable
Expand Down
5 changes: 4 additions & 1 deletion basic_system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ num-bigint = { version = "0.4", optional = true}
num-traits = { version = "*", optional = true}
system_hooks = { path = "../system_hooks", default-features = false }
cc-traits = { path = "src/system_implementation/ethereum_storage_model/supporting_crates/cc-traits" }
zks_get_proof_verifier = { path = "../zks_get_proof_verifier", default-features = false, optional = true }

cfg-if = "1.0.0"
const_for = "0.1.5"
Expand All @@ -35,7 +36,9 @@ paste = "1.0.15"
zerocopy = { workspace = true }

[features]
testing = ["zk_ee/testing", "evm_interpreter/testing", "serde", "ruint/serde", "rand", "crypto/testing", "num-bigint", "num-traits"]
serde = ["dep:serde"]
get-proof = ["dep:zks_get_proof_verifier"]
testing = ["zk_ee/testing", "evm_interpreter/testing", "serde", "ruint/serde", "rand", "crypto/testing", "num-bigint", "num-traits", "get-proof", "zks_get_proof_verifier/serde"]
default = ["testing"]
cycle_marker = ["cycle_marker/log_to_file"]
proving = ["crypto/proving"]
Expand Down
Loading
Loading