Skip to content

Commit

Permalink
Avoid changing directory in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtiomTr committed Sep 30, 2023
1 parent fb1533d commit e23cd92
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 66 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

16 changes: 5 additions & 11 deletions blst/tests/eip_4844.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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();

Check failure on line 256 in blst/tests/eip_4844.rs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest)

cannot find function `get_trusted_setup_path` in this scope

let c_settings = kzg_settings_to_c(&settings);

Expand Down
1 change: 1 addition & 0 deletions kzg-bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
136 changes: 81 additions & 55 deletions kzg-bench/src/tests/eip_4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -77,6 +78,22 @@ pub fn compute_powers_test<TFr: Fr>(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,
Expand All @@ -89,8 +106,7 @@ pub fn blob_to_kzg_commitment_test<
load_trusted_setup: &dyn Fn(&str) -> Result<TKZGSettings, String>,
blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result<TG1, String>,
) {
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")
Expand Down Expand Up @@ -136,8 +152,7 @@ pub fn compute_kzg_proof_test<
blob_to_polynomial: &dyn Fn(&[TFr]) -> Result<TPoly, String>,
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")
Expand Down Expand Up @@ -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<bool, String>,
) {
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 = {
Expand Down Expand Up @@ -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<bool, String>,
) {
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 {
Expand Down Expand Up @@ -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<bool, String>,
) {
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 = {
Expand Down Expand Up @@ -346,8 +358,7 @@ pub fn compute_and_verify_blob_kzg_proof_test<
compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result<TG1, String>,
verify_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TG1, &TKZGSettings) -> Result<bool, String>,
) {
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
Expand Down Expand Up @@ -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<TG1, String>,
verify_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TG1, &TKZGSettings) -> Result<bool, String>,
) {
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
Expand Down Expand Up @@ -422,8 +432,7 @@ pub fn verify_kzg_proof_batch_test<
&TKZGSettings,
) -> Result<bool, String>,
) {
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;
Expand Down Expand Up @@ -481,8 +490,7 @@ pub fn verify_kzg_proof_batch_fails_with_incorrect_proof_test<
&TKZGSettings,
) -> Result<bool, String>,
) {
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;
Expand Down Expand Up @@ -535,12 +543,15 @@ pub fn test_vectors_blob_to_kzg_commitment<
blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result<TG1, String>,
bytes_to_blob: &dyn Fn(&[u8]) -> Result<Vec<TFr>, String>,
) {
set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap();
let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap();
let test_files: Vec<PathBuf> = 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<PathBuf> = 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 {
Expand Down Expand Up @@ -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<Vec<TFr>, String>,
) {
set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap();
let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap();
let test_files: Vec<PathBuf> = 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<PathBuf> = 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 {
Expand Down Expand Up @@ -640,12 +654,15 @@ pub fn test_vectors_compute_blob_kzg_proof<
bytes_to_blob: &dyn Fn(&[u8]) -> Result<Vec<TFr>, String>,
compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result<TG1, String>,
) {
set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap();
let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap();
let test_files: Vec<PathBuf> = 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<PathBuf> = 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 {
Expand Down Expand Up @@ -698,12 +715,15 @@ pub fn test_vectors_verify_kzg_proof<
load_trusted_setup: &dyn Fn(&str) -> Result<TKZGSettings, String>,
verify_kzg_proof: &dyn Fn(&TG1, &TFr, &TFr, &TG1, &TKZGSettings) -> Result<bool, String>,
) {
set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap();
let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap();
let test_files: Vec<PathBuf> = 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<PathBuf> = 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 {
Expand Down Expand Up @@ -765,12 +785,15 @@ pub fn test_vectors_verify_blob_kzg_proof<
bytes_to_blob: &dyn Fn(&[u8]) -> Result<Vec<TFr>, String>,
verify_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TG1, &TKZGSettings) -> Result<bool, String>,
) {
set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap();
let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap();
let test_files: Vec<PathBuf> = 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<PathBuf> = 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 {
Expand Down Expand Up @@ -830,12 +853,15 @@ pub fn test_vectors_verify_blob_kzg_proof_batch<
&TKZGSettings,
) -> Result<bool, String>,
) {
set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap();
let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap();
let test_files: Vec<PathBuf> = 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<PathBuf> = 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 {
Expand Down

0 comments on commit e23cd92

Please sign in to comment.