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

feat!: use ACVM with non-homogeneous bb calls DO NOT MERGE #1332

Closed
wants to merge 12 commits into from
727 changes: 432 additions & 295 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ edition = "2021"
rust-version = "1.66"

[workspace.dependencies]
acvm = "0.10.3"
#acvm = "0.11.0"
acvm = { git = "https://github.com/noir-lang/acvm", rev = "018d6865e03ddd09ed409b383f38971ec9ec3310" }
arena = { path = "crates/arena" }
fm = { path = "crates/fm" }
iter-extended = { path = "crates/iter-extended" }
Expand Down
1 change: 0 additions & 1 deletion crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ rustc_version = "0.4.0"
acvm.workspace = true
noirc_abi.workspace = true
noirc_driver.workspace = true
iter-extended.workspace = true
toml.workspace = true
serde.workspace = true
thiserror.workspace = true
10 changes: 4 additions & 6 deletions crates/nargo/src/ops/codegen_verifier.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use acvm::SmartContract;

use crate::NargoError;

pub fn codegen_verifier(
backend: &impl SmartContract,
pub fn codegen_verifier<B: SmartContract>(
backend: &B,
verification_key: &[u8],
) -> Result<String, NargoError> {
Ok(backend.eth_contract_from_vk(verification_key))
) -> Result<String, B::Error> {
backend.eth_contract_from_vk(verification_key)
}
5 changes: 3 additions & 2 deletions crates/nargo/src/ops/execute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use acvm::pwg::{solve, PartialWitnessGeneratorStatus};
use acvm::PartialWitnessGenerator;
use acvm::{acir::circuit::Circuit, pwg::block::Blocks};
use acvm::{PartialWitnessGenerator, PartialWitnessGeneratorStatus};
use noirc_abi::WitnessMap;

use crate::NargoError;
Expand All @@ -10,7 +11,7 @@ pub fn execute_circuit(
mut initial_witness: WitnessMap,
) -> Result<WitnessMap, NargoError> {
let mut blocks = Blocks::default();
let solver_status = backend.solve(&mut initial_witness, &mut blocks, circuit.opcodes)?;
let solver_status = solve(backend, &mut initial_witness, &mut blocks, circuit.opcodes)?;
if matches!(solver_status, PartialWitnessGeneratorStatus::RequiresOracleData { .. }) {
todo!("Add oracle support to nargo execute")
}
Expand Down
37 changes: 18 additions & 19 deletions crates/nargo/src/ops/preprocess.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
use acvm::ProofSystemCompiler;
use iter_extended::vecmap;
use noirc_driver::{CompiledContract, CompiledProgram};

use crate::{
artifacts::{
contract::{PreprocessedContract, PreprocessedContractFunction},
program::PreprocessedProgram,
},
NargoError,
use crate::artifacts::{
contract::{PreprocessedContract, PreprocessedContractFunction},
program::PreprocessedProgram,
};

// TODO: pull this from backend.
const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg";

pub fn preprocess_program(
backend: &impl ProofSystemCompiler,
pub fn preprocess_program<B: ProofSystemCompiler>(
backend: &B,
compiled_program: CompiledProgram,
) -> Result<PreprocessedProgram, NargoError> {
) -> Result<PreprocessedProgram, B::Error> {
// TODO: currently `compiled_program`'s bytecode is already optimized for the backend.
// In future we'll need to apply those optimizations here.
let optimized_bytecode = compiled_program.circuit;
let (proving_key, verification_key) = backend.preprocess(&optimized_bytecode);
let (proving_key, verification_key) = backend.preprocess(&optimized_bytecode)?;

Ok(PreprocessedProgram {
backend: String::from(BACKEND_IDENTIFIER),
Expand All @@ -31,26 +27,29 @@ pub fn preprocess_program(
})
}

pub fn preprocess_contract(
backend: &impl ProofSystemCompiler,
pub fn preprocess_contract<B: ProofSystemCompiler>(
backend: &B,
compiled_contract: CompiledContract,
) -> Result<PreprocessedContract, NargoError> {
let preprocessed_contract_functions = vecmap(compiled_contract.functions, |func| {
) -> Result<PreprocessedContract, B::Error> {
let mut preprocessed_contract_functions = vec![];
for func in compiled_contract.functions.into_iter() {
// TODO: currently `func`'s bytecode is already optimized for the backend.
// In future we'll need to apply those optimizations here.
let optimized_bytecode = func.bytecode;
let (proving_key, verification_key) = backend.preprocess(&optimized_bytecode);
let (proving_key, verification_key) = backend.preprocess(&optimized_bytecode)?;

PreprocessedContractFunction {
let preprocessed = PreprocessedContractFunction {
name: func.name,
function_type: func.function_type,
abi: func.abi,

bytecode: optimized_bytecode,
proving_key,
verification_key,
}
});
};

preprocessed_contract_functions.push(preprocessed);
}

Ok(PreprocessedContract {
name: compiled_contract.name,
Expand Down
12 changes: 4 additions & 8 deletions crates/nargo/src/ops/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ use acvm::acir::circuit::Circuit;
use acvm::ProofSystemCompiler;
use noirc_abi::WitnessMap;

use crate::NargoError;

pub fn prove_execution(
backend: &impl ProofSystemCompiler,
pub fn prove_execution<B: ProofSystemCompiler>(
backend: &B,
circuit: &Circuit,
solved_witness: WitnessMap,
proving_key: &[u8],
) -> Result<Vec<u8>, NargoError> {
let proof = backend.prove_with_pk(circuit, solved_witness, proving_key);

Ok(proof)
) -> Result<Vec<u8>, B::Error> {
backend.prove_with_pk(circuit, solved_witness, proving_key)
}
12 changes: 4 additions & 8 deletions crates/nargo/src/ops/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ use acvm::acir::circuit::Circuit;
use acvm::ProofSystemCompiler;
use noirc_abi::WitnessMap;

use crate::NargoError;

pub fn verify_proof(
backend: &impl ProofSystemCompiler,
pub fn verify_proof<B: ProofSystemCompiler>(
backend: &B,
circuit: &Circuit,
proof: &[u8],
public_inputs: WitnessMap,
verification_key: &[u8],
) -> Result<bool, NargoError> {
let valid_proof = backend.verify_with_vk(proof, public_inputs, circuit, verification_key);

Ok(valid_proof)
) -> Result<bool, B::Error> {
backend.verify_with_vk(proof, public_inputs, circuit, verification_key)
}
4 changes: 3 additions & 1 deletion crates/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ termcolor = "1.1.2"
color-eyre = "0.6.2"

# Backends
acvm-backend-barretenberg = { git = "https://github.com/noir-lang/aztec_backend", rev = "677f10e07011849f8aa0d75fe80390bb3081b1e5", default-features = false }
#acvm-backend-barretenberg = { git = "https://github.com/noir-lang/aztec_backend", rev = "0aa72dec1367b4f546ba96a83894b8ae062240f4", default-features = false }
acvm-backend-barretenberg = { git = "https://github.com/noir-lang/aztec_backend", rev = "51457b47adf8f5b8c97d935ebe7176f3c8620e16", default-features = false }


[dev-dependencies]
tempdir = "0.3.7"
Expand Down
27 changes: 18 additions & 9 deletions crates/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{errors::CliError, resolver::Resolver};
use acvm::ProofSystemCompiler;
use acvm::Backend;
use clap::Args;
use iter_extended::btree_map;
use noirc_abi::{AbiParameter, AbiType, MAIN_RETURN_NAME};
Expand All @@ -17,15 +17,21 @@ pub(crate) struct CheckCommand {
compile_options: CompileOptions,
}

pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliError> {
check_from_path(config.program_dir, &args.compile_options)?;
pub(crate) fn run<B: Backend>(
backend: &B,
args: CheckCommand,
config: NargoConfig,
) -> Result<(), CliError<B>> {
check_from_path(backend, config.program_dir, &args.compile_options)?;
println!("Constraint system successfully built!");
Ok(())
}

fn check_from_path<P: AsRef<Path>>(p: P, compile_options: &CompileOptions) -> Result<(), CliError> {
let backend = crate::backends::ConcreteBackend::default();

fn check_from_path<B: Backend, P: AsRef<Path>>(
backend: &B,
p: P,
compile_options: &CompileOptions,
) -> Result<(), CliError<B>> {
let mut driver = Resolver::resolve_root_manifest(p.as_ref(), backend.np_language())?;

driver.check_crate(compile_options).map_err(|_| CliError::CompilationError)?;
Expand Down Expand Up @@ -148,12 +154,13 @@ d2 = ["", "", ""]
let pass_dir =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(format!("{TEST_DATA_DIR}/pass"));

let backend = crate::backends::ConcreteBackend::default();
let config = CompileOptions::default();
let paths = std::fs::read_dir(pass_dir).unwrap();
for path in paths.flatten() {
let path = path.path();
assert!(
super::check_from_path(path.clone(), &config).is_ok(),
super::check_from_path(&backend, path.clone(), &config).is_ok(),
"path: {}",
path.display()
);
Expand All @@ -166,12 +173,13 @@ d2 = ["", "", ""]
let fail_dir =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(format!("{TEST_DATA_DIR}/fail"));

let backend = crate::backends::ConcreteBackend::default();
let config = CompileOptions::default();
let paths = std::fs::read_dir(fail_dir).unwrap();
for path in paths.flatten() {
let path = path.path();
assert!(
super::check_from_path(path.clone(), &config).is_err(),
super::check_from_path(&backend, path.clone(), &config).is_err(),
"path: {}",
path.display()
);
Expand All @@ -183,13 +191,14 @@ d2 = ["", "", ""]
let pass_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join(format!("{TEST_DATA_DIR}/pass_dev_mode"));

let backend = crate::backends::ConcreteBackend::default();
let config = CompileOptions { allow_warnings: true, ..Default::default() };

let paths = std::fs::read_dir(pass_dir).unwrap();
for path in paths.flatten() {
let path = path.path();
assert!(
super::check_from_path(path.clone(), &config).is_ok(),
super::check_from_path(&backend, path.clone(), &config).is_ok(),
"path: {}",
path.display()
);
Expand Down
17 changes: 11 additions & 6 deletions crates/nargo_cli/src/cli/codegen_verifier_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
cli::compile_cmd::compile_circuit, constants::CONTRACT_DIR, constants::TARGET_DIR,
errors::CliError,
};
use acvm::Backend;
use clap::Args;
use nargo::ops::{codegen_verifier, preprocess_program};
use noirc_driver::CompileOptions;
Expand All @@ -18,9 +19,11 @@ pub(crate) struct CodegenVerifierCommand {
compile_options: CompileOptions,
}

pub(crate) fn run(args: CodegenVerifierCommand, config: NargoConfig) -> Result<(), CliError> {
let backend = crate::backends::ConcreteBackend::default();

pub(crate) fn run<B: Backend>(
backend: &B,
args: CodegenVerifierCommand,
config: NargoConfig,
) -> Result<(), CliError<B>> {
// TODO(#1201): Should this be a utility function?
let circuit_build_path = args
.circuit_name
Expand All @@ -30,12 +33,14 @@ pub(crate) fn run(args: CodegenVerifierCommand, config: NargoConfig) -> Result<(
Some(circuit_build_path) => read_program_from_file(circuit_build_path)?,
None => {
let compiled_program =
compile_circuit(&backend, config.program_dir.as_ref(), &args.compile_options)?;
preprocess_program(&backend, compiled_program)?
compile_circuit(backend, config.program_dir.as_ref(), &args.compile_options)?;
preprocess_program(backend, compiled_program)
.map_err(CliError::ProofSystemCompilerError)?
}
};

let smart_contract_string = codegen_verifier(&backend, &preprocessed_program.verification_key)?;
let smart_contract_string = codegen_verifier(backend, &preprocessed_program.verification_key)
.map_err(CliError::SmartContractError)?;

let contract_dir = config.program_dir.join(CONTRACT_DIR);
create_named_dir(&contract_dir, "contract");
Expand Down
36 changes: 21 additions & 15 deletions crates/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use acvm::ProofSystemCompiler;
use iter_extended::try_vecmap;
use acvm::Backend;
use noirc_driver::{CompileOptions, CompiledProgram, Driver};
use std::path::Path;

Expand Down Expand Up @@ -27,19 +26,25 @@ pub(crate) struct CompileCommand {
compile_options: CompileOptions,
}

pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliError> {
pub(crate) fn run<B: Backend>(
backend: &B,
args: CompileCommand,
config: NargoConfig,
) -> Result<(), CliError<B>> {
let circuit_dir = config.program_dir.join(TARGET_DIR);

let backend = crate::backends::ConcreteBackend::default();

// If contracts is set we're compiling every function in a 'contract' rather than just 'main'.
if args.contracts {
let mut driver = setup_driver(&backend, &config.program_dir)?;
let mut driver = setup_driver(backend, &config.program_dir)?;
let compiled_contracts = driver
.compile_contracts(&args.compile_options)
.map_err(|_| CliError::CompilationError)?;
let preprocessed_contracts =
try_vecmap(compiled_contracts, |contract| preprocess_contract(&backend, contract))?;
let mut preprocessed_contracts = vec![];
for contract in compiled_contracts {
let preprocessed = preprocess_contract(backend, contract)
.map_err(CliError::ProofSystemCompilerError)?;
preprocessed_contracts.push(preprocessed);
}
for contract in preprocessed_contracts {
save_contract_to_file(
&contract,
Expand All @@ -48,25 +53,26 @@ pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliEr
);
}
} else {
let program = compile_circuit(&backend, &config.program_dir, &args.compile_options)?;
let preprocessed_program = preprocess_program(&backend, program)?;
let program = compile_circuit(backend, &config.program_dir, &args.compile_options)?;
let preprocessed_program =
preprocess_program(backend, program).map_err(CliError::ProofSystemCompilerError)?;
save_program_to_file(&preprocessed_program, &args.circuit_name, circuit_dir);
}
Ok(())
}

fn setup_driver(
backend: &impl ProofSystemCompiler,
fn setup_driver<B: Backend>(
backend: &B,
program_dir: &Path,
) -> Result<Driver, DependencyResolutionError> {
Resolver::resolve_root_manifest(program_dir, backend.np_language())
}

pub(crate) fn compile_circuit(
backend: &impl ProofSystemCompiler,
pub(crate) fn compile_circuit<B: Backend>(
backend: &B,
program_dir: &Path,
compile_options: &CompileOptions,
) -> Result<CompiledProgram, CliError> {
) -> Result<CompiledProgram, CliError<B>> {
let mut driver = setup_driver(backend, program_dir)?;
driver.compile_main(compile_options).map_err(|_| CliError::CompilationError)
}
Loading