-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into phated/nix-ultra
* master: feat(nargo)!: define preprocessed artifacts for programs/contracts (#1126) feat: import core logic in cli from `nargo` crate (#1142) chore: enforce `clippy::semicolon_if_nothing_returned` linting rule (#1139) chore: borrow instead of cloning witness vectors in IR gen (#1127) fix: compiler identifying imported functions as being part of a contract (#1112) feat: Add new `Vec` type to frontend (#1103)
- Loading branch information
Showing
75 changed files
with
693 additions
and
536 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
#![forbid(unsafe_code)] | ||
#![warn(unused_crate_dependencies, unused_extern_crates)] | ||
#![warn(unreachable_pub)] | ||
#![warn(clippy::semicolon_if_nothing_returned)] | ||
|
||
// For now we use a wrapper around generational-arena | ||
pub use generational_arena::{Arena, Index}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use acvm::acir::circuit::Circuit; | ||
use noirc_abi::Abi; | ||
use noirc_driver::ContractFunctionType; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// `PreprocessedContract` represents a Noir contract which has been preprocessed by a particular backend proving system. | ||
/// | ||
/// This differs from a generic Noir contract artifact in that: | ||
/// - The ACIR bytecode has had an optimization pass applied to tailor it for the backend. | ||
/// - Proving and verification keys have been pregenerated based on this ACIR. | ||
#[derive(Serialize, Deserialize)] | ||
pub struct PreprocessedContract { | ||
/// The name of the contract. | ||
pub name: String, | ||
/// The identifier of the proving backend which this contract has been compiled for. | ||
pub backend: String, | ||
/// Each of the contract's functions are compiled into a separate program stored in this `Vec`. | ||
pub functions: Vec<PreprocessedContractFunction>, | ||
} | ||
|
||
/// Each function in the contract will be compiled as a separate noir program. | ||
/// | ||
/// A contract function unlike a regular Noir program however can have additional properties. | ||
/// One of these being a function type. | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct PreprocessedContractFunction { | ||
pub name: String, | ||
|
||
pub function_type: ContractFunctionType, | ||
|
||
pub abi: Abi, | ||
|
||
#[serde( | ||
serialize_with = "super::serialize_circuit", | ||
deserialize_with = "super::deserialize_circuit" | ||
)] | ||
pub bytecode: Circuit, | ||
|
||
pub proving_key: Vec<u8>, | ||
pub verification_key: Vec<u8>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//! This module defines the structure of Nargo's different compilation artifacts. | ||
//! | ||
//! These artifacts are intended to remain independent of any applications being built on top of Noir. | ||
//! Should any projects require/desire a different artifact format, it's expected that they will write a transformer | ||
//! to generate them using these artifacts as a starting point. | ||
|
||
use acvm::acir::circuit::Circuit; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
pub mod contract; | ||
pub mod program; | ||
|
||
// TODO: move these down into ACVM. | ||
fn serialize_circuit<S>(circuit: &Circuit, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let mut circuit_bytes: Vec<u8> = Vec::new(); | ||
circuit.write(&mut circuit_bytes).unwrap(); | ||
|
||
circuit_bytes.serialize(s) | ||
} | ||
|
||
fn deserialize_circuit<'de, D>(deserializer: D) -> Result<Circuit, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let circuit_bytes = Vec::<u8>::deserialize(deserializer)?; | ||
let circuit = Circuit::read(&*circuit_bytes).unwrap(); | ||
Ok(circuit) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use acvm::acir::circuit::Circuit; | ||
use noirc_abi::Abi; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// `PreprocessedProgram` represents a Noir program which has been preprocessed by a particular backend proving system. | ||
/// | ||
/// This differs from a generic Noir program artifact in that: | ||
/// - The ACIR bytecode has had an optimization pass applied to tailor it for the backend. | ||
/// - Proving and verification keys have been pregenerated based on this ACIR. | ||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct PreprocessedProgram { | ||
pub backend: String, | ||
pub abi: Abi, | ||
|
||
#[serde( | ||
serialize_with = "super::serialize_circuit", | ||
deserialize_with = "super::deserialize_circuit" | ||
)] | ||
pub bytecode: Circuit, | ||
|
||
pub proving_key: Vec<u8>, | ||
pub verification_key: Vec<u8>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use acvm::OpcodeResolutionError; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum NargoError { | ||
/// Error while compiling Noir into ACIR. | ||
#[error("Failed to compile circuit")] | ||
CompilationError, | ||
|
||
/// ACIR circuit solving error | ||
#[error(transparent)] | ||
SolvingError(#[from] OpcodeResolutionError), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
#![forbid(unsafe_code)] | ||
#![warn(unused_crate_dependencies, unused_extern_crates)] | ||
#![warn(unreachable_pub)] | ||
#![warn(clippy::semicolon_if_nothing_returned)] | ||
|
||
//! Nargo is the package manager for Noir | ||
//! This name was used because it sounds like `cargo` and | ||
//! Noir Package Manager abbreviated is npm, which is already taken. | ||
|
||
pub mod artifacts; | ||
mod errors; | ||
pub mod manifest; | ||
pub mod ops; | ||
|
||
pub use self::errors::NargoError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use acvm::SmartContract; | ||
|
||
use crate::NargoError; | ||
|
||
pub fn codegen_verifier( | ||
backend: &impl SmartContract, | ||
verification_key: &[u8], | ||
) -> Result<String, NargoError> { | ||
Ok(backend.eth_contract_from_vk(verification_key)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use acvm::PartialWitnessGenerator; | ||
use acvm::{acir::circuit::Circuit, pwg::block::Blocks}; | ||
use noirc_abi::WitnessMap; | ||
|
||
use crate::NargoError; | ||
|
||
pub fn execute_circuit( | ||
backend: &impl PartialWitnessGenerator, | ||
circuit: Circuit, | ||
mut initial_witness: WitnessMap, | ||
) -> Result<WitnessMap, NargoError> { | ||
let mut blocks = Blocks::default(); | ||
let (unresolved_opcodes, oracles) = | ||
backend.solve(&mut initial_witness, &mut blocks, circuit.opcodes)?; | ||
if !unresolved_opcodes.is_empty() || !oracles.is_empty() { | ||
todo!("Add oracle support to nargo execute") | ||
} | ||
|
||
Ok(initial_witness) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pub use self::codegen_verifier::codegen_verifier; | ||
pub use self::execute::execute_circuit; | ||
pub use self::preprocess::{preprocess_contract, preprocess_program}; | ||
pub use self::prove::prove_execution; | ||
pub use self::verify::verify_proof; | ||
|
||
mod codegen_verifier; | ||
mod execute; | ||
mod preprocess; | ||
mod prove; | ||
mod verify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use acvm::ProofSystemCompiler; | ||
use iter_extended::vecmap; | ||
use noirc_driver::{CompiledContract, CompiledProgram}; | ||
|
||
use crate::{ | ||
artifacts::{ | ||
contract::{PreprocessedContract, PreprocessedContractFunction}, | ||
program::PreprocessedProgram, | ||
}, | ||
NargoError, | ||
}; | ||
|
||
// TODO: pull this from backend. | ||
const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg"; | ||
|
||
pub fn preprocess_program( | ||
backend: &impl ProofSystemCompiler, | ||
compiled_program: CompiledProgram, | ||
) -> Result<PreprocessedProgram, NargoError> { | ||
// 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); | ||
|
||
Ok(PreprocessedProgram { | ||
backend: String::from(BACKEND_IDENTIFIER), | ||
abi: compiled_program.abi, | ||
bytecode: optimized_bytecode, | ||
proving_key, | ||
verification_key, | ||
}) | ||
} | ||
|
||
pub fn preprocess_contract( | ||
backend: &impl ProofSystemCompiler, | ||
compiled_contract: CompiledContract, | ||
) -> Result<PreprocessedContract, NargoError> { | ||
let preprocessed_contract_functions = vecmap(compiled_contract.functions, |func| { | ||
// 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); | ||
|
||
PreprocessedContractFunction { | ||
name: func.name, | ||
function_type: func.function_type, | ||
abi: func.abi, | ||
|
||
bytecode: optimized_bytecode, | ||
proving_key, | ||
verification_key, | ||
} | ||
}); | ||
|
||
Ok(PreprocessedContract { | ||
name: compiled_contract.name, | ||
backend: String::from(BACKEND_IDENTIFIER), | ||
functions: preprocessed_contract_functions, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use acvm::acir::circuit::Circuit; | ||
use acvm::ProofSystemCompiler; | ||
use noirc_abi::WitnessMap; | ||
|
||
use crate::NargoError; | ||
|
||
pub fn prove_execution( | ||
backend: &impl ProofSystemCompiler, | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use acvm::acir::circuit::Circuit; | ||
use acvm::ProofSystemCompiler; | ||
use noirc_abi::WitnessMap; | ||
|
||
use crate::NargoError; | ||
|
||
pub fn verify_proof( | ||
backend: &impl ProofSystemCompiler, | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.