-
Notifications
You must be signed in to change notification settings - Fork 81
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
Backend unification #417
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
165dd57
Mostly done.
lvella 36cc2e4
With all features enabled, it at least compiles.
lvella ce2fbbc
Fixed some bugs.
lvella 5d4a5f6
Being explicit about degree type.
lvella 3604afe
More tests passing.
lvella a10732a
Even more tests passing.
lvella 397fd82
Restoring optional Halo2 compilation.
lvella a745483
Merge branch 'main' into backend-unification
lvella 9c58166
Fixing bad merge.
lvella 2a773b3
Fixing lint errors.
lvella 1709b1c
More lint fixes.
lvella 7a9111a
Merge branch 'main' into backend-unification
lvella 3ead294
Split of the backend implementations into trait categories.
lvella 2a86f70
Some renamings.
lvella 3d6f36d
Much better function names.
lvella e3069a9
Removed option from witness.
lvella 594601d
Pleasing the lint.
lvella c6343b9
Returning the proof.
lvella e89924a
Removing filesystem stuff from backend.
lvella 366f5d0
Renaming function.
lvella ee0aaf9
Merge remote-tracking branch 'origin/main' into backend-unification
lvella b5870d4
Another function rename.
lvella 4a55617
Merge branch 'main' into backend-unification
leonardoalt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,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) | ||
} | ||
} |
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,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); | ||
#[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
|
||
} |
File renamed without changes.
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,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())) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 useshalo2_structs
?There was a problem hiding this comment.
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 thehalo2
crate, and the mock is defined in the modulehalo2_structs
of thebackend
crate that makes use of thehalo2
crate.