Skip to content

Commit

Permalink
Stwobackend prover skeleton (#1898)
Browse files Browse the repository at this point in the history
### **Progress on Integrating `Stwo`**

This PR makes incremental progress towards integrating the `stwo`
prover, refer to issue #1833 . It includes the following changes:

- **Add `stwo` feature to `Cargo.toml`:**  
The `Cargo.toml` files for the `cli`, `pipeline`, and `backend`
components have been updated to include the `stwo` feature.

- **Implement `StwoProverFactory`:**  
A skeleton structure for `StwoProverFactory` has been introduced. This
structure implements the `BackendFactory` trait.

- **Implement `StwoProver`:**  
Similarly, a skeleton implementation of `StwoProver` has been created,
implementing the `Backend` trait. .



### **Commands to run:**

```bash


# Run the project with the `stwo` feature
cargo run --features stwo pil test_data/pil/fibonacci.pil -o output -f --field m31 --prove-with stwo

---------

Co-authored-by: Thibaut Schaeffer <[email protected]>
Co-authored-by: Georg Wiese <[email protected]>
  • Loading branch information
3 people authored Oct 18, 2024
1 parent 612a0f9 commit 57d3c83
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
4 changes: 4 additions & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ default = []
halo2 = ["dep:halo2_proofs", "dep:halo2_curves", "dep:snark-verifier", "dep:halo2_solidity_verifier"]
estark-polygon = ["dep:pil-stark-prover"]
plonky3 = ["dep:powdr-plonky3"]
stwo = ["dep:stwo-prover"]

[dependencies]
powdr-ast.workspace = true
Expand All @@ -36,6 +37,9 @@ halo2_curves = { version = "0.6.1", package = "halo2curves", optional = true}
snark-verifier = { git = "https://github.com/powdr-labs/snark-verifier.git", rev = "55012261fd4b0b8d21b581a9782d05258afe4104", optional = true }
halo2_solidity_verifier = { git = "https://github.com/powdr-labs/halo2-solidity-verifier.git", rev = "ecae7fd2f62178c18b5fe18011630aa71da3371f", features = ["evm"], optional = true }

# TODO: Change this to main branch when the `andrew/dev/update-toolchain` branch is merged,the main branch is using "nightly-2024-01-04", not compatiable with plonky3
stwo-prover = { git= "https://github.com/starkware-libs/stwo.git",optional=true, rev="52d050c18b5dbc74af40214b3b441a6f60a20d41" }

strum = { version = "0.24.1", features = ["derive"] }
log = "0.4.17"
serde = "1.0"
Expand Down
6 changes: 6 additions & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod plonky3;

mod composite;
mod field_filter;
mod stwo;

use powdr_ast::analyzed::Analyzed;
use powdr_executor::{constant_evaluator::VariablySizedColumn, witgen::WitgenCallback};
Expand Down Expand Up @@ -46,6 +47,9 @@ pub enum BackendType {
#[cfg(feature = "plonky3")]
#[strum(serialize = "plonky3")]
Plonky3,
#[cfg(feature = "stwo")]
#[strum(serialize = "stwo")]
Stwo,
}

pub type BackendOptions = String;
Expand Down Expand Up @@ -84,6 +88,8 @@ impl BackendType {
}
#[cfg(feature = "plonky3")]
BackendType::Plonky3 => Box::new(plonky3::Factory),
#[cfg(feature = "stwo")]
BackendType::Stwo => Box::new(stwo::StwoProverFactory),
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions backend/src/stwo/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::io;
use std::path::PathBuf;
use std::sync::Arc;

use crate::{Backend, BackendFactory, BackendOptions, Error, Proof};
use powdr_ast::analyzed::Analyzed;
use powdr_executor::constant_evaluator::{get_uniquely_sized_cloned, VariablySizedColumn};
use powdr_executor::witgen::WitgenCallback;
use powdr_number::FieldElement;
use prover::StwoProver;

mod prover;

#[allow(dead_code)]
pub(crate) struct StwoProverFactory;

impl<F: FieldElement> BackendFactory<F> for StwoProverFactory {
#[allow(unreachable_code)]
#[allow(unused_variables)]
fn create(
&self,
pil: Arc<Analyzed<F>>,
fixed: Arc<Vec<(String, VariablySizedColumn<F>)>>,
_output_dir: Option<PathBuf>,
setup: 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 pil.degrees().len() > 1 {
return Err(Error::NoVariableDegreeAvailable);
}
let fixed = Arc::new(
get_uniquely_sized_cloned(&fixed).map_err(|_| Error::NoVariableDegreeAvailable)?,
);
let stwo = Box::new(StwoProver::new(pil, fixed, setup)?);
Ok(stwo)
}
}

impl<T: FieldElement> Backend<T> for StwoProver<T> {
#[allow(unused_variables)]
fn verify(&self, proof: &[u8], instances: &[Vec<T>]) -> Result<(), Error> {
assert!(instances.len() == 1);
unimplemented!()
}
#[allow(unreachable_code)]
#[allow(unused_variables)]
fn prove(
&self,
witness: &[(String, Vec<T>)],
prev_proof: Option<Proof>,
witgen_callback: WitgenCallback<T>,
) -> Result<Proof, Error> {
if prev_proof.is_some() {
return Err(Error::NoAggregationAvailable);
}
unimplemented!()
}
#[allow(unused_variables)]
fn export_verification_key(&self, output: &mut dyn io::Write) -> Result<(), Error> {
unimplemented!()
}
}
32 changes: 32 additions & 0 deletions backend/src/stwo/prover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use powdr_ast::analyzed::Analyzed;
use std::io;
use std::sync::Arc;

use powdr_number::FieldElement;

#[allow(unused_variables)]
pub struct StwoProver<F> {
_analyzed: Arc<Analyzed<F>>,
_fixed: Arc<Vec<(String, Vec<F>)>>,
/// Proving key placeholder
_proving_key: Option<()>,
/// Verifying key placeholder
_verifying_key: Option<()>,
}

impl<F: FieldElement> StwoProver<F> {
#[allow(dead_code)]
#[allow(unused_variables)]
pub fn new(
_analyzed: Arc<Analyzed<F>>,
_fixed: Arc<Vec<(String, Vec<F>)>>,
setup: Option<&mut dyn io::Read>,
) -> Result<Self, io::Error> {
Ok(Self {
_analyzed,
_fixed,
_proving_key: None,
_verifying_key: None,
})
}
}
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ estark-polygon = [
"powdr-backend/estark-polygon",
"powdr-pipeline/estark-polygon",
]
stwo=["powdr-backend/stwo","powdr-pipeline/stwo"]

[dependencies]
powdr-backend.workspace = true
Expand Down
1 change: 1 addition & 0 deletions pipeline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ default = [] # halo2 is disabled by default
halo2 = ["powdr-backend/halo2"]
plonky3 = ["powdr-backend/plonky3"]
estark-polygon = ["powdr-backend/estark-polygon"]
stwo = ["powdr-backend/stwo"]

[dependencies]
powdr-airgen.workspace = true
Expand Down

0 comments on commit 57d3c83

Please sign in to comment.