Skip to content

Commit

Permalink
Merge branch 'main' of github.com:powdr-labs/powdr into rust-constr-enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Schaeff committed Oct 24, 2024
2 parents 79946e0 + 51bbc12 commit 7803534
Show file tree
Hide file tree
Showing 27 changed files with 673 additions and 82 deletions.
5 changes: 5 additions & 0 deletions backend/src/composite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ impl<F: FieldElement, B: BackendFactory<F>> BackendFactory<F> for CompositeBacke
fixed: Arc<Vec<(String, VariablySizedColumn<F>)>>,
output_dir: Option<PathBuf>,
setup: Option<&mut dyn std::io::Read>,
proving_key: Option<&mut dyn std::io::Read>,
verification_key: Option<&mut dyn std::io::Read>,
verification_app_key: Option<&mut dyn std::io::Read>,
backend_options: BackendOptions,
) -> Result<Box<dyn Backend<F>>, Error> {
if proving_key.is_some() {
unimplemented!();
}
if verification_app_key.is_some() {
unimplemented!();
}
Expand Down Expand Up @@ -135,6 +139,7 @@ impl<F: FieldElement, B: BackendFactory<F>> BackendFactory<F> for CompositeBacke
fixed,
output_dir,
setup,
None, // TODO
verification_key,
// TODO: Handle verification_app_key
None,
Expand Down
7 changes: 7 additions & 0 deletions backend/src/estark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,23 @@ fn write_json_file<T: ?Sized + Serialize>(path: &Path, data: &T) -> Result<(), E
}

impl<'a, F: FieldElement> EStarkFilesCommon<F> {
#[allow(clippy::too_many_arguments)]
fn create(
analyzed: &'a Analyzed<F>,
fixed: Arc<Fixed<F>>,
output_dir: Option<PathBuf>,
setup: Option<&mut dyn std::io::Read>,
proving_key: Option<&mut dyn std::io::Read>,
verification_key: Option<&mut dyn std::io::Read>,
verification_app_key: Option<&mut dyn std::io::Read>,
options: BackendOptions,
) -> Result<Self, Error> {
if setup.is_some() {
return Err(Error::NoSetupAvailable);
}
if proving_key.is_some() {
return Err(Error::NoProvingKeyAvailable);
}
if verification_key.is_some() {
return Err(Error::NoVerificationAvailable);
}
Expand Down Expand Up @@ -228,6 +233,7 @@ impl<F: FieldElement> BackendFactory<F> for DumpFactory {
fixed: Arc<Vec<(String, VariablySizedColumn<F>)>>,
output_dir: Option<PathBuf>,
setup: Option<&mut dyn std::io::Read>,
proving_key: Option<&mut dyn std::io::Read>,
verification_key: Option<&mut dyn std::io::Read>,
verification_app_key: Option<&mut dyn std::io::Read>,
options: BackendOptions,
Expand All @@ -240,6 +246,7 @@ impl<F: FieldElement> BackendFactory<F> for DumpFactory {
fixed,
output_dir,
setup,
proving_key,
verification_key,
verification_app_key,
options,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/estark/polygon_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl<F: FieldElement> BackendFactory<F> for Factory {
fixed: Arc<Vec<(String, VariablySizedColumn<F>)>>,
output_dir: Option<PathBuf>,
setup: Option<&mut dyn std::io::Read>,
proving_key: Option<&mut dyn std::io::Read>,
verification_key: Option<&mut dyn std::io::Read>,
verification_app_key: Option<&mut dyn std::io::Read>,
options: BackendOptions,
Expand All @@ -32,6 +33,7 @@ impl<F: FieldElement> BackendFactory<F> for Factory {
fixed,
output_dir,
setup,
proving_key,
verification_key,
verification_app_key,
options,
Expand Down
4 changes: 4 additions & 0 deletions backend/src/estark/starky_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl BackendFactory<GoldilocksField> for RestrictedFactory {
fixed: Arc<Vec<(String, VariablySizedColumn<GoldilocksField>)>>,
_output_dir: Option<PathBuf>,
setup: Option<&mut dyn std::io::Read>,
proving_key: Option<&mut dyn std::io::Read>,
verification_key: Option<&mut dyn std::io::Read>,
verification_app_key: Option<&mut dyn std::io::Read>,
_options: BackendOptions,
Expand All @@ -40,6 +41,9 @@ impl BackendFactory<GoldilocksField> for RestrictedFactory {
return Err(Error::NoSetupAvailable);
}

if proving_key.is_some() {
return Err(Error::NoProvingKeyAvailable);
}
if verification_app_key.is_some() {
return Err(Error::NoAggregationAvailable);
}
Expand Down
2 changes: 2 additions & 0 deletions backend/src/field_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ macro_rules! generalize_factory {
)>>,
output_dir: std::option::Option<std::path::PathBuf>,
setup: std::option::Option<&mut dyn std::io::Read>,
proving_key: std::option::Option<&mut dyn std::io::Read>,
verification_key: std::option::Option<&mut dyn std::io::Read>,
verification_app_key: std::option::Option<&mut dyn std::io::Read>,
backend_options: crate::BackendOptions,
Expand All @@ -40,6 +41,7 @@ macro_rules! generalize_factory {
std::mem::transmute::<Arc<Vec<(String, VariablySizedColumn<F>)>>, Arc<Vec<(String, VariablySizedColumn<$supported_type>)>>>(fixed),
output_dir,
setup,
proving_key,
verification_key,
verification_app_key,
backend_options
Expand Down
9 changes: 9 additions & 0 deletions backend/src/halo2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,18 @@ impl BackendFactory<Bn254Field> for Bn254Factory {
fixed: Arc<Vec<(String, VariablySizedColumn<Bn254Field>)>>,
_output_dir: Option<PathBuf>,
setup: Option<&mut dyn io::Read>,
proving_key: Option<&mut dyn io::Read>,
verification_key: Option<&mut dyn io::Read>,
verification_app_key: Option<&mut dyn io::Read>,
options: BackendOptions,
) -> Result<Box<dyn crate::Backend<Bn254Field>>, Error> {
if pil.degrees().len() > 1 {
return Err(Error::NoVariableDegreeAvailable);
}
if proving_key.is_some() {
return Err(Error::NoProvingKeyAvailable);
}

let proof_type = ProofType::from(options);
let fixed = Arc::new(
get_uniquely_sized_cloned(&fixed).map_err(|_| Error::NoVariableDegreeAvailable)?,
Expand Down Expand Up @@ -193,13 +198,17 @@ impl<F: FieldElement> BackendFactory<F> for Halo2MockFactory {
fixed: Arc<Vec<(String, VariablySizedColumn<F>)>>,
_output_dir: Option<PathBuf>,
setup: Option<&mut dyn io::Read>,
proving_key: Option<&mut dyn io::Read>,
verification_key: Option<&mut dyn io::Read>,
verification_app_key: Option<&mut dyn io::Read>,
_options: BackendOptions,
) -> Result<Box<dyn crate::Backend<F>>, Error> {
if setup.is_some() {
return Err(Error::NoSetupAvailable);
}
if proving_key.is_some() {
return Err(Error::NoProvingKeyAvailable);
}
if verification_key.is_some() {
return Err(Error::NoVerificationAvailable);
}
Expand Down
7 changes: 7 additions & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ pub enum Error {
EmptyWitness,
#[error("the backend has no setup operations")]
NoSetupAvailable,
#[error("the backend does not use a proving key setup")]
NoProvingKeyAvailable,
#[error("the backend does not implement proof verification")]
NoVerificationAvailable,
#[error("the backend does not support Ethereum onchain verification")]
Expand Down Expand Up @@ -139,6 +141,7 @@ pub trait BackendFactory<F: FieldElement> {
fixed: Arc<Vec<(String, VariablySizedColumn<F>)>>,
output_dir: Option<PathBuf>,
setup: Option<&mut dyn io::Read>,
proving_key: Option<&mut dyn io::Read>,
verification_key: Option<&mut dyn io::Read>,
verification_app_key: Option<&mut dyn io::Read>,
backend_options: BackendOptions,
Expand Down Expand Up @@ -187,6 +190,10 @@ pub trait Backend<F: FieldElement>: Send {
Ok(())
}

fn export_proving_key(&self, _output: &mut dyn io::Write) -> Result<(), Error> {
Err(Error::NoProvingKeyAvailable)
}

fn verification_key_bytes(&self) -> Result<Vec<u8>, Error> {
Err(Error::NoVerificationAvailable)
}
Expand Down
26 changes: 20 additions & 6 deletions backend/src/plonky3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use powdr_ast::analyzed::Analyzed;
use powdr_executor::{constant_evaluator::VariablySizedColumn, witgen::WitgenCallback};
use powdr_number::{BabyBearField, GoldilocksField, KoalaBearField, Mersenne31Field};
use powdr_plonky3::{Commitment, FieldElementMap, ProverData};
use serde::{Deserialize, Serialize};
use stark::Plonky3Prover;

use crate::{
Expand All @@ -16,7 +17,7 @@ struct RestrictedFactory;

impl<T: FieldElementMap> BackendFactory<T> for RestrictedFactory
where
ProverData<T>: Send,
ProverData<T>: Send + Serialize + for<'a> Deserialize<'a>,
Commitment<T>: Send,
{
fn create(
Expand All @@ -25,6 +26,7 @@ where
fixed: Arc<Vec<(String, VariablySizedColumn<T>)>>,
_output_dir: Option<PathBuf>,
setup: Option<&mut dyn io::Read>,
proving_key: Option<&mut dyn io::Read>,
verification_key: Option<&mut dyn io::Read>,
verification_app_key: Option<&mut dyn io::Read>,
_: BackendOptions,
Expand All @@ -38,10 +40,14 @@ where

let mut p3 = Box::new(Plonky3Prover::new(pil.clone(), fixed));

if let Some(verification_key) = verification_key {
p3.set_verifying_key(verification_key);
} else {
p3.setup();
match (proving_key, verification_key) {
(Some(pk), Some(vk)) => {
p3.set_proving_key(pk);
p3.set_verifying_key(vk);
}
_ => {
p3.setup();
}
}

Ok(p3)
Expand All @@ -52,7 +58,7 @@ generalize_factory!(Factory <- RestrictedFactory, [BabyBearField, KoalaBearField

impl<T: FieldElementMap> Backend<T> for Plonky3Prover<T>
where
ProverData<T>: Send,
ProverData<T>: Send + Serialize + for<'a> Deserialize<'a>,
Commitment<T>: Send,
{
fn verify(&self, proof: &[u8], instances: &[Vec<T>]) -> Result<(), Error> {
Expand Down Expand Up @@ -82,4 +88,12 @@ where
output.write_all(&vk).unwrap();
Ok(())
}

fn export_proving_key(&self, output: &mut dyn io::Write) -> Result<(), Error> {
let pk = self
.export_proving_key()
.map_err(|e| Error::BackendError(e.to_string()))?;
output.write_all(&pk).unwrap();
Ok(())
}
}
28 changes: 22 additions & 6 deletions backend/src/plonky3/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use p3_commit::Pcs;
use p3_matrix::dense::RowMajorMatrix;
use powdr_backend_utils::machine_fixed_columns;
use powdr_executor::constant_evaluator::VariablySizedColumn;
use serde::{Deserialize, Serialize};

use core::fmt;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -38,21 +39,23 @@ where
verifying_key: Option<StarkVerifyingKey<T::Config>>,
}

pub enum VerificationKeyExportError {
pub enum KeyExportError {
NoProvingKey,
NoVerificationKey,
}

impl fmt::Display for VerificationKeyExportError {
impl fmt::Display for KeyExportError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoProvingKey => write!(f, "No proving key set"),
Self::NoVerificationKey => write!(f, "No verification key set"),
}
}
}

impl<T: FieldElementMap> Plonky3Prover<T>
where
ProverData<T>: Send,
ProverData<T>: Send + Serialize + for<'a> Deserialize<'a>,
Commitment<T>: Send,
{
pub fn new(
Expand All @@ -74,15 +77,28 @@ where
}
}

pub fn set_proving_key(&mut self, rdr: &mut dyn std::io::Read) {
self.proving_key = Some(bincode::deserialize_from(rdr).unwrap());
}

pub fn set_verifying_key(&mut self, rdr: &mut dyn std::io::Read) {
self.verifying_key = Some(bincode::deserialize_from(rdr).unwrap());
}

pub fn export_verifying_key(&self) -> Result<Vec<u8>, VerificationKeyExportError> {
pub fn export_proving_key(&self) -> Result<Vec<u8>, KeyExportError> {
Ok(bincode::serialize(
self.proving_key
.as_ref()
.ok_or(KeyExportError::NoProvingKey)?,
)
.unwrap())
}

pub fn export_verifying_key(&self) -> Result<Vec<u8>, KeyExportError> {
Ok(bincode::serialize(
self.verifying_key
.as_ref()
.ok_or(VerificationKeyExportError::NoVerificationKey)?,
.ok_or(KeyExportError::NoVerificationKey)?,
)
.unwrap())
}
Expand Down Expand Up @@ -306,7 +322,7 @@ mod tests {

fn run_test_publics_aux<F: FieldElementMap>(pil: &str, malicious_publics: &Option<Vec<usize>>)
where
ProverData<F>: Send,
ProverData<F>: Send + serde::Serialize + for<'a> serde::Deserialize<'a>,
Commitment<F>: Send,
{
let mut pipeline = Pipeline::<F>::default().from_pil_string(pil.to_string());
Expand Down
4 changes: 4 additions & 0 deletions backend/src/stwo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ impl<F: FieldElement> BackendFactory<F> for StwoProverFactory {
fixed: Arc<Vec<(String, VariablySizedColumn<F>)>>,
_output_dir: Option<PathBuf>,
setup: Option<&mut dyn io::Read>,
proving_key: Option<&mut dyn io::Read>,
verification_key: Option<&mut dyn io::Read>,
verification_app_key: Option<&mut dyn io::Read>,
options: BackendOptions,
) -> Result<Box<dyn crate::Backend<F>>, Error> {
if proving_key.is_some() {
return Err(Error::BackendError("Proving key unused".to_string()));
}
if pil.degrees().len() > 1 {
return Err(Error::NoVariableDegreeAvailable);
}
Expand Down
10 changes: 7 additions & 3 deletions cli-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ fn execute<F: FieldElement>(
.with_prover_inputs(inputs)
.with_output(output_dir.into(), true);

let generate_witness = |mut pipeline: Pipeline<F>| -> Result<(), Vec<String>> {
let generate_witness = |pipeline: &mut Pipeline<F>| -> Result<(), Vec<String>> {
pipeline.compute_witness().unwrap();
Ok(())
};
Expand All @@ -344,10 +344,14 @@ fn execute<F: FieldElement>(
(true, true) => {
let dry_run =
powdr_riscv::continuations::rust_continuations_dry_run(&mut pipeline, profiling);
powdr_riscv::continuations::rust_continuations(pipeline, generate_witness, dry_run)?;
powdr_riscv::continuations::rust_continuations(
&mut pipeline,
generate_witness,
dry_run,
)?;
}
(true, false) => {
generate_witness(pipeline)?;
generate_witness(&mut pipeline)?;
}
}

Expand Down
4 changes: 4 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ fn verification_key<T: FieldElement>(
let mut pipeline = Pipeline::<T>::default()
.from_file(file.to_path_buf())
.read_constants(dir)
.map_err(|e| vec![e])?
.with_setup_file(params.map(PathBuf::from))
.with_vkey_app_file(vkey_app.map(PathBuf::from))
.with_backend(*backend_type, backend_options);
Expand All @@ -603,6 +604,7 @@ fn export_verifier<T: FieldElement>(
let mut pipeline = Pipeline::<T>::default()
.from_file(file.to_path_buf())
.read_constants(dir)
.map_err(|e| vec![e])?
.with_setup_file(params.map(PathBuf::from))
.with_vkey_file(vkey.map(PathBuf::from))
.with_backend(*backend_type, backend_options);
Expand Down Expand Up @@ -695,6 +697,7 @@ fn read_and_prove<T: FieldElement>(
.from_maybe_pil_object(file.to_path_buf())?
.with_output(dir.to_path_buf(), true)
.read_witness(dir)
.map_err(|e| vec![e])?
.with_setup_file(params.map(PathBuf::from))
.with_vkey_app_file(vkey_app.map(PathBuf::from))
.with_vkey_file(vkey.map(PathBuf::from))
Expand Down Expand Up @@ -724,6 +727,7 @@ fn read_and_verify<T: FieldElement>(
let mut pipeline = Pipeline::<T>::default()
.from_file(file.to_path_buf())
.read_constants(dir)
.map_err(|e| vec![e])?
.with_setup_file(params.map(PathBuf::from))
.with_vkey_file(Some(vkey))
.with_backend(*backend_type, backend_options);
Expand Down
Loading

0 comments on commit 7803534

Please sign in to comment.