Skip to content

Commit

Permalink
feat(commitment-generator): Commitment for 1.4.2 (#1234)
Browse files Browse the repository at this point in the history
## What ❔

Adds support for 1.4.2 commitment (blob hashes are taken into account)

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.
- [ ] Linkcheck has been run via `zk linkcheck`.
  • Loading branch information
perekopskiy committed Feb 26, 2024
1 parent f6bff74 commit 9b773eb
Show file tree
Hide file tree
Showing 33 changed files with 1,155 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions checks-config/era.dic
Original file line number Diff line number Diff line change
Expand Up @@ -905,3 +905,4 @@ shivini
balancer
lookups
stateful
WIP
7 changes: 7 additions & 0 deletions core/bin/external_node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ pub struct OptionalENConfig {
/// 0 means that sealing is synchronous; this is mostly useful for performance comparison, testing etc.
#[serde(default = "OptionalENConfig::default_miniblock_seal_queue_capacity")]
pub miniblock_seal_queue_capacity: usize,
/// Path to KZG trusted setup path.
#[serde(default = "OptionalENConfig::default_kzg_trusted_setup_path")]
pub kzg_trusted_setup_path: String,
}

impl OptionalENConfig {
Expand Down Expand Up @@ -328,6 +331,10 @@ impl OptionalENConfig {
10
}

fn default_kzg_trusted_setup_path() -> String {
"./trusted_setup.json".to_owned()
}

pub fn polling_interval(&self) -> Duration {
Duration::from_millis(self.polling_interval)
}
Expand Down
5 changes: 4 additions & 1 deletion core/bin/external_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ async fn init_tasks(
.build()
.await
.context("failed to build a commitment_generator_pool")?;
let commitment_generator = CommitmentGenerator::new(commitment_generator_pool);
let commitment_generator = CommitmentGenerator::new(
commitment_generator_pool,
&config.optional.kzg_trusted_setup_path,
);
app_health.insert_component(commitment_generator.health_check());
let commitment_generator_handle = tokio::spawn(commitment_generator.run(stop_receiver.clone()));

Expand Down
5 changes: 3 additions & 2 deletions core/bin/zksync_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use zksync_config::{
},
fri_prover_group::FriProverGroupConfig,
house_keeper::HouseKeeperConfig,
FriProofCompressorConfig, FriProverConfig, FriWitnessGeneratorConfig, ObservabilityConfig,
PrometheusConfig, ProofDataHandlerConfig, WitnessGeneratorConfig,
FriProofCompressorConfig, FriProverConfig, FriWitnessGeneratorConfig, KzgConfig,
ObservabilityConfig, PrometheusConfig, ProofDataHandlerConfig, WitnessGeneratorConfig,
},
ApiConfig, ContractsConfig, DBConfig, ETHClientConfig, ETHSenderConfig, ETHWatchConfig,
GasAdjusterConfig, ObjectStoreConfig, PostgresConfig,
Expand Down Expand Up @@ -125,6 +125,7 @@ async fn main() -> anyhow::Result<()> {
eth_watch_config: ETHWatchConfig::from_env().ok(),
gas_adjuster_config: GasAdjusterConfig::from_env().ok(),
object_store_config: ObjectStoreConfig::from_env().ok(),
kzg_config: KzgConfig::from_env().ok(),
consensus_config: None,
};

Expand Down
14 changes: 14 additions & 0 deletions core/lib/config/src/configs/kzg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use serde::Deserialize;

#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct KzgConfig {
/// Path to KZG trusted setup file.
#[serde(default = "KzgConfig::default_trusted_setup_path")]
pub trusted_setup_path: String,
}

impl KzgConfig {
fn default_trusted_setup_path() -> String {
"./trusted_setup.json".to_owned()
}
}
2 changes: 2 additions & 0 deletions core/lib/config/src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use self::{
fri_prover_gateway::FriProverGatewayConfig,
fri_witness_generator::FriWitnessGeneratorConfig,
fri_witness_vector_generator::FriWitnessVectorGeneratorConfig,
kzg::KzgConfig,
object_store::ObjectStoreConfig,
observability::ObservabilityConfig,
proof_data_handler::ProofDataHandlerConfig,
Expand All @@ -37,6 +38,7 @@ pub mod fri_prover_group;
pub mod fri_witness_generator;
pub mod fri_witness_vector_generator;
pub mod house_keeper;
pub mod kzg;
pub mod object_store;
pub mod observability;
pub mod proof_data_handler;
Expand Down
5 changes: 5 additions & 0 deletions core/lib/constants/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ pub const COMPLEX_UPGRADER_ADDRESS: Address = H160([
0x00, 0x00, 0x80, 0x0f,
]);

pub const PUBDATA_CHUNK_PUBLISHER_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0x11,
]);

/// The `ecrecover` system contract address.
pub const ECRECOVER_PRECOMPILE_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down
6 changes: 6 additions & 0 deletions core/lib/constants/src/system_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ pub const L2_TO_L1_LOGS_TREE_ROOT_KEY: u32 = 0;

/// The key of the system log with value of the state diff hash
pub const STATE_DIFF_HASH_KEY: u32 = 2;

/// The key of the system log with value of the first blob linear hash
pub const BLOB1_LINEAR_HASH_KEY: u32 = 7;

/// The key of the system log with value of the second blob linear hash
pub const BLOB2_LINEAR_HASH_KEY: u32 = 8;
35 changes: 35 additions & 0 deletions core/lib/env_config/src/kzg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use zksync_config::configs::KzgConfig;

use crate::{envy_load, FromEnv};

impl FromEnv for KzgConfig {
fn from_env() -> anyhow::Result<Self> {
envy_load("kzg", "KZG_")
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::EnvMutex;

static MUTEX: EnvMutex = EnvMutex::new();

fn expected_config() -> KzgConfig {
KzgConfig {
trusted_setup_path: "dir/file.json".to_owned(),
}
}

#[test]
fn from_env() {
let mut lock = MUTEX.lock();
let config = r#"
KZG_TRUSTED_SETUP_PATH="dir/file.json"
"#;
lock.set_env(config);

let actual = KzgConfig::from_env().unwrap();
assert_eq!(actual, expected_config());
}
}
1 change: 1 addition & 0 deletions core/lib/env_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod fri_prover_group;
mod fri_witness_generator;
mod fri_witness_vector_generator;
mod house_keeper;
mod kzg;
pub mod object_store;
mod observability;
mod proof_data_handler;
Expand Down
40 changes: 39 additions & 1 deletion core/lib/l1_contract_interface/src/i_executor/commit/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::convert::TryInto;
use sha2::Sha256;
use sha3::{Digest, Keccak256};
use zkevm_test_harness_1_3_3::ff::{PrimeField, PrimeFieldRepr};
pub use zkevm_test_harness_1_4_1::kzg::KzgSettings;
use zkevm_test_harness_1_4_1::{
kzg::{compute_commitment, compute_proof, compute_proof_poly, KzgSettings},
kzg::{compute_commitment, compute_proof, compute_proof_poly},
zkevm_circuits::{
boojum::pairing::{
bls12_381::{Fr, FrRepr, G1Affine},
Expand All @@ -20,6 +21,7 @@ use zkevm_test_harness_1_4_1::{
},
},
};
use zksync_types::H256;

const ZK_SYNC_BYTES_PER_BLOB: usize = BLOB_CHUNK_SIZE * ELEMENTS_PER_4844_BLOCK;
const EIP_4844_BYTES_PER_BLOB: usize = 32 * ELEMENTS_PER_4844_BLOCK;
Expand Down Expand Up @@ -102,6 +104,20 @@ impl KzgInfo {
res
}

pub fn to_blob_commitment(&self) -> [u8; 32] {
let mut commitment = [0u8; 32];
let hash = &Keccak256::digest(
[
self.versioned_hash.to_vec(),
self.opening_point[16..].to_vec(),
self.opening_value.to_vec(),
]
.concat(),
);
commitment.copy_from_slice(hash);
commitment
}

/// Deserializes `Self::SERIALIZED_SIZE` bytes into `KzgInfo` struct
pub fn from_slice(data: &[u8]) -> Self {
assert_eq!(data.len(), Self::SERIALIZED_SIZE);
Expand Down Expand Up @@ -246,6 +262,28 @@ impl KzgInfo {
}
}

pub fn pubdata_to_blob_commitments(pubdata_input: &[u8], kzg_settings: &KzgSettings) -> [H256; 2] {
assert!(
pubdata_input.len() <= 2 * ZK_SYNC_BYTES_PER_BLOB,
"Pubdata length exceeds size of 2 blobs"
);

let blob_commitments = pubdata_input
.chunks(ZK_SYNC_BYTES_PER_BLOB)
.map(|blob| {
let kzg_info = KzgInfo::new(kzg_settings, blob.to_vec());
H256(kzg_info.to_blob_commitment())
})
.collect::<Vec<_>>();

// If length of `pubdata_input` is less than or equal to `ZK_SYNC_BYTES_PER_BLOB` (126976)
// then only one blob will be used and 32 zero bytes will be used as a commitment for the second blob.
[
blob_commitments.get(0).copied().unwrap_or_default(),
blob_commitments.get(1).copied().unwrap_or_default(),
]
}

#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod kzg;
pub mod kzg;
3 changes: 1 addition & 2 deletions core/lib/l1_contract_interface/src/i_executor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Different interfaces exposed by the `IExecutor.sol`.

mod commit;

pub mod commit;
pub mod methods;
pub mod structures;
9 changes: 6 additions & 3 deletions core/lib/multivm/src/versions/vm_latest/tests/block_tip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ use zksync_system_constants::{
CONTRACT_FORCE_DEPLOYER_ADDRESS, KNOWN_CODES_STORAGE_ADDRESS, L1_MESSENGER_ADDRESS,
};
use zksync_types::{
commitment::SerializeCommitment, get_code_key, l2_to_l1_log::L2ToL1Log,
writes::StateDiffRecord, Address, Execute, H256, U256,
commitment::SerializeCommitment,
get_code_key,
l2_to_l1_log::{l2_to_l1_logs_tree_size, L2ToL1Log},
writes::StateDiffRecord,
Address, Execute, ProtocolVersionId, H256, U256,
};
use zksync_utils::{bytecode::hash_bytecode, bytes_to_be_words, h256_to_u256, u256_to_h256};

Expand Down Expand Up @@ -209,7 +212,7 @@ fn test_dry_run_upper_bound() {
// 4. Lots of storage slot updates.

let max_logs = execute_test(L1MessengerTestData {
l2_to_l1_logs: L2ToL1Log::MIN_L2_L1_LOGS_TREE_SIZE,
l2_to_l1_logs: l2_to_l1_logs_tree_size(ProtocolVersionId::Version20),
..Default::default()
});

Expand Down
22 changes: 22 additions & 0 deletions core/lib/protobuf_config/src/kzg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use anyhow::Context as _;
use zksync_config::configs;
use zksync_protobuf::required;

use crate::{proto, repr::ProtoRepr};

impl ProtoRepr for proto::Kzg {
type Type = configs::KzgConfig;
fn read(&self) -> anyhow::Result<Self::Type> {
Ok(Self::Type {
trusted_setup_path: required(&self.trusted_setup_path)
.context("trusted_setup_path")?
.clone(),
})
}

fn build(this: &Self::Type) -> Self {
Self {
trusted_setup_path: Some(this.trusted_setup_path.clone()),
}
}
}
1 change: 1 addition & 0 deletions core/lib/protobuf_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod fri_prover_group;
mod fri_witness_generator;
mod fri_witness_vector_generator;
mod house_keeper;
mod kzg;
mod object_store;
mod observability;
mod proof_data_handler;
Expand Down
7 changes: 7 additions & 0 deletions core/lib/protobuf_config/src/proto/kzg.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package zksync.config;

message Kzg {
optional string trusted_setup_path = 1; // required; fs path
}
Loading

0 comments on commit 9b773eb

Please sign in to comment.