Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(provers): update Sp1 v1.0.1 #333

Merged
merged 20 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
296 changes: 207 additions & 89 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ risc0-build = { version = "1.0.1" }
risc0-binfmt = { version = "1.0.1" }

# SP1
sp1-sdk = { git = "https://github.com/succinctlabs/sp1.git", tag = "v1.0.5-testnet"}
sp1-zkvm = { git = "https://github.com/succinctlabs/sp1.git", tag = "v1.0.5-testnet"}
sp1-helper = { git = "https://github.com/succinctlabs/sp1.git", tag = "v1.0.5-testnet"}
sp1-sdk = { version = "1.0.1" }
sp1-zkvm = { version = "1.0.1" }
sp1-helper = { version = "1.0.1" }

# alloy
alloy-rlp = { version = "0.3.4", default-features = false }
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ The script `prove-block.sh` always sends a POST request to Raiko server and enqu
```shell
curl --location --request POST 'http://localhost:8080/proof/report'
```
To list all requested proofs with (chainID, blockhash, proofsys_id) and ID generated by the proof system:
CeciliaZ030 marked this conversation as resolved.
Show resolved Hide resolved
```shell
curl --location --request POST 'http://localhost:8080/proof/list'
```
To prune all tasks (the cancellation feature that kills prover is still WIP):
```shell
curl --location --request POST 'http://localhost:8080/proof/prune'
Expand Down
Binary file removed host/input-taiko_mainnet-196900.bin
Binary file not shown.
6 changes: 3 additions & 3 deletions host/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mod test {

use alloy_primitives::{Address, B256};
use alloy_provider::Provider;
use ethers_core::k256::elliptic_curve::rand_core::block;

use raiko_core::{
interfaces::{ProofRequest, ProofType},
provider::rpc::RpcBlockDataProvider,
Expand Down Expand Up @@ -138,8 +138,8 @@ mod test {

async fn get_latest_block_num(chain_spec: &ChainSpec) -> u64 {
let provider = RpcBlockDataProvider::new(&chain_spec.rpc, 0).unwrap();
let height = provider.provider.get_block_number().await.unwrap();
height

provider.provider.get_block_number().await.unwrap()
}

#[tokio::test]
Expand Down
6 changes: 5 additions & 1 deletion host/src/server/api/v1/proof.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use axum::{debug_handler, extract::State, routing::post, Json, Router};
use raiko_core::interfaces::ProofRequest;
use raiko_lib::prover::Proof;
use raiko_tasks::get_task_manager;
use serde_json::Value;
use utoipa::OpenApi;

Expand Down Expand Up @@ -42,11 +43,14 @@ async fn proof_handler(
inc_host_req_count(proof_request.block_number);
inc_guest_req_count(&proof_request.proof_type, proof_request.block_number);

// In memory task manager only for V1, cannot feature = "sqlite"
CeciliaZ030 marked this conversation as resolved.
Show resolved Hide resolved
let mut manager = get_task_manager(&raiko_tasks::TaskManagerOpts::default());

handle_proof(
&proof_request,
&prover_state.opts,
&prover_state.chain_specs,
None,
Some(&mut manager),
)
.await
.map_err(|e| {
Expand Down
36 changes: 36 additions & 0 deletions host/src/server/api/v2/proof/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use axum::{debug_handler, extract::State, routing::get, Json, Router};
use raiko_tasks::TaskManager;
use serde_json::Value;
use utoipa::OpenApi;

use crate::{interfaces::HostResult, ProverState};

#[utoipa::path(post, path = "/proof/list",
tag = "Proving",
responses (
(status = 200, description = "Successfully listed all proofs & Ids", body = CancelStatus)
)
)]
#[debug_handler(state = ProverState)]
/// List all tasks.
///
/// Retrieve a list of `{ chain_id, blockhash, prover_type, prover, status }` items.
async fn list_handler(State(prover_state): State<ProverState>) -> HostResult<Json<Value>> {
let mut manager = prover_state.task_manager();

let ids = manager.list_stored_ids().await?;

Ok(Json(serde_json::to_value(ids)?))
}

#[derive(OpenApi)]
#[openapi(paths(list_handler))]
struct Docs;

pub fn create_docs() -> utoipa::openapi::OpenApi {
Docs::openapi()
}

pub fn create_router() -> Router<ProverState> {
Router::new().route("/", get(list_handler))
}
3 changes: 3 additions & 0 deletions host/src/server/api/v2/proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
};

mod cancel;
mod list;
mod prune;
mod report;

Expand Down Expand Up @@ -109,6 +110,7 @@ pub fn create_docs() -> utoipa::openapi::OpenApi {
[
cancel::create_docs(),
report::create_docs(),
list::create_docs(),
prune::create_docs(),
]
.into_iter()
Expand All @@ -123,5 +125,6 @@ pub fn create_router() -> Router<ProverState> {
.route("/", post(proof_handler))
.nest("/cancel", cancel::create_router())
.nest("/report", report::create_router())
.nest("/list", list::create_router())
.nest("/prune", prune::create_router())
}
2 changes: 1 addition & 1 deletion provers/risc0/driver/src/methods/ecdsa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub const ECDSA_ELF: &[u8] =
include_bytes!("../../../guest/target/riscv32im-risc0-zkvm-elf/release/ecdsa");
pub const ECDSA_ID: [u32; 8] = [
2403615864, 3944723361, 1721066448, 3334617775, 1874245592, 1853933074, 3347288103, 3484988018,
3314277365, 903638368, 2823387338, 975292771, 2962241176, 3386670094, 1262198564, 423457744,
];
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,5 +1,5 @@
pub const RISC0_GUEST_ELF: &[u8] =
include_bytes!("../../../guest/target/riscv32im-risc0-zkvm-elf/release/risc0-guest");
pub const RISC0_GUEST_ID: [u32; 8] = [
4175756782, 445162214, 4177377390, 1094180202, 3909523218, 3532437525, 1083104193, 3682235852,
4088744154, 2973055310, 1548081772, 1076887772, 3229391767, 1145789405, 2599692868, 3836658436,
];
2 changes: 1 addition & 1 deletion provers/risc0/driver/src/methods/sha256.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub const SHA256_ELF: &[u8] =
include_bytes!("../../../guest/target/riscv32im-risc0-zkvm-elf/release/sha256");
pub const SHA256_ID: [u32; 8] = [
2276776674, 1530092210, 953274986, 4292586102, 1671654623, 3605429373, 2703161450, 1602935363,
3506084161, 1146489446, 485833862, 3404354046, 3626029993, 1928006034, 3833244069, 3073098029,
];
4 changes: 2 additions & 2 deletions provers/risc0/driver/src/methods/test_risc0_guest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub const TEST_RISC0_GUEST_ELF: &[u8] = include_bytes!(
"../../../guest/target/riscv32im-risc0-zkvm-elf/release/deps/risc0_guest-962b1e2c327a41e7"
"../../../guest/target/riscv32im-risc0-zkvm-elf/release/deps/risc0_guest-4b4f18d42a260659"
);
pub const TEST_RISC0_GUEST_ID: [u32; 8] = [
1845256462, 2792748830, 2660578418, 3478474367, 3459691673, 1097254092, 2300215542, 2186235231,
3216516244, 2583889163, 799150854, 107525368, 1015178806, 1451965571, 3377528142, 1073775,
];
4 changes: 4 additions & 0 deletions provers/sp1/contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ docs/

# Dotenv file
.env

# Sp1 Artifacts
*.bin
*.json
20 changes: 12 additions & 8 deletions provers/sp1/contracts/src/ISP1Verifier.sol
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
pragma solidity ^0.8.20;

/// @title SP1 Verifier Interface
/// @author Succinct Labs
/// @notice This contract is the interface for the SP1 Verifier.
interface ISP1Verifier {
/// @notice Returns the version of the SP1 Verifier.
function VERSION() external pure returns (string memory);

/// @notice Verifies a proof with given public values and vkey.
/// @param vkey The verification key for the RISC-V program.
/// @dev It is expected that the first 4 bytes of proofBytes must match the first 4 bytes of
/// target verifier's VERIFIER_HASH.
/// @param programVKey The verification key for the RISC-V program.
/// @param publicValues The public values encoded as bytes.
/// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
function verifyProof(
bytes32 vkey,
bytes memory publicValues,
bytes memory proofBytes
bytes32 programVKey,
bytes calldata publicValues,
bytes calldata proofBytes
) external view;
}

interface ISP1VerifierWithHash is ISP1Verifier {
/// @notice Returns the hash of the verifier.
function VERIFIER_HASH() external pure returns (bytes32);
}
2 changes: 1 addition & 1 deletion provers/sp1/contracts/src/RaikoVerifier.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import {SP1Verifier} from "./SP1Verifier.sol";
import {SP1Verifier} from "./exports/SP1Verifier.sol";
import "forge-std/console.sol";


Expand Down
23 changes: 0 additions & 23 deletions provers/sp1/contracts/src/SP1MockVerifier.sol

This file was deleted.

38 changes: 0 additions & 38 deletions provers/sp1/contracts/src/SP1Verifier.sol

This file was deleted.

1 change: 1 addition & 0 deletions provers/sp1/contracts/src/SP1_COMMIT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c3e6f5a44 v1.0.1
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,34 @@ contract PlonkVerifier {
uint256 private constant VK_DOMAIN_SIZE = 67108864;
uint256 private constant VK_INV_DOMAIN_SIZE = 21888242545679039938882419398440172875981108180010270949818755658014750055173;
uint256 private constant VK_OMEGA = 7419588552507395652481651088034484897579724952953562618697845598160172257810;
uint256 private constant VK_QL_COM_X = 11453021816558077199587453448133554449717227349905110102377551937287065432844;
uint256 private constant VK_QL_COM_Y = 9109454861639227276420512127158153200309304123653914732354951393328030738490;
uint256 private constant VK_QR_COM_X = 7821644493905086061243882786350714717685134318611500828127397521557784035499;
uint256 private constant VK_QR_COM_Y = 13041052004552404684631357942397749930688211338192136370697682907414153685909;
uint256 private constant VK_QM_COM_X = 1736617282271757071221168350710970296482875652045937117438551712909537507658;
uint256 private constant VK_QM_COM_Y = 5345542319716290612762422434959528912635502292626234402933058385633964347549;
uint256 private constant VK_QO_COM_X = 18014979952384567403919168968189231706227326297377482789091451813310066870667;
uint256 private constant VK_QO_COM_Y = 3708432301267678677786506706811217632085917639583680393547668223045541307479;
uint256 private constant VK_QK_COM_X = 865219285821188524465935330081001805464204673171276609279513133008346933102;
uint256 private constant VK_QK_COM_Y = 18703726972600526633323567676728586120328686581664702366183492539017488518098;
uint256 private constant VK_QL_COM_X = 1857909580169511349802659750796971843479875254100869630971241462994601728261;
uint256 private constant VK_QL_COM_Y = 9939759850806356864248111238536739205234573451362715673452883653355711153246;
uint256 private constant VK_QR_COM_X = 6682611358305124826092418545540029461432487755379853611917824622377793681568;
uint256 private constant VK_QR_COM_Y = 16146922041381506357194904478990761050971992194239724995214701093157406267535;
uint256 private constant VK_QM_COM_X = 15270607150655824285054831129475047993605012643483198806957835428128518328796;
uint256 private constant VK_QM_COM_Y = 8906362973243319533315777687407111724128929417944634665393917181717423040184;
uint256 private constant VK_QO_COM_X = 9238170507682156721257970724659753490472678230401730180879150452419856085130;
uint256 private constant VK_QO_COM_Y = 15002448341063781545868877705193346445491658209276170799557947939621037358673;
uint256 private constant VK_QK_COM_X = 4086391840028422412061902204531573940095106457258494694809601617012281376035;
uint256 private constant VK_QK_COM_Y = 6000044933528001852326622237429126033256712477967937051806041381512720593429;

uint256 private constant VK_S1_COM_X = 15838704022916757697514152719576664453825928657894015885754578605399919756856;
uint256 private constant VK_S1_COM_Y = 13387218978462600937448147418911963779105027838559913842027032523066810277894;
uint256 private constant VK_S1_COM_X = 18063299339199395103121690488363784429622765365901109337915377451812660612109;
uint256 private constant VK_S1_COM_Y = 2651903669339824861982388949922724224439321134438217364315690905899874053942;

uint256 private constant VK_S2_COM_X = 8305448485555792443785892674312793099639480632975263652331649209215498687903;
uint256 private constant VK_S2_COM_Y = 10616560339600329516818280331708877801874279040952666458845297443257568678018;
uint256 private constant VK_S2_COM_X = 10182215668646701259081683125255467829083669530554379855308469038959443338435;
uint256 private constant VK_S2_COM_Y = 9795420890085293666907175333011202594919622773371320681596120239068622591079;

uint256 private constant VK_S3_COM_X = 5758551426093048145915996102270540307839916135212724540929869593580613639236;
uint256 private constant VK_S3_COM_Y = 8329248325292414275499503861965434456596681093250791731115342865906364573529;
uint256 private constant VK_S3_COM_X = 11878547998011411033206750795964196348923817193060950180216147593592566658964;
uint256 private constant VK_S3_COM_Y = 2125473645138965118806391247175500356381463688616823813996167270375899160338;

uint256 private constant VK_COSET_SHIFT = 5;


uint256 private constant VK_QCP_0_X = 17635741098095958263934242315624493230424349111255370147390685295718328991108;
uint256 private constant VK_QCP_0_Y = 2694645204534071204078571296638854522310743386863537396039207026993856963119;
uint256 private constant VK_QCP_0_X = 13925008146309787259689624423365656079663403784687986805943581679042369678786;
uint256 private constant VK_QCP_0_Y = 18803925277742447221178851473422295758100099815833023568452434419788190270841;


uint256 private constant VK_INDEX_COMMIT_API_0 = 28657845;
uint256 private constant VK_INDEX_COMMIT_API_0 = 31810189;
uint256 private constant VK_NB_CUSTOM_GATES = 1;

// ------------------------------------------------
Expand Down
55 changes: 55 additions & 0 deletions provers/sp1/contracts/src/exports/SP1Verifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {ISP1Verifier, ISP1VerifierWithHash} from "../ISP1Verifier.sol";
import {PlonkVerifier} from "./PlonkVerifier.sol";

/// @title SP1 Verifier
/// @author Succinct Labs
/// @notice This contracts implements a solidity verifier for SP1.
contract SP1Verifier is PlonkVerifier, ISP1VerifierWithHash {
/// @notice Thrown when the verifier selector from this proof does not match the one in this
/// verifier. This indicates that this proof was sent to the wrong verifier.
/// @param received The verifier selector from the first 4 bytes of the proof.
/// @param expected The verifier selector from the first 4 bytes of the VERIFIER_HASH().
error WrongVerifierSelector(bytes4 received, bytes4 expected);

/// @notice Thrown when the proof is invalid.
error InvalidProof();

function VERSION() external pure returns (string memory) {
return "v1.1.0";
}

/// @inheritdoc ISP1VerifierWithHash
function VERIFIER_HASH() public pure returns (bytes32) {
return 0xfedc1fcc72d8f24db2e1b4a58b4e89cc8caf1be7070ee68f684d7ccd42b053e4;
}

/// @notice Hashes the public values to a field elements inside Bn254.
/// @param publicValues The public values.
function hashPublicValues(bytes calldata publicValues) public pure returns (bytes32) {
return sha256(publicValues) & bytes32(uint256((1 << 253) - 1));
}

/// @notice Verifies a proof with given public values and vkey.
/// @param programVKey The verification key for the RISC-V program.
/// @param publicValues The public values encoded as bytes.
/// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
function verifyProof(bytes32 programVKey, bytes calldata publicValues, bytes calldata proofBytes) external view {
bytes4 receivedSelector = bytes4(proofBytes[:4]);
bytes4 expectedSelector = bytes4(VERIFIER_HASH());
if (receivedSelector != expectedSelector) {
revert WrongVerifierSelector(receivedSelector, expectedSelector);
}

bytes32 publicValuesDigest = hashPublicValues(publicValues);
uint256[] memory inputs = new uint256[](2);
inputs[0] = uint256(programVKey);
inputs[1] = uint256(publicValuesDigest);
bool success = this.Verify(proofBytes[4:], inputs);
if (!success) {
revert InvalidProof();
}
}
}
1 change: 1 addition & 0 deletions provers/sp1/contracts/src/exports/SP1_COMMIT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c3e6f5a44 v1.0.1
Loading
Loading