Skip to content

Commit

Permalink
feat(prover): enable GuestInput serialization in native path (#281)
Browse files Browse the repository at this point in the history
* change native param & ProverError

* change scirpt & compiled guest

* fix prove-block.sh typo

* fmt

* fix test_proof_params

* "write_guest_input_path": null

* prover

* fix

* cargo update

* api changes from AffinePoint::<Bn254, 16>::from

* fix

* remove default = ["enable"] & default = ["sgx"]

* sgx-prover = { path = "../prover", features = ["enable"] }

---------

Co-authored-by: d1onys1us <[email protected]>
  • Loading branch information
CeciliaZ030 and dionysuzx authored Jun 9, 2024
1 parent 37f6c49 commit 0ba89ae
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 35 deletions.
8 changes: 8 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ mod tests {

fn test_proof_params() -> HashMap<String, Value> {
let mut prover_args = HashMap::new();
prover_args.insert(
"native".to_string(),
json! {
{
"write_guest_input_path": null
}
},
);
prover_args.insert(
"risc0".to_string(),
json! {
Expand Down
31 changes: 28 additions & 3 deletions core/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
use std::path::Path;

use raiko_lib::{
consts::VerifierType,
input::{GuestInput, GuestOutput},
protocol_instance::ProtocolInstance,
prover::{to_proof, Proof, Prover, ProverError, ProverResult},
prover::{to_proof, Proof, Prover, ProverConfig, ProverError, ProverResult},
};
use serde::{Deserialize, Serialize};
use serde::{de::Error, Deserialize, Serialize};
use serde_with::serde_as;
use tracing::trace;

pub struct NativeProver;

#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NativeParam {
pub write_guest_input_path: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NativeResponse {
pub output: GuestOutput,
Expand All @@ -18,8 +27,24 @@ impl Prover for NativeProver {
async fn run(
input: GuestInput,
output: &GuestOutput,
_request: &serde_json::Value,
config: &ProverConfig,
) -> ProverResult<Proof> {
let param = config
.get("native")
.map(|v| NativeParam::deserialize(v))
.ok_or(ProverError::Param(serde_json::Error::custom(
"native param not provided",
)))??;

if let Some(path) = param.write_guest_input_path {
let path = Path::new(&path);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let json = serde_json::to_string(&input)?;
std::fs::write(path, json)?;
}

trace!("Running the native prover for input {input:?}");

let pi = ProtocolInstance::new(&input, &output.header, VerifierType::None)
Expand Down
5 changes: 4 additions & 1 deletion host/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"snark": true,
"profile": false,
"execution_po2": 20
}
},
"native" : {
"write_guest_input_path": null
}
}
15 changes: 5 additions & 10 deletions lib/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
use std::fmt;

use serde::Serialize;
use thiserror::Error as ThisError;

use crate::input::{GuestInput, GuestOutput};

#[derive(ThisError, Debug)]
pub enum ProverError {
#[error("ProverError::GuestError `{0}`")]
GuestError(String),
}

impl fmt::Display for ProverError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ProverError::GuestError(e) => e.fmt(f),
}
}
#[error("ProverError::FileIo `{0}`")]
FileIo(#[from] std::io::Error),
#[error("ProverError::Param `{0}`")]
Param(#[from] serde_json::Error),
}

impl From<String> for ProverError {
Expand Down
2 changes: 1 addition & 1 deletion provers/risc0/driver/src/methods/risc0_guest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub const RISC0_GUEST_ELF: &[u8] =
include_bytes!("../../../guest/target/riscv32im-risc0-zkvm-elf/release/risc0-guest");
pub const RISC0_GUEST_ID: [u32; 8] = [
405814261, 645472475, 3368860906, 1069727513, 2312368391, 2313520942, 2156489466, 779875178,
1444642754, 3434511061, 2910616417, 2829025913, 3284452016, 1678600137, 1001540409, 1336920303,
];
pub const RISC0_GUEST_PATH: &str =
r#"/home/ubuntu/raiko/provers/risc0/guest/target/riscv32im-risc0-zkvm-elf/release/risc0-guest"#;
19 changes: 1 addition & 18 deletions provers/sgx/setup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
# sp1-driver = { path = "../provers/sp1/driver", optional = true }
# risc0-driver = { path = "../provers/risc0/driver", optional = true }
sgx-prover = { path = "../prover", optional = true }
sgx-prover = { path = "../prover", features = ["enable"] }

# raiko
raiko-lib = { workspace = true, features = ["c-kzg"] }
Expand Down Expand Up @@ -61,18 +59,3 @@ dirs = { workspace = true }
assert_cmd = { workspace = true }
rstest = { workspace = true }
ethers-core = { workspace = true }

# [build-dependencies]
# sp1-helper = { workspace = true }

[features]
default = ["sgx"]
# sp1 = [
# "dep:sp1-driver",
# "sp1-driver/enable",
# ]
# risc0 = [
# "dep:risc0-driver",
# "risc0-driver/enable",
# ]
sgx = ["dep:sgx-prover", "sgx-prover/enable"]
1 change: 0 additions & 1 deletion provers/sp1/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ regex = "1.5.4"


[features]
default = ["enable"]
enable = [
"serde",
"serde_json",
Expand Down
Binary file modified provers/sp1/guest/elf/sp1-guest
Binary file not shown.
5 changes: 4 additions & 1 deletion script/prove-block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ fi

if [ "$proof" == "native" ]; then
proofParam='
"proof_type": "native"
"proof_type": "native",
"native" : {
"write_guest_input_path": null
}
'
elif [ "$proof" == "sp1" ]; then
proofParam='
Expand Down

0 comments on commit 0ba89ae

Please sign in to comment.