From 20e5781a37dc1a7ac4ee921411ae99e51ac91102 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Tue, 16 May 2023 16:27:17 -0700 Subject: [PATCH] chore: Switch to tokio test macro for async function --- Cargo.lock | 81 +++++++++++++++++---------- Cargo.toml | 2 +- src/acvm_interop/smart_contract.rs | 89 +++++++++++++++--------------- src/composer.rs | 53 +++++++++--------- src/crs.rs | 70 ++++++++++------------- 5 files changed, 152 insertions(+), 143 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d967d05c..32a00f3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -178,7 +178,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ed4aa4fe255d0bc6d79373f7e31d2ea147bcf486cba1be5ba7ea85abdb92348" dependencies = [ "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -191,7 +191,7 @@ dependencies = [ "num-traits", "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -227,7 +227,7 @@ checksum = "ae3281bc6d0fd7e549af32b52511e1302185bd688fd3359fa36423346ff682ea" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -248,7 +248,7 @@ checksum = "b84f9ebcc6c1f5b8cb160f6990096a5c127f423fcb6e1ccc46c370cbdfb75dfc" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -319,7 +319,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn", + "syn 1.0.107", "which", ] @@ -401,7 +401,7 @@ checksum = "13e576ebe98e605500b3c8041bb888e966653577172df6dd97398714eb30b9bf" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -667,7 +667,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -678,7 +678,7 @@ checksum = "b36230598a2d5de7ec1c6f51f72d8a99a9208daff41de2084d06e3fd3ea56685" dependencies = [ "darling_core", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -698,7 +698,7 @@ checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -808,7 +808,7 @@ checksum = "c134c37760b27a871ba422106eedbb8247da973a09e82558bf26d619c882b159" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -829,7 +829,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -938,7 +938,7 @@ checksum = "95a73af87da33b5acf53acfebdc339fe592ecf5357ac7c0a7734ab9d8c876a70" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -1374,7 +1374,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0fbfc88337168279f2e9ae06e157cfed4efd3316e14dc96ed074d4f2e6c5952" dependencies = [ "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -1653,7 +1653,7 @@ dependencies = [ "proc-macro-error-attr", "proc-macro2", "quote", - "syn", + "syn 1.0.107", "version_check", ] @@ -1670,9 +1670,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.51" +version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +checksum = "c4ec6d5fe0b140acb27c9a0444118cf55bfbb4e0b259739429abb4521dd67c16" dependencies = [ "unicode-ident", ] @@ -1694,14 +1694,14 @@ checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] name = "quote" -version = "1.0.23" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" dependencies = [ "proc-macro2", ] @@ -1922,7 +1922,7 @@ checksum = "ff26ed6c7c4dfc2aa9480b86a60e3c7233543a270a680e10758a507c5a4ce476" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -1968,7 +1968,7 @@ dependencies = [ "quote", "rust-embed-utils", "shellexpand", - "syn", + "syn 1.0.107", "walkdir", ] @@ -2100,7 +2100,7 @@ checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -2264,6 +2264,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "synstructure" version = "0.12.6" @@ -2272,7 +2283,7 @@ checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", "unicode-xid", ] @@ -2313,7 +2324,7 @@ checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -2353,9 +2364,21 @@ dependencies = [ "mio", "pin-project-lite", "socket2", + "tokio-macros", "windows-sys 0.45.0", ] +[[package]] +name = "tokio-macros" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.16", +] + [[package]] name = "tokio-rustls" version = "0.23.4" @@ -2408,7 +2431,7 @@ checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -2564,7 +2587,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 1.0.107", "wasm-bindgen-shared", ] @@ -2598,7 +2621,7 @@ checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2719,7 +2742,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -3117,6 +3140,6 @@ checksum = "44bf07cb3e50ea2003396695d58bf46bc9887a1f362260446fad6bc4e79bd36c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", "synstructure", ] diff --git a/Cargo.toml b/Cargo.toml index a200c72c..db436b4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ pkg-config = "0.3" blake2 = "0.10.6" sled = "0.34.6" tempfile = "3.3" -tokio = "1.0" +tokio = { version = "1.0", features = [ "macros" ] } [features] default = ["native"] diff --git a/src/acvm_interop/smart_contract.rs b/src/acvm_interop/smart_contract.rs index 341b8e25..b612e3a0 100644 --- a/src/acvm_interop/smart_contract.rs +++ b/src/acvm_interop/smart_contract.rs @@ -75,47 +75,50 @@ impl SmartContract for Barretenberg { } } -#[test] -fn test_smart_contract() -> Result<(), BackendError> { - use crate::barretenberg_structures::{Constraint, ConstraintSystem}; - use crate::composer::Composer; - use crate::Barretenberg; - use acvm::FieldElement; - use tokio::runtime::Builder; - - let constraint = Constraint { - a: 1, - b: 2, - c: 3, - qm: FieldElement::zero(), - ql: FieldElement::one(), - qr: FieldElement::one(), - qo: -FieldElement::one(), - qc: FieldElement::zero(), - }; - - let constraint_system = ConstraintSystem::new() - .var_num(4) - .public_inputs(vec![1, 2]) - .constraints(vec![constraint]); - - let bb = Barretenberg::new(); - let crs = Builder::new_current_thread() - .enable_all() - .build() - .unwrap() - .block_on(bb.get_crs(&constraint_system))?; - - let proving_key = bb.compute_proving_key(&constraint_system)?; - let verification_key = bb.compute_verification_key(&crs, &proving_key)?; - - let common_reference_string: Vec = crs.try_into()?; - - let contract = bb.eth_contract_from_vk(&common_reference_string, &verification_key)?; - - assert!(contract.contains("contract BaseUltraVerifier")); - assert!(contract.contains("contract UltraVerifier")); - assert!(contract.contains("library UltraVerificationKey")); - - Ok(()) +#[cfg(test)] +mod tests { + use acvm::SmartContract; + use tokio::test; + + use crate::BackendError; + + #[test] + async fn test_smart_contract() -> Result<(), BackendError> { + use crate::barretenberg_structures::{Constraint, ConstraintSystem}; + use crate::composer::Composer; + use crate::Barretenberg; + use acvm::FieldElement; + + let constraint = Constraint { + a: 1, + b: 2, + c: 3, + qm: FieldElement::zero(), + ql: FieldElement::one(), + qr: FieldElement::one(), + qo: -FieldElement::one(), + qc: FieldElement::zero(), + }; + + let constraint_system = ConstraintSystem::new() + .var_num(4) + .public_inputs(vec![1, 2]) + .constraints(vec![constraint]); + + let bb = Barretenberg::new(); + let crs = bb.get_crs(&constraint_system).await?; + + let proving_key = bb.compute_proving_key(&constraint_system)?; + let verification_key = bb.compute_verification_key(&crs, &proving_key)?; + + let common_reference_string: Vec = crs.try_into()?; + + let contract = bb.eth_contract_from_vk(&common_reference_string, &verification_key)?; + + assert!(contract.contains("contract BaseUltraVerifier")); + assert!(contract.contains("contract UltraVerifier")); + assert!(contract.contains("library UltraVerificationKey")); + + Ok(()) + } } diff --git a/src/composer.rs b/src/composer.rs index b18ec53d..9665612c 100644 --- a/src/composer.rs +++ b/src/composer.rs @@ -407,6 +407,7 @@ fn prepend_public_inputs(proof: Vec, public_inputs: Assignments) -> Vec #[cfg(test)] mod test { use acvm::FieldElement; + use tokio::test; use super::*; use crate::{ @@ -419,7 +420,7 @@ mod test { }; #[test] - fn test_no_constraints_no_pub_inputs() -> Result<(), Error> { + async fn test_no_constraints_no_pub_inputs() -> Result<(), Error> { let constraint_system = ConstraintSystem::new(); let case_1 = WitnessResult { @@ -429,11 +430,11 @@ mod test { }; let test_cases = vec![case_1]; - test_composer_with_pk_vk(constraint_system, test_cases) + test_composer_with_pk_vk(constraint_system, test_cases).await } #[test] - fn test_a_single_constraint_no_pub_inputs() -> Result<(), Error> { + async fn test_a_single_constraint_no_pub_inputs() -> Result<(), Error> { let constraint = Constraint { a: 1, b: 2, @@ -486,10 +487,11 @@ mod test { }; let test_cases = vec![case_1, case_2, case_3, case_4, case_5]; - test_composer_with_pk_vk(constraint_system, test_cases) + test_composer_with_pk_vk(constraint_system, test_cases).await } + #[test] - fn test_a_single_constraint_with_pub_inputs() -> Result<(), Error> { + async fn test_a_single_constraint_with_pub_inputs() -> Result<(), Error> { let constraint = Constraint { a: 1, b: 2, @@ -558,11 +560,11 @@ mod test { /*case_1,*/ case_2, case_3, /*case_4,*/ case_5, case_6, ]; - test_composer_with_pk_vk(constraint_system, test_cases) + test_composer_with_pk_vk(constraint_system, test_cases).await } #[test] - fn test_multiple_constraints() -> Result<(), Error> { + async fn test_multiple_constraints() -> Result<(), Error> { let constraint = Constraint { a: 1, b: 2, @@ -600,11 +602,11 @@ mod test { result: false, }; - test_composer_with_pk_vk(constraint_system, vec![case_1, case_2]) + test_composer_with_pk_vk(constraint_system, vec![case_1, case_2]).await } #[test] - fn test_schnorr_constraints() -> Result<(), Error> { + async fn test_schnorr_constraints() -> Result<(), Error> { let mut signature_indices = [0i32; 64]; for i in 13..(13 + 64) { signature_indices[i - 13] = i as i32; @@ -677,11 +679,11 @@ mod test { result: true, }; - test_composer_with_pk_vk(constraint_system, vec![case_1]) + test_composer_with_pk_vk(constraint_system, vec![case_1]).await } #[test] - fn test_keccak256_constraint() -> Result<(), Error> { + async fn test_keccak256_constraint() -> Result<(), Error> { let input_value: u128 = 0xbd; let input_index = 1; @@ -728,10 +730,11 @@ mod test { result: true, }; - test_composer_with_pk_vk(constraint_system, vec![case_1]) + test_composer_with_pk_vk(constraint_system, vec![case_1]).await } + #[test] - fn test_ped_constraints() -> Result<(), Error> { + async fn test_ped_constraints() -> Result<(), Error> { let constraint = PedersenConstraint { inputs: vec![1, 2], result_x: 3, @@ -780,11 +783,11 @@ mod test { result: true, }; - test_composer_with_pk_vk(constraint_system, vec![case_1]) + test_composer_with_pk_vk(constraint_system, vec![case_1]).await } #[test] - fn test_memory_constraints() -> Result<(), Error> { + async fn test_memory_constraints() -> Result<(), Error> { let two_field = FieldElement::one() + FieldElement::one(); let one = Constraint { a: 0, @@ -886,11 +889,11 @@ mod test { result: false, }; - test_composer_with_pk_vk(constraint_system, vec![case_1, case_2]) + test_composer_with_pk_vk(constraint_system, vec![case_1, case_2]).await } #[test] - fn test_compute_merkle_root_constraint() -> Result<(), Error> { + async fn test_compute_merkle_root_constraint() -> Result<(), Error> { use tempfile::tempdir; let temp_dir = tempdir().unwrap(); let mut msg_hasher: blake2::Blake2s256 = MessageHasher::new(); @@ -937,11 +940,11 @@ mod test { result: true, }; - test_composer_with_pk_vk(constraint_system, vec![case_1]) + test_composer_with_pk_vk(constraint_system, vec![case_1]).await } #[test] - fn test_logic_constraints() -> Result<(), Error> { + async fn test_logic_constraints() -> Result<(), Error> { /* * constraints produced by Noir program: * fn main(x : u32, y : pub u32) { @@ -1027,7 +1030,7 @@ mod test { result: true, }; - test_composer_with_pk_vk(constraint_system, vec![case_1]) + test_composer_with_pk_vk(constraint_system, vec![case_1]).await } #[derive(Clone, Debug)] @@ -1037,18 +1040,12 @@ mod test { result: bool, } - fn test_composer_with_pk_vk( + async fn test_composer_with_pk_vk( constraint_system: ConstraintSystem, test_cases: Vec, ) -> Result<(), Error> { - use tokio::runtime::Builder; - let bb = Barretenberg::new(); - let crs = Builder::new_current_thread() - .enable_all() - .build() - .unwrap() - .block_on(bb.get_crs(&constraint_system))?; + let crs = bb.get_crs(&constraint_system).await?; let proving_key = bb.compute_proving_key(&constraint_system)?; let verification_key = bb.compute_verification_key(&crs, &proving_key)?; diff --git a/src/crs.rs b/src/crs.rs index fddb7b52..76b4bf10 100644 --- a/src/crs.rs +++ b/src/crs.rs @@ -145,57 +145,43 @@ pub(crate) async fn download_crs(num_points: usize) -> Result { }) } -#[cfg(feature = "native")] -#[test] -fn does_not_panic() -> Result<(), Error> { - use tokio::runtime::Builder; +#[cfg(test)] +mod tests { + use tokio::test; - let num_points = 4 * 1024; + use crate::{crs::download_crs, Error}; - let crs = Builder::new_current_thread() - .enable_all() - .build() - .unwrap() - .block_on(download_crs(num_points))?; + #[test] + async fn does_not_panic() -> Result<(), Error> { + use crate::Barretenberg; - let p_points = barretenberg_sys::pippenger::new(&crs.g1_data); + let backend = Barretenberg::default(); + let num_points = 4 * 1024; - unsafe { - Vec::from_raw_parts(p_points as *mut u8, num_points * 32, num_points * 32); - } - //TODO check that p_points memory is properly free + let crs = download_crs(num_points).await?; - Ok(()) -} + let _pippenger = backend.get_pippenger(&crs.g1_data)?; -#[test] -fn crs_update() -> Result<(), Error> { - use tokio::runtime::Builder; + //TODO check that p_points memory is properly free - let partial_num_points = 2; - let full_num_points = 12; + Ok(()) + } - // Create a partial CRS - let mut partial_crs = Builder::new_current_thread() - .enable_all() - .build() - .unwrap() - .block_on(download_crs(partial_num_points))?; + #[test] + async fn crs_update() -> Result<(), Error> { + let partial_num_points = 2; + let full_num_points = 12; - // Update the partial CRS to the full number of points - Builder::new_current_thread() - .enable_all() - .build() - .unwrap() - .block_on(partial_crs.update(full_num_points))?; + // Create a partial CRS + let mut partial_crs = download_crs(partial_num_points).await?; - // Fetch a full CRS to compare - let full_crs = Builder::new_current_thread() - .enable_all() - .build() - .unwrap() - .block_on(download_crs(full_num_points))?; + // Update the partial CRS to the full number of points + partial_crs.update(full_num_points).await?; + + // Fetch a full CRS to compare + let full_crs = download_crs(full_num_points).await?; - assert_eq!(partial_crs, full_crs); - Ok(()) + assert_eq!(partial_crs, full_crs); + Ok(()) + } }