Skip to content

Commit 4db49af

Browse files
committed
refactoring rust codebase
1 parent d2f7663 commit 4db49af

25 files changed

Lines changed: 4451 additions & 679 deletions
Lines changed: 2731 additions & 381 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[workspace]
2+
members = [
3+
"common/libzkp/impl",
4+
"common/libzkp/l2geth",
5+
"common/libzkp/interface",
6+
"common/libzkp/integration_test",
7+
"zkvm-prover",
8+
]
9+
10+
resolver = "2"
11+
12+
[workspace.package]
13+
authors = ["Scroll developers"]
14+
edition = "2021"
15+
homepage = "https://scroll.io"
16+
readme = "README.md"
17+
repository = "https://github.com/scroll-tech/scroll"
18+
version = "4.5.8"
19+
20+
[workspace.dependencies]
21+
scroll-zkvm-prover-euclid = { git = "https://github.com/scroll-tech/zkvm-prover", branch = "refactor/scroll_3", package = "scroll-zkvm-prover" }
22+
scroll-zkvm-verifier-euclid = { git = "https://github.com/scroll-tech/zkvm-prover", branch = "refactor/scroll_3", package = "scroll-zkvm-verifier" }
23+
scroll-zkvm-types = { git = "https://github.com/scroll-tech/zkvm-prover", branch = "refactor/scroll_3" }
24+
25+
sbv-primitives = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade", features = ["scroll"] }
26+
sbv-utils = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade" }
27+
28+
metrics = "0.23.0"
29+
metrics-util = "0.17"
30+
metrics-tracing-context = "0.16.0"
31+
32+
anyhow = "1.0"
33+
alloy = { version = "0.11", default-features = false }
34+
alloy-primitives = { version = "0.8", default-features = false }
35+
# also use this to trigger "serde" feature for primitives
36+
alloy-serde = { version = "0.8", default-features = false }
37+
38+
rkyv = "0.8"
39+
serde = { version = "1", default-features = false, features = ["derive"] }
40+
serde_json = { version = "1.0" }
41+
serde_derive = "1.0"
42+
serde_with = "3.11.0"
43+
itertools = "0.14"
44+
tiny-keccak = "2.0"
45+
tracing = "0.1"
46+
eyre = "0.6"
47+
bincode_v1 = { version = "1.3", package = "bincode"}
48+
snark-verifier-sdk = { version = "0.2.0", default-features = false, features = [
49+
"loader_halo2",
50+
"halo2-axiom",
51+
"display",
52+
] }
53+
once_cell = "1.20"
54+
base64 = "0.22"
55+
56+
#TODO: upgrade when Feyman
57+
vm-zstd = { git = "https://github.com/scroll-tech/rust-zstd-decompressor.git", tag = "v0.1.1" }
58+
59+
[patch.crates-io]
60+
alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.18-euclid-upgrade" }
61+
ruint = { git = "https://github.com/scroll-tech/uint.git", branch = "v1.12.3" }
62+
tiny-keccak = { git = "https://github.com/scroll-tech/tiny-keccak", branch = "scroll-patch-v2.0.2-euclid-upgrade" }
63+
64+
[profile.maxperf]
65+
inherits = "release"
66+
lto = "fat"
67+
codegen-units = 1

common/libzkp/impl/Cargo.toml

Lines changed: 18 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,23 @@
11
[package]
2-
name = "zkp"
3-
version = "0.1.0"
4-
edition = "2021"
2+
name = "libzkp"
3+
version.workspace = true
4+
edition.workspace = true
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7-
[lib]
8-
crate-type = ["cdylib"]
9-
10-
[patch.crates-io]
11-
alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.18-euclid-upgrade" }
12-
ruint = { git = "https://github.com/scroll-tech/uint.git", branch = "v1.12.3" }
13-
tiny-keccak = { git = "https://github.com/scroll-tech/tiny-keccak", branch = "scroll-patch-v2.0.2-euclid-upgrade" }
14-
157
[dependencies]
16-
euclid_prover = { git = "https://github.com/scroll-tech/zkvm-prover.git", package = "scroll-zkvm-prover" }
17-
euclid_verifier = { git = "https://github.com/scroll-tech/zkvm-prover.git", package = "scroll-zkvm-verifier" }
18-
19-
base64 = "0.13.0"
20-
env_logger = "0.9.0"
21-
log = "0.4"
22-
once_cell = "1.19"
23-
serde = "1.0"
24-
serde_derive = "1.0"
25-
serde_json = "1.0.66"
26-
anyhow = "1.0.86"
27-
28-
[profile.test]
29-
opt-level = 3
30-
31-
[profile.release]
32-
opt-level = 3
33-
34-
# [patch."https://github.com/openvm-org/stark-backend.git"]
35-
# openvm-stark-backend = { git = "ssh://git@github.com/scroll-tech/openvm-stark-gpu.git", branch = "main", features = ["gpu"] }
36-
# openvm-stark-sdk = { git = "ssh://git@github.com/scroll-tech/openvm-stark-gpu.git", branch = "main", features = ["gpu"] }
8+
scroll-zkvm-types.workspace = true
9+
scroll-zkvm-verifier-euclid.workspace = true
10+
11+
sbv-primitives.workspace = true
12+
base64.workspace = true
13+
serde.workspace = true
14+
serde_derive.workspace = true
15+
serde_json = { workspace = true, features = ["raw_value"]}
16+
tracing.workspace = true
17+
eyre.workspace = true
18+
19+
git-version = "0.3.5"
20+
serde_stacker = "0.1"
21+
regex = "1.11"
22+
c-kzg = { version = "1.0", features = ["serde"] }
3723

38-
# [patch."https://github.com/Plonky3/Plonky3.git"]
39-
# p3-air = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
40-
# p3-field = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
41-
# p3-commit = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
42-
# p3-matrix = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
43-
# p3-baby-bear = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", features = [
44-
# "nightly-features",
45-
# ], tag = "v0.2.0" }
46-
# p3-koala-bear = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
47-
# p3-util = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
48-
# p3-challenger = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
49-
# p3-dft = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
50-
# p3-fri = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
51-
# p3-goldilocks = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
52-
# p3-keccak = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
53-
# p3-keccak-air = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
54-
# p3-blake3 = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
55-
# p3-mds = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
56-
# p3-merkle-tree = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
57-
# p3-monty-31 = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
58-
# p3-poseidon = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
59-
# p3-poseidon2 = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
60-
# p3-poseidon2-air = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
61-
# p3-symmetric = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
62-
# p3-uni-stark = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
63-
# p3-maybe-rayon = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" } # the "parallel" feature is NOT on by default to allow single-threaded benchmarking
64-
# p3-bn254-fr = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }

common/libzkp/impl/src/lib.rs

Lines changed: 100 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,121 @@
1+
pub mod proofs;
2+
pub mod tasks;
3+
pub mod verifier;
4+
pub use verifier::{TaskType, VerifierConfig};
15
mod utils;
2-
mod verifier;
3-
4-
use std::{
5-
ffi::{c_char, c_int, CString},
6-
path::Path,
7-
};
8-
9-
use crate::utils::{c_char_to_str, c_char_to_vec};
10-
use verifier::{TaskType, VerifierConfig};
11-
12-
/// # Safety
13-
#[no_mangle]
14-
pub unsafe extern "C" fn init_verifier(config: *const c_char) {
15-
let config_str = c_char_to_str(config);
16-
let verifier_config = serde_json::from_str::<VerifierConfig>(config_str).unwrap();
17-
verifier::init(verifier_config);
18-
}
196

20-
/// # Safety
21-
#[no_mangle]
22-
pub unsafe extern "C" fn verify_chunk_proof(
23-
proof: *const c_char,
24-
fork_name: *const c_char,
25-
) -> c_char {
26-
verify_proof(proof, fork_name, TaskType::Chunk)
7+
use sbv_primitives::B256;
8+
use scroll_zkvm_types::util::vec_as_base64;
9+
use serde::{Deserialize, Serialize};
10+
use serde_json::value::RawValue;
11+
use std::path::Path;
12+
use tasks::chunk_interpreter::{ChunkInterpreter, TryFromWithInterpreter};
13+
14+
/// Turn the coordinator's chunk task into a json string for formal chunk proving
15+
/// task (with full witnesses)
16+
pub fn checkout_chunk_task(
17+
task_json: &str,
18+
interpreter: impl ChunkInterpreter,
19+
) -> eyre::Result<String> {
20+
let chunk_task = serde_json::from_str::<tasks::ChunkTask>(task_json)?;
21+
let ret = serde_json::to_string(&tasks::ChunkProvingTask::try_from_with_interpret(
22+
chunk_task,
23+
interpreter,
24+
)?)?;
25+
Ok(ret)
2726
}
2827

29-
fn verify_proof(proof: *const c_char, fork_name: *const c_char, task_type: TaskType) -> c_char {
30-
let fork_name_str = c_char_to_str(fork_name);
31-
let proof = c_char_to_vec(proof);
32-
let verifier = verifier::get_verifier(fork_name_str);
33-
34-
if let Err(e) = verifier {
35-
log::warn!("failed to get verifier, error: {:#}", e);
36-
return 0 as c_char;
28+
/// Generate required staff for proving tasks
29+
pub fn gen_universal_task(
30+
task_type: i32,
31+
task_json: &str,
32+
fork_name: &str,
33+
interpreter: Option<impl ChunkInterpreter>,
34+
) -> eyre::Result<(B256, String, String)> {
35+
use proofs::*;
36+
use tasks::*;
37+
38+
/// Wrapper for metadata
39+
#[derive(Clone, Debug, Serialize, Deserialize)]
40+
#[serde(untagged)]
41+
enum AnyMetaData {
42+
Chunk(ChunkProofMetadata),
43+
Batch(BatchProofMetadata),
44+
Bundle(BundleProofMetadata),
3745
}
38-
match verifier.unwrap().verify(task_type, proof) {
39-
Err(e) => {
40-
log::error!("{:?} verify failed, error: {:#}", task_type, e);
41-
false as c_char
42-
}
43-
Ok(result) => result as c_char,
44-
}
45-
}
4646

47-
/// # Safety
48-
#[no_mangle]
49-
pub unsafe extern "C" fn verify_batch_proof(
50-
proof: *const c_char,
51-
fork_name: *const c_char,
52-
) -> c_char {
53-
verify_proof(proof, fork_name, TaskType::Batch)
54-
}
55-
56-
/// # Safety
57-
#[no_mangle]
58-
pub unsafe extern "C" fn verify_bundle_proof(
59-
proof: *const c_char,
60-
fork_name: *const c_char,
61-
) -> c_char {
62-
verify_proof(proof, fork_name, TaskType::Bundle)
47+
let (pi_hash, metadata, u_task) = match task_type {
48+
x if x == TaskType::Chunk as i32 => {
49+
let task = serde_json::from_str::<ChunkProvingTask>(task_json)?;
50+
let (pi_hash, metadata, u_task) =
51+
gen_universal_chunk_task(task, fork_name.into(), interpreter)?;
52+
(pi_hash, AnyMetaData::Chunk(metadata), u_task)
53+
}
54+
x if x == TaskType::Batch as i32 => {
55+
let task = serde_json::from_str::<BatchProvingTask>(task_json)?;
56+
let (pi_hash, metadata, u_task) = gen_universal_batch_task(task, fork_name.into())?;
57+
(pi_hash, AnyMetaData::Batch(metadata), u_task)
58+
}
59+
x if x == TaskType::Bundle as i32 => {
60+
let task = serde_json::from_str::<BundleProvingTask>(task_json)?;
61+
let (pi_hash, metadata, u_task) = gen_universal_bundle_task(task, fork_name.into())?;
62+
(pi_hash, AnyMetaData::Bundle(metadata), u_task)
63+
}
64+
_ => return Err(eyre::eyre!("unrecognized task type {task_type}")),
65+
};
66+
67+
Ok((
68+
pi_hash,
69+
serde_json::to_string(&metadata)?,
70+
serde_json::to_string(&u_task)?,
71+
))
6372
}
6473

65-
/// # Safety
66-
#[no_mangle]
67-
pub unsafe extern "C" fn dump_vk(fork_name: *const c_char, file: *const c_char) {
68-
_dump_vk(fork_name, file);
69-
}
74+
/// helper to rearrange the proof return by universal prover into corresponding wrapped proof
75+
pub fn gen_wrapped_proof(proof_json: &str, metadata: &str, vk: &[u8]) -> eyre::Result<String> {
76+
#[derive(Serialize)]
77+
struct RearrangeWrappedProofJson<'a> {
78+
#[serde(borrow)]
79+
pub metadata: &'a RawValue,
80+
#[serde(borrow)]
81+
pub proof: &'a RawValue,
82+
#[serde(with = "vec_as_base64", default)]
83+
pub vk: Vec<u8>,
84+
pub git_version: String,
85+
}
7086

71-
fn _dump_vk(fork_name: *const c_char, file: *const c_char) {
72-
let fork_name_str = c_char_to_str(fork_name);
73-
let verifier = verifier::get_verifier(fork_name_str);
87+
let re_arrange = RearrangeWrappedProofJson {
88+
metadata: serde_json::from_str(metadata)?,
89+
proof: serde_json::from_str(proof_json)?,
90+
vk: vk.to_vec(),
91+
git_version: utils::short_git_version(),
92+
};
7493

75-
if let Ok(verifier) = verifier {
76-
verifier.as_ref().dump_vk(Path::new(c_char_to_str(file)));
77-
}
94+
let ret = serde_json::to_string(&re_arrange)?;
95+
Ok(ret)
7896
}
7997

80-
/// Represents the result of generating a universal task
81-
#[repr(C)]
82-
pub struct HandlingResult {
83-
pub ok: bool,
84-
pub universal_task: *mut c_char,
85-
pub metadata: *mut c_char,
86-
pub expected_pi_hash: [u8; 32],
98+
/// init verifier
99+
pub fn verifier_init(config: &str) -> eyre::Result<()> {
100+
let cfg: VerifierConfig = serde_json::from_str(config)?;
101+
verifier::init(cfg);
102+
Ok(())
87103
}
88104

89-
/// # Safety
90-
#[no_mangle]
91-
pub unsafe extern "C" fn gen_universal_task(
92-
_task_type: c_int,
93-
_task: *const c_char,
94-
_fork_name: *const c_char,
95-
) -> HandlingResult {
96-
unimplemented!("implementation will be added in later PRs");
97-
}
105+
/// verify proof
106+
pub fn verify_proof(proof: Vec<u8>, fork_name: &str, task_type: TaskType) -> eyre::Result<bool> {
107+
let verifier = verifier::get_verifier(fork_name)?;
98108

99-
/// # Safety
100-
#[no_mangle]
101-
pub unsafe extern "C" fn release_task_result(result: HandlingResult) {
102-
// Free the allocated strings
103-
if !result.universal_task.is_null() {
104-
let _ = CString::from_raw(result.universal_task);
105-
}
109+
let ret = verifier.verify(task_type, proof)?;
106110

107-
if !result.metadata.is_null() {
108-
let _ = CString::from_raw(result.metadata);
109-
}
111+
Ok(ret)
110112
}
111113

112-
/// # Safety
113-
#[no_mangle]
114-
pub unsafe extern "C" fn gen_wrapped_proof(
115-
_proof_json: *const c_char,
116-
_metadata: *const c_char,
117-
_vk: *const c_char,
118-
_vk_len: usize,
119-
) -> *mut c_char {
120-
unimplemented!("implementation will be added in later PRs");
121-
}
114+
/// dump vk
115+
pub fn dump_vk(fork_name: &str, file: &str) -> eyre::Result<()> {
116+
let verifier = verifier::get_verifier(fork_name)?;
122117

123-
/// # Safety
124-
#[no_mangle]
125-
pub unsafe extern "C" fn release_string(string_ptr: *mut c_char) {
126-
if !string_ptr.is_null() {
127-
let _ = CString::from_raw(string_ptr);
128-
}
129-
}
118+
verifier.dump_vk(Path::new(file));
130119

131-
/// # Safety
132-
#[no_mangle]
133-
pub unsafe extern "C" fn init_l2geth(_config: *const c_char) {
134-
unimplemented!("implementation will be added in later PRs");
120+
Ok(())
135121
}

0 commit comments

Comments
 (0)