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

Backend unification #417

Merged
merged 23 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ pil_analyzer = { path = "../pil_analyzer" }
number = { path = "../number" }
strum = { version = "0.24.1", features = ["derive"] }
ast = { version = "0.1.0", path = "../ast" }
log = "0.4.17"
json = "^0.12"
thiserror = "1.0.43"

[dev-dependencies]
mktemp = "0.5.0"
test-log = "0.2.12"
env_logger = "0.10.0"
62 changes: 62 additions & 0 deletions backend/src/halo2_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::io::{self};

use crate::{BackendImpl, BackendImplWithSetup, Proof};
use ast::analyzed::Analyzed;
use halo2::Halo2Prover;
use number::{DegreeType, FieldElement};

impl<T: FieldElement> BackendImpl<T> for Halo2Prover {
fn new(degree: DegreeType) -> Self {
Halo2Prover::assert_field_is_compatible::<T>();
Halo2Prover::new(degree)
}

fn prove(
&self,
pil: &Analyzed<T>,
fixed: &[(&str, Vec<T>)],
witness: &[(&str, Vec<T>)],
prev_proof: Option<Proof>,
) -> (Option<Proof>, Option<String>) {
let proof = match prev_proof {
Some(proof) => self.prove_aggr(pil, fixed, witness, proof),
None => self.prove_ast(pil, fixed, witness),
};

(Some(proof), None)
}
}

impl<T: FieldElement> BackendImplWithSetup<T> for halo2::Halo2Prover {
fn new_from_setup(mut input: &mut dyn io::Read) -> Result<Self, io::Error> {
Halo2Prover::assert_field_is_compatible::<T>();
Halo2Prover::new_from_setup(&mut input)
}

fn write_setup(&self, mut output: &mut dyn io::Write) -> Result<(), io::Error> {
self.write_setup(&mut output)
}
}

pub struct Halo2Mock;
impl<T: FieldElement> BackendImpl<T> for Halo2Mock {
fn new(_degree: DegreeType) -> Self {
Self
}

fn prove(
&self,
pil: &Analyzed<T>,
fixed: &[(&str, Vec<T>)],
witness: &[(&str, Vec<T>)],
prev_proof: Option<Proof>,
) -> (Option<Proof>, Option<String>) {
if prev_proof.is_some() {
unimplemented!("Halo2Mock backend does not support aggregation");
}

halo2::mock_prove(pil, fixed, witness);

(None, None)
}
}
228 changes: 155 additions & 73 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,104 +1,186 @@
#[cfg(feature = "halo2")]
mod halo2_impl;
mod pilcom_cli;

use ast::analyzed::Analyzed;
use number::FieldElement;
use std::io;
use number::{DegreeType, FieldElement};
use std::{io, marker::PhantomData};
use strum::{Display, EnumString, EnumVariantNames};

#[derive(Clone, EnumString, EnumVariantNames, Display)]
pub enum Backend {
pub enum BackendType {
#[cfg(feature = "halo2")]
#[strum(serialize = "halo2")]
Halo2,
#[cfg(feature = "halo2")]
#[strum(serialize = "halo2-aggr")]
Halo2Aggr,
#[cfg(feature = "halo2")]
#[strum(serialize = "halo2-mock")]
Halo2Mock,
// At the moment, this enum is empty without halo2, but it is always built
// as part of the infrastructure to eventually support other backends.
#[strum(serialize = "pilcom-cli")]
PilcomCli,
}

/// Create a proof for a given PIL, fixed column values and witness column values
/// using the chosen backend.
impl BackendType {
pub fn factory<T: FieldElement>(&self) -> &'static dyn BackendFactory<T> {
#[cfg(feature = "halo2")]
const HALO2_FACTORY: WithSetupFactory<halo2::Halo2Prover> = WithSetupFactory(PhantomData);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this one use halo2::... and Mock uses halo2_structs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because Halo2Prover is defined in the halo2 crate, and the mock is defined in the module halo2_structs of the backend crate that makes use of the halo2 crate.

#[cfg(feature = "halo2")]
const HALO2_MOCK_FACTORY: WithoutSetupFactory<halo2_impl::Halo2Mock> =
WithoutSetupFactory(PhantomData);
const PILCOM_CLI_FACTORY: WithoutSetupFactory<pilcom_cli::PilcomCli> =
WithoutSetupFactory(PhantomData);

pub type Proof = Vec<u8>;
pub type Params = Vec<u8>;
match self {
#[cfg(feature = "halo2")]
BackendType::Halo2 => &HALO2_FACTORY,
#[cfg(feature = "halo2")]
BackendType::Halo2Mock => &HALO2_MOCK_FACTORY,
BackendType::PilcomCli => &PILCOM_CLI_FACTORY,
}
}
}

pub trait ProverWithParams {
fn prove<T: FieldElement, R: io::Read>(
pil: &Analyzed<T>,
fixed: Vec<(&str, Vec<T>)>,
witness: Vec<(&str, Vec<T>)>,
params: R,
) -> Option<Proof>;
/// Factory for backends without setup.
struct WithoutSetupFactory<B>(PhantomData<B>);

fn generate_params<T: FieldElement>(size: usize) -> Params;
}
/// Factory implementation for backends without setup.
impl<F: FieldElement, B: BackendImpl<F> + 'static> BackendFactory<F> for WithoutSetupFactory<B> {
fn create(&self, degree: DegreeType) -> Box<dyn Backend<F>> {
Box::new(ConcreteBackendWithoutSetup(B::new(degree)))
}

pub trait ProverWithoutParams {
fn prove<T: FieldElement>(
pil: &Analyzed<T>,
fixed: Vec<(&str, Vec<T>)>,
witness: Vec<(&str, Vec<T>)>,
) -> Option<Proof>;
fn create_from_setup(&self, _input: &mut dyn io::Read) -> Result<Box<dyn Backend<F>>, Error> {
Err(Error::NoSetupAvailable)
}
}

pub trait ProverAggregationWithParams {
fn prove<T: FieldElement, R1: io::Read, R2: io::Read>(
pil: &Analyzed<T>,
fixed: Vec<(&str, Vec<T>)>,
witness: Vec<(&str, Vec<T>)>,
proof: R1,
params: R2,
) -> Proof;
}
/// Concrete dynamic dispatch Backend object, for backends without setup.
struct ConcreteBackendWithoutSetup<B>(B);

#[cfg(feature = "halo2")]
pub struct Halo2Backend;
/// Concrete implementation for backends with setup.
impl<F: FieldElement, B: BackendImpl<F>> Backend<F> for ConcreteBackendWithoutSetup<B> {
fn prove(
&self,
pil: &Analyzed<F>,
fixed: &[(&str, Vec<F>)],
witness: &[(&str, Vec<F>)],
prev_proof: Option<Proof>,
leonardoalt marked this conversation as resolved.
Show resolved Hide resolved
) -> (Option<Proof>, Option<String>) {
self.0.prove(pil, fixed, witness, prev_proof)
}

#[cfg(feature = "halo2")]
pub struct Halo2MockBackend;
fn write_setup(&self, _output: &mut dyn io::Write) -> Result<(), Error> {
Err(Error::NoSetupAvailable)
}
}

#[cfg(feature = "halo2")]
pub struct Halo2AggregationBackend;
/// Factory for backends with setup.
struct WithSetupFactory<B>(PhantomData<B>);

#[cfg(feature = "halo2")]
impl ProverWithParams for Halo2Backend {
fn prove<T: FieldElement, R: io::Read>(
pil: &Analyzed<T>,
fixed: Vec<(&str, Vec<T>)>,
witness: Vec<(&str, Vec<T>)>,
params: R,
) -> Option<Proof> {
Some(halo2::prove_ast_read_params(pil, fixed, witness, params))
/// Factory implementation for backends with setup.
impl<F: FieldElement, B: BackendImplWithSetup<F> + 'static> BackendFactory<F>
for WithSetupFactory<B>
{
fn create(&self, degree: DegreeType) -> Box<dyn Backend<F>> {
Box::new(ConcreteBackendWithSetup(B::new(degree)))
}

fn generate_params<T: FieldElement>(size: usize) -> Params {
halo2::generate_params::<T>(size)
fn create_from_setup(&self, input: &mut dyn io::Read) -> Result<Box<dyn Backend<F>>, Error> {
Ok(Box::new(ConcreteBackendWithSetup(B::new_from_setup(
input,
)?)))
}
}

#[cfg(feature = "halo2")]
impl ProverWithoutParams for Halo2MockBackend {
fn prove<T: FieldElement>(
pil: &Analyzed<T>,
fixed: Vec<(&str, Vec<T>)>,
witness: Vec<(&str, Vec<T>)>,
) -> Option<Proof> {
halo2::mock_prove(pil, fixed, witness);
None
/// Concrete dynamic dispatch Backend object, for backends with setup.
struct ConcreteBackendWithSetup<B>(B);

/// Concrete implementation for backends with setup.
impl<F: FieldElement, B: BackendImplWithSetup<F>> Backend<F> for ConcreteBackendWithSetup<B> {
fn prove(
&self,
pil: &Analyzed<F>,
fixed: &[(&str, Vec<F>)],
witness: &[(&str, Vec<F>)],
prev_proof: Option<Proof>,
leonardoalt marked this conversation as resolved.
Show resolved Hide resolved
) -> (Option<Proof>, Option<String>) {
self.0.prove(pil, fixed, witness, prev_proof)
}
}

#[cfg(feature = "halo2")]
impl ProverAggregationWithParams for Halo2AggregationBackend {
fn prove<T: FieldElement, R1: io::Read, R2: io::Read>(
pil: &Analyzed<T>,
fixed: Vec<(&str, Vec<T>)>,
witness: Vec<(&str, Vec<T>)>,
proof: R1,
params: R2,
) -> Proof {
halo2::prove_aggr_read_proof_params(pil, fixed, witness, proof, params)
fn write_setup(&self, output: &mut dyn io::Write) -> Result<(), Error> {
Ok(self.0.write_setup(output)?)
}
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("input/output error")]
IO(#[from] std::io::Error),
#[error("the backend has not setup operations")]
NoSetupAvailable,
}

pub type Proof = Vec<u8>;

/*
Bellow are the public interface traits. They are implemented in this
module, wrapping the traits implemented by each backend.
*/

/// Dynamic interface for a backend.
pub trait Backend<F: FieldElement> {
/// Perform the proving.
///
/// If prev_proof is provided, proof aggregation is performed.
///
/// Returns the generated proof, and the string serialization of the
/// constraints.
fn prove(
&self,
pil: &Analyzed<F>,
fixed: &[(&str, Vec<F>)],
witness: &[(&str, Vec<F>)],
prev_proof: Option<Proof>,
) -> (Option<Proof>, Option<String>);

/// Write the prover setup to a file, so that it can be loaded later.
fn write_setup(&self, output: &mut dyn io::Write) -> Result<(), Error>;
}

/// Dynamic interface for a backend factory.
pub trait BackendFactory<F: FieldElement> {
/// Maybe perform the setup, and create a new backend object.
fn create(&self, degree: DegreeType) -> Box<dyn Backend<F>>;

/// Create a backend object from a prover setup loaded from a file.
fn create_from_setup(&self, input: &mut dyn io::Read) -> Result<Box<dyn Backend<F>>, Error>;
}

/*
Below are the traits implemented by the backends.
*/

/// Trait implemented by all backends.
trait BackendImpl<F: FieldElement> {
leonardoalt marked this conversation as resolved.
Show resolved Hide resolved
fn new(degree: DegreeType) -> Self;

fn prove(
&self,
pil: &Analyzed<F>,
fixed: &[(&str, Vec<F>)],
witness: &[(&str, Vec<F>)],
prev_proof: Option<Proof>,
) -> (Option<Proof>, Option<String>);
}

/// Trait implemented by backends that have a setup phase that must be saved to
/// a file.
trait BackendImplWithSetup<F: FieldElement>
where
Self: Sized + BackendImpl<F>,
{
/// Create a backend object from a setup loaded from a file.
fn new_from_setup(input: &mut dyn io::Read) -> Result<Self, io::Error>;

/// Write the setup to a file.
fn write_setup(&self, output: &mut dyn io::Write) -> Result<(), io::Error>;
leonardoalt marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ impl<'a, T: FieldElement> Exporter<'a, T> {

#[cfg(test)]
mod test {
use pil_analyzer::analyze;
use std::fs;
use std::process::Command;
use test_log::test;
Expand All @@ -357,7 +358,7 @@ mod test {

let file = std::path::PathBuf::from("../test_data/polygon-hermez/").join(file);

let analyzed = crate::analyze::<GoldilocksField>(&file);
let analyzed = analyze::<GoldilocksField>(&file);
let json_out = export(&analyzed);

let pilcom = std::env::var("PILCOM").expect(
Expand Down
27 changes: 27 additions & 0 deletions backend/src/pilcom_cli/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
mod json_exporter;

use crate::{BackendImpl, Proof};
use ast::analyzed::Analyzed;
use number::{DegreeType, FieldElement};

pub struct PilcomCli;

impl<T: FieldElement> BackendImpl<T> for PilcomCli {
fn new(_degree: DegreeType) -> Self {
Self
}
leonardoalt marked this conversation as resolved.
Show resolved Hide resolved

fn prove(
&self,
pil: &Analyzed<T>,
_fixed: &[(&str, Vec<T>)],
_witness: &[(&str, Vec<T>)],
prev_proof: Option<Proof>,
) -> (Option<Proof>, Option<String>) {
if prev_proof.is_some() {
unimplemented!("Aggregration is not implemented for Pilcom CLI backend");
leonardoalt marked this conversation as resolved.
Show resolved Hide resolved
}

(None, Some(json_exporter::export(pil).to_string()))
}
}
Loading