From e23cd920c765229c5badec2e81f1aeb0fbf4438a Mon Sep 17 00:00:00 2001 From: sirse Date: Sat, 30 Sep 2023 21:49:36 +0300 Subject: [PATCH] Avoid changing directory in tests --- Cargo.lock | 7 ++ blst/tests/eip_4844.rs | 16 ++-- kzg-bench/Cargo.toml | 1 + kzg-bench/src/tests/eip_4844.rs | 136 +++++++++++++++++++------------- 4 files changed, 94 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9238f130..22a979c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -821,6 +821,7 @@ dependencies = [ "glob", "hex", "kzg", + "pathdiff", "rand", "serde", "serde_yaml", @@ -974,6 +975,12 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" +[[package]] +name = "pathdiff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" + [[package]] name = "pest" version = "2.7.3" diff --git a/blst/tests/eip_4844.rs b/blst/tests/eip_4844.rs index ba15f974..7fb089a7 100644 --- a/blst/tests/eip_4844.rs +++ b/blst/tests/eip_4844.rs @@ -1,10 +1,7 @@ #[cfg(test)] mod tests { - use std::env::set_current_dir; - use kzg::eip_4844::{ Blob, KZGCommitment, BYTES_PER_COMMITMENT, BYTES_PER_FIELD_ELEMENT, C_KZG_RET_BADARGS, - TRUSTED_SETUP_PATH, }; use kzg_bench::tests::eip_4844::{ blob_to_kzg_commitment_test, bytes_to_bls_field_test, @@ -17,10 +14,10 @@ mod tests { }; #[cfg(not(feature = "minimal-spec"))] use kzg_bench::tests::eip_4844::{ - compute_and_verify_kzg_proof_within_domain_test, test_vectors_blob_to_kzg_commitment, - test_vectors_compute_blob_kzg_proof, test_vectors_compute_kzg_proof, - test_vectors_verify_blob_kzg_proof, test_vectors_verify_blob_kzg_proof_batch, - test_vectors_verify_kzg_proof, + compute_and_verify_kzg_proof_within_domain_test, get_trusted_setup_path, + test_vectors_blob_to_kzg_commitment, test_vectors_compute_blob_kzg_proof, + test_vectors_compute_kzg_proof, test_vectors_verify_blob_kzg_proof, + test_vectors_verify_blob_kzg_proof_batch, test_vectors_verify_kzg_proof, }; use rust_kzg_blst::eip_4844::{ blob_to_kzg_commitment, blob_to_kzg_commitment_rust, blob_to_polynomial_rust, @@ -256,10 +253,7 @@ mod tests { #[test] pub fn blob_to_kzg_commitment_invalid_blob() { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let settings = - load_trusted_setup_filename_rust(&format!("../kzg-bench/{}", TRUSTED_SETUP_PATH)) - .unwrap(); + let settings = load_trusted_setup_filename_rust(get_trusted_setup_path().as_str()).unwrap(); let c_settings = kzg_settings_to_c(&settings); diff --git a/kzg-bench/Cargo.toml b/kzg-bench/Cargo.toml index c1a1c90f..09c963cd 100644 --- a/kzg-bench/Cargo.toml +++ b/kzg-bench/Cargo.toml @@ -11,6 +11,7 @@ serde_yaml = "0.9.17" glob = "0.3.1" serde = { version = "1.0", features = ["derive"] } hex = "0.4.2" +pathdiff = "0.2.1" [features] minimal-spec = ["kzg/minimal-spec"] diff --git a/kzg-bench/src/tests/eip_4844.rs b/kzg-bench/src/tests/eip_4844.rs index 715e3e49..a14dfe00 100644 --- a/kzg-bench/src/tests/eip_4844.rs +++ b/kzg-bench/src/tests/eip_4844.rs @@ -9,9 +9,10 @@ use kzg::eip_4844::{ FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_PATH, }; use kzg::{FFTSettings, Fr, KZGSettings, Poly, G1, G2}; +use pathdiff::diff_paths; use rand::rngs::ThreadRng; use rand::Rng; -use std::env::set_current_dir; +use std::env::current_dir; use std::fs; use std::path::PathBuf; @@ -77,6 +78,22 @@ pub fn compute_powers_test(compute_powers: &dyn Fn(&TFr, usize) -> Vec< } } +fn get_manifest_dir() -> String { + let current = current_dir().unwrap(); + let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let relative = diff_paths(manifest, current).unwrap(); + + relative.into_os_string().into_string().unwrap() +} + +pub fn get_trusted_setup_path() -> String { + PathBuf::from(get_manifest_dir()) + .join(TRUSTED_SETUP_PATH) + .into_os_string() + .into_string() + .unwrap() +} + #[allow(clippy::type_complexity)] pub fn blob_to_kzg_commitment_test< TFr: Fr + Copy, @@ -89,8 +106,7 @@ pub fn blob_to_kzg_commitment_test< load_trusted_setup: &dyn Fn(&str) -> Result, blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); let field_element = TFr::from_hex("0x14629a3a39f7b854e6aa49aa2edb450267eac2c14bb2d4f97a0b81a3f57055ad") @@ -136,8 +152,7 @@ pub fn compute_kzg_proof_test< blob_to_polynomial: &dyn Fn(&[TFr]) -> Result, evaluate_polynomial_in_evaluation_form: &dyn Fn(&TPoly, &TFr, &TKZGSettings) -> TFr, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); let field_element = TFr::from_hex("0x69386e69dbae0357b399b8d645a57a3062dfbe00bd8e97170b9bdd6bc6168a13") @@ -194,8 +209,7 @@ pub fn compute_and_verify_kzg_proof_round_trip_test< evaluate_polynomial_in_evaluation_form: &dyn Fn(&TPoly, &TFr, &TKZGSettings) -> TFr, verify_kzg_proof: &dyn Fn(&TG1, &TFr, &TFr, &TG1, &TKZGSettings) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); let mut rng = rand::thread_rng(); let z_fr = { @@ -247,8 +261,7 @@ pub fn compute_and_verify_kzg_proof_within_domain_test< evaluate_polynomial_in_evaluation_form: &dyn Fn(&TPoly, &TFr, &TKZGSettings) -> TFr, verify_kzg_proof: &dyn Fn(&TG1, &TFr, &TFr, &TG1, &TKZGSettings) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); let mut rng = rand::thread_rng(); for i in 0..25 { @@ -296,8 +309,7 @@ pub fn compute_and_verify_kzg_proof_fails_with_incorrect_proof_test< evaluate_polynomial_in_evaluation_form: &dyn Fn(&TPoly, &TFr, &TKZGSettings) -> TFr, verify_kzg_proof: &dyn Fn(&TG1, &TFr, &TFr, &TG1, &TKZGSettings) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); let mut rng = rand::thread_rng(); let z_fr = { @@ -346,8 +358,7 @@ pub fn compute_and_verify_blob_kzg_proof_test< compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result, verify_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TG1, &TKZGSettings) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); let mut rng = rand::thread_rng(); // Some preparation @@ -380,8 +391,7 @@ pub fn compute_and_verify_blob_kzg_proof_fails_with_incorrect_proof_test< compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result, verify_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TG1, &TKZGSettings) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); let mut rng = rand::thread_rng(); // Some preparation @@ -422,8 +432,7 @@ pub fn verify_kzg_proof_batch_test< &TKZGSettings, ) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); let mut rng = rand::thread_rng(); const N_SAMPLES: usize = 16; @@ -481,8 +490,7 @@ pub fn verify_kzg_proof_batch_fails_with_incorrect_proof_test< &TKZGSettings, ) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); let mut rng = rand::thread_rng(); const N_SAMPLES: usize = 2; @@ -535,12 +543,15 @@ pub fn test_vectors_blob_to_kzg_commitment< blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); - let test_files: Vec = glob::glob(BLOB_TO_KZG_COMMITMENT_TESTS) - .unwrap() - .map(Result::unwrap) - .collect(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); + let test_files: Vec = glob::glob(&format!( + "{}/{}", + get_manifest_dir(), + BLOB_TO_KZG_COMMITMENT_TESTS + )) + .unwrap() + .map(Result::unwrap) + .collect(); assert!(!test_files.is_empty()); for test_file in test_files { @@ -579,12 +590,15 @@ pub fn test_vectors_compute_kzg_proof< compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> Result<(TG1, TFr), String>, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); - let test_files: Vec = glob::glob(COMPUTE_KZG_PROOF_TESTS) - .unwrap() - .map(Result::unwrap) - .collect(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); + let test_files: Vec = glob::glob(&format!( + "{}/{}", + get_manifest_dir(), + COMPUTE_KZG_PROOF_TESTS + )) + .unwrap() + .map(Result::unwrap) + .collect(); assert!(!test_files.is_empty()); for test_file in test_files { @@ -640,12 +654,15 @@ pub fn test_vectors_compute_blob_kzg_proof< bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); - let test_files: Vec = glob::glob(COMPUTE_BLOB_KZG_PROOF_TESTS) - .unwrap() - .map(Result::unwrap) - .collect(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); + let test_files: Vec = glob::glob(&format!( + "{}/{}", + get_manifest_dir(), + COMPUTE_BLOB_KZG_PROOF_TESTS + )) + .unwrap() + .map(Result::unwrap) + .collect(); assert!(!test_files.is_empty()); for test_file in test_files { @@ -698,12 +715,15 @@ pub fn test_vectors_verify_kzg_proof< load_trusted_setup: &dyn Fn(&str) -> Result, verify_kzg_proof: &dyn Fn(&TG1, &TFr, &TFr, &TG1, &TKZGSettings) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); - let test_files: Vec = glob::glob(VERIFY_KZG_PROOF_TESTS) - .unwrap() - .map(Result::unwrap) - .collect(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); + let test_files: Vec = glob::glob(&format!( + "{}/{}", + get_manifest_dir(), + VERIFY_KZG_PROOF_TESTS + )) + .unwrap() + .map(Result::unwrap) + .collect(); assert!(!test_files.is_empty()); for test_file in test_files { @@ -765,12 +785,15 @@ pub fn test_vectors_verify_blob_kzg_proof< bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, verify_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TG1, &TKZGSettings) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); - let test_files: Vec = glob::glob(VERIFY_BLOB_KZG_PROOF_TESTS) - .unwrap() - .map(Result::unwrap) - .collect(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); + let test_files: Vec = glob::glob(&format!( + "{}/{}", + get_manifest_dir(), + VERIFY_BLOB_KZG_PROOF_TESTS + )) + .unwrap() + .map(Result::unwrap) + .collect(); assert!(!test_files.is_empty()); for test_file in test_files { @@ -830,12 +853,15 @@ pub fn test_vectors_verify_blob_kzg_proof_batch< &TKZGSettings, ) -> Result, ) { - set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); - let test_files: Vec = glob::glob(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS) - .unwrap() - .map(Result::unwrap) - .collect(); + let ts = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); + let test_files: Vec = glob::glob(&format!( + "{}/{}", + get_manifest_dir(), + VERIFY_BLOB_KZG_PROOF_BATCH_TESTS + )) + .unwrap() + .map(Result::unwrap) + .collect(); assert!(!test_files.is_empty()); for test_file in test_files {