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

Adapt ckzg changes for runtime parameters #4818

Closed
wants to merge 8 commits into from
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
21 changes: 2 additions & 19 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ pub struct BeaconChain<T: BeaconChainTypes> {
/// they are collected and combined.
pub data_availability_checker: Arc<DataAvailabilityChecker<T>>,
/// The KZG trusted setup used by this chain.
pub kzg: Option<Arc<Kzg<<T::EthSpec as EthSpec>::Kzg>>>,
pub kzg: Option<Arc<Kzg>>,
}

type BeaconBlockAndState<T, Payload> = (
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/blob_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ impl<T: EthSpec> KzgVerifiedBlob<T> {
/// Returns an error if the kzg verification check fails.
pub fn verify_kzg_for_blob<T: EthSpec>(
blob: Arc<BlobSidecar<T>>,
kzg: &Kzg<T::Kzg>,
kzg: &Kzg,
) -> Result<KzgVerifiedBlob<T>, AvailabilityCheckError> {
let _timer = crate::metrics::start_timer(&crate::metrics::KZG_VERIFICATION_SINGLE_TIMES);
//TODO(sean) remove clone
Expand All @@ -519,7 +519,7 @@ pub fn verify_kzg_for_blob<T: EthSpec>(
/// in a loop since this function kzg verifies a list of blobs more efficiently.
pub fn verify_kzg_for_blob_list<T: EthSpec>(
blob_list: &BlobSidecarList<T>,
kzg: &Kzg<T::Kzg>,
kzg: &Kzg,
) -> Result<(), AvailabilityCheckError> {
let _timer = crate::metrics::start_timer(&crate::metrics::KZG_VERIFICATION_BATCH_TIMES);
let (blobs, (commitments, proofs)): (Vec<_>, (Vec<_>, Vec<_>)) = blob_list
Expand Down
3 changes: 3 additions & 0 deletions beacon_node/beacon_chain/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,9 @@ where
let kzg = if let Some(trusted_setup) = self.trusted_setup {
let kzg = Kzg::new_from_trusted_setup(trusted_setup)
.map_err(|e| format!("Failed to load trusted setup: {:?}", e))?;
if TEthSpec::field_elements_per_blob() != kzg.field_elements_per_blob() {
return Err("Trusted setup is inconsistent with spec preset".to_string());
}
let kzg_arc = Arc::new(kzg);
Some(kzg_arc)
} else {
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/data_availability_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub struct DataAvailabilityChecker<T: BeaconChainTypes> {
processing_cache: RwLock<ProcessingCache<T::EthSpec>>,
availability_cache: Arc<OverflowLRUCache<T>>,
slot_clock: T::SlotClock,
kzg: Option<Arc<Kzg<<T::EthSpec as EthSpec>::Kzg>>>,
log: Logger,
kzg: Option<Arc<Kzg>>,
spec: ChainSpec,
}

Expand Down Expand Up @@ -79,7 +79,7 @@ impl<T: EthSpec> Debug for Availability<T> {
impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
pub fn new(
slot_clock: T::SlotClock,
kzg: Option<Arc<Kzg<<T::EthSpec as EthSpec>::Kzg>>>,
kzg: Option<Arc<Kzg>>,
store: BeaconStore<T>,
log: &Logger,
spec: ChainSpec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ pub mod tests {

pub fn pre_setup() -> Setup<E> {
let trusted_setup: TrustedSetup =
serde_json::from_reader(get_trusted_setup::<<E as EthSpec>::Kzg>()).unwrap();
serde_json::from_reader(get_trusted_setup::<E>()).unwrap();
let kzg = Kzg::new_from_trusted_setup(trusted_setup).unwrap();

let mut rng = StdRng::seed_from_u64(0xDEADBEEF0BAD5EEDu64);
Expand Down
39 changes: 12 additions & 27 deletions beacon_node/beacon_chain/src/kzg_utils.rs
Original file line number Diff line number Diff line change
@@ -1,75 +1,60 @@
use kzg::{Error as KzgError, Kzg, KzgPreset};
use kzg::{Error as KzgError, Kzg};
use types::{Blob, EthSpec, Hash256, KzgCommitment, KzgProof};

/// Converts a blob ssz List object to an array to be used with the kzg
/// crypto library.
fn ssz_blob_to_crypto_blob<T: EthSpec>(
blob: &Blob<T>,
) -> Result<<<T as EthSpec>::Kzg as KzgPreset>::Blob, KzgError> {
T::blob_from_bytes(blob.as_ref())
}

/// Validate a single blob-commitment-proof triplet from a `BlobSidecar`.
pub fn validate_blob<T: EthSpec>(
kzg: &Kzg<T::Kzg>,
kzg: &Kzg,
blob: Blob<T>,
kzg_commitment: KzgCommitment,
kzg_proof: KzgProof,
) -> Result<bool, KzgError> {
kzg.verify_blob_kzg_proof(
&ssz_blob_to_crypto_blob::<T>(&blob)?,
kzg_commitment,
kzg_proof,
)
kzg.verify_blob_kzg_proof(blob.as_ref(), kzg_commitment, kzg_proof)
}

/// Validate a batch of blob-commitment-proof triplets from multiple `BlobSidecars`.
pub fn validate_blobs<T: EthSpec>(
kzg: &Kzg<T::Kzg>,
kzg: &Kzg,
expected_kzg_commitments: &[KzgCommitment],
blobs: &[Blob<T>],
kzg_proofs: &[KzgProof],
) -> Result<bool, KzgError> {
let blobs = blobs
.iter()
.map(|blob| ssz_blob_to_crypto_blob::<T>(blob)) // Avoid this clone
.collect::<Result<Vec<_>, KzgError>>()?;
let blobs = blobs.iter().map(|blob| blob.as_ref()).collect::<Vec<_>>();

kzg.verify_blob_kzg_proof_batch(&blobs, expected_kzg_commitments, kzg_proofs)
}

/// Compute the kzg proof given an ssz blob and its kzg commitment.
pub fn compute_blob_kzg_proof<T: EthSpec>(
kzg: &Kzg<T::Kzg>,
kzg: &Kzg,
blob: &Blob<T>,
kzg_commitment: KzgCommitment,
) -> Result<KzgProof, KzgError> {
// Avoid this blob clone
kzg.compute_blob_kzg_proof(&ssz_blob_to_crypto_blob::<T>(blob)?, kzg_commitment)
kzg.compute_blob_kzg_proof(blob.as_ref(), kzg_commitment)
}

/// Compute the kzg commitment for a given blob.
pub fn blob_to_kzg_commitment<T: EthSpec>(
kzg: &Kzg<T::Kzg>,
kzg: &Kzg,
blob: &Blob<T>,
) -> Result<KzgCommitment, KzgError> {
kzg.blob_to_kzg_commitment(&ssz_blob_to_crypto_blob::<T>(blob)?)
kzg.blob_to_kzg_commitment(blob.as_ref())
}

/// Compute the kzg proof for a given blob and an evaluation point z.
pub fn compute_kzg_proof<T: EthSpec>(
kzg: &Kzg<T::Kzg>,
kzg: &Kzg,
blob: &Blob<T>,
z: Hash256,
) -> Result<(KzgProof, Hash256), KzgError> {
let z = z.0.into();
kzg.compute_kzg_proof(&ssz_blob_to_crypto_blob::<T>(blob)?, &z)
kzg.compute_kzg_proof(blob.as_ref(), &z)
.map(|(proof, z)| (proof, Hash256::from_slice(&z.to_vec())))
}

/// Verify a `kzg_proof` for a `kzg_commitment` that evaluating a polynomial at `z` results in `y`
pub fn verify_kzg_proof<T: EthSpec>(
kzg: &Kzg<T::Kzg>,
kzg: &Kzg,
kzg_commitment: KzgCommitment,
kzg_proof: KzgProof,
z: Hash256,
Expand Down
6 changes: 3 additions & 3 deletions beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ where
.expect("cannot build without validator keypairs");
let chain_config = self.chain_config.unwrap_or_default();
let trusted_setup: TrustedSetup =
serde_json::from_reader(eth2_network_config::get_trusted_setup::<E::Kzg>())
serde_json::from_reader(eth2_network_config::get_trusted_setup::<E>())
.map_err(|e| format!("Unable to read trusted setup file: {}", e))
.unwrap();

Expand Down Expand Up @@ -571,7 +571,7 @@ pub fn mock_execution_layer_from_parts<T: EthSpec>(
});

let trusted_setup: TrustedSetup =
serde_json::from_reader(eth2_network_config::get_trusted_setup::<T::Kzg>())
serde_json::from_reader(eth2_network_config::get_trusted_setup::<T>())
.map_err(|e| format!("Unable to read trusted setup file: {}", e))
.expect("should have trusted setup");
let kzg = Kzg::new_from_trusted_setup(trusted_setup).expect("should create kzg");
Expand Down Expand Up @@ -2526,7 +2526,7 @@ pub enum NumBlobs {
pub fn generate_rand_block_and_blobs<E: EthSpec>(
fork_name: ForkName,
num_blobs: NumBlobs,
kzg: &Kzg<E::Kzg>,
kzg: &Kzg,
rng: &mut impl Rng,
) -> (SignedBeaconBlock<E, FullPayload<E>>, Vec<BlobSidecar<E>>) {
let inner = map_fork_name!(fork_name, BeaconBlock, <_>::random_for_test(rng));
Expand Down
7 changes: 3 additions & 4 deletions beacon_node/beacon_chain/tests/store_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2154,10 +2154,9 @@ async fn weak_subjectivity_sync_test(slots: Vec<Slot>, checkpoint_slot: Slot) {
let store = get_store(&temp2);
let spec = test_spec::<E>();
let seconds_per_slot = spec.seconds_per_slot;
let trusted_setup: TrustedSetup =
serde_json::from_reader(get_trusted_setup::<<E as EthSpec>::Kzg>())
.map_err(|e| println!("Unable to read trusted setup file: {}", e))
.unwrap();
let trusted_setup: TrustedSetup = serde_json::from_reader(get_trusted_setup::<E>())
.map_err(|e| println!("Unable to read trusted setup file: {}", e))
.unwrap();

let mock =
mock_execution_layer_from_parts(&harness.spec, harness.runtime.task_executor.clone(), None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub struct ExecutionBlockGenerator<T: EthSpec> {
* deneb stuff
*/
pub blobs_bundles: HashMap<PayloadId, BlobsBundle<T>>,
pub kzg: Option<Arc<Kzg<T::Kzg>>>,
pub kzg: Option<Arc<Kzg>>,
rng: Arc<Mutex<StdRng>>,
}

Expand All @@ -148,7 +148,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
terminal_block_hash: ExecutionBlockHash,
shanghai_time: Option<u64>,
cancun_time: Option<u64>,
kzg: Option<Kzg<T::Kzg>>,
kzg: Option<Kzg>,
) -> Self {
let mut gen = Self {
head_block: <_>::default(),
Expand Down Expand Up @@ -645,7 +645,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {

pub fn generate_random_blobs<T: EthSpec, R: Rng>(
n_blobs: usize,
kzg: &Kzg<T::Kzg>,
kzg: &Kzg,
rng: &mut R,
) -> Result<(BlobsBundle<T>, Transactions<T>), String> {
let mut bundle = BlobsBundle::<T>::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<T: EthSpec> MockExecutionLayer<T> {
builder_threshold: Option<u128>,
jwt_key: Option<JwtKey>,
spec: ChainSpec,
kzg: Option<Kzg<T::Kzg>>,
kzg: Option<Kzg>,
) -> Self {
let handle = executor.handle().unwrap();

Expand Down
4 changes: 2 additions & 2 deletions beacon_node/execution_layer/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<T: EthSpec> MockServer<T> {
pub fn new_with_config(
handle: &runtime::Handle,
config: MockExecutionConfig,
kzg: Option<Kzg<T::Kzg>>,
kzg: Option<Kzg>,
) -> Self {
let MockExecutionConfig {
jwt_key,
Expand Down Expand Up @@ -188,7 +188,7 @@ impl<T: EthSpec> MockServer<T> {
terminal_block_hash: ExecutionBlockHash,
shanghai_time: Option<u64>,
cancun_time: Option<u64>,
kzg: Option<Kzg<T::Kzg>>,
kzg: Option<Kzg>,
) -> Self {
Self::new_with_config(
handle,
Expand Down
3 changes: 2 additions & 1 deletion beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ pub fn get_config<E: EthSpec>(
client_config.trusted_setup = context
.eth2_network_config
.as_ref()
.and_then(|config| config.kzg_trusted_setup.clone());
.and_then(|config| config.kzg_trusted_setup.clone())
.and_then(|trusted_setup_bytes| serde_json::from_slice(trusted_setup_bytes.as_ref()).ok());

// Override default trusted setup file if required
// TODO: consider removing this when we get closer to launch
Expand Down
1 change: 0 additions & 1 deletion common/eth2_network_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ tokio = { workspace = true }
serde_yaml = { workspace = true }
serde_json = { workspace = true }
types = { workspace = true }
kzg = { workspace = true }
ethereum_ssz = { workspace = true }
eth2_config = { workspace = true }
discv5 = { workspace = true }
Expand Down
28 changes: 13 additions & 15 deletions common/eth2_network_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use bytes::Bytes;
use discv5::enr::{CombinedKey, Enr};
use eth2_config::{instantiate_hardcoded_nets, HardcodedNet};
use kzg::{KzgPreset, KzgPresetId, TrustedSetup};
use pretty_reqwest_error::PrettyReqwestError;
use reqwest::{Client, Error};
use sensitive_url::SensitiveUrl;
Expand Down Expand Up @@ -53,29 +52,28 @@ const TRUSTED_SETUP: &[u8] =
include_bytes!("../built_in_network_configs/testing_trusted_setups.json");

const TRUSTED_SETUP_MINIMAL: &[u8] =
include_bytes!("../built_in_network_configs/minimal_testing_trusted_setups.json");
include_bytes!("../built_in_network_configs/testing_trusted_setups.json");

pub fn get_trusted_setup<P: KzgPreset>() -> &'static [u8] {
get_trusted_setup_from_id(P::spec_name())
pub fn get_trusted_setup<E: EthSpec>() -> &'static [u8] {
get_trusted_setup_from_id(E::spec_name())
}

pub fn get_trusted_setup_from_id(id: KzgPresetId) -> &'static [u8] {
match id {
KzgPresetId::Mainnet => TRUSTED_SETUP,
KzgPresetId::Minimal => TRUSTED_SETUP_MINIMAL,
pub fn get_trusted_setup_from_id(spec_id: EthSpecId) -> &'static [u8] {
match spec_id {
EthSpecId::Mainnet | EthSpecId::Gnosis => TRUSTED_SETUP,
EthSpecId::Minimal => TRUSTED_SETUP_MINIMAL,
}
}

fn get_trusted_setup_from_config(config: &Config) -> Result<Option<TrustedSetup>, String> {
fn get_trusted_setup_from_config(config: &Config) -> Result<Option<Vec<u8>>, String> {
config
.deneb_fork_epoch
.filter(|epoch| epoch.value != Epoch::max_value())
.map(|_| {
let id = KzgPresetId::from_str(&config.preset_base)
.map_err(|e| format!("Unable to parse preset_base as KZG preset: {:?}", e))?;
let trusted_setup_bytes = get_trusted_setup_from_id(id);
serde_json::from_reader(trusted_setup_bytes)
.map_err(|e| format!("Unable to read trusted setup file: {}", e))
let id = config
.eth_spec_id()
.ok_or_else(|| "Unable to parse preset_base as KZG preset".to_string())?;
Ok(get_trusted_setup_from_id(id).to_vec())
})
.transpose()
}
Expand Down Expand Up @@ -121,7 +119,7 @@ pub struct Eth2NetworkConfig {
pub genesis_state_source: GenesisStateSource,
pub genesis_state_bytes: Option<GenesisStateBytes>,
pub config: Config,
pub kzg_trusted_setup: Option<TrustedSetup>,
pub kzg_trusted_setup: Option<Vec<u8>>,
}

impl Eth2NetworkConfig {
Expand Down
Loading
Loading