-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfbd4ce
commit 0e609d8
Showing
11 changed files
with
375 additions
and
46 deletions.
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# r0prover-python | ||
Python bindings for RISC0 prover | ||
## Python/Ray wrapper for RISC Zero prover | ||
|
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 |
---|---|---|
|
@@ -4,6 +4,11 @@ build-backend = "maturin" | |
|
||
[project] | ||
name = "l2_r0prover" | ||
version = "0.0.1" | ||
authors = [ | ||
{ name = "Weikeng Chen", email = "[email protected]" } | ||
] | ||
description = "" | ||
requires-python = ">=3.7" | ||
classifiers = [ | ||
"Programming Language :: Rust", | ||
|
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,24 +1,46 @@ | ||
use crate::serialization::Pickleable; | ||
use anyhow::Result; | ||
use pyo3::prelude::*; | ||
use risc0_binfmt::{MemoryImage, Program}; | ||
use risc0_zkvm_platform::memory::GUEST_MAX_MEM; | ||
use risc0_zkvm_platform::PAGE_SIZE; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[pyclass] | ||
#[pyclass(module = "l2_r0prover")] | ||
#[derive(Serialize, Deserialize, Clone)] | ||
pub struct Image { | ||
memory_image: MemoryImage, | ||
memory_image: Option<MemoryImage>, | ||
} | ||
|
||
impl Image { | ||
pub fn from_elf(elf: &[u8]) -> Result<Self> { | ||
let program = Program::load_elf(elf, GUEST_MAX_MEM as u32)?; | ||
let image = MemoryImage::new(&program, PAGE_SIZE as u32)?; | ||
Ok(Self { | ||
memory_image: image, | ||
memory_image: Some(image), | ||
}) | ||
} | ||
|
||
pub fn get_image(&self) -> MemoryImage { | ||
self.memory_image.clone() | ||
self.memory_image.as_ref().unwrap().clone() | ||
} | ||
} | ||
|
||
impl Pickleable for Image {} | ||
|
||
#[pymethods] | ||
impl Image { | ||
#[new] | ||
fn new_init() -> Self { | ||
Self { memory_image: None } | ||
} | ||
|
||
fn __getstate__(&self, py: Python<'_>) -> PyResult<PyObject> { | ||
self.to_bytes(py) | ||
} | ||
|
||
fn __setstate__(&mut self, py: Python<'_>, state: PyObject) -> PyResult<()> { | ||
*self = Self::from_bytes(state, py)?; | ||
Ok(()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,29 +1,83 @@ | ||
use crate::serialization::Pickleable; | ||
use anyhow::Result; | ||
use pyo3::prelude::*; | ||
use risc0_zkvm::VerifierContext; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[pyclass] | ||
#[pyclass(module = "l2_r0prover")] | ||
#[derive(Serialize, Deserialize, Clone)] | ||
pub struct Segment { | ||
segment: risc0_zkvm::Segment, | ||
segment: Option<risc0_zkvm::Segment>, | ||
} | ||
|
||
impl Segment { | ||
pub fn new(segment: risc0_zkvm::Segment) -> Self { | ||
Self { segment } | ||
Self { | ||
segment: Some(segment), | ||
} | ||
} | ||
|
||
pub fn prove(&self, verifier_context: &VerifierContext) -> Result<SegmentReceipt> { | ||
Ok(SegmentReceipt::new(self.segment.prove(verifier_context)?)) | ||
Ok(SegmentReceipt::new( | ||
self.segment.as_ref().unwrap().prove(verifier_context)?, | ||
)) | ||
} | ||
} | ||
|
||
#[pyclass] | ||
impl Pickleable for Segment {} | ||
|
||
#[pymethods] | ||
impl Segment { | ||
#[new] | ||
fn new_init() -> Self { | ||
Self { segment: None } | ||
} | ||
|
||
fn __getstate__(&self, py: Python<'_>) -> PyResult<PyObject> { | ||
self.to_bytes(py) | ||
} | ||
|
||
fn __setstate__(&mut self, py: Python<'_>, state: PyObject) -> PyResult<()> { | ||
*self = Self::from_bytes(state, py)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[pyclass(module = "l2_r0prover")] | ||
#[derive(Serialize, Deserialize, Clone)] | ||
pub struct SegmentReceipt { | ||
segment_receipt: risc0_zkvm::SegmentReceipt, | ||
segment_receipt: Option<risc0_zkvm::SegmentReceipt>, | ||
} | ||
|
||
impl SegmentReceipt { | ||
pub fn new(segment_receipt: risc0_zkvm::SegmentReceipt) -> Self { | ||
Self { segment_receipt } | ||
Self { | ||
segment_receipt: Some(segment_receipt), | ||
} | ||
} | ||
|
||
pub fn get_segment_receipt_ref(&self) -> &risc0_zkvm::SegmentReceipt { | ||
&self.segment_receipt.as_ref().unwrap() | ||
} | ||
} | ||
|
||
impl Pickleable for SegmentReceipt {} | ||
|
||
#[pymethods] | ||
impl SegmentReceipt { | ||
#[new] | ||
fn new_init() -> Self { | ||
Self { | ||
segment_receipt: None, | ||
} | ||
} | ||
|
||
fn __getstate__(&self, py: Python<'_>) -> PyResult<PyObject> { | ||
self.to_bytes(py) | ||
} | ||
|
||
fn __setstate__(&mut self, py: Python<'_>, state: PyObject) -> PyResult<()> { | ||
*self = Self::from_bytes(state, py)?; | ||
Ok(()) | ||
} | ||
} |
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,30 @@ | ||
use anyhow::anyhow; | ||
use pyo3::types::PyBytes; | ||
use pyo3::{PyObject, PyResult, Python, ToPyObject}; | ||
use serde::de::DeserializeOwned; | ||
use serde::Serialize; | ||
|
||
// The code here that implements pickle for PyO3 classes comes from | ||
// https://github.com/rth/vtext | ||
// which is under Apache 2.0. | ||
// | ||
// And it is related to this issue in PyO3: | ||
// https://github.com/PyO3/pyo3/issues/100 | ||
|
||
pub trait Pickleable: Serialize + DeserializeOwned + Clone { | ||
fn to_bytes(&self, py: Python<'_>) -> PyResult<PyObject> { | ||
let bytes = bincode::serialize(&self).map_err(|e| anyhow!("failed to serialize: {}", e))?; | ||
Ok(PyBytes::new(py, &bytes).to_object(py)) | ||
} | ||
|
||
fn from_bytes(state: PyObject, py: Python<'_>) -> PyResult<Self> { | ||
match state.extract::<&PyBytes>(py) { | ||
Ok(s) => { | ||
let res: Self = bincode::deserialize(s.as_bytes()) | ||
.map_err(|e| anyhow!("failed to deserialize: {}", e))?; | ||
Ok(res) | ||
} | ||
Err(e) => Err(anyhow!("failed to parse the pickled data as bytes: {}", e).into()), | ||
} | ||
} | ||
} |
Oops, something went wrong.