Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
feat(acvm)!: Add CommonReferenceString backend trait
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed May 11, 2023
1 parent a83333b commit 7da75f7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions acvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ k256 = { version = "0.7.2", features = [
] }
indexmap = "1.7.0"
thiserror = "1.0.21"
async-trait = "0.1"

[features]
default = ["bn254"]
Expand Down
52 changes: 48 additions & 4 deletions acvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use core::fmt::Debug;
use std::collections::BTreeMap;
use thiserror::Error;

// We re-export async-trait so consumers can attach it to their impl
pub use async_trait::async_trait;

// re-export acir
pub use acir;
pub use acir::FieldElement;
Expand Down Expand Up @@ -54,10 +57,41 @@ pub enum OpcodeResolutionError {
}

pub trait Backend:
SmartContract + ProofSystemCompiler + PartialWitnessGenerator + Default + Debug
SmartContract
+ ProofSystemCompiler
+ PartialWitnessGenerator
+ CommonReferenceString
+ Default
+ Debug
{
}

#[async_trait]
pub trait CommonReferenceString {
/// The Error type returned by failed function calls in the CommonReferenceString trait.
type Error: std::error::Error; // fully-qualified named because thiserror is `use`d at the top of the crate

/// Provides the common reference string that is needed by other traits
async fn generate_common_reference_string(
&self,
circuit: &Circuit,
) -> Result<Vec<u8>, Self::Error>;

/// Updates a cached common reference string within the context of a circuit
///
/// This function will be called if the common reference string has been cached previously
/// and the backend can update it if necessary. This may happen if the common reference string
/// contains fewer than the number of points needed by the circuit, or any other checks the backend
/// must do.
///
/// If the common reference string doesn't need any updates, implementors can return the value passed.
async fn update_common_reference_string(
&self,
common_reference_string: Vec<u8>,
circuit: &Circuit,
) -> Result<Vec<u8>, Self::Error>;
}

/// This component will generate the backend specific output for
/// each OPCODE.
/// Returns an Error if the backend does not support that OPCODE
Expand Down Expand Up @@ -157,8 +191,12 @@ pub trait SmartContract {

// TODO: Allow a backend to support multiple smart contract platforms

/// Returns an Ethereum smart contract to verify proofs against a given verification key.
fn eth_contract_from_vk(&self, verification_key: &[u8]) -> Result<String, Self::Error>;
/// Returns an Ethereum smart contract to verify proofs against a given common reference string and verification key.
fn eth_contract_from_vk(
&self,
common_reference_string: &[u8],
verification_key: &[u8],
) -> Result<String, Self::Error>;
}

pub trait ProofSystemCompiler {
Expand All @@ -179,13 +217,18 @@ pub trait ProofSystemCompiler {

/// Generates a proving and verification key given the circuit description
/// These keys can then be used to construct a proof and for its verification
fn preprocess(&self, circuit: &Circuit) -> Result<(Vec<u8>, Vec<u8>), Self::Error>;
fn preprocess(
&self,
common_reference_string: &[u8],
circuit: &Circuit,
) -> Result<(Vec<u8>, Vec<u8>), Self::Error>;

/// Creates a Proof given the circuit description, the initial witness values, and the proving key
/// It is important to note that the intermediate witnesses for black box functions will not generated
/// This is the responsibility of the proof system.
fn prove_with_pk(
&self,
common_reference_string: &[u8],
circuit: &Circuit,
witness_values: BTreeMap<Witness, FieldElement>,
proving_key: &[u8],
Expand All @@ -194,6 +237,7 @@ pub trait ProofSystemCompiler {
/// Verifies a Proof, given the circuit description, the circuit's public inputs, and the verification key
fn verify_with_vk(
&self,
common_reference_string: &[u8],
proof: &[u8],
public_inputs: BTreeMap<Witness, FieldElement>,
circuit: &Circuit,
Expand Down

0 comments on commit 7da75f7

Please sign in to comment.