-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from tolak/local-prover
Local prover
- Loading branch information
Showing
9 changed files
with
682 additions
and
210 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,118 @@ | ||
// Copyright 2024 RISC Zero, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// The following library provides utility functions to help with sending | ||
// off-chain proof requests to the Bonsai proving service and publish the | ||
// received proofs directly to a deployed app contract on Ethereum. | ||
// | ||
// Please note that both `risc0_zkvm` and `bonsai_sdk` crates are still | ||
// under active development. As such, this library might change to adapt to | ||
// the upstream changes. | ||
|
||
use std::time::Duration; | ||
|
||
use alloy_primitives::FixedBytes; | ||
use anyhow::{Context, Result}; | ||
use bonsai_sdk::alpha as bonsai_sdk; | ||
use risc0_ethereum_contracts::groth16::Seal; | ||
use risc0_zkvm::{compute_image_id, Receipt}; | ||
|
||
/// An implementation of a Prover that runs on Bonsai. | ||
pub struct BonsaiProver {} | ||
impl BonsaiProver { | ||
/// Generates a snark proof as a triplet (`Vec<u8>`, `FixedBytes<32>`, | ||
/// `Vec<u8>) for the given elf and input. | ||
pub fn prove(elf: &[u8], input: &[u8]) -> Result<(Vec<u8>, FixedBytes<32>, Vec<u8>)> { | ||
let client = bonsai_sdk::Client::from_env(risc0_zkvm::VERSION)?; | ||
|
||
// Compute the image_id, then upload the ELF with the image_id as its key. | ||
let image_id = compute_image_id(elf)?; | ||
let image_id_hex = image_id.to_string(); | ||
client.upload_img(&image_id_hex, elf.to_vec())?; | ||
log::info!("Image ID: 0x{}", image_id_hex); | ||
|
||
// Prepare input data and upload it. | ||
let input_id = client.upload_input(input.to_vec())?; | ||
|
||
// Start a session running the prover. | ||
let session = client.create_session(image_id_hex, input_id, vec![])?; | ||
log::info!("Created session: {}", session.uuid); | ||
let _receipt = loop { | ||
let res = session.status(&client)?; | ||
if res.status == "RUNNING" { | ||
log::info!( | ||
"Current status: {} - state: {} - continue polling...", | ||
res.status, | ||
res.state.unwrap_or_default() | ||
); | ||
std::thread::sleep(Duration::from_secs(15)); | ||
continue; | ||
} | ||
if res.status == "SUCCEEDED" { | ||
// Download the receipt, containing the output. | ||
let receipt_url = res | ||
.receipt_url | ||
.context("API error, missing receipt on completed session")?; | ||
|
||
let receipt_buf = client.download(&receipt_url)?; | ||
let receipt: Receipt = bincode::deserialize(&receipt_buf)?; | ||
|
||
break receipt; | ||
} | ||
|
||
panic!( | ||
"Workflow exited: {} - | err: {}", | ||
res.status, | ||
res.error_msg.unwrap_or_default() | ||
); | ||
}; | ||
|
||
// Fetch the snark. | ||
let snark_session = client.create_snark(session.uuid)?; | ||
log::info!("Created snark session: {}", snark_session.uuid); | ||
let snark_receipt = loop { | ||
let res = snark_session.status(&client)?; | ||
match res.status.as_str() { | ||
"RUNNING" => { | ||
log::info!("Current status: {} - continue polling...", res.status,); | ||
std::thread::sleep(Duration::from_secs(15)); | ||
continue; | ||
} | ||
"SUCCEEDED" => { | ||
break res.output.context("No snark generated :(")?; | ||
} | ||
_ => { | ||
panic!( | ||
"Workflow exited: {} err: {}", | ||
res.status, | ||
res.error_msg.unwrap_or_default() | ||
); | ||
} | ||
} | ||
}; | ||
|
||
let snark = snark_receipt.snark; | ||
log::debug!("Snark proof!: {snark:?}"); | ||
|
||
let seal = Seal::abi_encode(snark).context("Read seal")?; | ||
let post_state_digest: FixedBytes<32> = snark_receipt | ||
.post_state_digest | ||
.as_slice() | ||
.try_into() | ||
.context("Read post_state_digest")?; | ||
let journal = snark_receipt.journal; | ||
|
||
Ok((journal, post_state_digest, seal)) | ||
} | ||
} |
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,160 +1,2 @@ | ||
// Copyright 2024 RISC Zero, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// The following library provides utility functions to help with sending | ||
// off-chain proof requests to the Bonsai proving service and publish the | ||
// received proofs directly to a deployed app contract on Ethereum. | ||
// | ||
// Please note that both `risc0_zkvm` and `bonsai_sdk` crates are still | ||
// under active development. As such, this library might change to adapt to | ||
// the upstream changes. | ||
|
||
use std::time::Duration; | ||
|
||
use alloy_primitives::FixedBytes; | ||
use anyhow::{Context, Result}; | ||
use bonsai_sdk::alpha as bonsai_sdk; | ||
use ethers::prelude::*; | ||
use risc0_ethereum_contracts::groth16::Seal; | ||
use risc0_zkvm::{compute_image_id, Receipt}; | ||
|
||
/// Wrapper of a `SignerMiddleware` client to send transactions to the given | ||
/// contract's `Address`. | ||
pub struct TxSender { | ||
chain_id: u64, | ||
client: SignerMiddleware<Provider<Http>, Wallet<k256::ecdsa::SigningKey>>, | ||
contract: Address, | ||
} | ||
|
||
impl TxSender { | ||
/// Creates a new `TxSender`. | ||
pub fn new(chain_id: u64, rpc_url: &str, private_key: &str, contract: &str) -> Result<Self> { | ||
let provider = Provider::<Http>::try_from(rpc_url)?; | ||
let wallet: LocalWallet = private_key.parse::<LocalWallet>()?.with_chain_id(chain_id); | ||
let client = SignerMiddleware::new(provider.clone(), wallet.clone()); | ||
let contract = contract.parse::<Address>()?; | ||
|
||
Ok(TxSender { | ||
chain_id, | ||
client, | ||
contract, | ||
}) | ||
} | ||
|
||
/// Send a transaction with the given calldata. | ||
pub async fn send(&self, calldata: Vec<u8>) -> Result<Option<TransactionReceipt>> { | ||
let tx = TransactionRequest::new() | ||
.chain_id(self.chain_id) | ||
.to(self.contract) | ||
.from(self.client.address()) | ||
.data(calldata); | ||
|
||
log::info!("Transaction request: {:?}", &tx); | ||
|
||
let tx = self.client.send_transaction(tx, None).await?.await?; | ||
|
||
log::info!("Transaction receipt: {:?}", &tx); | ||
|
||
Ok(tx) | ||
} | ||
} | ||
|
||
/// An implementation of a Prover that runs on Bonsai. | ||
pub struct BonsaiProver {} | ||
impl BonsaiProver { | ||
/// Generates a snark proof as a triplet (`Vec<u8>`, `FixedBytes<32>`, | ||
/// `Vec<u8>) for the given elf and input. | ||
pub fn prove(elf: &[u8], input: &[u8]) -> Result<(Vec<u8>, FixedBytes<32>, Vec<u8>)> { | ||
let client = bonsai_sdk::Client::from_env(risc0_zkvm::VERSION)?; | ||
|
||
// Compute the image_id, then upload the ELF with the image_id as its key. | ||
let image_id = compute_image_id(elf)?; | ||
let image_id_hex = image_id.to_string(); | ||
client.upload_img(&image_id_hex, elf.to_vec())?; | ||
log::info!("Image ID: 0x{}", image_id_hex); | ||
|
||
// Prepare input data and upload it. | ||
let input_id = client.upload_input(input.to_vec())?; | ||
|
||
// Start a session running the prover. | ||
let session = client.create_session(image_id_hex, input_id, vec![])?; | ||
log::info!("Created session: {}", session.uuid); | ||
let _receipt = loop { | ||
let res = session.status(&client)?; | ||
if res.status == "RUNNING" { | ||
log::info!( | ||
"Current status: {} - state: {} - continue polling...", | ||
res.status, | ||
res.state.unwrap_or_default() | ||
); | ||
std::thread::sleep(Duration::from_secs(15)); | ||
continue; | ||
} | ||
if res.status == "SUCCEEDED" { | ||
// Download the receipt, containing the output. | ||
let receipt_url = res | ||
.receipt_url | ||
.context("API error, missing receipt on completed session")?; | ||
|
||
let receipt_buf = client.download(&receipt_url)?; | ||
let receipt: Receipt = bincode::deserialize(&receipt_buf)?; | ||
|
||
break receipt; | ||
} | ||
|
||
panic!( | ||
"Workflow exited: {} - | err: {}", | ||
res.status, | ||
res.error_msg.unwrap_or_default() | ||
); | ||
}; | ||
|
||
// Fetch the snark. | ||
let snark_session = client.create_snark(session.uuid)?; | ||
log::info!("Created snark session: {}", snark_session.uuid); | ||
let snark_receipt = loop { | ||
let res = snark_session.status(&client)?; | ||
match res.status.as_str() { | ||
"RUNNING" => { | ||
log::info!("Current status: {} - continue polling...", res.status,); | ||
std::thread::sleep(Duration::from_secs(15)); | ||
continue; | ||
} | ||
"SUCCEEDED" => { | ||
break res.output.context("No snark generated :(")?; | ||
} | ||
_ => { | ||
panic!( | ||
"Workflow exited: {} err: {}", | ||
res.status, | ||
res.error_msg.unwrap_or_default() | ||
); | ||
} | ||
} | ||
}; | ||
|
||
let snark = snark_receipt.snark; | ||
log::debug!("Snark proof!: {snark:?}"); | ||
|
||
let seal = Seal::abi_encode(snark).context("Read seal")?; | ||
let post_state_digest: FixedBytes<32> = snark_receipt | ||
.post_state_digest | ||
.as_slice() | ||
.try_into() | ||
.context("Read post_state_digest")?; | ||
let journal = snark_receipt.journal; | ||
|
||
Ok((journal, post_state_digest, seal)) | ||
} | ||
} | ||
pub mod bonsai_prover; | ||
pub mod local_prover; |
Oops, something went wrong.