Skip to content

Commit 69ff09a

Browse files
committed
add types-rs
Signed-off-by: noelwei <[email protected]>
1 parent 318c46e commit 69ff09a

File tree

40 files changed

+2646
-765
lines changed

40 files changed

+2646
-765
lines changed

Cargo.lock

Lines changed: 118 additions & 725 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
[workspace]
22
members = [
3+
"common/types-rs/base",
4+
"common/types-rs/circuit",
5+
"common/types-rs/aggregation",
6+
"common/types-rs/chunk",
7+
"common/types-rs/batch",
8+
"common/types-rs/bundle",
39
"zkvm-prover",
410
]
511

@@ -16,7 +22,33 @@ version = "4.5.8"
1622
[workspace.dependencies]
1723
scroll-zkvm-prover-euclid = { git = "https://github.com/scroll-tech/zkvm-prover", tag = "v0.3.0", package = "scroll-zkvm-prover"}
1824

25+
openvm = { git = "https://github.com/openvm-org/openvm.git", rev = "3c35e9f", default-features = false }
26+
openvm-custom-insn = { git = "https://github.com/openvm-org/openvm.git", rev = "3c35e9f", default-features = false }
27+
openvm-rv32im-guest = { git = "https://github.com/openvm-org/openvm.git", rev = "3c35e9f", default-features = false }
28+
29+
sbv-core = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade", features = ["scroll"] }
30+
sbv-primitives = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade", features = ["scroll"] }
31+
sbv-kv = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade" }
32+
sbv-trie = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade" }
33+
34+
alloy-primitives = { version = "0.8", default-features = false }
35+
alloy-serde = { version = "0.11", default-features = false }
36+
37+
rkyv = "0.8"
38+
serde = { version = "1", default-features = false, features = ["derive"] }
39+
serde_json = { version = "1.0" }
40+
serde_with = "3.11.0"
41+
itertools = "0.14"
42+
tiny-keccak = "2.0"
43+
#TODO: upgrade
44+
vm-zstd = { git = "https://github.com/scroll-tech/rust-zstd-decompressor.git", tag = "v0.1.1" }
45+
1946
[patch.crates-io]
20-
alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.21" }
47+
alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.18-euclid-upgrade" }
2148
ruint = { git = "https://github.com/scroll-tech/uint.git", branch = "v1.12.3" }
22-
tiny-keccak = { git = "https://github.com/scroll-tech/tiny-keccak", branch = "scroll-patch-v2.0.2-openvm-v1.0.0-rc.1" }
49+
tiny-keccak = { git = "https://github.com/scroll-tech/tiny-keccak", branch = "scroll-patch-v2.0.2-euclid-upgrade" }
50+
51+
[profile.maxperf]
52+
inherits = "release"
53+
lto = "fat"
54+
codegen-units = 1

common/types-rs/Cargo.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[package]
2+
name = "scroll-zkvm-types"
3+
authors.workspace = true
4+
edition.workspace = true
5+
homepage.workspace = true
6+
readme.workspace = true
7+
repository.workspace = true
8+
version = "0.2.0"
9+
10+
[dependencies]
11+
alloy-primitives = { workspace = true, default-features = false, features = ["std", "map-hashbrown", "map-fxhash", "rkyv"] }
12+
alloy-serde.workspace = true
13+
rkyv.workspace = true
14+
sbv-trie = { workspace = true }
15+
sbv-core = { workspace = true }
16+
sbv-primitives = { workspace = true }
17+
sbv-kv = { workspace = true }
18+
vm-zstd = { workspace = true }
19+
serde.workspace = true
20+
itertools.workspace = true
21+
tiny-keccak = { workspace = true }
22+
23+
openvm = { workspace = true, features = ["std"] }
24+
openvm-rv32im-guest = { workspace = true }
25+
openvm-custom-insn = { workspace = true }
26+
sha3 = "0.10.8"
27+
sha2 = "0.10.8"
28+
29+
[features]
30+
default = []
31+
openvm = ["sbv-trie/openvm", "sbv-core/openvm", "sbv-primitives/openvm", "tiny-keccak/openvm"]

common/types-rs/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Input Types for circuits
2+
3+
A series of separated crates for the input types accepted by circuits as input.
4+
5+
This crate help decoupling circuits with other crates and keep their dependencies neat and controllable. Avoiding to involve crates which is not compatible with the tootlchain of openvm from indirect dependency.
6+
7+
### Code structure
8+
```
9+
types-rs
10+
11+
├── base
12+
13+
├── circuit
14+
15+
├── aggregation
16+
17+
<following are layer-oriented crates>
18+
19+
├── chunk
20+
21+
├── batch
22+
23+
└── bundle
24+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "scroll-zkvm-circuit-input-types-aggregation"
3+
authors.workspace = true
4+
edition.workspace = true
5+
homepage.workspace = true
6+
readme.workspace = true
7+
repository.workspace = true
8+
version = "0.2.0"
9+
10+
[dependencies]
11+
alloy-primitives = { workspace = true, default-features = false, features = ["std", "map-hashbrown", "map-fxhash", "rkyv"] }
12+
alloy-serde.workspace = true
13+
rkyv.workspace = true
14+
serde.workspace = true
15+
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/// Represents an openvm program commitments and public values.
2+
#[derive(
3+
Clone,
4+
Debug,
5+
rkyv::Archive,
6+
rkyv::Deserialize,
7+
rkyv::Serialize,
8+
serde::Deserialize,
9+
serde::Serialize,
10+
)]
11+
#[rkyv(derive(Debug))]
12+
pub struct AggregationInput {
13+
/// Public values.
14+
pub public_values: Vec<u32>,
15+
/// Represent the commitment needed to verify a root proof
16+
pub commitment: ProgramCommitment,
17+
}
18+
19+
/// Represent the commitment needed to verify a [`RootProof`].
20+
#[derive(
21+
Clone,
22+
Debug,
23+
Default,
24+
rkyv::Archive,
25+
rkyv::Deserialize,
26+
rkyv::Serialize,
27+
serde::Deserialize,
28+
serde::Serialize,
29+
)]
30+
#[rkyv(derive(Debug))]
31+
pub struct ProgramCommitment {
32+
/// The commitment to the child program exe.
33+
pub exe: [u32; 8],
34+
/// The commitment to the child program leaf.
35+
pub leaf: [u32; 8],
36+
}
37+
38+
impl ProgramCommitment {
39+
pub fn deserialize(commitment_bytes: &[u8]) -> Self {
40+
// TODO: temporary skip deserialize if no vk is provided
41+
if commitment_bytes.is_empty() {
42+
return Default::default();
43+
}
44+
45+
let archived_data =
46+
rkyv::access::<ArchivedProgramCommitment, rkyv::rancor::BoxedError>(commitment_bytes)
47+
.unwrap();
48+
49+
Self {
50+
exe: archived_data.exe.map(|u32_le| u32_le.to_native()),
51+
leaf: archived_data.leaf.map(|u32_le| u32_le.to_native()),
52+
}
53+
}
54+
55+
pub fn serialize(&self) -> Vec<u8> {
56+
rkyv::to_bytes::<rkyv::rancor::BoxedError>(self)
57+
.map(|v| v.to_vec())
58+
.unwrap()
59+
}
60+
}
61+
62+
impl From<&ArchivedProgramCommitment> for ProgramCommitment {
63+
fn from(archived: &ArchivedProgramCommitment) -> Self {
64+
Self {
65+
exe: archived.exe.map(|u32_le| u32_le.to_native()),
66+
leaf: archived.leaf.map(|u32_le| u32_le.to_native()),
67+
}
68+
}
69+
}
70+
71+
/// Number of public-input values, i.e. [u32; N].
72+
///
73+
/// Note that the actual value for each u32 is a byte.
74+
const NUM_PUBLIC_VALUES: usize = 32;
75+
76+
/// Verify a root proof. The real "proof" will be loaded from StdIn.
77+
pub fn verify_proof(commitment: &ProgramCommitment, public_inputs: &[u32]) {
78+
// Sanity check for the number of public-input values.
79+
assert_eq!(public_inputs.len(), NUM_PUBLIC_VALUES);
80+
81+
// Extend the public-input values by prepending the commitments to the root verifier's exe and
82+
// leaf.
83+
let mut extended_public_inputs = vec![];
84+
extended_public_inputs.extend(commitment.exe);
85+
extended_public_inputs.extend(commitment.leaf);
86+
extended_public_inputs.extend_from_slice(public_inputs);
87+
// Pass through kernel and verify against root verifier's ASM.
88+
exec_kernel(extended_public_inputs.as_ptr());
89+
}
90+
91+
fn exec_kernel(_pi_ptr: *const u32) {
92+
// reserve x29, x30, x31 for kernel
93+
let mut _buf1: u32 = 0;
94+
let mut _buf2: u32 = 0;
95+
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
96+
unsafe {
97+
std::arch::asm!(
98+
include_str!("../../../build-guest/root_verifier.asm"),
99+
in("x29") _pi_ptr,
100+
inout("x30") _buf1,
101+
inout("x31") _buf2,
102+
)
103+
}
104+
}
105+
106+
/// Witness for an [`AggregationCircuit`][AggCircuit] that also carries proofs that are being
107+
/// aggregated.
108+
pub trait ProofCarryingWitness {
109+
/// Get the root proofs from the witness.
110+
fn get_proofs(&self) -> Vec<AggregationInput>;
111+
}

common/types-rs/base/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "scroll-zkvm-circuit-input-types-base"
3+
authors.workspace = true
4+
edition.workspace = true
5+
homepage.workspace = true
6+
readme.workspace = true
7+
repository.workspace = true
8+
version = "0.2.0"
9+
10+
[dependencies]
11+
alloy-primitives = { workspace = true, default-features = false, features = ["std", "map-hashbrown", "map-fxhash", "rkyv"] }
12+
alloy-serde.workspace = true
13+
rkyv.workspace = true
14+
serde.workspace = true
15+
itertools.workspace = true
16+
tiny-keccak = { workspace = true }
17+
sha3 = "0.10.8"
18+
sha2 = "0.10.8"
19+
20+
[features]
21+
default = []

common/types-rs/base/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod utils;
2+
pub mod public_inputs;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use alloy_primitives::B256;
2+
pub mod chunk;
3+
pub mod batch;
4+
pub mod bundle;
5+
6+
/// Defines behaviour to be implemented by types representing the public-input values of a circuit.
7+
pub trait PublicInputs {
8+
/// Keccak-256 digest of the public inputs. The public-input hash are revealed as public values
9+
/// via [`openvm::io::reveal`].
10+
fn pi_hash(&self) -> B256;
11+
12+
/// Validation logic between public inputs of two contiguous instances.
13+
fn validate(&self, prev_pi: &Self);
14+
}
15+
16+
17+
#[derive(
18+
Default,
19+
Debug,
20+
Copy,
21+
Clone,
22+
PartialEq,
23+
Eq,
24+
rkyv::Archive,
25+
rkyv::Deserialize,
26+
rkyv::Serialize,
27+
serde::Deserialize,
28+
serde::Serialize,
29+
)]
30+
#[rkyv(derive(Debug))]
31+
pub enum ForkName {
32+
#[default]
33+
EuclidV1,
34+
EuclidV2,
35+
}
36+
37+
impl From<&ArchivedForkName> for ForkName {
38+
fn from(archived: &ArchivedForkName) -> Self {
39+
match archived {
40+
ArchivedForkName::EuclidV1 => ForkName::EuclidV1,
41+
ArchivedForkName::EuclidV2 => ForkName::EuclidV2,
42+
}
43+
}
44+
}
45+
46+
impl From<Option<&str>> for ForkName {
47+
fn from(value: Option<&str>) -> Self {
48+
match value {
49+
None => Default::default(),
50+
Some("euclidv1") => ForkName::EuclidV1,
51+
Some("euclidv2") => ForkName::EuclidV2,
52+
Some(s) => unreachable!("hardfork not accepted: {s}"),
53+
}
54+
}
55+
}
56+
57+
impl From<&str> for ForkName {
58+
fn from(value: &str) -> Self {
59+
match value {
60+
"euclidv1" => ForkName::EuclidV1,
61+
"euclidv2" => ForkName::EuclidV2,
62+
s => unreachable!("hardfork not accepted: {s}"),
63+
}
64+
}
65+
}
66+
67+
/// helper trait to extend PublicInputs
68+
pub trait MultiVersionPublicInputs {
69+
fn pi_hash_by_fork(&self, fork_name: ForkName) -> B256;
70+
fn validate(&self, prev_pi: &Self, fork_name: ForkName);
71+
}
72+
73+
impl<T: MultiVersionPublicInputs> PublicInputs for (T, ForkName) {
74+
fn pi_hash(&self) -> B256 {
75+
self.0.pi_hash_by_fork(self.1)
76+
}
77+
78+
fn validate(&self, prev_pi: &Self) {
79+
assert_eq!(self.1, prev_pi.1);
80+
self.0.validate(&prev_pi.0, self.1)
81+
}
82+
}

0 commit comments

Comments
 (0)