From 8e2361aaddba08b9374631120e64a13851a6f0bb Mon Sep 17 00:00:00 2001 From: kevaundray Date: Sun, 20 Aug 2023 18:49:20 +0000 Subject: [PATCH] use acvm branch --- Cargo.lock | 104 +++++++++++++----- Cargo.toml | 3 + crates/nargo/src/ops/codegen_verifier.rs | 7 +- crates/nargo/src/ops/prove.rs | 6 +- crates/nargo/src/ops/verify.rs | 6 +- .../nargo_cli/src/cli/codegen_verifier_cmd.rs | 33 +----- crates/nargo_cli/src/cli/compile_cmd.rs | 84 ++++++-------- .../src/cli/fs/common_reference_string.rs | 55 --------- crates/nargo_cli/src/cli/fs/mod.rs | 1 - crates/nargo_cli/src/cli/prove_cmd.rs | 21 +--- crates/nargo_cli/src/cli/verify_cmd.rs | 20 +--- crates/nargo_cli/src/errors.rs | 9 +- 12 files changed, 132 insertions(+), 217 deletions(-) delete mode 100644 crates/nargo_cli/src/cli/fs/common_reference_string.rs diff --git a/Cargo.lock b/Cargo.lock index 3b68b5004d8..5814fbb10fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,19 +50,22 @@ dependencies = [ [[package]] name = "acvm-backend-barretenberg" version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "438eb3837cfc37e0798e18f4a0ebae595e4cbe32a3f4cecfb47ccc1354180dc8" +source = "git+https://github.com/noir-lang/acvm-backend-barretenberg?branch=kw/dyn-backends#65ef893a1e99a19566b0f5c0b16f53968af8352f" dependencies = [ "acvm", "barretenberg-sys", - "bincode", - "bytesize", + "base64", + "build-target", + "const_format", + "dirs 5.0.1", + "flate2", "getrandom", "pkg-config", "reqwest", "rust-embed", - "serde", - "serde-big-array", + "tar", + "tempdir", + "tempfile", "thiserror", "wasmer", ] @@ -578,6 +581,12 @@ dependencies = [ "safe-regex", ] +[[package]] +name = "build-target" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "832133bbabbbaa9fbdba793456a2827627a7d2b8fb96032fa1e7666d7895832b" + [[package]] name = "bumpalo" version = "3.13.0" @@ -624,12 +633,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" -[[package]] -name = "bytesize" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38fcc2979eff34a4b84e1cf9a1e3da42a7d44b3b690a40cdcb23e3d556cfb2e5" - [[package]] name = "cast" version = "0.3.0" @@ -1223,7 +1226,16 @@ version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" dependencies = [ - "dirs-sys", + "dirs-sys 0.3.7", +] + +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys 0.4.1", ] [[package]] @@ -1247,6 +1259,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "dirs-sys-next" version = "0.1.2" @@ -1420,6 +1444,18 @@ dependencies = [ "subtle", ] +[[package]] +name = "filetime" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4029edd3e734da6fe05b6cd7bd2960760a616bd2ddd0d59a0124746d6272af0" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.3.5", + "windows-sys 0.48.0", +] + [[package]] name = "findshlibs" version = "0.10.2" @@ -2255,7 +2291,7 @@ dependencies = [ "color-eyre", "const_format", "criterion", - "dirs", + "dirs 4.0.0", "fm", "hex", "iai", @@ -2287,7 +2323,7 @@ dependencies = [ name = "nargo_toml" version = "0.10.3" dependencies = [ - "dirs", + "dirs 4.0.0", "fm", "nargo", "noirc_frontend", @@ -2512,6 +2548,12 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "owo-colors" version = "3.5.0" @@ -3336,15 +3378,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde-big-array" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11fc7cc2c76d73e0f27ee52abbd64eec84d46f370c88371120433196934e4b7f" -dependencies = [ - "serde", -] - [[package]] name = "serde-wasm-bindgen" version = "0.4.5" @@ -3475,7 +3508,7 @@ version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ccc8076840c4da029af4f87e4e8daeb0fca6b87bbb02e10cb60b791450e11e4" dependencies = [ - "dirs", + "dirs 4.0.0", ] [[package]] @@ -3688,6 +3721,17 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "tar" +version = "0.4.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b16afcea1f22891c49a00c751c7b63b2233284064f11a200fc624137c51e2ddb" +dependencies = [ + "filetime", + "libc", + "xattr", +] + [[package]] name = "target-lexicon" version = "0.12.10" @@ -3849,6 +3893,7 @@ dependencies = [ "bytes", "libc", "mio", + "num_cpus", "pin-project-lite", "socket2", "tokio-macros", @@ -4623,6 +4668,15 @@ dependencies = [ "tap", ] +[[package]] +name = "xattr" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4686009f71ff3e5c4dbcf1a282d0a44db3f021ba69350cd42086b3e5f1c6985" +dependencies = [ + "libc", +] + [[package]] name = "zeroize" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index 76ec9edfa0d..18834c5197a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,3 +57,6 @@ url = "2.2.0" wasm-bindgen = { version = "=0.2.86", features = ["serde-serialize"] } wasm-bindgen-test = "0.3.33" base64 = "0.21.2" + +[patch.crates-io] +acvm-backend-barretenberg = { git = "https://github.com/noir-lang/acvm-backend-barretenberg", branch = "kw/dyn-backends" } diff --git a/crates/nargo/src/ops/codegen_verifier.rs b/crates/nargo/src/ops/codegen_verifier.rs index 851f735ae55..5893854ce22 100644 --- a/crates/nargo/src/ops/codegen_verifier.rs +++ b/crates/nargo/src/ops/codegen_verifier.rs @@ -2,9 +2,10 @@ use acvm::{acir::circuit::Circuit, SmartContract}; pub fn codegen_verifier( backend: &B, - common_reference_string: &[u8], circuit: &Circuit, - verification_key: &[u8], ) -> Result { - backend.eth_contract_from_vk(common_reference_string, circuit, verification_key) + // Nargo no longer handles logic related to proving/verifying with keys or the CRS. + let common_reference_string = Vec::new(); + let verification_key = Vec::new(); + backend.eth_contract_from_vk(&common_reference_string, circuit, &verification_key) } diff --git a/crates/nargo/src/ops/prove.rs b/crates/nargo/src/ops/prove.rs index 20d921f32aa..e881d3c3043 100644 --- a/crates/nargo/src/ops/prove.rs +++ b/crates/nargo/src/ops/prove.rs @@ -3,12 +3,12 @@ use acvm::ProofSystemCompiler; pub fn prove_execution( backend: &B, - common_reference_string: &[u8], circuit: &Circuit, solved_witness: WitnessMap, ) -> Result, B::Error> { // TODO(#1569): update from not just accepting `false` once we get nargo to interop with dynamic backend - // Nargo no longer handles logic related to proving/verifying with keys. + // Nargo no longer handles logic related to proving/verifying with keys or the CRS. + let common_reference_string = Vec::new(); let proving_key = Vec::new(); - backend.prove_with_pk(common_reference_string, circuit, solved_witness, &proving_key, false) + backend.prove_with_pk(&common_reference_string, circuit, solved_witness, &proving_key, false) } diff --git a/crates/nargo/src/ops/verify.rs b/crates/nargo/src/ops/verify.rs index f6ec005262e..2d8d9c40d6c 100644 --- a/crates/nargo/src/ops/verify.rs +++ b/crates/nargo/src/ops/verify.rs @@ -3,16 +3,16 @@ use acvm::ProofSystemCompiler; pub fn verify_proof( backend: &B, - common_reference_string: &[u8], circuit: &Circuit, proof: &[u8], public_inputs: WitnessMap, ) -> Result { // TODO(#1569): update from not just accepting `false` once we get nargo to interop with dynamic backend - // Nargo no longer handles logic related to proving/verifying with keys. + // Nargo no longer handles logic related to proving/verifying with keys or the CRS. + let common_reference_string = Vec::new(); let verification_key = Vec::new(); backend.verify_with_vk( - common_reference_string, + &common_reference_string, proof, public_inputs, circuit, diff --git a/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs b/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs index 8f6f613897b..04c7114a565 100644 --- a/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs +++ b/crates/nargo_cli/src/cli/codegen_verifier_cmd.rs @@ -3,15 +3,7 @@ use std::path::PathBuf; use super::NargoConfig; use super::{ compile_cmd::compile_package, - fs::{ - common_reference_string::{ - read_cached_common_reference_string, update_common_reference_string, - write_cached_common_reference_string, - }, - create_named_dir, - program::read_program_from_file, - write_to_file, - }, + fs::{create_named_dir, program::read_program_from_file, write_to_file}, }; use crate::errors::CliError; use acvm::Backend; @@ -90,27 +82,8 @@ fn smart_contract_for_package( } }; - let common_reference_string = read_cached_common_reference_string(); - let common_reference_string = update_common_reference_string( - backend, - &common_reference_string, - &preprocessed_program.bytecode, - ) - .map_err(CliError::CommonReferenceStringError)?; - - let (_, verification_key) = backend - .preprocess(&common_reference_string, &preprocessed_program.bytecode) - .map_err(CliError::ProofSystemCompilerError)?; - - let smart_contract_string = codegen_verifier( - backend, - &common_reference_string, - &preprocessed_program.bytecode, - &verification_key, - ) - .map_err(CliError::SmartContractError)?; - - write_cached_common_reference_string(&common_reference_string); + let smart_contract_string = codegen_verifier(backend, &preprocessed_program.bytecode) + .map_err(CliError::SmartContractError)?; Ok(smart_contract_string) } diff --git a/crates/nargo_cli/src/cli/compile_cmd.rs b/crates/nargo_cli/src/cli/compile_cmd.rs index 878d27362a7..ba6da305721 100644 --- a/crates/nargo_cli/src/cli/compile_cmd.rs +++ b/crates/nargo_cli/src/cli/compile_cmd.rs @@ -1,5 +1,5 @@ use acvm::{acir::circuit::Circuit, compiler::AcirTransformationMap, Backend}; -use iter_extended::try_vecmap; +use iter_extended::{try_vecmap, vecmap}; use nargo::artifacts::contract::PreprocessedContractFunction; use nargo::artifacts::debug::DebugArtifact; use nargo::artifacts::program::PreprocessedProgram; @@ -19,13 +19,8 @@ use clap::Args; use crate::errors::{CliError, CompileError}; -use super::fs::program::save_debug_artifact_to_file; -use super::fs::{ - common_reference_string::{ - read_cached_common_reference_string, update_common_reference_string, - write_cached_common_reference_string, - }, - program::{save_contract_to_file, save_program_to_file}, +use super::fs::program::{ + save_contract_to_file, save_debug_artifact_to_file, save_program_to_file, }; use super::NargoConfig; @@ -67,8 +62,6 @@ pub(crate) fn run( let workspace = resolve_workspace_from_toml(&toml_path, selection)?; let circuit_dir = workspace.target_directory_path(); - let mut common_reference_string = read_cached_common_reference_string(); - for package in &workspace { let (mut context, crate_id) = prepare_package(package); // If `contract` package type, we're compiling every function in a 'contract' rather than just 'main'. @@ -82,44 +75,35 @@ pub(crate) fn run( // As can be seen here, It seems like a leaky abstraction where ContractFunctions (essentially CompiledPrograms) // are compiled via nargo-core and then the PreprocessedContract is constructed here. // This is due to EACH function needing it's own CRS, PKey, and VKey from the backend. - let preprocessed_contracts: Result< - Vec<(PreprocessedContract, Vec)>, - CliError, - > = try_vecmap(optimized_contracts, |contract| { - let preprocess_result = try_vecmap(contract.functions, |func| { - common_reference_string = update_common_reference_string( - backend, - &common_reference_string, - &func.bytecode, - ) - .map_err(CliError::CommonReferenceStringError)?; - - Ok::<_, CliError>(( - PreprocessedContractFunction { - name: func.name, - function_type: func.function_type, - is_internal: func.is_internal, - abi: func.abi, - - bytecode: func.bytecode, + let preprocessed_contracts: Vec<(PreprocessedContract, Vec)> = + vecmap(optimized_contracts, |contract| { + let preprocess_result = vecmap(contract.functions, |func| { + ( + PreprocessedContractFunction { + name: func.name, + function_type: func.function_type, + is_internal: func.is_internal, + abi: func.abi, + + bytecode: func.bytecode, + }, + func.debug, + ) + }); + + let (preprocessed_contract_functions, debug_infos): (Vec<_>, Vec<_>) = + preprocess_result.into_iter().unzip(); + + ( + PreprocessedContract { + name: contract.name, + backend: String::from(BACKEND_IDENTIFIER), + functions: preprocessed_contract_functions, }, - func.debug, - )) - })?; - - let (preprocessed_contract_functions, debug_infos): (Vec<_>, Vec<_>) = - preprocess_result.into_iter().unzip(); - - Ok(( - PreprocessedContract { - name: contract.name, - backend: String::from(BACKEND_IDENTIFIER), - functions: preprocessed_contract_functions, - }, - debug_infos, - )) - }); - for (contract, debug_infos) in preprocessed_contracts? { + debug_infos, + ) + }); + for (contract, debug_infos) in preprocessed_contracts { save_contract_to_file( &contract, &format!("{}-{}", package.name, contract.name), @@ -138,10 +122,6 @@ pub(crate) fn run( } else { let (context, program) = compile_package(backend, package, &args.compile_options)?; - common_reference_string = - update_common_reference_string(backend, &common_reference_string, &program.circuit) - .map_err(CliError::CommonReferenceStringError)?; - let preprocessed_program = PreprocessedProgram { backend: String::from(BACKEND_IDENTIFIER), abi: program.abi, @@ -158,8 +138,6 @@ pub(crate) fn run( } } - write_cached_common_reference_string(&common_reference_string); - Ok(()) } diff --git a/crates/nargo_cli/src/cli/fs/common_reference_string.rs b/crates/nargo_cli/src/cli/fs/common_reference_string.rs deleted file mode 100644 index a22c819d402..00000000000 --- a/crates/nargo_cli/src/cli/fs/common_reference_string.rs +++ /dev/null @@ -1,55 +0,0 @@ -use std::{env, path::PathBuf}; - -use acvm::{acir::circuit::Circuit, CommonReferenceString}; - -use super::{create_named_dir, write_to_file}; - -// TODO(#1388): pull this from backend. -const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg"; -const TRANSCRIPT_NAME: &str = "common-reference-string.bin"; - -fn common_reference_string_location() -> PathBuf { - let cache_dir = match env::var("NARGO_BACKEND_CACHE_DIR") { - Ok(cache_dir) => PathBuf::from(cache_dir), - Err(_) => dirs::home_dir().unwrap().join(".nargo").join("backends"), - }; - cache_dir.join(BACKEND_IDENTIFIER).join(TRANSCRIPT_NAME) -} - -pub(crate) fn read_cached_common_reference_string() -> Vec { - let crs_path = common_reference_string_location(); - - // TODO(#1390): Implement checksum - match std::fs::read(crs_path) { - Ok(common_reference_string) => common_reference_string, - Err(_) => vec![], - } -} - -pub(crate) fn update_common_reference_string( - backend: &B, - common_reference_string: &[u8], - circuit: &Circuit, -) -> Result, B::Error> { - use tokio::runtime::Builder; - - let runtime = Builder::new_current_thread().enable_all().build().unwrap(); - - // TODO(#1391): Implement retries - // If the read data is empty, we don't have a CRS and need to generate one - let fut = if common_reference_string.is_empty() { - backend.generate_common_reference_string(circuit) - } else { - backend.update_common_reference_string(common_reference_string.to_vec(), circuit) - }; - - runtime.block_on(fut) -} - -pub(crate) fn write_cached_common_reference_string(common_reference_string: &[u8]) { - let crs_path = common_reference_string_location(); - - create_named_dir(crs_path.parent().unwrap(), "crs"); - - write_to_file(common_reference_string, &crs_path); -} diff --git a/crates/nargo_cli/src/cli/fs/mod.rs b/crates/nargo_cli/src/cli/fs/mod.rs index 73229e0476c..4ebce3b3325 100644 --- a/crates/nargo_cli/src/cli/fs/mod.rs +++ b/crates/nargo_cli/src/cli/fs/mod.rs @@ -6,7 +6,6 @@ use std::{ use crate::errors::FilesystemError; -pub(super) mod common_reference_string; pub(super) mod inputs; pub(super) mod program; pub(super) mod proof; diff --git a/crates/nargo_cli/src/cli/prove_cmd.rs b/crates/nargo_cli/src/cli/prove_cmd.rs index 26783604fd0..2ec1df2fdd4 100644 --- a/crates/nargo_cli/src/cli/prove_cmd.rs +++ b/crates/nargo_cli/src/cli/prove_cmd.rs @@ -12,9 +12,7 @@ use noirc_driver::CompileOptions; use noirc_frontend::graph::CrateName; use super::compile_cmd::compile_package; -use super::fs::common_reference_string::update_common_reference_string; use super::fs::{ - common_reference_string::read_cached_common_reference_string, inputs::{read_inputs_from_file, write_inputs_to_file}, program::read_program_from_file, proof::save_proof_to_dir, @@ -107,18 +105,6 @@ pub(crate) fn prove_package( (preprocessed_program, Some((program.debug, context))) }; - let common_reference_string = read_cached_common_reference_string(); - let common_reference_string = update_common_reference_string( - backend, - &common_reference_string, - &preprocessed_program.bytecode, - ) - .map_err(CliError::CommonReferenceStringError)?; - - let (proving_key, verification_key) = backend - .preprocess(&common_reference_string, &preprocessed_program.bytecode) - .map_err(CliError::ProofSystemCompilerError)?; - let PreprocessedProgram { abi, bytecode, .. } = preprocessed_program; // Parse the initial witness values from Prover.toml @@ -140,14 +126,13 @@ pub(crate) fn prove_package( Format::Toml, )?; - let proof = prove_execution(backend, &common_reference_string, &bytecode, solved_witness) + let proof = prove_execution(backend, &bytecode, solved_witness) .map_err(CliError::ProofSystemCompilerError)?; if check_proof { let public_inputs = public_abi.encode(&public_inputs, return_value)?; - let valid_proof = - verify_proof(backend, &common_reference_string, &bytecode, &proof, public_inputs) - .map_err(CliError::ProofSystemCompilerError)?; + let valid_proof = verify_proof(backend, &bytecode, &proof, public_inputs) + .map_err(CliError::ProofSystemCompilerError)?; if !valid_proof { return Err(CliError::InvalidProof("".into())); diff --git a/crates/nargo_cli/src/cli/verify_cmd.rs b/crates/nargo_cli/src/cli/verify_cmd.rs index 346c4dd71e6..dfae743c0c7 100644 --- a/crates/nargo_cli/src/cli/verify_cmd.rs +++ b/crates/nargo_cli/src/cli/verify_cmd.rs @@ -1,6 +1,3 @@ -use super::fs::common_reference_string::{ - read_cached_common_reference_string, update_common_reference_string, -}; use super::NargoConfig; use super::{ compile_cmd::compile_package, @@ -91,18 +88,6 @@ fn verify_package( } }; - let common_reference_string = read_cached_common_reference_string(); - let common_reference_string = update_common_reference_string( - backend, - &common_reference_string, - &preprocessed_program.bytecode, - ) - .map_err(CliError::CommonReferenceStringError)?; - - let (_, verification_key) = backend - .preprocess(&common_reference_string, &preprocessed_program.bytecode) - .map_err(CliError::ProofSystemCompilerError)?; - let PreprocessedProgram { abi, bytecode, .. } = preprocessed_program; // Load public inputs (if any) from `verifier_name`. @@ -113,9 +98,8 @@ fn verify_package( let public_inputs = public_abi.encode(&public_inputs_map, return_value)?; let proof = load_hex_data(proof_path)?; - let valid_proof = - verify_proof(backend, &common_reference_string, &bytecode, &proof, public_inputs) - .map_err(CliError::ProofSystemCompilerError)?; + let valid_proof = verify_proof(backend, &bytecode, &proof, public_inputs) + .map_err(CliError::ProofSystemCompilerError)?; if valid_proof { Ok(()) diff --git a/crates/nargo_cli/src/errors.rs b/crates/nargo_cli/src/errors.rs index dade4262431..ac59576692b 100644 --- a/crates/nargo_cli/src/errors.rs +++ b/crates/nargo_cli/src/errors.rs @@ -1,7 +1,4 @@ -use acvm::{ - acir::native_types::WitnessMapError, Backend, CommonReferenceString, ProofSystemCompiler, - SmartContract, -}; +use acvm::{acir::native_types::WitnessMapError, Backend, ProofSystemCompiler, SmartContract}; use hex::FromHexError; use nargo::NargoError; use nargo_toml::ManifestError; @@ -74,10 +71,6 @@ pub(crate) enum CliError { /// Backend error caused by a function on the ProofSystemCompiler trait #[error(transparent)] ProofSystemCompilerError(::Error), // Unfortunately, Rust won't let us `impl From` over an Associated Type on a generic - - /// Backend error caused by a function on the CommonReferenceString trait - #[error(transparent)] - CommonReferenceStringError(::Error), // Unfortunately, Rust won't let us `impl From` over an Associated Type on a generic } /// Errors covering situations where a package cannot be compiled.