Compare commits

..

26 Commits

Author SHA1 Message Date
noelwei
1464d1091d fix lint issue
Signed-off-by: noelwei <fan@scroll.io>
2025-05-19 22:09:53 +09:00
noelwei
14f355b528 integrage libzkp into workspace
Signed-off-by: noelwei <fan@scroll.io>
2025-05-18 22:20:23 +09:00
noelwei
4b91057a05 move config sample file
Signed-off-by: noelwei <fan@scroll.io>
2025-05-18 22:11:30 +09:00
noelwei
904b97b70d apply prehandler instead of proving-sdk
Signed-off-by: noelwei <fan@scroll.io>
2025-05-18 14:24:12 +09:00
noelwei
a8a0a816d1 Merge remote-tracking branch 'origin/develop' into refactor/zkvm 2025-05-17 22:09:20 +09:00
noelwei
9c5c29e465 trivial: exclude some crates from workspace, move dockerignore
Signed-off-by: noelwei <fan@scroll.io>
2025-05-16 12:52:38 +09:00
noelwei
1135291329 update paths
Signed-off-by: noelwei <fan@scroll.io>
2025-05-16 11:41:21 +09:00
noelwei
9b10ad8f14 add build scripts
Signed-off-by: noelwei <fan@scroll.io>
2025-05-16 11:33:15 +09:00
noelwei
d0869d44d6 add integration
Signed-off-by: noelwei <fan@scroll.io>
2025-05-15 11:35:41 +09:00
noelwei
bcd48b846f zkvm-prover immigrates here
Signed-off-by: noelwei <fan@scroll.io>
2025-05-15 09:23:02 +09:00
noelwei
591a833146 update dep
Signed-off-by: noelwei <fan@scroll.io>
2025-05-14 13:40:11 +09:00
noelwei
d4f1e7d981 move out types-circuit
Signed-off-by: noelwei <fan@scroll.io>
2025-05-14 11:12:45 +09:00
noelwei
9801992724 re-export agg traits
Signed-off-by: noelwei <fan@scroll.io>
2025-05-13 22:00:25 +09:00
noelwei
bff0e6f398 Merge remote-tracking branch 'origin/develop' into refactor/zkvm 2025-05-13 21:59:58 +09:00
noelwei
85f3cc35e0 re-export base crate in circuit
Signed-off-by: noelwei <fan@scroll.io>
2025-05-12 15:21:22 +09:00
noelwei
15c872d5a5 + update openvm
+ fmt
+ trivial updates

Signed-off-by: noelwei <fan@scroll.io>
2025-05-12 11:26:17 +09:00
noelwei
c0f773c014 Merge remote-tracking branch 'origin/develop' into refactor/zkvm 2025-05-12 09:38:34 +09:00
noelwei
a300fa284b separate prover bin
Signed-off-by: noelwei <fan@scroll.io>
2025-05-10 09:09:35 +09:00
noelwei
932be72b88 zkvm-prover: separate bin and lib
Signed-off-by: noelwei <fan@scroll.io>
2025-05-08 11:48:55 +09:00
noelwei
0c4c410d2c refine deps in zkvm-prover
Signed-off-by: noelwei <fan@scroll.io>
2025-05-08 10:21:49 +09:00
noelwei
0360cd2c6f update alloy deps
Signed-off-by: noelwei <fan@scroll.io>
2025-05-08 10:05:53 +09:00
noelwei
a6d49b24d8 update proving-sdk dep
Signed-off-by: noelwei <fan@scroll.io>
2025-05-07 11:38:16 +09:00
noelwei
8ed77bd780 update zkvm-prover dep
Signed-off-by: noelwei <fan@scroll.io>
2025-05-06 20:18:43 +09:00
noelwei
66b1095e25 fmt
Signed-off-by: noelwei <fan@scroll.io>
2025-05-06 13:29:23 +09:00
noelwei
69ff09a7e6 add types-rs
Signed-off-by: noelwei <fan@scroll.io>
2025-05-06 13:28:18 +09:00
noelwei
318c46ebc9 induce workspace
Signed-off-by: noelwei <fan@scroll.io>
2025-04-27 21:51:18 +09:00
177 changed files with 12243 additions and 3510 deletions

16
.dockerignore Normal file
View File

@@ -0,0 +1,16 @@
.github
.gitignore
.dockerignore
Dockerfile
Dockerfile.backup
.output
docs
openvm-clippy
target

View File

@@ -42,11 +42,15 @@ jobs:
uses: Swatinem/rust-cache@v2
with:
workspaces: "common/libzkp/impl -> target"
# - name: Lint
# working-directory: 'common'
# run: |
# rm -rf $HOME/.cache/golangci-lint
# make lint
- name: Setup SSH for private repos
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.OPENVM_GPU_SSH_PRIVATE_KEY }}
- name: Lint
working-directory: 'common'
run: |
rm -rf $HOME/.cache/golangci-lint
make lint
goimports-lint:
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest

View File

@@ -307,48 +307,13 @@ jobs:
REPOSITORY: coordinator-api
run: |
aws --region ${{ env.AWS_REGION }} ecr describe-repositories --repository-names ${{ env.REPOSITORY }} && : || aws --region ${{ env.AWS_REGION }} ecr create-repository --repository-name ${{ env.REPOSITORY }}
- name: Setup SSH for repositories and clone them
- name: Setup SSH for private repos
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.OPENVM_GPU_SSH_PRIVATE_KEY }}
- name: Run custom script
run: |
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# Setup for plonky3-gpu
echo "${{ secrets.PLONKY3_GPU_SSH_PRIVATE_KEY }}" > ~/.ssh/plonky3_gpu_key
chmod 600 ~/.ssh/plonky3_gpu_key
eval "$(ssh-agent -s)" > /dev/null
ssh-add ~/.ssh/plonky3_gpu_key 2>/dev/null
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts 2>/dev/null
echo "Loaded plonky3-gpu key"
# Clone plonky3-gpu repository
./build/dockerfiles/coordinator-api/clone_plonky3_gpu.sh
# Setup for openvm-stark-gpu
echo "${{ secrets.OPENVM_STARK_GPU_SSH_PRIVATE_KEY }}" > ~/.ssh/openvm_stark_gpu_key
chmod 600 ~/.ssh/openvm_stark_gpu_key
eval "$(ssh-agent -s)" > /dev/null
ssh-add ~/.ssh/openvm_stark_gpu_key 2>/dev/null
echo "Loaded openvm-stark-gpu key"
# Clone openvm-stark-gpu repository
./build/dockerfiles/coordinator-api/clone_openvm_stark_gpu.sh
# Setup for openvm-gpu
echo "${{ secrets.OPENVM_GPU_SSH_PRIVATE_KEY }}" > ~/.ssh/openvm_gpu_key
chmod 600 ~/.ssh/openvm_gpu_key
eval "$(ssh-agent -s)" > /dev/null
ssh-add ~/.ssh/openvm_gpu_key 2>/dev/null
echo "Loaded openvm-gpu key"
# Clone openvm-gpu repository
./build/dockerfiles/coordinator-api/clone_openvm_gpu.sh
# Show number of loaded keys
echo "Number of loaded keys: $(ssh-add -l | wc -l)"
- name: Checkout specific commits
run: |
./build/dockerfiles/coordinator-api/checkout_all.sh
./build/dockerfiles/coordinator-api/init-openvm.sh
- name: Build and push
uses: docker/build-push-action@v3
env:

File diff suppressed because it is too large Load Diff

93
Cargo.toml Normal file
View File

@@ -0,0 +1,93 @@
[workspace]
members = [
"common/types-rs",
"common/types-rs/base",
"common/types-rs/aggregation",
"common/types-rs/chunk",
"common/types-rs/batch",
"common/types-rs/bundle",
"common/libzkp/impl",
"zkvm-prover/prover",
"zkvm-prover/verifier",
"zkvm-prover/integration",
"zkvm-prover/bin",
]
exclude = [
"prover"
]
resolver = "2"
[workspace.package]
authors = ["Scroll developers"]
edition = "2021"
homepage = "https://scroll.io"
readme = "README.md"
repository = "https://github.com/scroll-tech/scroll"
version = "4.5.8"
[workspace.dependencies]
scroll-zkvm-prover-euclid = { git = "https://github.com/scroll-tech/zkvm-prover", package = "scroll-zkvm-prover"}
openvm = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false }
openvm-build = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false }
openvm-transpiler = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false }
openvm-custom-insn = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false }
openvm-rv32im-guest = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false }
openvm-circuit = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false }
openvm-native-circuit = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false }
openvm-native-compiler = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false }
openvm-native-recursion = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false }
openvm-native-transpiler = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false }
openvm-continuations = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false }
openvm-sdk = { git = "https://github.com/openvm-org/openvm.git", rev = "a0ae88f", default-features = false, features = ["parallel", "bench-metrics", "evm-prove"] }
openvm-stark-sdk = { git = "https://github.com/openvm-org/stark-backend.git", tag = "v1.0.1" }
sbv-core = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade", features = ["scroll"] }
sbv-primitives = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade", features = ["scroll"] }
sbv-kv = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade" }
sbv-trie = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade" }
sbv-utils = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade" }
metrics = "0.23.0"
metrics-util = "0.17"
metrics-tracing-context = "0.16.0"
alloy = { version = "0.11", default-features = false }
alloy-primitives = { version = "0.8", default-features = false }
# also use this to trigger "serde" feature for primitives
alloy-serde = { version = "0.8", default-features = false }
rkyv = "0.8"
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = { version = "1.0" }
serde_with = "3.11.0"
itertools = "0.14"
tiny-keccak = "2.0"
tracing = "0.1"
eyre = "0.6"
bincode_v1 = { version = "1.3", package = "bincode"}
snark-verifier-sdk = { version = "0.2.0", default-features = false, features = [
"loader_halo2",
"halo2-axiom",
"display",
] }
once_cell = "1.20"
base64 = "0.22"
#TODO: upgrade
vm-zstd = { git = "https://github.com/scroll-tech/rust-zstd-decompressor.git", tag = "v0.1.1" }
scroll-zkvm-circuit-input-types = { path = "common/types-rs"}
scroll-zkvm-verifier = { path = "zkvm-prover/verifier"}
scroll-zkvm-prover = { path = "zkvm-prover/prover"}
[patch.crates-io]
alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.18-euclid-upgrade" }
ruint = { git = "https://github.com/scroll-tech/uint.git", branch = "v1.12.3" }
tiny-keccak = { git = "https://github.com/scroll-tech/tiny-keccak", branch = "scroll-patch-v2.0.2-euclid-upgrade" }
[profile.maxperf]
inherits = "release"
lto = "fat"
codegen-units = 1

6
build/.cargo/config.toml Normal file
View File

@@ -0,0 +1,6 @@
[patch."https://github.com/scroll-tech/scroll.git"]
scroll-zkvm-circuit-input-types-base = { path = "../common/types-rs/base"}
scroll-zkvm-circuit-input-types-aggregation = { path = "../common/types-rs/aggregation"}
scroll-zkvm-circuit-input-types-chunk = { path = "../common/types-rs/chunk"}
scroll-zkvm-circuit-input-types-batch = { path = "../common/types-rs/batch"}
scroll-zkvm-circuit-input-types-bundle = { path = "../common/types-rs/bundle"}

View File

@@ -1,5 +1,5 @@
# Build libzkp dependency
FROM scrolltech/cuda-go-rust-builder:cuda-11.7.1-go-1.21-rust-nightly-2023-12-03 as chef
FROM scrolltech/go-rust-builder:go-1.22-rust-nightly-2023-12-03 as chef
WORKDIR app
FROM chef as planner
@@ -9,9 +9,7 @@ RUN cargo chef prepare --recipe-path recipe.json
FROM chef as zkp-builder
COPY ./common/libzkp/impl/rust-toolchain ./
COPY --from=planner /app/recipe.json recipe.json
# run scripts to get openvm-gpu
COPY ./build/dockerfiles/coordinator-api/plonky3-gpu /plonky3-gpu
COPY ./build/dockerfiles/coordinator-api/openvm-stark-gpu /openvm-stark-gpu
# run ./build/dockerfiles/coordinator-api/init-openvm.sh to get openvm-gpu
COPY ./build/dockerfiles/coordinator-api/openvm-gpu /openvm-gpu
COPY ./build/dockerfiles/coordinator-api/gitconfig /root/.gitconfig
COPY ./build/dockerfiles/coordinator-api/config.toml /root/.cargo/config.toml
@@ -22,7 +20,7 @@ RUN cargo build --release
# Download Go dependencies
FROM scrolltech/cuda-go-rust-builder:cuda-11.7.1-go-1.21-rust-nightly-2023-12-03 as base
FROM scrolltech/go-rust-builder:go-1.21-rust-nightly-2023-12-03 as base
WORKDIR /src
COPY go.work* ./
COPY ./rollup/go.* ./rollup/
@@ -42,7 +40,7 @@ COPY --from=zkp-builder /app/target/release/libzkp.so ./coordinator/internal/log
RUN cd ./coordinator && CGO_LDFLAGS="-Wl,--no-as-needed -ldl" make coordinator_api_skip_libzkp && mv ./build/bin/coordinator_api /bin/coordinator_api && mv internal/logic/verifier/lib /bin/
# Pull coordinator into a second stage deploy ubuntu container
FROM nvidia/cuda:11.7.1-runtime-ubuntu22.04
FROM ubuntu:20.04
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/src/coordinator/internal/logic/verifier/lib
ENV CGO_LDFLAGS="-Wl,--no-as-needed -ldl"
# ENV CHAIN_ID=534353

View File

@@ -1,17 +0,0 @@
#!/bin/bash
set -uex
PLONKY3_GPU_COMMIT=261b322 # v0.2.0
OPENVM_STARK_GPU_COMMIT=3082234 # PR#48
OPENVM_GPU_COMMIT=8094b4f # branch: patch-v1.2.0
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)
# checkout plonky3-gpu
cd $DIR/plonky3-gpu && git checkout ${PLONKY3_GPU_COMMIT}
# checkout openvm-stark-gpu
cd $DIR/openvm-stark-gpu && git checkout ${OPENVM_STARK_GPU_COMMIT}
# checkout openvm-gpu
cd $DIR/openvm-gpu && git checkout ${OPENVM_GPU_COMMIT}

View File

@@ -1,10 +0,0 @@
#!/bin/bash
set -uex
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)
# clone openvm-stark-gpu if not exists
if [ ! -d $DIR/openvm-stark-gpu ]; then
git clone git@github.com:scroll-tech/openvm-stark-gpu.git $DIR/openvm-stark-gpu
fi
cd $DIR/openvm-stark-gpu && git fetch --all --force

View File

@@ -1,10 +0,0 @@
#!/bin/bash
set -uex
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)
# clone plonky3-gpu if not exists
if [ ! -d $DIR/plonky3-gpu ]; then
git clone git@github.com:scroll-tech/plonky3-gpu.git $DIR/plonky3-gpu
fi
cd $DIR/plonky3-gpu && git fetch --all --force

View File

@@ -19,74 +19,6 @@ openvm-native-transpiler = { path = "/openvm-gpu/extensions/native/transpiler",
openvm-pairing-guest = { path = "/openvm-gpu/extensions/pairing/guest", default-features = false }
openvm-rv32im-guest = { path = "/openvm-gpu/extensions/rv32im/guest", default-features = false }
openvm-rv32im-transpiler = { path = "/openvm-gpu/extensions/rv32im/transpiler", default-features = false }
openvm-sdk = { path = "/openvm-gpu/crates/sdk", default-features = false, features = ["parallel", "bench-metrics", "evm-prove"] }
openvm-sdk = { path = "/openvm-gpu/crates/sdk", default-features = false, features = ["parallel", "bench-metrics"] }
openvm-sha256-guest = { path = "/openvm-gpu/extensions/sha256/guest", default-features = false }
openvm-transpiler = { path = "/openvm-gpu/crates/toolchain/transpiler", default-features = false }
# stark-backend
[patch."https://github.com/openvm-org/stark-backend.git"]
openvm-stark-backend = { path = "/openvm-stark-gpu/crates/stark-backend", features = ["gpu"] }
openvm-stark-sdk = { path = "/openvm-stark-gpu/crates/stark-sdk", features = ["gpu"] }
[patch."ssh://git@github.com/scroll-tech/openvm-stark-gpu.git"]
openvm-stark-backend = { path = "/openvm-stark-gpu/crates/stark-backend", features = ["gpu"] }
openvm-stark-sdk = { path = "/openvm-stark-gpu/crates/stark-sdk", features = ["gpu"] }
# plonky3
[patch."https://github.com/Plonky3/Plonky3.git"]
p3-air = { path = "/plonky3-gpu/air" }
p3-field = { path = "/plonky3-gpu/field" }
p3-commit = { path = "/plonky3-gpu/commit" }
p3-matrix = { path = "/plonky3-gpu/matrix" }
p3-baby-bear = { path = "/plonky3-gpu/baby-bear" }
p3-koala-bear = { path = "/plonky3-gpu/koala-bear" }
p3-util = { path = "/plonky3-gpu/util" }
p3-challenger = { path = "/plonky3-gpu/challenger" }
p3-dft = { path = "/plonky3-gpu/dft" }
p3-fri = { path = "/plonky3-gpu/fri" }
p3-goldilocks = { path = "/plonky3-gpu/goldilocks" }
p3-keccak = { path = "/plonky3-gpu/keccak" }
p3-keccak-air = { path = "/plonky3-gpu/keccak-air" }
p3-blake3 = { path = "/plonky3-gpu/blake3" }
p3-mds = { path = "/plonky3-gpu/mds" }
p3-monty-31 = { path = "/plonky3-gpu/monty-31" }
p3-merkle-tree = { path = "/plonky3-gpu/merkle-tree" }
p3-poseidon = { path = "/plonky3-gpu/poseidon" }
p3-poseidon2 = { path = "/plonky3-gpu/poseidon2" }
p3-poseidon2-air = { path = "/plonky3-gpu/poseidon2-air" }
p3-symmetric = { path = "/plonky3-gpu/symmetric" }
p3-uni-stark = { path = "/plonky3-gpu/uni-stark" }
p3-maybe-rayon = { path = "/plonky3-gpu/maybe-rayon" }
p3-bn254-fr = { path = "/plonky3-gpu/bn254-fr" }
# gpu crates
[patch."ssh://git@github.com/scroll-tech/plonky3-gpu.git"]
p3-gpu-base = { path = "/plonky3-gpu/gpu-base" }
p3-gpu-build = { path = "/plonky3-gpu/gpu-build" }
p3-gpu-field = { path = "/plonky3-gpu/gpu-field" }
p3-gpu-backend = { path = "/plonky3-gpu/gpu-backend" }
p3-gpu-module = { path = "/plonky3-gpu/gpu-module" }
p3-air = { path = "/plonky3-gpu/air" }
p3-field = { path = "/plonky3-gpu/field" }
p3-commit = { path = "/plonky3-gpu/commit" }
p3-matrix = { path = "/plonky3-gpu/matrix" }
p3-baby-bear = { path = "/plonky3-gpu/baby-bear" }
p3-koala-bear = { path = "/plonky3-gpu/koala-bear" }
p3-util = { path = "/plonky3-gpu/util" }
p3-challenger = { path = "/plonky3-gpu/challenger" }
p3-dft = { path = "/plonky3-gpu/dft" }
p3-fri = { path = "/plonky3-gpu/fri" }
p3-goldilocks = { path = "/plonky3-gpu/goldilocks" }
p3-keccak = { path = "/plonky3-gpu/keccak" }
p3-keccak-air = { path = "/plonky3-gpu/keccak-air" }
p3-blake3 = { path = "/plonky3-gpu/blake3" }
p3-mds = { path = "/plonky3-gpu/mds" }
p3-monty-31 = { path = "/plonky3-gpu/monty-31" }
p3-merkle-tree = { path = "/plonky3-gpu/merkle-tree" }
p3-poseidon = { path = "/plonky3-gpu/poseidon" }
p3-poseidon2 = { path = "/plonky3-gpu/poseidon2" }
p3-poseidon2-air = { path = "/plonky3-gpu/poseidon2-air" }
p3-symmetric = { path = "/plonky3-gpu/symmetric" }
p3-uni-stark = { path = "/plonky3-gpu/uni-stark" }
p3-maybe-rayon = { path = "/plonky3-gpu/maybe-rayon" }
p3-bn254-fr = { path = "/plonky3-gpu/bn254-fr" }

View File

@@ -1,10 +1,12 @@
#!/bin/bash
set -uex
OPENVM_GPU_COMMIT=dfa10b4
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)
# clone openvm-gpu if not exists
# checkout openvm-gpu
if [ ! -d $DIR/openvm-gpu ]; then
git clone git@github.com:scroll-tech/openvm-gpu.git $DIR/openvm-gpu
fi
cd $DIR/openvm-gpu && git fetch --all --force
cd $DIR/openvm-gpu && git fetch && git checkout ${OPENVM_GPU_COMMIT}

File diff suppressed because it is too large Load Diff

View File

@@ -7,60 +7,22 @@ edition = "2021"
[lib]
crate-type = ["cdylib"]
[patch.crates-io]
# patched add rkyv support & MSRV 1.77
alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.21" }
ruint = { git = "https://github.com/scroll-tech/uint.git", branch = "v1.12.3" }
tiny-keccak = { git = "https://github.com/scroll-tech/tiny-keccak", branch = "scroll-patch-v2.0.2-openvm-v1.0.0-rc.1" }
[dependencies]
euclid_prover = { git = "https://github.com/scroll-tech/zkvm-prover.git", tag = "v0.4.2", package = "scroll-zkvm-prover" }
euclid_verifier = { git = "https://github.com/scroll-tech/zkvm-prover.git", tag = "v0.4.2", package = "scroll-zkvm-verifier" }
scroll-zkvm-prover.workspace = true
scroll-zkvm-verifier.workspace = true
base64 = "0.13.0"
env_logger = "0.9.0"
env_logger = "0.11.0"
libc = "0.2"
log = "0.4"
once_cell = "1.19"
serde = "1.0"
base64.workspace = true
once_cell.workspace = true
serde.workspace = true
serde_derive = "1.0"
serde_json = "1.0.66"
anyhow = "1.0.86"
serde_json.workspace = true
anyhow = "1"
[profile.test]
opt-level = 3
[profile.release]
opt-level = 3
[patch."https://github.com/openvm-org/stark-backend.git"]
openvm-stark-backend = { git = "ssh://git@github.com/scroll-tech/openvm-stark-gpu.git", branch = "main", features = ["gpu"] }
openvm-stark-sdk = { git = "ssh://git@github.com/scroll-tech/openvm-stark-gpu.git", branch = "main", features = ["gpu"] }
[patch."https://github.com/Plonky3/Plonky3.git"]
p3-air = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-field = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-commit = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-matrix = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-baby-bear = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", features = [
"nightly-features",
], tag = "v0.2.0" }
p3-koala-bear = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-util = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-challenger = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-dft = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-fri = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-goldilocks = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-keccak = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-keccak-air = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-blake3 = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-mds = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-merkle-tree = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-monty-31 = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-poseidon = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-poseidon2 = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-poseidon2-air = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-symmetric = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-uni-stark = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
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
p3-bn254-fr = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }

View File

@@ -31,11 +31,13 @@ pub trait ProofVerifier {
#[derive(Debug, Serialize, Deserialize)]
pub struct CircuitConfig {
pub fork_name: String,
pub params_path: String,
pub assets_path: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct VerifierConfig {
pub low_version_circuit: CircuitConfig,
pub high_version_circuit: CircuitConfig,
}

View File

@@ -3,8 +3,8 @@ use super::{ProofVerifier, TaskType, VKDump};
use anyhow::Result;
use crate::utils::panic_catch;
use euclid_prover::{BatchProof, BundleProof, ChunkProof};
use euclid_verifier::verifier::{BatchVerifier, BundleVerifierEuclidV1, ChunkVerifier};
use scroll_zkvm_prover::{BatchProof, BundleProof, ChunkProof};
use scroll_zkvm_verifier::verifier::{BatchVerifier, BundleVerifierEuclidV1, ChunkVerifier};
use std::{fs::File, path::Path};
pub struct EuclidVerifier {
@@ -53,12 +53,13 @@ impl ProofVerifier for EuclidVerifier {
}
fn dump_vk(&self, file: &Path) {
use base64::{prelude::BASE64_STANDARD, Engine};
let f = File::create(file).expect("Failed to open file to dump VK");
let dump = VKDump {
chunk_vk: base64::encode(self.chunk_verifier.get_app_vk()),
batch_vk: base64::encode(self.batch_verifier.get_app_vk()),
bundle_vk: base64::encode(self.bundle_verifier.get_app_vk()),
chunk_vk: BASE64_STANDARD.encode(self.chunk_verifier.get_app_vk()),
batch_vk: BASE64_STANDARD.encode(self.batch_verifier.get_app_vk()),
bundle_vk: BASE64_STANDARD.encode(self.bundle_verifier.get_app_vk()),
};
serde_json::to_writer(f, &dump).expect("Failed to dump VK");
}

View File

@@ -3,8 +3,8 @@ use super::{ProofVerifier, TaskType, VKDump};
use anyhow::Result;
use crate::utils::panic_catch;
use euclid_prover::{BatchProof, BundleProof, ChunkProof};
use euclid_verifier::verifier::{BatchVerifier, BundleVerifierEuclidV2, ChunkVerifier};
use scroll_zkvm_prover::{BatchProof, BundleProof, ChunkProof};
use scroll_zkvm_verifier::verifier::{BatchVerifier, BundleVerifierEuclidV2, ChunkVerifier};
use std::{fs::File, path::Path};
pub struct EuclidV2Verifier {
@@ -53,12 +53,13 @@ impl ProofVerifier for EuclidV2Verifier {
}
fn dump_vk(&self, file: &Path) {
use base64::{prelude::BASE64_STANDARD, Engine};
let f = File::create(file).expect("Failed to open file to dump VK");
let dump = VKDump {
chunk_vk: base64::encode(self.chunk_verifier.get_app_vk()),
batch_vk: base64::encode(self.batch_verifier.get_app_vk()),
bundle_vk: base64::encode(self.bundle_verifier.get_app_vk()),
chunk_vk: BASE64_STANDARD.encode(self.chunk_verifier.get_app_vk()),
batch_vk: BASE64_STANDARD.encode(self.batch_verifier.get_app_vk()),
bundle_vk: BASE64_STANDARD.encode(self.bundle_verifier.get_app_vk()),
};
serde_json::to_writer(f, &dump).expect("Failed to dump VK");
}

View File

@@ -0,0 +1,17 @@
[package]
name = "scroll-zkvm-circuit-input-types"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
readme.workspace = true
repository.workspace = true
version = "0.2.0"
[dependencies]
types-base = { path = "base", package = "scroll-zkvm-circuit-input-types-base"}
types-agg = { path = "aggregation", package = "scroll-zkvm-circuit-input-types-aggregation"}
types-chunk = { path = "chunk", package = "scroll-zkvm-circuit-input-types-chunk"}
types-batch = { path = "batch", package = "scroll-zkvm-circuit-input-types-batch"}
types-bundle = { path = "bundle", package = "scroll-zkvm-circuit-input-types-bundle"}

24
common/types-rs/README.md Normal file
View File

@@ -0,0 +1,24 @@
# Input Types for circuits
A series of separated crates for the input types accepted by circuits as input.
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.
### Code structure
```
types-rs
├── base
├── circuit
├── aggregation
<following are layer-oriented crates>
├── chunk
├── batch
└── bundle
```

View File

@@ -0,0 +1,14 @@
[package]
name = "scroll-zkvm-circuit-input-types-aggregation"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
readme.workspace = true
repository.workspace = true
version = "0.2.0"
[dependencies]
alloy-primitives = { workspace = true, default-features = false, features = ["std", "map-hashbrown", "map-fxhash", "rkyv"] }
rkyv.workspace = true
serde.workspace = true

View File

@@ -0,0 +1,81 @@
/// Represents an openvm program commitments and public values.
#[derive(
Clone,
Debug,
rkyv::Archive,
rkyv::Deserialize,
rkyv::Serialize,
serde::Deserialize,
serde::Serialize,
)]
#[rkyv(derive(Debug))]
pub struct AggregationInput {
/// Public values.
pub public_values: Vec<u32>,
/// Represent the commitment needed to verify a root proof
pub commitment: ProgramCommitment,
}
/// Represent the commitment needed to verify a [`RootProof`].
#[derive(
Clone,
Debug,
Default,
rkyv::Archive,
rkyv::Deserialize,
rkyv::Serialize,
serde::Deserialize,
serde::Serialize,
)]
#[rkyv(derive(Debug))]
pub struct ProgramCommitment {
/// The commitment to the child program exe.
pub exe: [u32; 8],
/// The commitment to the child program leaf.
pub leaf: [u32; 8],
}
impl ProgramCommitment {
pub fn deserialize(commitment_bytes: &[u8]) -> Self {
// TODO: temporary skip deserialize if no vk is provided
if commitment_bytes.is_empty() {
return Default::default();
}
let archived_data =
rkyv::access::<ArchivedProgramCommitment, rkyv::rancor::BoxedError>(commitment_bytes)
.unwrap();
Self {
exe: archived_data.exe.map(|u32_le| u32_le.to_native()),
leaf: archived_data.leaf.map(|u32_le| u32_le.to_native()),
}
}
pub fn serialize(&self) -> Vec<u8> {
rkyv::to_bytes::<rkyv::rancor::BoxedError>(self)
.map(|v| v.to_vec())
.unwrap()
}
}
impl From<&ArchivedProgramCommitment> for ProgramCommitment {
fn from(archived: &ArchivedProgramCommitment) -> Self {
Self {
exe: archived.exe.map(|u32_le| u32_le.to_native()),
leaf: archived.leaf.map(|u32_le| u32_le.to_native()),
}
}
}
/// Number of public-input values, i.e. [u32; N].
///
/// Note that the actual value for each u32 is a byte.
pub const NUM_PUBLIC_VALUES: usize = 32;
/// Witness for an [`AggregationCircuit`][AggCircuit] that also carries proofs that are being
/// aggregated.
pub trait ProofCarryingWitness {
/// Get the root proofs from the witness.
fn get_proofs(&self) -> Vec<AggregationInput>;
}

View File

@@ -0,0 +1,21 @@
[package]
name = "scroll-zkvm-circuit-input-types-base"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
readme.workspace = true
repository.workspace = true
version = "0.2.0"
[dependencies]
alloy-primitives = { workspace = true, default-features = false, features = ["std", "map-hashbrown", "map-fxhash", "rkyv"] }
alloy-serde.workspace = true
rkyv.workspace = true
serde.workspace = true
itertools.workspace = true
tiny-keccak = { workspace = true }
sha3 = "0.10.8"
sha2 = "0.10.8"
[features]
default = []

View File

@@ -0,0 +1,2 @@
pub mod public_inputs;
pub mod utils;

View File

@@ -0,0 +1,81 @@
use alloy_primitives::B256;
pub mod batch;
pub mod bundle;
pub mod chunk;
/// Defines behaviour to be implemented by types representing the public-input values of a circuit.
pub trait PublicInputs {
/// Keccak-256 digest of the public inputs. The public-input hash are revealed as public values
/// via [`openvm::io::reveal`].
fn pi_hash(&self) -> B256;
/// Validation logic between public inputs of two contiguous instances.
fn validate(&self, prev_pi: &Self);
}
#[derive(
Default,
Debug,
Copy,
Clone,
PartialEq,
Eq,
rkyv::Archive,
rkyv::Deserialize,
rkyv::Serialize,
serde::Deserialize,
serde::Serialize,
)]
#[rkyv(derive(Debug))]
pub enum ForkName {
#[default]
EuclidV1,
EuclidV2,
}
impl From<&ArchivedForkName> for ForkName {
fn from(archived: &ArchivedForkName) -> Self {
match archived {
ArchivedForkName::EuclidV1 => ForkName::EuclidV1,
ArchivedForkName::EuclidV2 => ForkName::EuclidV2,
}
}
}
impl From<Option<&str>> for ForkName {
fn from(value: Option<&str>) -> Self {
match value {
None => Default::default(),
Some("euclidv1") => ForkName::EuclidV1,
Some("euclidv2") => ForkName::EuclidV2,
Some(s) => unreachable!("hardfork not accepted: {s}"),
}
}
}
impl From<&str> for ForkName {
fn from(value: &str) -> Self {
match value {
"euclidv1" => ForkName::EuclidV1,
"euclidv2" => ForkName::EuclidV2,
s => unreachable!("hardfork not accepted: {s}"),
}
}
}
/// helper trait to extend PublicInputs
pub trait MultiVersionPublicInputs {
fn pi_hash_by_fork(&self, fork_name: ForkName) -> B256;
fn validate(&self, prev_pi: &Self, fork_name: ForkName);
}
impl<T: MultiVersionPublicInputs> PublicInputs for (T, ForkName) {
fn pi_hash(&self) -> B256 {
self.0.pi_hash_by_fork(self.1)
}
fn validate(&self, prev_pi: &Self) {
assert_eq!(self.1, prev_pi.1);
self.0.validate(&prev_pi.0, self.1)
}
}

View File

@@ -0,0 +1,144 @@
use alloy_primitives::B256;
use crate::{
public_inputs::{ForkName, MultiVersionPublicInputs},
utils::keccak256,
};
/// Represents public-input values for a batch.
#[derive(
Clone,
Debug,
rkyv::Archive,
rkyv::Deserialize,
rkyv::Serialize,
serde::Deserialize,
serde::Serialize,
)]
#[rkyv(derive(Debug))]
pub struct BatchInfo {
/// The state root before applying the batch.
#[rkyv()]
pub parent_state_root: B256,
/// The batch hash of the parent batch.
#[rkyv()]
pub parent_batch_hash: B256,
/// The state root after applying txs in the batch.
#[rkyv()]
pub state_root: B256,
/// The batch header hash of the batch.
#[rkyv()]
pub batch_hash: B256,
/// The EIP-155 chain ID of all txs in the batch.
#[rkyv()]
pub chain_id: u64,
/// The withdraw root of the last block in the last chunk in the batch.
#[rkyv()]
pub withdraw_root: B256,
/// The L1 msg queue hash at the end of the previous batch.
#[rkyv()]
pub prev_msg_queue_hash: B256,
/// The L1 msg queue hash at the end of the current batch.
#[rkyv()]
pub post_msg_queue_hash: B256,
}
impl From<&ArchivedBatchInfo> for BatchInfo {
fn from(archived: &ArchivedBatchInfo) -> Self {
Self {
parent_state_root: archived.parent_state_root.into(),
parent_batch_hash: archived.parent_batch_hash.into(),
state_root: archived.state_root.into(),
batch_hash: archived.batch_hash.into(),
chain_id: archived.chain_id.into(),
withdraw_root: archived.withdraw_root.into(),
prev_msg_queue_hash: archived.prev_msg_queue_hash.into(),
post_msg_queue_hash: archived.post_msg_queue_hash.into(),
}
}
}
impl BatchInfo {
/// Public input hash for a batch (euclidv1 or da-codec@v6) is defined as
///
/// keccak(
/// parent state root ||
/// parent batch hash ||
/// state root ||
/// batch hash ||
/// chain id ||
/// withdraw root ||
/// )
fn pi_hash_euclidv1(&self) -> B256 {
keccak256(
std::iter::empty()
.chain(self.parent_state_root.as_slice())
.chain(self.parent_batch_hash.as_slice())
.chain(self.state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.withdraw_root.as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
}
/// Public input hash for a batch (euclidv2 or da-codec@v7) is defined as
///
/// keccak(
/// parent state root ||
/// parent batch hash ||
/// state root ||
/// batch hash ||
/// chain id ||
/// withdraw root ||
/// prev msg queue hash ||
/// post msg queue hash
/// )
fn pi_hash_euclidv2(&self) -> B256 {
keccak256(
std::iter::empty()
.chain(self.parent_state_root.as_slice())
.chain(self.parent_batch_hash.as_slice())
.chain(self.state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.withdraw_root.as_slice())
.chain(self.prev_msg_queue_hash.as_slice())
.chain(self.post_msg_queue_hash.as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
}
}
pub type VersionedBatchInfo = (BatchInfo, ForkName);
impl MultiVersionPublicInputs for BatchInfo {
fn pi_hash_by_fork(&self, fork_name: ForkName) -> B256 {
match fork_name {
ForkName::EuclidV1 => self.pi_hash_euclidv1(),
ForkName::EuclidV2 => self.pi_hash_euclidv2(),
}
}
/// Validate public inputs between 2 contiguous batches.
///
/// - chain id MUST match
/// - state roots MUST be chained
/// - batch hashes MUST be chained
/// - L1 msg queue hashes MUST be chained
fn validate(&self, prev_pi: &Self, fork_name: ForkName) {
assert_eq!(self.chain_id, prev_pi.chain_id);
assert_eq!(self.parent_state_root, prev_pi.state_root);
assert_eq!(self.parent_batch_hash, prev_pi.batch_hash);
assert_eq!(self.prev_msg_queue_hash, prev_pi.post_msg_queue_hash);
if fork_name == ForkName::EuclidV1 {
assert_eq!(self.prev_msg_queue_hash, B256::ZERO);
assert_eq!(prev_pi.prev_msg_queue_hash, B256::ZERO);
assert_eq!(self.post_msg_queue_hash, B256::ZERO);
assert_eq!(prev_pi.post_msg_queue_hash, B256::ZERO);
}
}
}

View File

@@ -0,0 +1,149 @@
use alloy_primitives::B256;
use crate::{
public_inputs::{ForkName, MultiVersionPublicInputs, PublicInputs},
utils::keccak256,
};
/// Represents fields required to compute the public-inputs digest of a bundle.
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct BundleInfo {
/// The EIP-155 chain ID of all txs in the bundle.
pub chain_id: u64,
/// The L1 msg queue hash at the end of the last batch in the bundle.
/// Not a phase 1 field so we make it omitable
#[serde(default)]
pub msg_queue_hash: B256,
/// The number of batches bundled together in the bundle.
pub num_batches: u32,
/// The last finalized on-chain state root.
pub prev_state_root: B256,
/// The last finalized on-chain batch hash.
pub prev_batch_hash: B256,
/// The state root after applying every batch in the bundle.
///
/// Upon verification of the EVM-verifiable bundle proof, this state root will be finalized
/// on-chain.
pub post_state_root: B256,
/// The batch hash of the last batch in the bundle.
///
/// Upon verification of the EVM-verifiable bundle proof, this batch hash will be finalized
/// on-chain.
pub batch_hash: B256,
/// The withdrawals root at the last block in the last chunk in the last batch in the bundle.
pub withdraw_root: B256,
}
impl BundleInfo {
/// Public input hash for a bundle (euclidv1 or da-codec@v6) is defined as
///
/// keccak(
/// chain id ||
/// num batches ||
/// prev state root ||
/// prev batch hash ||
/// post state root ||
/// batch hash ||
/// withdraw root
/// )
pub fn pi_hash_euclidv1(&self) -> B256 {
keccak256(
std::iter::empty()
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.num_batches.to_be_bytes().as_slice())
.chain(self.prev_state_root.as_slice())
.chain(self.prev_batch_hash.as_slice())
.chain(self.post_state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.withdraw_root.as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
}
/// Public input hash for a bundle (euclidv2 or da-codec@v7) is defined as
///
/// keccak(
/// chain id ||
/// msg_queue_hash ||
/// num batches ||
/// prev state root ||
/// prev batch hash ||
/// post state root ||
/// batch hash ||
/// withdraw root
/// )
pub fn pi_hash_euclidv2(&self) -> B256 {
keccak256(
std::iter::empty()
.chain(self.chain_id.to_be_bytes().as_slice())
.chain(self.msg_queue_hash.as_slice())
.chain(self.num_batches.to_be_bytes().as_slice())
.chain(self.prev_state_root.as_slice())
.chain(self.prev_batch_hash.as_slice())
.chain(self.post_state_root.as_slice())
.chain(self.batch_hash.as_slice())
.chain(self.withdraw_root.as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
}
pub fn pi_hash(&self, fork_name: ForkName) -> B256 {
match fork_name {
ForkName::EuclidV1 => self.pi_hash_euclidv1(),
ForkName::EuclidV2 => self.pi_hash_euclidv2(),
}
}
}
impl MultiVersionPublicInputs for BundleInfo {
fn pi_hash_by_fork(&self, fork_name: ForkName) -> B256 {
match fork_name {
ForkName::EuclidV1 => self.pi_hash_euclidv1(),
ForkName::EuclidV2 => self.pi_hash_euclidv2(),
}
}
fn validate(&self, _prev_pi: &Self, _fork_name: ForkName) {
unreachable!("bundle is the last layer and is not aggregated by any other circuit");
}
}
#[derive(Clone, Debug)]
pub struct BundleInfoV1(pub BundleInfo);
#[derive(Clone, Debug)]
pub struct BundleInfoV2(pub BundleInfo);
impl From<BundleInfo> for BundleInfoV1 {
fn from(value: BundleInfo) -> Self {
Self(value)
}
}
impl From<BundleInfo> for BundleInfoV2 {
fn from(value: BundleInfo) -> Self {
Self(value)
}
}
impl PublicInputs for BundleInfoV1 {
fn pi_hash(&self) -> B256 {
self.0.pi_hash_euclidv1()
}
fn validate(&self, _prev_pi: &Self) {
unreachable!("bundle is the last layer and is not aggregated by any other circuit");
}
}
impl PublicInputs for BundleInfoV2 {
fn pi_hash(&self) -> B256 {
self.0.pi_hash_euclidv2()
}
fn validate(&self, _prev_pi: &Self) {
unreachable!("bundle is the last layer and is not aggregated by any other circuit");
}
}

View File

@@ -0,0 +1,248 @@
use alloy_primitives::{B256, U256};
use crate::{
public_inputs::{ForkName, MultiVersionPublicInputs},
utils::keccak256,
};
/// Number of bytes used to serialise [`BlockContextV2`].
pub const SIZE_BLOCK_CTX: usize = 52;
/// Represents the version 2 of block context.
///
/// The difference between v2 and v1 is that the block number field has been removed since v2.
#[derive(
Debug,
Clone,
PartialEq,
rkyv::Archive,
rkyv::Deserialize,
rkyv::Serialize,
serde::Deserialize,
serde::Serialize,
)]
#[rkyv(derive(Debug))]
pub struct BlockContextV2 {
/// The timestamp of the block.
pub timestamp: u64,
/// The base fee of the block.
pub base_fee: U256,
/// The gas limit of the block.
pub gas_limit: u64,
/// The number of transactions in the block, including both L1 msg txs as well as L2 txs.
pub num_txs: u16,
/// The number of L1 msg txs in the block.
pub num_l1_msgs: u16,
}
impl From<&ArchivedBlockContextV2> for BlockContextV2 {
fn from(archived: &ArchivedBlockContextV2) -> Self {
Self {
timestamp: archived.timestamp.into(),
base_fee: archived.base_fee.into(),
gas_limit: archived.gas_limit.into(),
num_txs: archived.num_txs.into(),
num_l1_msgs: archived.num_l1_msgs.into(),
}
}
}
impl From<&[u8]> for BlockContextV2 {
fn from(bytes: &[u8]) -> Self {
assert_eq!(bytes.len(), SIZE_BLOCK_CTX);
let timestamp = u64::from_be_bytes(bytes[0..8].try_into().expect("should not fail"));
let base_fee = U256::from_be_slice(&bytes[8..40]);
let gas_limit = u64::from_be_bytes(bytes[40..48].try_into().expect("should not fail"));
let num_txs = u16::from_be_bytes(bytes[48..50].try_into().expect("should not fail"));
let num_l1_msgs = u16::from_be_bytes(bytes[50..52].try_into().expect("should not fail"));
Self {
timestamp,
base_fee,
gas_limit,
num_txs,
num_l1_msgs,
}
}
}
impl BlockContextV2 {
/// Serialize the block context in packed form.
pub fn to_bytes(&self) -> Vec<u8> {
std::iter::empty()
.chain(self.timestamp.to_be_bytes())
.chain(self.base_fee.to_be_bytes::<32>())
.chain(self.gas_limit.to_be_bytes())
.chain(self.num_txs.to_be_bytes())
.chain(self.num_l1_msgs.to_be_bytes())
.collect()
}
}
/// Represents header-like information for the chunk.
#[derive(
Debug,
Clone,
rkyv::Archive,
rkyv::Deserialize,
rkyv::Serialize,
serde::Deserialize,
serde::Serialize,
)]
#[rkyv(derive(Debug))]
pub struct ChunkInfo {
/// The EIP-155 chain ID for all txs in the chunk.
#[rkyv()]
pub chain_id: u64,
/// The state root before applying the chunk.
#[rkyv()]
pub prev_state_root: B256,
/// The state root after applying the chunk.
#[rkyv()]
pub post_state_root: B256,
/// The withdrawals root after applying the chunk.
#[rkyv()]
pub withdraw_root: B256,
/// Digest of L1 message txs force included in the chunk.
/// It is a legacy field and can be omitted in new defination
#[rkyv()]
#[serde(default)]
pub data_hash: B256,
/// Digest of L2 tx data flattened over all L2 txs in the chunk.
#[rkyv()]
pub tx_data_digest: B256,
/// The L1 msg queue hash at the end of the previous chunk.
#[rkyv()]
pub prev_msg_queue_hash: B256,
/// The L1 msg queue hash at the end of the current chunk.
#[rkyv()]
pub post_msg_queue_hash: B256,
/// The length of rlp encoded L2 tx bytes flattened over all L2 txs in the chunk.
#[rkyv()]
pub tx_data_length: u64,
/// The block number of the first block in the chunk.
#[rkyv()]
pub initial_block_number: u64,
/// The block contexts of the blocks in the chunk.
#[rkyv()]
pub block_ctxs: Vec<BlockContextV2>,
}
impl ChunkInfo {
/// Public input hash for a given chunk (euclidv1 or da-codec@v6) is defined as
///
/// keccak(
/// chain id ||
/// prev state root ||
/// post state root ||
/// withdraw root ||
/// chunk data hash ||
/// tx data hash
/// )
pub fn pi_hash_euclidv1(&self) -> B256 {
keccak256(
std::iter::empty()
.chain(&self.chain_id.to_be_bytes())
.chain(self.prev_state_root.as_slice())
.chain(self.post_state_root.as_slice())
.chain(self.withdraw_root.as_slice())
.chain(self.data_hash.as_slice())
.chain(self.tx_data_digest.as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
}
/// Public input hash for a given chunk (euclidv2 or da-codec@v7) is defined as
///
/// keccak(
/// chain id ||
/// prev state root ||
/// post state root ||
/// withdraw root ||
/// tx data digest ||
/// prev msg queue hash ||
/// post msg queue hash ||
/// initial block number ||
/// block_ctx for block_ctx in block_ctxs
/// )
pub fn pi_hash_euclidv2(&self) -> B256 {
keccak256(
std::iter::empty()
.chain(&self.chain_id.to_be_bytes())
.chain(self.prev_state_root.as_slice())
.chain(self.post_state_root.as_slice())
.chain(self.withdraw_root.as_slice())
.chain(self.tx_data_digest.as_slice())
.chain(self.prev_msg_queue_hash.as_slice())
.chain(self.post_msg_queue_hash.as_slice())
.chain(&self.initial_block_number.to_be_bytes())
.chain(
self.block_ctxs
.iter()
.flat_map(|block_ctx| block_ctx.to_bytes())
.collect::<Vec<u8>>()
.as_slice(),
)
.cloned()
.collect::<Vec<u8>>(),
)
}
}
impl From<&ArchivedChunkInfo> for ChunkInfo {
fn from(archived: &ArchivedChunkInfo) -> Self {
Self {
chain_id: archived.chain_id.into(),
prev_state_root: archived.prev_state_root.into(),
post_state_root: archived.post_state_root.into(),
withdraw_root: archived.withdraw_root.into(),
data_hash: archived.data_hash.into(),
tx_data_digest: archived.tx_data_digest.into(),
prev_msg_queue_hash: archived.prev_msg_queue_hash.into(),
post_msg_queue_hash: archived.post_msg_queue_hash.into(),
tx_data_length: archived.tx_data_length.into(),
initial_block_number: archived.initial_block_number.into(),
block_ctxs: archived
.block_ctxs
.iter()
.map(BlockContextV2::from)
.collect(),
}
}
}
pub type VersionedChunkInfo = (ChunkInfo, ForkName);
impl MultiVersionPublicInputs for ChunkInfo {
/// Compute the public input hash for the chunk.
fn pi_hash_by_fork(&self, fork_name: ForkName) -> B256 {
match fork_name {
ForkName::EuclidV1 => {
assert_ne!(self.data_hash, B256::ZERO, "v6 must has valid data hash");
self.pi_hash_euclidv1()
}
ForkName::EuclidV2 => self.pi_hash_euclidv2(),
}
}
/// Validate public inputs between 2 contiguous chunks.
///
/// - chain id MUST match
/// - state roots MUST be chained
/// - L1 msg queue hash MUST be chained
fn validate(&self, prev_pi: &Self, fork_name: ForkName) {
assert_eq!(self.chain_id, prev_pi.chain_id);
assert_eq!(self.prev_state_root, prev_pi.post_state_root);
assert_eq!(self.prev_msg_queue_hash, prev_pi.post_msg_queue_hash);
// message queue hash is used only after euclidv2 (da-codec@v7)
if fork_name == ForkName::EuclidV1 {
assert_eq!(self.prev_msg_queue_hash, B256::ZERO);
assert_eq!(prev_pi.prev_msg_queue_hash, B256::ZERO);
assert_eq!(self.post_msg_queue_hash, B256::ZERO);
assert_eq!(prev_pi.post_msg_queue_hash, B256::ZERO);
}
}
}

View File

@@ -0,0 +1,35 @@
use alloy_primitives::B256;
use tiny_keccak::{Hasher, Keccak};
/// From the utility of ether-rs
///
/// Computes the Keccak-256 hash of input bytes.
///
/// Note that strings are interpreted as UTF-8 bytes,
pub fn keccak256<T: AsRef<[u8]>>(bytes: T) -> B256 {
let mut output = [0u8; 32];
let mut hasher = Keccak::v256();
hasher.update(bytes.as_ref());
hasher.finalize(&mut output);
B256::from(output)
}
pub fn keccak256_rv32<T: AsRef<[u8]>>(bytes: T) -> B256 {
use sha3::{Digest, Keccak256};
let mut output = [0u8; 32];
let mut hasher = Keccak256::new();
hasher.update(bytes.as_ref());
output.copy_from_slice(hasher.finalize().as_ref());
B256::from(output)
}
pub fn sha256_rv32<T: AsRef<[u8]>>(bytes: T) -> B256 {
use sha2::{Digest, Sha256};
let mut output = [0u8; 32];
let mut hasher = Sha256::new();
hasher.update(bytes.as_ref());
output.copy_from_slice(hasher.finalize().as_ref());
B256::from(output)
}

View File

@@ -0,0 +1,2 @@
mod hash;
pub use hash::{keccak256, keccak256_rv32, sha256_rv32};

View File

@@ -0,0 +1,21 @@
[package]
name = "scroll-zkvm-circuit-input-types-batch"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
readme.workspace = true
repository.workspace = true
version = "0.2.0"
[dependencies]
alloy-primitives = { workspace = true, default-features = false, features = ["std", "map-hashbrown", "map-fxhash", "rkyv"] }
rkyv.workspace = true
serde.workspace = true
itertools.workspace = true
vm-zstd = { workspace = true }
types-base = { path = "../base", package = "scroll-zkvm-circuit-input-types-base"}
types-agg = { path = "../aggregation", package = "scroll-zkvm-circuit-input-types-aggregation"}
[features]
default = []

View File

@@ -0,0 +1,30 @@
use alloy_primitives::B256;
pub mod v6;
pub mod v7;
pub trait BatchHeader {
/// The DA-codec version for the batch header.
fn version(&self) -> u8;
/// The incremental index of the batch.
fn index(&self) -> u64;
/// The batch header digest of the parent batch.
fn parent_batch_hash(&self) -> B256;
/// The batch header digest.
fn batch_hash(&self) -> B256;
}
/// Reference header indicate the version of batch header base on which batch hash
/// should be calculated.
#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
#[rkyv(derive(Debug))]
pub enum ReferenceHeader {
/// Represents DA-codec v6.
V6(v6::BatchHeaderV6),
/// Represents DA-codec v7.
V7(v7::BatchHeaderV7),
}

View File

@@ -0,0 +1,151 @@
use super::BatchHeader;
use alloy_primitives::B256;
use types_base::utils::keccak256;
/// Represents the header summarising the batch of chunks as per DA-codec v6.
#[derive(
Clone,
Copy,
Debug,
Default,
rkyv::Archive,
rkyv::Deserialize,
rkyv::Serialize,
serde::Deserialize,
serde::Serialize,
)]
#[rkyv(derive(Debug))]
pub struct BatchHeaderV6 {
/// The DA-codec version for the batch.
#[rkyv()]
pub version: u8,
/// The index of the batch
#[rkyv()]
pub batch_index: u64,
/// Number of L1 messages popped in the batch
#[rkyv()]
pub l1_message_popped: u64,
/// Number of total L1 messages popped after the batch
#[rkyv()]
pub total_l1_message_popped: u64,
/// The parent batch hash
#[rkyv()]
pub parent_batch_hash: B256,
/// The timestamp of the last block in this batch
#[rkyv()]
pub last_block_timestamp: u64,
/// The data hash of the batch
#[rkyv()]
pub data_hash: B256,
/// The versioned hash of the blob with this batch's data
#[rkyv()]
pub blob_versioned_hash: B256,
/// The blob data proof: z (32), y (32)
#[rkyv()]
pub blob_data_proof: [B256; 2],
}
impl BatchHeader for BatchHeaderV6 {
fn version(&self) -> u8 {
self.version
}
fn index(&self) -> u64 {
self.batch_index
}
fn parent_batch_hash(&self) -> B256 {
self.parent_batch_hash
}
/// Batch hash as per DA-codec v6:
///
/// keccak(
/// version ||
/// batch index ||
/// l1 message popped ||
/// total l1 message popped ||
/// batch data hash ||
/// versioned hash ||
/// parent batch hash ||
/// last block timestamp ||
/// z ||
/// y
/// )
fn batch_hash(&self) -> B256 {
keccak256(
std::iter::empty()
.chain(vec![self.version].as_slice())
.chain(self.batch_index.to_be_bytes().as_slice())
.chain(self.l1_message_popped.to_be_bytes().as_slice())
.chain(self.total_l1_message_popped.to_be_bytes().as_slice())
.chain(self.data_hash.as_slice())
.chain(self.blob_versioned_hash.as_slice())
.chain(self.parent_batch_hash.as_slice())
.chain(self.last_block_timestamp.to_be_bytes().as_slice())
.chain(self.blob_data_proof[0].as_slice())
.chain(self.blob_data_proof[1].as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
}
}
impl BatchHeader for ArchivedBatchHeaderV6 {
fn version(&self) -> u8 {
self.version
}
fn index(&self) -> u64 {
self.batch_index.into()
}
fn parent_batch_hash(&self) -> B256 {
self.parent_batch_hash.into()
}
fn batch_hash(&self) -> B256 {
let batch_index: u64 = self.batch_index.into();
let l1_message_popped: u64 = self.l1_message_popped.into();
let total_l1_message_popped: u64 = self.total_l1_message_popped.into();
let data_hash: B256 = self.data_hash.into();
let blob_versioned_hash: B256 = self.blob_versioned_hash.into();
let parent_batch_hash: B256 = self.parent_batch_hash.into();
let last_block_timestamp: u64 = self.last_block_timestamp.into();
let blob_data_proof: [B256; 2] = self.blob_data_proof.map(|h| h.into());
keccak256(
std::iter::empty()
.chain(vec![self.version].as_slice())
.chain(batch_index.to_be_bytes().as_slice())
.chain(l1_message_popped.to_be_bytes().as_slice())
.chain(total_l1_message_popped.to_be_bytes().as_slice())
.chain(data_hash.as_slice())
.chain(blob_versioned_hash.as_slice())
.chain(parent_batch_hash.as_slice())
.chain(last_block_timestamp.to_be_bytes().as_slice())
.chain(blob_data_proof[0].as_slice())
.chain(blob_data_proof[1].as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
}
}
impl From<&ArchivedBatchHeaderV6> for BatchHeaderV6 {
fn from(archived: &ArchivedBatchHeaderV6) -> Self {
Self {
version: archived.version,
batch_index: archived.batch_index.into(),
l1_message_popped: archived.l1_message_popped.into(),
total_l1_message_popped: archived.total_l1_message_popped.into(),
parent_batch_hash: archived.parent_batch_hash.into(),
last_block_timestamp: archived.last_block_timestamp.into(),
data_hash: archived.data_hash.into(),
blob_versioned_hash: archived.blob_versioned_hash.into(),
blob_data_proof: [
archived.blob_data_proof[0].into(),
archived.blob_data_proof[1].into(),
],
}
}
}

View File

@@ -0,0 +1,106 @@
use alloy_primitives::B256;
use super::BatchHeader;
use types_base::utils::keccak256;
/// Represents the header summarising the batch of chunks as per DA-codec v7.
#[derive(
Clone,
Copy,
Debug,
Default,
rkyv::Archive,
rkyv::Deserialize,
rkyv::Serialize,
serde::Deserialize,
serde::Serialize,
)]
#[rkyv(derive(Debug))]
pub struct BatchHeaderV7 {
/// The DA-codec version for the batch.
#[rkyv()]
pub version: u8,
/// The index of the batch
#[rkyv()]
pub batch_index: u64,
/// The parent batch hash
#[rkyv()]
pub parent_batch_hash: B256,
/// The versioned hash of the blob with this batch's data
#[rkyv()]
pub blob_versioned_hash: B256,
}
impl BatchHeader for BatchHeaderV7 {
fn version(&self) -> u8 {
self.version
}
fn index(&self) -> u64 {
self.batch_index
}
fn parent_batch_hash(&self) -> B256 {
self.parent_batch_hash
}
/// Batch hash as per DA-codec v7:
///
/// keccak(
/// version ||
/// batch index ||
/// versioned hash ||
/// parent batch hash
/// )
fn batch_hash(&self) -> B256 {
keccak256(
std::iter::empty()
.chain(vec![self.version].as_slice())
.chain(self.batch_index.to_be_bytes().as_slice())
.chain(self.blob_versioned_hash.as_slice())
.chain(self.parent_batch_hash.as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
}
}
impl BatchHeader for ArchivedBatchHeaderV7 {
fn version(&self) -> u8 {
self.version
}
fn index(&self) -> u64 {
self.batch_index.into()
}
fn parent_batch_hash(&self) -> B256 {
self.parent_batch_hash.into()
}
fn batch_hash(&self) -> B256 {
let batch_index: u64 = self.batch_index.into();
let blob_versioned_hash: B256 = self.blob_versioned_hash.into();
let parent_batch_hash: B256 = self.parent_batch_hash.into();
keccak256(
std::iter::empty()
.chain(vec![self.version].as_slice())
.chain(batch_index.to_be_bytes().as_slice())
.chain(blob_versioned_hash.as_slice())
.chain(parent_batch_hash.as_slice())
.cloned()
.collect::<Vec<u8>>(),
)
}
}
impl From<&ArchivedBatchHeaderV7> for BatchHeaderV7 {
fn from(archived: &ArchivedBatchHeaderV7) -> Self {
Self {
version: archived.version,
batch_index: archived.batch_index.into(),
parent_batch_hash: archived.parent_batch_hash.into(),
blob_versioned_hash: archived.blob_versioned_hash.into(),
}
}
}

View File

@@ -0,0 +1,17 @@
mod header;
pub use header::{
ArchivedReferenceHeader, BatchHeader, ReferenceHeader,
v6::{ArchivedBatchHeaderV6, BatchHeaderV6},
v7::{ArchivedBatchHeaderV7, BatchHeaderV7},
};
mod payload;
pub use payload::{
v6::{EnvelopeV6, PayloadV6},
v7::{EnvelopeV7, PayloadV7},
};
pub use payload::{BLOB_WIDTH, N_BLOB_BYTES, N_DATA_BYTES_PER_COEFFICIENT};
mod witness;
pub use witness::{ArchivedBatchWitness, BatchWitness, Bytes48, PointEvalWitness};

View File

@@ -0,0 +1,15 @@
pub mod v6;
pub mod v7;
/// The number data bytes we pack each BLS12-381 scalar into. The most-significant byte is 0.
pub const N_DATA_BYTES_PER_COEFFICIENT: usize = 31;
/// The number of BLS12-381 scalar fields that effectively represent an EIP-4844 blob.
pub const BLOB_WIDTH: usize = 4096;
/// The effective (reduced) number of bytes we can use within a blob.
///
/// EIP-4844 requires that each 32-bytes chunk of bytes represent a BLS12-381 scalar field element
/// in its canonical form. As a result, we set the most-significant byte in each such chunk to 0.
/// This allows us to use only up to 31 bytes in each such chunk, hence the reduced capacity.
pub const N_BLOB_BYTES: usize = BLOB_WIDTH * N_DATA_BYTES_PER_COEFFICIENT;

View File

@@ -0,0 +1,212 @@
use alloy_primitives::B256;
use itertools::Itertools;
use crate::BatchHeaderV6;
use types_base::{public_inputs::chunk::ChunkInfo, utils::keccak256};
/// The default max chunks for v6 payload
pub const N_MAX_CHUNKS: usize = 45;
/// The number of bytes to encode number of chunks in a batch.
const N_BYTES_NUM_CHUNKS: usize = 2;
/// The number of rows to encode chunk size (u32).
const N_BYTES_CHUNK_SIZE: usize = 4;
impl From<&[u8]> for EnvelopeV6 {
fn from(blob_bytes: &[u8]) -> Self {
let is_encoded = blob_bytes[0] & 1 == 1;
Self {
is_encoded,
envelope_bytes: if blob_bytes[0] & 1 == 1 {
vm_zstd::process(&blob_bytes[1..]).unwrap().decoded_data
} else {
Vec::from(&blob_bytes[1..])
},
}
}
}
#[derive(Debug, Clone)]
pub struct EnvelopeV6 {
/// The original envelope bytes supplied.
///
/// Caching just for re-use later in challenge digest computation.
pub envelope_bytes: Vec<u8>,
/// If the enveloped bytes is encoded (compressed) in envelop
pub is_encoded: bool,
}
impl EnvelopeV6 {
/// Parse payload bytes and obtain challenge digest
pub fn challenge_digest(&self, versioned_hash: B256) -> B256 {
let payload = Payload::from(self);
payload.get_challenge_digest(versioned_hash)
}
}
impl From<&EnvelopeV6> for Payload {
fn from(envelope: &EnvelopeV6) -> Self {
Self::from_payload(&envelope.envelope_bytes)
}
}
/// Payload that describes a batch.
#[derive(Clone, Debug, Default)]
pub struct Payload {
/// Metadata that encodes the sizes of every chunk in the batch.
pub metadata_digest: B256,
/// The Keccak digests of transaction bytes for every chunk in the batch.
///
/// The `chunk_data_digest` is a part of the chunk-circuit's public input and hence used to
/// verify that the transaction bytes included in the chunk-circuit indeed match the
/// transaction bytes made available in the batch.
pub chunk_data_digests: Vec<B256>,
}
pub type PayloadV6 = Payload;
impl Payload {
/// For raw payload data (read from decompressed enveloped data), which is raw batch bytes
/// with metadata, this function segments the byte stream into chunk segments.
///
/// This method is used INSIDE OF zkvm since we can not generate (compress) batch data within
/// the vm program
///
/// The structure of batch bytes is as follows:
///
/// | Byte Index | Size | Hint |
/// |--------------------------------------------------------------|-------------------------------|-------------------------------------|
/// | 0 | N_BYTES_NUM_CHUNKS | Number of chunks |
/// | N_BYTES_NUM_CHUNKS | N_BYTES_CHUNK_SIZE | Size of chunks[0] |
/// | N_BYTES_NUM_CHUNKS + N_BYTES_CHUNK_SIZE | N_BYTES_CHUNK_SIZE | Size of chunks[1] |
/// | N_BYTES_NUM_CHUNKS + (i * N_BYTES_CHUNK_SIZE) | N_BYTES_CHUNK_SIZE | Size of chunks[i] |
/// | N_BYTES_NUM_CHUNKS + ((N_MAX_CHUNKS-1) * N_BYTES_CHUNK_SIZE) | N_BYTES_CHUNK_SIZE | Size of chunks[N_MAX_CHUNKS-1] |
/// | N_BYTES_NUM_CHUNKS + (N_MAX_CHUNKS * N_BYTES_CHUNK_SIZE) | Size of chunks[0] | L2 tx bytes of chunks[0] |
/// | "" + Size_of_chunks[0] | Size of chunks[1] | L2 tx bytes of chunks[1] |
/// | "" + Size_of_chunks[i-1] | Size of chunks[i] | L2 tx bytes of chunks[i] |
/// | "" + Size_of_chunks[Num_chunks-1] | Size of chunks[Num_chunks-1] | L2 tx bytes of chunks[Num_chunks-1] |
pub fn from_payload(batch_bytes_with_metadata: &[u8]) -> Self {
// Get the metadata bytes and metadata digest.
let n_bytes_metadata = Self::n_bytes_metadata();
let metadata_bytes = &batch_bytes_with_metadata[..n_bytes_metadata];
let metadata_digest = keccak256(metadata_bytes);
// The remaining bytes represent the chunk data (L2 tx bytes) segmented as chunks.
let batch_bytes = &batch_bytes_with_metadata[n_bytes_metadata..];
// The number of chunks in the batch.
let valid_chunks = metadata_bytes[..N_BYTES_NUM_CHUNKS]
.iter()
.fold(0usize, |acc, &d| acc * 256usize + d as usize);
// The size of each chunk in the batch.
let chunk_sizes = metadata_bytes[N_BYTES_NUM_CHUNKS..]
.iter()
.chunks(N_BYTES_CHUNK_SIZE)
.into_iter()
.map(|bytes| bytes.fold(0usize, |acc, &d| acc * 256usize + d as usize))
.collect::<Vec<usize>>();
// For every unused chunk, the chunk size should be set to 0.
for &unused_chunk_size in chunk_sizes.iter().skip(valid_chunks) {
assert_eq!(unused_chunk_size, 0, "unused chunk has size 0");
}
// Segment the batch bytes based on the chunk sizes.
let (segmented_batch_data, remaining_bytes) =
chunk_sizes.into_iter().take(valid_chunks).fold(
(Vec::new(), batch_bytes),
|(mut datas, rest_bytes), size| {
datas.push(Vec::from(&rest_bytes[..size]));
(datas, &rest_bytes[size..])
},
);
// After segmenting the batch data into chunks, no bytes should be left.
assert!(
remaining_bytes.is_empty(),
"chunk segmentation len must add up to the correct value"
);
// Compute the chunk data digests based on the segmented data.
let chunk_data_digests = segmented_batch_data
.iter()
.map(|bytes| B256::from(keccak256(bytes)))
.collect();
Self {
metadata_digest,
chunk_data_digests,
}
}
/// Compute the challenge digest from blob bytes. which is the combination of
/// digest for bytes in each chunk
pub fn get_challenge_digest(&self, versioned_hash: B256) -> B256 {
keccak256(self.get_challenge_digest_preimage(versioned_hash))
}
/// The number of bytes in payload Data to represent the "payload metadata" section: a u16 to
/// represent the size of chunks and max_chunks * u32 to represent chunk sizes
const fn n_bytes_metadata() -> usize {
N_BYTES_NUM_CHUNKS + (N_MAX_CHUNKS * N_BYTES_CHUNK_SIZE)
}
/// Validate the payload contents.
pub fn validate<'a>(
&self,
header: &BatchHeaderV6,
chunk_infos: &'a [ChunkInfo],
) -> (&'a ChunkInfo, &'a ChunkInfo) {
// There should be at least 1 chunk info.
assert!(!chunk_infos.is_empty(), "at least 1 chunk info");
// Get the first and last chunks' info, to construct the batch info.
let (first_chunk, last_chunk) = (
chunk_infos.first().expect("at least one chunk in batch"),
chunk_infos.last().expect("at least one chunk in batch"),
);
for (&chunk_data_digest, chunk_info) in self.chunk_data_digests.iter().zip_eq(chunk_infos) {
assert_eq!(chunk_data_digest, chunk_info.tx_data_digest)
}
// Validate the l1-msg identifier data_hash for the batch.
let batch_data_hash_preimage = chunk_infos
.iter()
.flat_map(|chunk_info| chunk_info.data_hash.0)
.collect::<Vec<_>>();
let batch_data_hash = keccak256(batch_data_hash_preimage);
assert_eq!(batch_data_hash, header.data_hash);
(first_chunk, last_chunk)
}
/// Get the preimage for the challenge digest.
pub(crate) fn get_challenge_digest_preimage(&self, versioned_hash: B256) -> Vec<u8> {
// preimage =
// metadata_digest ||
// chunk[0].chunk_data_digest || ...
// chunk[N_SNARKS-1].chunk_data_digest ||
// blob_versioned_hash
//
// where chunk_data_digest for a padded chunk is set equal to the "last valid chunk"'s
// chunk_data_digest.
let mut preimage = self.metadata_digest.to_vec();
let last_digest = self
.chunk_data_digests
.last()
.expect("at least we have one");
for chunk_digest in self
.chunk_data_digests
.iter()
.chain(std::iter::repeat(last_digest))
.take(N_MAX_CHUNKS)
{
preimage.extend_from_slice(chunk_digest.as_slice());
}
preimage.extend_from_slice(versioned_hash.as_slice());
preimage
}
}

View File

@@ -0,0 +1,256 @@
use alloy_primitives::B256;
use crate::BatchHeaderV7;
use types_base::{
public_inputs::chunk::{BlockContextV2, ChunkInfo, SIZE_BLOCK_CTX},
utils::keccak256,
};
use super::N_BLOB_BYTES;
/// da-codec@v7
const DA_CODEC_VERSION: u8 = 7;
/// Represents the data contained within an EIP-4844 blob that is published on-chain.
///
/// The bytes following some metadata represent zstd-encoded [`PayloadV7`] if the envelope is
/// indicated as `is_encoded == true`.
#[derive(Debug, Clone)]
pub struct EnvelopeV7 {
/// The original envelope bytes supplied.
///
/// Caching just for re-use later in challenge digest computation.
pub envelope_bytes: Vec<u8>,
/// The version from da-codec, i.e. v7 in this case.
pub version: u8,
/// A single byte boolean flag (value is 0 or 1) to denote whether or not the following blob
/// bytes represent a batch in its zstd-encoded or raw form.
pub is_encoded: u8,
/// The unpadded bytes that possibly encode the [`PayloadV7`].
pub unpadded_bytes: Vec<u8>,
}
impl From<&[u8]> for EnvelopeV7 {
fn from(blob_bytes: &[u8]) -> Self {
// The number of bytes is as expected.
assert_eq!(blob_bytes.len(), N_BLOB_BYTES);
// The version of the blob encoding was as expected, i.e. da-codec@v7.
let version = blob_bytes[0];
assert_eq!(version, DA_CODEC_VERSION);
// Calculate the unpadded size of the encoded payload.
//
// It should be at most the maximum number of bytes allowed.
let unpadded_size = (blob_bytes[1] as usize) * 256 * 256
+ (blob_bytes[2] as usize) * 256
+ blob_bytes[3] as usize;
assert!(unpadded_size <= N_BLOB_BYTES - 5);
// Whether the envelope represents encoded payload or raw payload.
//
// Is a boolean.
let is_encoded = blob_bytes[4];
assert!(is_encoded <= 1);
// The padded bytes are all 0s.
for &padded_byte in blob_bytes.iter().skip(5 + unpadded_size) {
assert_eq!(padded_byte, 0);
}
Self {
version,
is_encoded,
unpadded_bytes: blob_bytes[5..(5 + unpadded_size)].to_vec(),
envelope_bytes: blob_bytes.to_vec(),
}
}
}
impl EnvelopeV7 {
/// The verification of the EIP-4844 blob is done via point-evaluation precompile
/// implemented in-circuit.
///
/// We require a random challenge point for this, and using Fiat-Shamir we compute it with
/// every byte in the blob along with the blob's versioned hash, i.e. an identifier for its KZG
/// commitment.
///
/// keccak256(
/// keccak256(envelope) ||
/// versioned hash
/// )
pub fn challenge_digest(&self, versioned_hash: B256) -> B256 {
keccak256(
std::iter::empty()
.chain(keccak256(&self.envelope_bytes))
.chain(versioned_hash.0)
.collect::<Vec<u8>>(),
)
}
}
/// Represents the batch data, eventually encoded into an [`EnvelopeV7`].
///
/// | Field | # Bytes | Type | Index |
/// |------------------------|---------|----------------|---------------|
/// | prevL1MessageQueueHash | 32 | bytes32 | 0 |
/// | postL1MessageQueueHash | 32 | bytes32 | 32 |
/// | initialL2BlockNumber | 8 | u64 | 64 |
/// | numBlocks | 2 | u16 | 72 |
/// | blockCtxs[0] | 52 | BlockContextV2 | 74 |
/// | ... blockCtxs[i] ... | 52 | BlockContextV2 | 74 + 52*i |
/// | blockCtxs[n-1] | 52 | BlockContextV2 | 74 + 52*(n-1) |
/// | l2TxsData | dynamic | bytes | 74 + 52*n |
#[derive(Debug, Clone)]
pub struct PayloadV7 {
/// The version from da-codec, i.e. v7 in this case.
///
/// Note: This is not really a part of payload, simply coopied from the envelope for
/// convenience.
pub version: u8,
/// Message queue hash at the end of the previous batch.
pub prev_msg_queue_hash: B256,
/// Message queue hash at the end of the current batch.
pub post_msg_queue_hash: B256,
/// The block number of the first block in the batch.
pub initial_block_number: u64,
/// The number of blocks in the batch.
pub num_blocks: u16,
/// The block contexts of each block in the batch.
pub block_contexts: Vec<BlockContextV2>,
/// The L2 tx data flattened over every tx in every block in the batch.
pub tx_data: Vec<u8>,
}
const INDEX_PREV_MSG_QUEUE_HASH: usize = 0;
const INDEX_POST_MSG_QUEUE_HASH: usize = INDEX_PREV_MSG_QUEUE_HASH + 32;
const INDEX_L2_BLOCK_NUM: usize = INDEX_POST_MSG_QUEUE_HASH + 32;
const INDEX_NUM_BLOCKS: usize = INDEX_L2_BLOCK_NUM + 8;
const INDEX_BLOCK_CTX: usize = INDEX_NUM_BLOCKS + 2;
impl From<&EnvelopeV7> for PayloadV7 {
fn from(envelope: &EnvelopeV7) -> Self {
// Conditionally decode depending on the flag set in the envelope.
let payload_bytes = if envelope.is_encoded & 1 == 1 {
vm_zstd::process(&envelope.unpadded_bytes)
.expect("zstd decode should succeed")
.decoded_data
} else {
envelope.unpadded_bytes.to_vec()
};
// Sanity check on the payload size.
assert!(payload_bytes.len() >= INDEX_BLOCK_CTX);
let num_blocks = u16::from_be_bytes(
payload_bytes[INDEX_NUM_BLOCKS..INDEX_BLOCK_CTX]
.try_into()
.expect("should not fail"),
);
assert!(payload_bytes.len() >= INDEX_BLOCK_CTX + ((num_blocks as usize) * SIZE_BLOCK_CTX));
// Deserialize the other fields.
let prev_msg_queue_hash =
B256::from_slice(&payload_bytes[INDEX_PREV_MSG_QUEUE_HASH..INDEX_POST_MSG_QUEUE_HASH]);
let post_msg_queue_hash =
B256::from_slice(&payload_bytes[INDEX_POST_MSG_QUEUE_HASH..INDEX_L2_BLOCK_NUM]);
let initial_block_number = u64::from_be_bytes(
payload_bytes[INDEX_L2_BLOCK_NUM..INDEX_NUM_BLOCKS]
.try_into()
.expect("should not fail"),
);
// Deserialize block contexts depending on the number of blocks in the batch.
let mut block_contexts = Vec::with_capacity(num_blocks as usize);
for i in 0..num_blocks {
let start = (i as usize) * SIZE_BLOCK_CTX + INDEX_BLOCK_CTX;
block_contexts.push(BlockContextV2::from(
&payload_bytes[start..(start + SIZE_BLOCK_CTX)],
));
}
// All remaining bytes are flattened L2 txs.
let tx_data =
payload_bytes[INDEX_BLOCK_CTX + ((num_blocks as usize) * SIZE_BLOCK_CTX)..].to_vec();
Self {
version: envelope.version,
prev_msg_queue_hash,
post_msg_queue_hash,
initial_block_number,
num_blocks,
block_contexts,
tx_data,
}
}
}
impl PayloadV7 {
/// Validate the payload contents.
pub fn validate<'a>(
&self,
header: &BatchHeaderV7,
chunk_infos: &'a [ChunkInfo],
) -> (&'a ChunkInfo, &'a ChunkInfo) {
// Get the first and last chunks' info, to construct the batch info.
let (first_chunk, last_chunk) = (
chunk_infos.first().expect("at least one chunk in batch"),
chunk_infos.last().expect("at least one chunk in batch"),
);
// version from payload is what's present in the on-chain batch header
assert_eq!(self.version, header.version);
// number of blocks in the batch
assert_eq!(
usize::from(self.num_blocks),
chunk_infos
.iter()
.flat_map(|chunk_info| &chunk_info.block_ctxs)
.count()
);
assert_eq!(usize::from(self.num_blocks), self.block_contexts.len());
// the block number of the first block in the batch
assert_eq!(self.initial_block_number, first_chunk.initial_block_number);
// prev message queue hash
assert_eq!(self.prev_msg_queue_hash, first_chunk.prev_msg_queue_hash);
// post message queue hash
assert_eq!(self.post_msg_queue_hash, last_chunk.post_msg_queue_hash);
// for each chunk, the tx_data_digest, i.e. keccak digest of the rlp-encoded L2 tx bytes
// flattened over every tx in the chunk, should be re-computed and matched against the
// public input of the chunk-circuit.
//
// first check that the total size of rlp-encoded tx data flattened over all txs in the
// chunk is in fact the size available from the payload.
assert_eq!(
u64::try_from(self.tx_data.len()).expect("len(tx-data) is u64"),
chunk_infos
.iter()
.map(|chunk_info| chunk_info.tx_data_length)
.sum::<u64>(),
);
let mut index: usize = 0;
for chunk_info in chunk_infos.iter() {
let chunk_size = chunk_info.tx_data_length as usize;
let chunk_tx_data_digest =
keccak256(&self.tx_data.as_slice()[index..(index + chunk_size)]);
assert_eq!(chunk_tx_data_digest, chunk_info.tx_data_digest);
index += chunk_size;
}
// for each block in the batch, check that the block context matches what's provided as
// witness.
for (block_ctx, witness_block_ctx) in self.block_contexts.iter().zip(
chunk_infos
.iter()
.flat_map(|chunk_info| &chunk_info.block_ctxs),
) {
assert_eq!(block_ctx, witness_block_ctx);
}
(first_chunk, last_chunk)
}
}

View File

@@ -0,0 +1,57 @@
use crate::header::ReferenceHeader;
use types_agg::{AggregationInput, ProgramCommitment, ProofCarryingWitness};
use types_base::public_inputs::{ForkName, chunk::ChunkInfo};
/// Simply rewrap byte48 to avoid unnecessary dep
pub type Bytes48 = [u8; 48];
/// Witness required by applying point evaluation
#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
#[rkyv(derive(Debug))]
pub struct PointEvalWitness {
/// kzg commitment
#[rkyv()]
pub kzg_commitment: Bytes48,
/// kzg proof
#[rkyv()]
pub kzg_proof: Bytes48,
}
/// Witness to the batch circuit.
#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
#[rkyv(derive(Debug))]
pub struct BatchWitness {
/// Flattened root proofs from all chunks in the batch.
#[rkyv()]
pub chunk_proofs: Vec<AggregationInput>,
/// Chunk infos.
#[rkyv()]
pub chunk_infos: Vec<ChunkInfo>,
/// Blob bytes.
#[rkyv()]
pub blob_bytes: Vec<u8>,
/// Witness for point evaluation
pub point_eval_witness: PointEvalWitness,
/// Header for reference.
#[rkyv()]
pub reference_header: ReferenceHeader,
/// The code version specify the chain spec
#[rkyv()]
pub fork_name: ForkName,
}
impl ProofCarryingWitness for ArchivedBatchWitness {
fn get_proofs(&self) -> Vec<AggregationInput> {
self.chunk_proofs
.iter()
.map(|archived| AggregationInput {
public_values: archived
.public_values
.iter()
.map(|u32_le| u32_le.to_native())
.collect(),
commitment: ProgramCommitment::from(&archived.commitment),
})
.collect()
}
}

View File

@@ -0,0 +1,21 @@
[package]
name = "scroll-zkvm-circuit-input-types-bundle"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
readme.workspace = true
repository.workspace = true
version = "0.2.0"
[dependencies]
alloy-primitives = { workspace = true, default-features = false, features = ["std", "map-hashbrown", "map-fxhash", "rkyv"] }
rkyv.workspace = true
serde.workspace = true
itertools.workspace = true
vm-zstd = { workspace = true }
types-base = { path = "../base", package = "scroll-zkvm-circuit-input-types-base"}
types-agg = { path = "../aggregation", package = "scroll-zkvm-circuit-input-types-aggregation"}
[features]
default = []

View File

@@ -0,0 +1,2 @@
mod witness;
pub use witness::{ArchivedBundleWitness, BundleWitness};

View File

@@ -0,0 +1,30 @@
use types_agg::{AggregationInput, ProgramCommitment, ProofCarryingWitness};
use types_base::public_inputs::batch::BatchInfo;
/// The witness for the bundle circuit.
#[derive(Clone, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
#[rkyv(derive(Debug))]
pub struct BundleWitness {
/// Batch proofs being aggregated in the bundle.
#[rkyv()]
pub batch_proofs: Vec<AggregationInput>,
/// Public-input values for the corresponding batch proofs.
#[rkyv()]
pub batch_infos: Vec<BatchInfo>,
}
impl ProofCarryingWitness for ArchivedBundleWitness {
fn get_proofs(&self) -> Vec<AggregationInput> {
self.batch_proofs
.iter()
.map(|archived| AggregationInput {
public_values: archived
.public_values
.iter()
.map(|u32_le| u32_le.to_native())
.collect(),
commitment: ProgramCommitment::from(&archived.commitment),
})
.collect()
}
}

View File

@@ -0,0 +1,28 @@
[package]
name = "scroll-zkvm-circuit-input-types-chunk"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
readme.workspace = true
repository.workspace = true
version = "0.2.0"
[dependencies]
alloy-primitives = { workspace = true, default-features = false, features = ["std", "map-hashbrown", "map-fxhash", "rkyv"] }
rkyv.workspace = true
sbv-trie = { workspace = true }
sbv-core = { workspace = true }
sbv-primitives = { workspace = true }
sbv-kv = { workspace = true }
serde.workspace = true
itertools.workspace = true
openvm = { workspace = true, features = ["std"] }
openvm-rv32im-guest = { workspace = true }
openvm-custom-insn = { workspace = true }
types-base = { path = "../base", package = "scroll-zkvm-circuit-input-types-base"}
[features]
default = []
openvm = ["sbv-trie/openvm", "sbv-core/openvm", "sbv-primitives/openvm"]

View File

@@ -0,0 +1,167 @@
use sbv_core::{EvmDatabase, EvmExecutor};
use sbv_primitives::{
BlockWitness,
chainspec::{
BaseFeeParams, BaseFeeParamsKind, Chain, MAINNET,
reth_chainspec::ChainSpec,
scroll::{ScrollChainConfig, ScrollChainSpec},
},
ext::{BlockWitnessChunkExt, TxBytesHashExt},
hardforks::SCROLL_DEV_HARDFORKS,
types::{
consensus::BlockHeader,
reth::{Block, BlockWitnessRethExt, RecoveredBlock},
scroll::ChunkInfoBuilder,
},
};
use crate::{ArchivedChunkWitness, make_providers, manually_drop_on_zkvm};
use types_base::public_inputs::{
ForkName,
chunk::{BlockContextV2, ChunkInfo},
};
fn block_ctxv2_from_block(value: &RecoveredBlock<Block>) -> BlockContextV2 {
use alloy_primitives::U256;
BlockContextV2 {
timestamp: value.timestamp,
gas_limit: value.gas_limit,
base_fee: U256::from(value.base_fee_per_gas().expect("base_fee_expected")),
num_txs: u16::try_from(value.body().transactions.len()).expect("num txs u16"),
num_l1_msgs: u16::try_from(
value
.body()
.transactions
.iter()
.filter(|tx| tx.is_l1_message())
.count(),
)
.expect("num l1 msgs u16"),
}
}
type Witness = ArchivedChunkWitness;
pub fn execute(witness: &Witness) -> Result<ChunkInfo, String> {
if witness.blocks.is_empty() {
return Err("At least one witness must be provided in chunk mode".into());
}
if !witness.blocks.has_same_chain_id() {
return Err("All witnesses must have the same chain id in chunk mode".into());
}
if !witness.blocks.has_seq_block_number() {
return Err("All witnesses must have sequential block numbers in chunk mode".into());
}
// Get the blocks to build the basic chunk-info.
let blocks = manually_drop_on_zkvm!(
witness
.blocks
.iter()
.map(|w| w.build_reth_block())
.collect::<Result<Vec<RecoveredBlock<Block>>, _>>()
.map_err(|e| e.to_string())?
);
let pre_state_root = witness.blocks[0].pre_state_root;
let fork_name = ForkName::from(&witness.fork_name);
let chain = Chain::from_id(witness.blocks[0].chain_id());
// SCROLL_DEV_HARDFORKS will enable all forks
let mut hardforks = (*SCROLL_DEV_HARDFORKS).clone();
if fork_name == ForkName::EuclidV1 {
// disable EuclidV2 fork for legacy chunk
use sbv_primitives::{chainspec::ForkCondition, hardforks::ScrollHardfork};
hardforks.insert(ScrollHardfork::EuclidV2, ForkCondition::Never);
}
let inner = ChainSpec {
chain,
genesis_hash: Default::default(),
genesis: Default::default(),
genesis_header: Default::default(),
paris_block_and_final_difficulty: Default::default(),
hardforks,
deposit_contract: Default::default(),
base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
prune_delete_limit: 20000,
blob_params: Default::default(),
};
let config = ScrollChainConfig::mainnet();
let chain_spec: ScrollChainSpec = ScrollChainSpec { inner, config };
let (code_db, nodes_provider, block_hashes) = make_providers(&witness.blocks);
let nodes_provider = manually_drop_on_zkvm!(nodes_provider);
let prev_state_root = witness.blocks[0].pre_state_root();
let mut db = manually_drop_on_zkvm!(
EvmDatabase::new_from_root(code_db, prev_state_root, &nodes_provider, block_hashes)
.map_err(|e| format!("failed to create EvmDatabase: {}", e))?
);
for block in blocks.iter() {
let output = manually_drop_on_zkvm!(
EvmExecutor::new(std::sync::Arc::new(chain_spec.clone()), &db, block)
.execute()
.map_err(|e| format!("failed to execute block: {}", e))?
);
db.update(&nodes_provider, output.state.state.iter())
.map_err(|e| format!("failed to update db: {}", e))?;
}
let post_state_root = db.commit_changes();
let withdraw_root = db
.withdraw_root()
.map_err(|e| format!("failed to get withdraw root: {}", e))?;
let mut rlp_buffer = manually_drop_on_zkvm!(Vec::with_capacity(2048));
let (tx_data_length, tx_data_digest) = blocks
.iter()
.flat_map(|b| b.body().transactions.iter())
.tx_bytes_hash_in(rlp_buffer.as_mut());
let _ = tx_data_length;
let sbv_chunk_info = {
#[allow(unused_mut)]
let mut builder = ChunkInfoBuilder::new(&chain_spec, pre_state_root.into(), &blocks);
if fork_name == ForkName::EuclidV2 {
builder.set_prev_msg_queue_hash(witness.prev_msg_queue_hash.into());
}
builder.build(withdraw_root)
};
if post_state_root != sbv_chunk_info.post_state_root() {
return Err(format!(
"state root mismatch: expected={}, found={}",
sbv_chunk_info.post_state_root(),
post_state_root
));
}
let chunk_info = ChunkInfo {
chain_id: sbv_chunk_info.chain_id(),
prev_state_root: sbv_chunk_info.prev_state_root(),
post_state_root: sbv_chunk_info.post_state_root(),
data_hash: sbv_chunk_info
.clone()
.into_legacy()
.map(|x| x.data_hash)
.unwrap_or_default(),
withdraw_root,
tx_data_digest,
tx_data_length: u64::try_from(tx_data_length).expect("tx_data_length: u64"),
initial_block_number: blocks[0].header().number,
prev_msg_queue_hash: witness.prev_msg_queue_hash.into(),
post_msg_queue_hash: sbv_chunk_info
.into_euclid_v2()
.map(|x| x.post_msg_queue_hash)
.unwrap_or_default(),
block_ctxs: blocks.iter().map(block_ctxv2_from_block).collect(),
};
openvm::io::println(format!("withdraw_root = {:?}", withdraw_root));
openvm::io::println(format!("tx_bytes_hash = {:?}", tx_data_digest));
// We should never touch that lazy lock... Or else we introduce 40M useless cycles.
assert!(std::sync::LazyLock::get(&MAINNET).is_none());
Ok(chunk_info)
}

View File

@@ -0,0 +1,11 @@
#![feature(lazy_get)]
mod utils;
mod witness;
pub use utils::make_providers;
pub use witness::{ArchivedChunkWitness, ChunkWitness};
mod execute;
pub use execute::execute;

View File

@@ -0,0 +1,27 @@
use alloy_primitives::{B256, U256};
use sbv_primitives::types::{
consensus::BlockHeader,
reth::{Block, RecoveredBlock},
};
use types_base::public_inputs::chunk::BlockContextV2;
impl From<&RecoveredBlock<Block>> for BlockContextV2 {
fn from(value: &RecoveredBlock<Block>) -> Self {
Self {
timestamp: value.timestamp,
gas_limit: value.gas_limit,
base_fee: U256::from(value.base_fee_per_gas().expect("base_fee_expected")),
num_txs: u16::try_from(value.body().transactions.len()).expect("num txs u16"),
num_l1_msgs: u16::try_from(
value
.body()
.transactions
.iter()
.filter(|tx| tx.is_l1_message())
.count(),
)
.expect("num l1 msgs u16"),
}
}
}

View File

@@ -0,0 +1,48 @@
use sbv_kv::nohash::NoHashMap;
use sbv_primitives::{B256, BlockWitness, Bytes, ext::BlockWitnessExt};
use sbv_trie::{BlockWitnessTrieExt, TrieNode};
type CodeDb = NoHashMap<B256, Bytes>;
type NodesProvider = NoHashMap<B256, TrieNode>;
type BlockHashProvider = sbv_kv::null::NullProvider;
pub fn make_providers<W: BlockWitness>(
witnesses: &[W],
) -> (CodeDb, NodesProvider, BlockHashProvider) {
let code_db = {
// build code db
let num_codes = witnesses.iter().map(|w| w.codes_iter().len()).sum();
let mut code_db =
NoHashMap::<B256, Bytes>::with_capacity_and_hasher(num_codes, Default::default());
witnesses.import_codes(&mut code_db);
code_db
};
let nodes_provider = {
let num_states = witnesses.iter().map(|w| w.states_iter().len()).sum();
let mut nodes_provider =
NoHashMap::<B256, TrieNode>::with_capacity_and_hasher(num_states, Default::default());
witnesses.import_nodes(&mut nodes_provider).unwrap();
nodes_provider
};
let block_hashes = sbv_kv::null::NullProvider;
(code_db, nodes_provider, block_hashes)
}
#[macro_export]
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
macro_rules! manually_drop_on_zkvm {
($e:expr) => {
std::mem::ManuallyDrop::new($e)
};
}
#[macro_export]
#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))]
macro_rules! manually_drop_on_zkvm {
($e:expr) => {
$e
};
}

View File

@@ -0,0 +1,71 @@
use alloy_primitives::B256;
use sbv_primitives::types::BlockWitness;
use std::collections::HashSet;
use types_base::public_inputs::ForkName;
/// The witness type accepted by the chunk-circuit.
#[derive(
Clone,
Debug,
serde::Deserialize,
serde::Serialize,
rkyv::Archive,
rkyv::Deserialize,
rkyv::Serialize,
)]
#[rkyv(derive(Debug))]
pub struct ChunkWitness {
/// The block witness for each block in the chunk.
pub blocks: Vec<BlockWitness>,
/// The on-chain rolling L1 message queue hash before enqueueing any L1 msg tx from the chunk.
pub prev_msg_queue_hash: B256,
/// The code version specify the chain spec
pub fork_name: ForkName,
}
impl ChunkWitness {
pub fn new(blocks: &[BlockWitness], prev_msg_queue_hash: B256, fork_name: ForkName) -> Self {
let num_codes = blocks.iter().map(|w| w.codes.len()).sum();
let num_states = blocks.iter().map(|w| w.states.len()).sum();
let mut codes = HashSet::with_capacity(num_codes);
let mut states = HashSet::with_capacity(num_states);
let blocks = blocks
.iter()
.map(|block| BlockWitness {
chain_id: block.chain_id,
header: block.header.clone(),
pre_state_root: block.pre_state_root,
transaction: block.transaction.clone(),
withdrawals: block.withdrawals.clone(),
states: block
.states
.iter()
.filter(|s| states.insert(*s))
.cloned()
.collect(),
codes: block
.codes
.iter()
.filter(|c| codes.insert(*c))
.cloned()
.collect(),
})
.collect();
Self {
blocks,
prev_msg_queue_hash,
fork_name,
}
}
pub fn new_v1(blocks: &[BlockWitness]) -> Self {
Self::new(blocks, Default::default(), ForkName::EuclidV1)
}
pub fn new_v2(blocks: &[BlockWitness], prev_msg_queue_hash: B256) -> Self {
Self::new(blocks, prev_msg_queue_hash, ForkName::EuclidV2)
}
}

View File

@@ -0,0 +1,21 @@
// re-export for a compatible interface with old circuit/types for prover
pub mod bundle {
pub use types_base::public_inputs::bundle::{BundleInfo, BundleInfoV1, BundleInfoV2};
pub use types_bundle::*;
}
pub mod batch {
pub use types_base::public_inputs::batch::{ArchivedBatchInfo, BatchInfo, VersionedBatchInfo};
pub use types_batch::*;
}
pub mod chunk {
pub use types_base::public_inputs::chunk::{
ArchivedChunkInfo, BlockContextV2, ChunkInfo, SIZE_BLOCK_CTX, VersionedChunkInfo,
};
pub use types_chunk::*;
}
pub use types_agg;
pub use types_base::{public_inputs, utils};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1 @@
{"metadata":{"bundle_info":{"chain_id":333333,"msg_queue_hash":"0x0101010101010101010101010101010101010101010101010101010101010101","num_batches":2,"prev_state_root":"0x5302a56cbbec7d14d48d592b805d4ec3c7011439dfaa90d44deee02a9326d203","prev_batch_hash":"0xabacadaeaf000000000000000000000000000000000000000000000000000000","post_state_root":"0xaf6696afb2e11052490051f0f9f6444be6e9f5bb82beb3c3dae846cfa59ed6e0","batch_hash":"0xf0ee5d6b9cd739eb1ff816a58486af8b08d42a8c50d6e5998e7a3947c7aae2a9","withdraw_root":"0x0000000000000000000000000000000000000000000000000000000000000000"},"bundle_pi_hash":"0x2028510c403837c6ed77660fd92814ba61d7b746e7268cc8dfc14d163d45e6bd"},"proof":{"proof":"CfpNiL6UpegsK3VcoAj9ey5daMbZDFiF1XpCKvrOeN0MPPLNCDrllJL/gN0E3qmq20kGLYpBQ8aZ3sgUrxpSyA+9GKK8NhZoIM75adOnV8AYCLXpmxfS81MxIai/+ghxDIUvJQJVgWKJPsMQp4lO/Qltc4eCNWeoR2jHua/VzSASQXDDQ5ozD6i448TVkjKiyEcHwFFMMuOebFUzDc85hA4AJGM1T9bPl5VVQkEpijdNF+1lzUfi27U0XRQbYz8aE8hiCLxR8Z2bHg65dvfa+TsaDI8oAlz33Q1yIadZBtceKsH53P5u6vwWp0dQvw8DGNv8G5zvsayHPNCvy4xz8hRT3E4G0Ome8voqqOxrc/A8u2fE6LoXKswvU6Uquv+LHwGMbTugRvQ0BBXlLQ29Hvj18rDzS6ll0OnEcRiaaEkGOZy7Kq1PGiF7ZxMZsJYCbhyPgg4TKpesYDUJygEN0iGNX90dmyzGLTTgJATMYBGD2U+XP/T+UOMbxFTl3TFNHWlCPhEAu5LBwZ0pD3XV1xNW1iUqwTSfg7Qz1SOUYkot10Q8EAKeXk3hluHK+nSQhOMfWC4tnvfQdMqepfymwwArzA/9GMA/Two7yuzgCz7vHb+56YKPZiDrh4cqSvpVI92hCF8GWHaTqWDR0fikx2Y7GLX8YBM3Rx8reQE+LYYGEJHJzD4cIc0MKiuet605ZPSAaKpb8JM2EgrCAfw+QAhBiwXQ3HOQkrt17tzqNJH7IeHF761v43D9w+IeqvetKEgYXEH3fHmN00dLV2Uws8C4956qze+SG81ScnZzbrIeiO9lnmUXSFzrL40K+3NqCZcFnfLhVidyEJepzJi50yOK5BUJdMFdNtvHtprICqLKyb7aRg39qoZ7RqyJTg5nAjQQBGelvRu/AN6zdyxja73Jo5gEovdIiMybi/IhfMwKGWgiRaOGxyHx9KZ/ZA/w7r3rce6vuDsUhk5hsgVj4wUW3BqoZ8iRIH8X6AjK1xli+S/HfgAkfmUVwNNBOcgYEcrqEbswsfYKOcoFn71DISLK0jmB44LTNyGxoWBMpIAOf/gGhQSNk0ojd4n4UXxShsqmJ57Kudw/mGimMm+Crhr5asxeiFH0eJNBgUEXDuveqE1d20UTRJ1UJ/hZGomsDLebTojSTtsMLWTtx/4Mqg+g3Odte1WKN6CgxF4kGRcW2tE3D1jiBys5FTHMAhmka3mUBwlciT7syDWBDlYVuSmwppCghdBMQfQL4s3Uh0vRG28LkU+UXcwYXwh3UK6cA1bBnKfAa9k7P5BuMxVh8p6he6EZr0kGNjKGPSxuVxgczO/C32GP+HVVsWlIMNmgB4GeMHIN3yJampOrLZIMlQuP9d9kOicvRia1ge5sFtT+Vmthnp1F7sR3P+ADB/WxKSxVbiLaVBo+zm/rZbyM9vU0CVLD69lzPC6xKcFkxewlWJU6o7rOz1qzh47fT+8qUcVYfpCSEtT/U8eX2JFnXCb0PPXWivofI28tnsuS8GjwUiOyzCoxxuIEOyz1HNRXBcO2dSKR2qM41zUs0btA2JkA3hTVW8YWn8czHxrZyooooaumzbUPQBOqO3fewnLLyQ9etBcjZJ8Xm/B1EBk9cRPWDjgx5Hq8C0soA+EsoNoaSQJu67HuFTRd/OWvKSliCoj1XVcqBobnJWmTU7kAgi73pMaq/G4ot2rRFSL9MbkJgHCyxBkrl9nkCVUJC5GphsrDS5P5/bmRS3iTNdxiXAzdwOIQqJpEO54oN+3CHZuZuUOgCcWTI3uxWq/gBDJrBTsv8EUqtNQJve0qwIh2PUuJl5DIqF0CvswN649gywc=","instances":"AAAAAAAAAAAAAAAAAAAAAAAAAAAApvhdIlw19IwSvukAAAAAAAAAAAAAAAAAAAAAAAAAAAAl72fyrHk3TaguHQAAAAAAAAAAAAAAAAAAAAAAAAAAAAALh9HvEG69AvDlAAAAAAAAAAAAAAAAAAAAAAAAAAAAkGY9R6S+t36FIrAAAAAAAAAAAAAAAAAAAAAAAAAAAACoNqt7QwZoXUpj/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAdaREhypq22OmnAAAAAAAAAAAAAAAAAAAAAAAAAAAAOXf2Vj0jGD1q4xQAAAAAAAAAAAAAAAAAAAAAAAAAAADZYAdKTg7m4hBHGgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAll4nXKE0us1IMAAAAAAAAAAAAAAAAAAAAAAAAAAAAFfnJ8YXlwczTsyEAAAAAAAAAAAAAAAAAAAAAAAAAAAArXqULkWYvNST9PQAAAAAAAAAAAAAAAAAAAAAAAAAAAAArqteSdJMySnbMAC5TUWus+SXtvRWUNmCSMiMb4aZvb4hpJ5yXqjtih6gAIn9WQUOx/Z/rbbdComU0hCSwKwrewQgB3KolXKensAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA7QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA5wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADfAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAATQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAL0="},"vk":"AhYAAAAABAAAAD2PumVP6pqldS0PKWW8Q4IvnE/rvtm5/2fXvG196sYhKtVFtg+WFGYJrU+eMUKZVjPurMpM8kbYiXvE18bnsU4Nu8s47Xabxy0EViND1dzsu5HicdAWl0xG5C+VpO2faJdK4nGwtD4WHtbdqWY72nSY5aKSDxAYO85vLy+9cJZlQsMNQlhTi/2q9PYQpC4D3Uf8E+yZ7gvLhd6cFdErlg4Oq/nthQkfxPAarVYLUFNGW80SgIloMDhutrky34D+Csw8T9j5UXpHz3K/2yuVSXK6OvMG4/058TXG09qKgXYP","git_version":"9f48bc4"}

File diff suppressed because one or more lines are too long

View File

@@ -5,7 +5,7 @@ import (
"runtime/debug"
)
var tag = "v4.5.12"
var tag = "v4.5.10"
var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {

View File

@@ -54,8 +54,7 @@
"batch_submission": {
"min_batches": 1,
"max_batches": 6,
"timeout": 7200,
"backlog_max": 75
"timeout": 300
},
"gas_oracle_config": {
"min_gas_price": 0,

View File

@@ -38,8 +38,6 @@ type BatchSubmission struct {
MaxBatches int `json:"max_batches"`
// The time in seconds after which a batch is considered stale and should be submitted ignoring the min batch count.
TimeoutSec int64 `json:"timeout"`
// The maximum number of pending batches to keep in the backlog.
BacklogMax int64 `json:"backlog_max"`
}
// ChainMonitor this config is used to get batch status from chain_monitor API.

View File

@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"math"
"math/big"
"sort"
"strings"
@@ -34,32 +33,6 @@ import (
rutils "scroll-tech/rollup/internal/utils"
)
// RelaxType enumerates the relaxation functions we support when
// turning a baseline fee into a “target” fee.
type RelaxType int
const (
// NoRelaxation means “dont touch the baseline” (i.e. fallback/default).
NoRelaxation RelaxType = iota
Exponential
Sigmoid
)
const secondsPerBlock = 12
// BaselineType enumerates the baseline types we support when
// turning a baseline fee into a “target” fee.
type BaselineType int
const (
// PctMin means “take the minimum of the last N blocks fees, then
// take the PCT of that”.
PctMin BaselineType = iota
// EWMA means “take the exponentiallyweighted moving average of
// the last N blocks fees”.
EWMA
)
// Layer2Relayer is responsible for:
// i. committing and finalizing L2 blocks on L1.
// ii. updating L2 gas price oracle contract on L1.
@@ -73,7 +46,6 @@ type Layer2Relayer struct {
batchOrm *orm.Batch
chunkOrm *orm.Chunk
l2BlockOrm *orm.L2Block
l1BlockOrm *orm.L1Block
cfg *config.RelayerConfig
@@ -89,26 +61,6 @@ type Layer2Relayer struct {
metrics *l2RelayerMetrics
chainCfg *params.ChainConfig
lastFetchedBlock uint64 // highest block number ever pulled
feeHistory []*big.Int // sliding window of blob fees
batchStrategy StrategyParams
}
// StrategyParams holds the perwindow feesubmission rules.
type StrategyParams struct {
BaselineType BaselineType // "pct_min" or "ewma"
BaselineParam float64 // percentile (01) or α for EWMA
Gamma float64 // relaxation γ
Beta float64 // relaxation β
RelaxType RelaxType // Exponential or Sigmoid
}
// bestParams maps your 2h/5h/12h windows to their best rules.
var bestParams = map[uint64]StrategyParams{
2 * 3600: {BaselineType: PctMin, BaselineParam: 0.10, Gamma: 0.4, Beta: 8, RelaxType: Exponential},
5 * 3600: {BaselineType: PctMin, BaselineParam: 0.30, Gamma: 0.6, Beta: 20, RelaxType: Sigmoid},
12 * 3600: {BaselineType: PctMin, BaselineParam: 0.50, Gamma: 0.5, Beta: 20, RelaxType: Sigmoid},
}
// NewLayer2Relayer will return a new instance of Layer2RelayerClient
@@ -154,7 +106,6 @@ func NewLayer2Relayer(ctx context.Context, l2Client *ethclient.Client, db *gorm.
bundleOrm: orm.NewBundle(db),
batchOrm: orm.NewBatch(db),
l1BlockOrm: orm.NewL1Block(db),
l2BlockOrm: orm.NewL2Block(db),
chunkOrm: orm.NewChunk(db),
@@ -165,9 +116,9 @@ func NewLayer2Relayer(ctx context.Context, l2Client *ethclient.Client, db *gorm.
l1RollupABI: bridgeAbi.ScrollChainABI,
l2GasOracleABI: bridgeAbi.L2GasPriceOracleABI,
batchStrategy: bestParams[uint64(cfg.BatchSubmission.TimeoutSec)],
cfg: cfg,
chainCfg: chainCfg,
cfg: cfg,
chainCfg: chainCfg,
}
// chain_monitor client
@@ -315,10 +266,6 @@ func (r *Layer2Relayer) commitGenesisBatch(batchHash string, batchHeader []byte,
}
// ProcessPendingBatches processes the pending batches by sending commitBatch transactions to layer 1.
// Pending batchess are submitted if one of the following conditions is met:
// - the first batch is too old -> forceSubmit
// - backlogCount > r.cfg.BatchSubmission.BacklogMax -> forceSubmit
// - we have at least minBatches AND price hits a desired target price
func (r *Layer2Relayer) ProcessPendingBatches() {
// get pending batches from database in ascending order by their index.
dbBatches, err := r.batchOrm.GetFailedAndPendingBatches(r.ctx, r.cfg.BatchSubmission.MaxBatches)
@@ -327,40 +274,8 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
return
}
// if backlog outgrow max size, forcesubmit enough oldest batches
backlogCount, err := r.batchOrm.GetFailedAndPendingBatchesCount(r.ctx)
if err != nil {
log.Error("Failed to fetch pending L2 batches", "err", err)
return
}
var forceSubmit bool
oldestBatchTimestamp := dbBatches[0].CreatedAt
// if the batch with the oldest index is too old, we force submit all batches that we have so far in the next step
if r.cfg.BatchSubmission.TimeoutSec > 0 && time.Since(oldestBatchTimestamp) > time.Duration(r.cfg.BatchSubmission.TimeoutSec)*time.Second {
forceSubmit = true
}
// force submit if backlog is too big
if backlogCount > r.cfg.BatchSubmission.BacklogMax {
forceSubmit = true
}
if !forceSubmit {
// check if we should skip submitting the batch based on the fee target
skip, err := r.skipSubmitByFee(oldestBatchTimestamp)
// return if not hitting target price
if skip {
log.Debug("Skipping batch submission", "reason", err)
return
}
if err != nil {
log.Warn("Failed to check if we should skip batch submission, fallback to immediate submission", "err", err)
}
}
var batchesToSubmit []*dbBatchWithChunksAndParent
var forceSubmit bool
for i, dbBatch := range dbBatches {
if i == 0 && encoding.CodecVersion(dbBatch.CodecVersion) < encoding.CodecV7 {
// if the first batch is not >= V7 then we need to submit batches one by one
@@ -421,6 +336,11 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
break
}
// if one of the batches is too old, we force submit all batches that we have so far in the next step
if r.cfg.BatchSubmission.TimeoutSec > 0 && !forceSubmit && time.Since(dbBatch.CreatedAt) > time.Duration(r.cfg.BatchSubmission.TimeoutSec)*time.Second {
forceSubmit = true
}
if batchesToSubmitLen < r.cfg.BatchSubmission.MaxBatches {
batchesToSubmit = append(batchesToSubmit, &dbBatchWithChunksAndParent{
Batch: dbBatch,
@@ -1198,136 +1118,6 @@ func (r *Layer2Relayer) StopSenders() {
}
}
// fetchBlobFeeHistory returns the last WindowSec seconds of blobfee samples,
// by reading L1Block tables BlobBaseFee column.
func (r *Layer2Relayer) fetchBlobFeeHistory(windowSec uint64) ([]*big.Int, error) {
latest, err := r.l1BlockOrm.GetLatestL1BlockHeight(r.ctx)
if err != nil {
return nil, fmt.Errorf("GetLatestL1BlockHeight: %w", err)
}
// bootstrap on first call
if r.lastFetchedBlock == 0 {
// start window
r.lastFetchedBlock = latest - windowSec/secondsPerBlock
}
from := r.lastFetchedBlock + 1
//if new blocks
if from <= latest {
raw, err := r.l1BlockOrm.GetBlobFeesInRange(r.ctx, from, latest)
if err != nil {
return nil, fmt.Errorf("GetBlobFeesInRange: %w", err)
}
// append them
for _, v := range raw {
r.feeHistory = append(r.feeHistory, new(big.Int).SetUint64(v))
r.lastFetchedBlock++
}
}
maxLen := int(windowSec / secondsPerBlock)
if len(r.feeHistory) > maxLen {
r.feeHistory = r.feeHistory[len(r.feeHistory)-maxLen:]
}
return r.feeHistory, nil
}
// calculateTargetPrice applies pct_min/ewma + relaxation to get a BigInt target
func calculateTargetPrice(windowSec uint64, strategy StrategyParams, firstTime time.Time, history []*big.Int) *big.Int {
var baseline float64 // baseline in Gwei (converting to float, small loss of precision)
n := len(history)
if n == 0 {
return big.NewInt(0)
}
switch strategy.BaselineType {
case PctMin:
// make a copy, sort by big.Int.Cmp, then pick the percentile element
sorted := make([]*big.Int, n)
copy(sorted, history)
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Cmp(sorted[j]) < 0
})
idx := int(strategy.BaselineParam * float64(n-1))
if idx < 0 {
idx = 0
}
baseline, _ = new(big.Float).
Quo(new(big.Float).SetInt(sorted[idx]), big.NewFloat(1e9)).
Float64()
case EWMA:
one := big.NewFloat(1)
alpha := big.NewFloat(strategy.BaselineParam)
oneMinusAlpha := new(big.Float).Sub(one, alpha)
// start from first history point
ewma := new(big.Float).
Quo(new(big.Float).SetInt(history[0]), big.NewFloat(1e9))
for i := 1; i < n; i++ {
curr := new(big.Float).
Quo(new(big.Float).SetInt(history[i]), big.NewFloat(1e9))
term1 := new(big.Float).Mul(alpha, curr)
term2 := new(big.Float).Mul(oneMinusAlpha, ewma)
ewma = new(big.Float).Add(term1, term2)
}
baseline, _ = ewma.Float64()
default:
// fallback to last element
baseline, _ = new(big.Float).
Quo(new(big.Float).SetInt(history[n-1]), big.NewFloat(1e9)).
Float64()
} // now baseline holds our baseline in float64 Gwei
// relaxation
age := time.Since(firstTime).Seconds()
frac := age / float64(windowSec)
var adjusted float64
switch strategy.RelaxType {
case Exponential:
adjusted = baseline * (1 + strategy.Gamma*math.Exp(strategy.Beta*(frac-1)))
case Sigmoid:
adjusted = baseline * (1 + strategy.Gamma/(1+math.Exp(-strategy.Beta*(frac-0.5))))
default:
adjusted = baseline
}
// back to wei
f := new(big.Float).Mul(big.NewFloat(adjusted), big.NewFloat(1e9))
out, _ := f.Int(nil)
return out
}
// skipSubmitByFee returns (true, nil) when submission should be skipped right now
// because the blobfee is above target and the timeout window hasnt yet elapsed.
// Otherwise returns (false, err)
func (r *Layer2Relayer) skipSubmitByFee(oldest time.Time) (bool, error) {
windowSec := uint64(r.cfg.BatchSubmission.TimeoutSec)
hist, err := r.fetchBlobFeeHistory(windowSec)
if err != nil || len(hist) == 0 {
return false, fmt.Errorf(
"blob-fee history unavailable or empty: %w (history_length=%d)",
err, len(hist),
)
}
// calculate target & get current (in wei)
target := calculateTargetPrice(windowSec, r.batchStrategy, oldest, hist)
current := hist[len(hist)-1]
// if current fee > target and still inside the timeout window, skip
if current.Cmp(target) > 0 && time.Since(oldest) < time.Duration(windowSec)*time.Second {
return true, fmt.Errorf(
"blob-fee above target & window not yet passed; current=%s target=%s age=%s",
current.String(), target.String(), time.Since(oldest),
)
}
// otherwise proceed with submission
return false, nil
}
func addrFromSignerConfig(config *config.SignerConfig) (common.Address, error) {
switch config.SignerType {
case sender.PrivateKeySignerType:

View File

@@ -218,18 +218,6 @@ func (o *Batch) GetRollupStatusByHashList(ctx context.Context, hashes []string)
return statuses, nil
}
func (o *Batch) GetFailedAndPendingBatchesCount(ctx context.Context) (int64, error) {
db := o.db.WithContext(ctx)
db = db.Model(&Batch{})
db = db.Where("rollup_status = ? OR rollup_status = ?", types.RollupCommitFailed, types.RollupPending)
var count int64
if err := db.Count(&count).Error; err != nil {
return 0, fmt.Errorf("Batch.GetFailedAndPendingBatchesCount error: %w", err)
}
return count, nil
}
// GetFailedAndPendingBatches retrieves batches with failed or pending status up to the specified limit.
// The returned batches are sorted in ascending order by their index.
func (o *Batch) GetFailedAndPendingBatches(ctx context.Context, limit int) ([]*Batch, error) {

View File

@@ -71,20 +71,6 @@ func (o *L1Block) GetL1Blocks(ctx context.Context, fields map[string]interface{}
return l1Blocks, nil
}
// GetBlobFeesInRange returns all blob_base_fee values for blocks
// with number ∈ [startBlock..endBlock], ordered by block number ascending.
func (o *L1Block) GetBlobFeesInRange(ctx context.Context, startBlock, endBlock uint64) ([]uint64, error) {
var fees []uint64
db := o.db.WithContext(ctx).
Model(&L1Block{}).
Where("number >= ? AND number <= ?", startBlock, endBlock).
Order("number ASC")
if err := db.Pluck("blob_base_fee", &fees).Error; err != nil {
return nil, fmt.Errorf("L1Block.GetBlobFeesInRange error: %w", err)
}
return fees, nil
}
// InsertL1Blocks batch inserts l1 blocks.
// If there's a block number conflict (e.g., due to reorg), soft deletes the existing block and inserts the new one.
func (o *L1Block) InsertL1Blocks(ctx context.Context, blocks []L1Block) error {

4
rust-toolchain Normal file
View File

@@ -0,0 +1,4 @@
[toolchain]
channel = "nightly-2025-02-14"
targets = ["riscv32im-unknown-none-elf", "x86_64-unknown-linux-gnu"]
components = ["llvm-tools", "rustc-dev"]

8
rustfmt.toml Normal file
View File

@@ -0,0 +1,8 @@
comment_width = 300
edition = "2024"
imports_granularity = "Crate"
max_width = 100
newline_style = "Unix"
normalize_comments = true
style_edition = "2024"
wrap_comments = false

View File

@@ -1,79 +0,0 @@
[package]
name = "prover"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[patch.crates-io]
alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.21" }
ruint = { git = "https://github.com/scroll-tech/uint.git", branch = "v1.12.3" }
tiny-keccak = { git = "https://github.com/scroll-tech/tiny-keccak", branch = "scroll-patch-v2.0.2-openvm-v1.0.0-rc.1" }
[dependencies]
anyhow = "1.0"
log = "0.4"
env_logger = "0.11.3"
serde = { version = "1.0.198", features = ["derive"] }
serde_json = "1.0.116"
futures = "0.3.30"
scroll-zkvm-prover-euclid = { git = "https://github.com/scroll-tech/zkvm-prover", tag = "v0.4.2", package = "scroll-zkvm-prover" }
ethers-core = { git = "https://github.com/scroll-tech/ethers-rs.git", branch = "v2.0.7" }
ethers-providers = { git = "https://github.com/scroll-tech/ethers-rs.git", branch = "v2.0.7" }
scroll-proving-sdk = { git = "https://github.com/scroll-tech/scroll-proving-sdk.git", branch = "main", features = [
"openvm",
] }
sbv-primitives = { git = "https://github.com/scroll-tech/stateless-block-verifier", branch = "zkvm/euclid-upgrade", features = [
"scroll",
] }
base64 = "0.13.1"
reqwest = { version = "0.12.4", features = ["gzip"] }
reqwest-middleware = "0.3"
reqwest-retry = "0.5"
once_cell = "1.19.0"
hex = "0.4.3"
tiny-keccak = { version = "2.0.0", features = ["sha3", "keccak"] }
rand = "0.8.5"
eth-keystore = "0.5.0"
rlp = "0.5.2"
tokio = "1.37.0"
async-trait = "0.1"
sled = "0.34.7"
http = "1.1.0"
clap = { version = "4.5", features = ["derive"] }
ctor = "0.2.8"
url = "2.5.4"
serde_bytes = "0.11.15"
[patch."https://github.com/openvm-org/stark-backend.git"]
openvm-stark-backend = { git = "ssh://git@github.com/scroll-tech/openvm-stark-gpu.git", branch = "main", features = ["gpu"] }
openvm-stark-sdk = { git = "ssh://git@github.com/scroll-tech/openvm-stark-gpu.git", branch = "main", features = ["gpu"] }
[patch."https://github.com/Plonky3/Plonky3.git"]
p3-air = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-field = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-commit = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-matrix = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-baby-bear = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", features = [
"nightly-features",
], tag = "v0.2.0" }
p3-koala-bear = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-util = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-challenger = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-dft = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-fri = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-goldilocks = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-keccak = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-keccak-air = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-blake3 = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-mds = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-merkle-tree = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-monty-31 = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-poseidon = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-poseidon2 = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-poseidon2-air = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-symmetric = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
p3-uni-stark = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }
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
p3-bn254-fr = { git = "ssh://git@github.com/scroll-tech/plonky3-gpu.git", tag = "v0.2.0" }

18
zkvm-prover/Dockerfile Normal file
View File

@@ -0,0 +1,18 @@
FROM rust:1.85
RUN rustup toolchain install nightly-2025-02-14-x86_64-unknown-linux-gnu
RUN rustup component add rust-src --toolchain nightly-2025-02-14-x86_64-unknown-linux-gnu
WORKDIR /app
RUN git clone --branch refactor/scroll_mono --single-branch --depth 1 https://github.com/scroll-tech/zkvm-prover.git zkvm-circuits
ADD common/types-rs /app/common/types-rs
ADD zkvm-prover /app/zkvm-prover
ADD Cargo.* /app/
ADD rust-toolchain /app/
ADD build/.cargo /app/zkvm-circuits/.cargo
WORKDIR /app/zkvm-circuits
RUN cargo fetch
ENTRYPOINT ["/app/zkvm-circuits/build-guest-actions-entrypoint.sh"]

View File

@@ -1,48 +1,92 @@
.PHONY: prover lint tests_binary
RUST_MIN_STACK ?= 16777216
export RUST_MIN_STACK
ifeq (4.3,$(firstword $(sort $(MAKE_VERSION) 4.3)))
PLONKY3_VERSION=$(shell grep -m 1 "Plonky3.git" ./Cargo.lock | cut -d "#" -f2 | cut -c-7)
RUST_BACKTRACE ?= 1
export RUST_BACKTRACE
RUST_LOG ?= off,scroll_zkvm_integration=debug,scroll_zkvm_verifier=debug,scroll_zkvm_prover=debug,openvm_circuit=debug
export RUST_LOG
ifdef LEGACY
FEATURE := --no-default-features
TESTDATA_PATH := integration/testdata/phase1
CHUNK_PROOF := 12508460-12508463
else
PLONKY3_VERSION=$(shell grep -m 1 "Plonky3.git" ./Cargo.lock | cut -d "\#" -f2 | cut -c-7)
FEATURE :=
TESTDATA_PATH := integration/testdata/phase2
CHUNK_PROOF := 1-4
endif
ZKVM_VERSION=$(shell ./print_high_zkvm_version.sh)
ifeq (${ZKVM_VERSION},)
$(error ZKVM_VERSION not set)
else
$(info ZKVM_VERSION is ${ZKVM_VERSION})
endif
$(info FEATURE set to: $(FEATURE))
$(info TESTDATA_PATH set to: $(TESTDATA_PATH))
ZKVM_COMMIT=$(shell echo ${ZKVM_VERSION} | cut -d " " -f2)
$(info ZKVM_COMMIT is ${ZKVM_COMMIT})
download-release:
sh download-release.sh
PLONKY3_GPU_VERSION=$(shell ./print_plonky3gpu_version.sh | sed -n '2p')
fmt:
@cargo fmt --all
GIT_REV=$(shell git rev-parse --short HEAD)
GO_TAG=$(shell grep "var tag = " ../common/version/version.go | cut -d "\"" -f2)
clippy:
# @cargo clippy --tests --manifest-path crates/circuits/types/Cargo.toml -- -D warnings
# sh openvm-clippy.sh
@cargo clippy --tests --all-features --manifest-path verifier/Cargo.toml -- -D warnings
@cargo clippy --tests --all-features --manifest-path prover/Cargo.toml -- -D warnings
@cargo clippy --tests --all-features --manifest-path integration/Cargo.toml -- -D warnings
# @cargo clippy --tests --all-features --manifest-path build-guest/Cargo.toml -- -D warnings
ifeq (${GO_TAG},)
$(error GO_TAG not set)
else
$(info GO_TAG is ${GO_TAG})
endif
clean-guest:
docker rmi build-guest:local
ifeq (${PLONKY3_GPU_VERSION},)
# use plonky3 with CPU
ZK_VERSION=${ZKVM_COMMIT}-${PLONKY3_VERSION}
else
# use halo2_gpu
ZK_VERSION=${ZKVM_COMMIT}-${PLONKY3_GPU_VERSION}
endif
build-guest:
sh build-guest.sh
prover:
GO_TAG=${GO_TAG} GIT_REV=${GIT_REV} ZK_VERSION=${ZK_VERSION} cargo build --release
clean-build-guest: clean-guest build-guest
tests_binary:
cargo clean && cargo test --release --no-run
ls target/release/deps/prover* | grep -v "\.d" | xargs -I{} ln -sf {} ./prover.test
clean-test-cache:
@rm -f $(TESTDATA_PATH)/proofs/*.json
lint:
cargo check --all-features
cargo clippy --all-features --all-targets -- -D warnings
cargo fmt --all
$(TESTDATA_PATH)/proofs/chunk-%.json:
@OUTPUT_DIR=$(realpath $(TESTDATA_PATH)/proofs) $(MAKE) test-single-chunk
cp -f $(TESTDATA_PATH)/proofs/chunk/proofs/*.json $(TESTDATA_PATH)/proofs
profile-chunk:
@GUEST_PROFILING=true cargo test --release -p scroll-zkvm-integration --test chunk_circuit guest_profiling -- --exact --nocapture
test-execute-chunk:
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test chunk_circuit test_execute -- --exact --nocapture
test-execute-chunk-multi:
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test chunk_circuit test_execute_multi -- --exact --nocapture
test-cycle:
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test chunk_circuit test_cycle -- --exact --nocapture
test-execute-batch: $(TESTDATA_PATH)/proofs/chunk-$(CHUNK_PROOF).json
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test batch_circuit test_e2e_execute -- --exact --nocapture
test-execute-batch-fast: $(TESTDATA_PATH)/tasks/batch-task.json
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test batch_circuit test_execute -- --exact --nocapture
test-execute-bundle:
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test bundle_circuit test_execute -- --exact --nocapture
test-single-chunk:
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test chunk_circuit setup_prove_verify_single -- --exact --nocapture
test-multi-chunk:
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test chunk_circuit setup_prove_verify_multi -- --exact --nocapture
test-single-batch: $(TESTDATA_PATH)/tasks/batch-task.json
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test batch_circuit setup_prove_verify_single -- --exact --nocapture
test-e2e-batch:
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test batch_circuit e2e -- --exact --nocapture
test-bundle:
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test bundle_circuit setup_prove_verify -- --exact --nocapture
test-bundle-local:
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test bundle_circuit setup_prove_verify_local_task -- --exact --nocapture
test-e2e-bundle:
@cargo test --release -p scroll-zkvm-integration $(FEATURE) --test bundle_circuit e2e -- --exact --nocapture

168
zkvm-prover/README.md Normal file
View File

@@ -0,0 +1,168 @@
# Scroll zkVM
**zkVM-based Circuits (Guest Programs) with a complete Scroll Prover implementation**
## Repository
This repository contains the following member crates:
- [scroll-zkvm-circuit-types](./crates/circuits/types): Primitive and Common types used by the circuits
- [scroll-zkvm-chunk-circuit](./crates/circuits/chunk-circuit): Circuit for verification of a Scroll [chunk](TODO:doc)
- [scroll-zkvm-batch-circuit](./crates/circuits/batch-circuit): Circuit for verification of a Scroll [batch](TODO:doc)
- [scroll-zkvm-bundle-circuit](./crates/circuits/bundle-circuit): Circuit for verification of a Scroll [bundle](TODO:doc)
- [scroll-zkvm-prover](./crates/prover): Implementation for a Scroll Prover
- [scroll-zkvm-verifier](./crates/verifier): Implementation for a Verifier-only mode
- [scroll-zkvm-integration](./crates/integration): Integration tests for the Scroll Prover
## Overview
The Scroll zkVM Circuits are [openvm](https://book.openvm.dev/) based Guest Programs.
The [prover](./crates/prover) crate offers a minimalistic API for setting up, generating and verifying proofs for Scroll's zk-rollup.
For a deeper dive into our implementation, please refer the [interfaces](./docs/interfaces.md) doc.
## Testing
For more commands please refer the [Makefile](./Makefile).
### Build Guest Programs
In case you have made any changes to the guest programs, it is important to build them before running the tests.
```shell
$ make build-guest
```
Upon building the guest programs, the child commitments in [batch-circuit](./crates/circuits/batch-circuit/src/child_commitments.rs) and [bundle-circuit](./crates/circuits/bundle-circuit/src/child_commitments.rs) will be overwritten by `build-guest`.
### End-to-end tests for chunk-prover
```shell
$ RUST_MIN_STACK=16777216 make test-single-chunk
```
### End-to-end tests for batch-prover
```shell
$ RUST_MIN_STACK=16777216 make test-e2e-batch
```
### End-to-end tests for bundle-prover
```shell
$ RUST_MIN_STACK=16777216 make test-e2e-bundle
```
*Note*: Configure `RUST_LOG=debug` for debug logs or `RUST_LOG=none,scroll_zkvm_prover=debug` for logs specifically from the `scroll-zkvm-prover` crate.
## Usage of Prover API
### Dependency
Add the following dependency in your `Cargo.toml`:
```toml
[dependencies]
scroll-zkvm-prover = { git = "https://github.com/scroll-tech/zkvm-prover", branch = "master" }
```
### Chunk Prover
Prover capable of generating STARK proofs for a Scroll [chunk](TODO:doc):
```rust
use std::path::Path;
use scroll_zkvm_prover::{ChunkProver, task::ChunkProvingTask};
// Paths to the application exe and application config.
let path_exe = Path::new("./path/to/app.vmexe");
let path_app_config = Path::new("./path/to/openvm.toml");
// Optional directory to cache generated proofs on disk.
let cache_dir = Path::new("./path/to/cache/proofs");
// Setup prover.
let prover = ChunkProver::setup(&path_exe, &path_app_config, Some(&cache_dir))?;
// Proving task of a chunk with 3 blocks.
let block_witnesses = vec![
sbv::primitives::types::BlockWitness { /* */ },
sbv::primitives::types::BlockWitness { /* */ },
sbv::primitives::types::BlockWitness { /* */ },
];
let task = ChunkProvingTask { block_witnesses };
// Generate a proof.
let proof = prover.gen_proof(&task)?;
// Verify proof.
prover.verify_proof(&proof)?;
```
### Batch Prover
Prover capable of generating STARK proofs for a Scroll [batch](TODO:doc):
```rust
use std::path::Path;
use scroll_zkvm_prover::{BatchProver, task::BatchProvingTask};
// Paths to the application exe and application config.
let path_exe = Path::new("./path/to/app.vmexe");
let path_app_config = Path::new("./path/to/openvm.toml");
// Optional directory to cache generated proofs on disk.
let cache_dir = Path::new("./path/to/cache/proofs");
// Setup prover.
let prover = BatchProver::setup(&path_exe, &path_app_config, Some(&cache_dir))?;
// Task that proves batching of a number of chunks.
let task = BatchProvingTask {
chunk_proofs, // chunk proofs being aggregated in this batch
batch_header, // the header for the batch
blob_bytes, // the EIP-4844 blob that makes this batch data available on L1
};
// Generate a proof.
let proof = prover.gen_proof(&task)?;
// Verify proof.
prover.verify_proof(&proof)?;
```
### Bundle Prover
Prover capable of generating EVM-verifiable halo2-based SNARK proofs for a Scroll [bundle](TODO:doc):
```rust
use std::path::Path;
use scroll_zkvm_prover::{BundleProver, task::BundleProvingTask};
// Paths to the application exe and application config.
let path_exe = Path::new("./path/to/app.vmexe");
let path_app_config = Path::new("./path/to/openvm.toml");
// Optional directory to cache generated proofs on disk.
let cache_dir = Path::new("./path/to/cache/proofs");
// Setup prover.
//
// The Bundle Prover's setup also looks into $HOME/.openvm for halo2-based setup parameters.
let prover = BundleProver::setup(&path_exe, &path_app_config, Some(&cache_dir))?;
// Task that proves batching of a number of chunks.
let task = BundleProvingTask {
batch_proofs, // batch proofs being aggregated in this bundle
};
// Generate a proof.
let evm_proof = prover.gen_proof_evm(&task)?;
// Verify proof.
prover.verify_proof_evm(&evm_proof)?;
```

View File

@@ -0,0 +1,37 @@
[package]
name = "zkvm-prover-bin"
version = "0.1.0"
edition.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
env_logger = "0.11.3"
clap = { version = "4.5", features = ["derive"] }
base64.workspace = true
# Re-export from sbv
anyhow = "1.0"
tracing = "0.1"
futures = "0.3"
alloy = { workspace = true, features = ["provider-http", "transport-http", "reqwest", "reqwest-rustls-tls", "json-rpc"] }
itertools.workspace = true
url = ">=2.5.3"
# Re-export from proving-sdk
reqwest = { version = "0.12.4", features = ["gzip"] }
reqwest-middleware = "0.3"
reqwest-retry = "0.5"
hex = "0.4.3"
tokio = "1.37"
rlp = "0.5"
rand = "0.8"
async-trait = "0.1"
tiny-keccak = { workspace = true, features = ["sha3", "keccak"] }
scroll-zkvm-prover.workspace = true
scroll-proving-sdk = { git = "https://github.com/scroll-tech/scroll-proving-sdk.git", branch = "refactor/scroll"}
sbv-primitives.workspace = true
sbv-utils = { workspace = true, features = ["scroll"] }
serde.workspace = true
serde_json.workspace = true

48
zkvm-prover/bin/Makefile Normal file
View File

@@ -0,0 +1,48 @@
.PHONY: prover lint tests_binary
ifeq (4.3,$(firstword $(sort $(MAKE_VERSION) 4.3)))
PLONKY3_VERSION=$(shell grep -m 1 "Plonky3.git" ./Cargo.lock | cut -d "#" -f2 | cut -c-7)
else
PLONKY3_VERSION=$(shell grep -m 1 "Plonky3.git" ./Cargo.lock | cut -d "\#" -f2 | cut -c-7)
endif
ZKVM_VERSION=$(shell ./print_high_zkvm_version.sh)
ifeq (${ZKVM_VERSION},)
$(error ZKVM_VERSION not set)
else
$(info ZKVM_VERSION is ${ZKVM_VERSION})
endif
ZKVM_COMMIT=$(shell echo ${ZKVM_VERSION} | cut -d " " -f2)
$(info ZKVM_COMMIT is ${ZKVM_COMMIT})
PLONKY3_GPU_VERSION=$(shell ./print_plonky3gpu_version.sh | sed -n '2p')
GIT_REV=$(shell git rev-parse --short HEAD)
GO_TAG=$(shell grep "var tag = " ../common/version/version.go | cut -d "\"" -f2)
ifeq (${GO_TAG},)
$(error GO_TAG not set)
else
$(info GO_TAG is ${GO_TAG})
endif
ifeq (${PLONKY3_GPU_VERSION},)
# use plonky3 with CPU
ZK_VERSION=${ZKVM_COMMIT}-${PLONKY3_VERSION}
else
# use halo2_gpu
ZK_VERSION=${ZKVM_COMMIT}-${PLONKY3_GPU_VERSION}
endif
prover:
GO_TAG=${GO_TAG} GIT_REV=${GIT_REV} ZK_VERSION=${ZK_VERSION} cargo build --release
tests_binary:
cargo clean && cargo test --release --no-run
ls target/release/deps/prover* | grep -v "\.d" | xargs -I{} ln -sf {} ./prover.test
lint:
cargo check --all-features
cargo clippy --all-features --all-targets -- -D warnings
cargo fmt --all

View File

@@ -1,13 +1,12 @@
mod prover;
mod types;
mod zk_circuits_handler;
use clap::{ArgAction, Parser};
use prover::{LocalProver, LocalProverConfig};
use scroll_proving_sdk::{
prover::ProverBuilder,
utils::{get_version, init_tracing},
};
mod prover;
mod types;
mod zk_circuits_handler;
use prover::{LocalProver, LocalProverConfig};
#[derive(Parser, Debug)]
#[command(disable_version_flag = true)]

View File

@@ -1,16 +1,17 @@
use crate::zk_circuits_handler::{
euclid::EuclidHandler, euclidV2::EuclidV2Handler, CircuitsHandler,
CircuitsHandler, RequestPreHandler, RpcConfig, euclid::EuclidHandler, euclidV2::EuclidV2Handler,
};
use anyhow::{anyhow, Result};
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use base64::{Engine, prelude::BASE64_STANDARD};
use scroll_proving_sdk::{
config::Config as SdkConfig,
prover::{
ProvingService,
proving_service::{
GetVkRequest, GetVkResponse, ProveRequest, ProveResponse, QueryTaskRequest,
QueryTaskResponse, TaskStatus,
},
ProvingService,
},
};
use serde::{Deserialize, Serialize};
@@ -25,6 +26,8 @@ use tokio::{runtime::Handle, sync::Mutex, task::JoinHandle};
#[derive(Clone, Serialize, Deserialize)]
pub struct LocalProverConfig {
pub sdk_config: SdkConfig,
#[serde(alias = "l2geth")]
pub rpc_config: RpcConfig,
pub circuits: HashMap<String, CircuitConfig>,
}
@@ -54,6 +57,7 @@ pub struct LocalProver {
current_task: Option<JoinHandle<Result<String>>>,
active_handler: Option<(String, Arc<dyn CircuitsHandler>)>,
pre_handler: Option<RequestPreHandler>,
}
#[async_trait]
@@ -69,7 +73,7 @@ impl ProvingService for LocalProver {
let vk = handler.get_vk(*proof_type).await;
if let Some(vk) = vk {
vks.push(base64::encode(vk));
vks.push(BASE64_STANDARD.encode(vk));
}
}
}
@@ -139,6 +143,7 @@ impl LocalProver {
next_task_id: 0,
current_task: None,
active_handler: None,
pre_handler: None,
}
}
@@ -147,10 +152,21 @@ impl LocalProver {
req: ProveRequest,
handler: Arc<dyn CircuitsHandler>,
) -> Result<ProveResponse> {
if self.pre_handler.is_none() {
self.pre_handler
.replace(RequestPreHandler::create(&self.config.rpc_config)?);
}
self.next_task_id += 1;
let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let created_at = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;
let req = self
.pre_handler
.as_ref()
.expect("has been created")
.on_request(req)
.await?;
let req_clone = req.clone();
let handle = Handle::current();
let task_handle =

View File

@@ -1,21 +1,27 @@
use alloy::primitives::B256;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use scroll_proving_sdk::prover::types::CircuitType;
#[derive(Serialize, Deserialize, Default)]
pub struct Task {
#[serde(rename = "type", default)]
pub task_type: CircuitType,
pub task_data: String,
#[serde(default)]
pub hard_fork_name: String,
}
type CommonHash = B256;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChunkTaskDetail {
pub block_hashes: Vec<CommonHash>,
pub prev_msg_queue_hash: CommonHash,
pub fork_name: String,
}
#[derive(Serialize, Deserialize, Default)]
pub struct ProofDetail {
pub id: String,
#[serde(rename = "type", default)]
pub proof_type: CircuitType,
pub proof_data: String,
pub error: String,
}

View File

@@ -0,0 +1,140 @@
pub mod euclid;
#[allow(non_snake_case)]
pub mod euclidV2;
use anyhow::Result;
use async_trait::async_trait;
use scroll_proving_sdk::prover::{ProofType, proving_service::ProveRequest};
#[async_trait]
pub trait CircuitsHandler: Sync + Send {
async fn get_vk(&self, task_type: ProofType) -> Option<Vec<u8>>;
async fn get_proof_data(&self, prove_request: ProveRequest) -> Result<String>;
}
use alloy::{
providers::{Provider, ProviderBuilder, RootProvider},
rpc::client::ClientBuilder,
transports::layers::RetryBackoffLayer,
};
use sbv_primitives::types::Network;
use serde::{Deserialize, Serialize};
fn default_max_retry() -> u32 {
10
}
fn default_backoff() -> u64 {
100
}
fn default_cups() -> u64 {
100
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct RpcConfig {
#[serde(alias = "endpoint")]
pub rpc_url: String,
// Concurrency Limit, default 10
// pub max_concurrency: usize,
// Retry parameters
#[serde(default = "default_max_retry")]
pub max_retry: u32,
// backoff duration in milliseconds, default 100ms
#[serde(default = "default_backoff")]
pub backoff: u64,
// compute units per second: default 100
#[serde(default = "default_cups")]
pub cups: u64,
}
pub struct RequestPreHandler {
provider: RootProvider<Network>,
}
impl RequestPreHandler {
pub fn create(config: &RpcConfig) -> Result<Self> {
let rpc = url::Url::parse(&config.rpc_url)?;
tracing::info!("Using RPC: {}", rpc);
let retry_layer = RetryBackoffLayer::new(config.max_retry, config.backoff, config.cups);
let client = ClientBuilder::default().layer(retry_layer).http(rpc);
Ok(Self {
provider: ProviderBuilder::<_, _, Network>::default().on_client(client),
})
}
async fn on_chunk_request(&self, input: String) -> Result<String> {
use crate::types::ChunkTaskDetail;
use alloy::network::primitives::BlockTransactionsKind;
use sbv_utils::{rpc::ProviderExt, witness::WitnessBuilder};
use scroll_zkvm_prover::task::chunk::ChunkProvingTask;
let chunk_task: ChunkTaskDetail = serde_json::from_str(&input)?;
let chain_id = self.provider.get_chain_id().await?;
// we need block number but only get hashes, which cause much extra cost for query the block
// number from hash according to https://github.com/scroll-tech/scroll/blob/932be72b88ba2ebb6f9457e8480ee08d612d35a7/coordinator/internal/orm/l2_block.go#L53
// the hashes is ordered by ascending in block number so a heuristic way is applied
let mut block_witnesses = Vec::new();
for block_hash in chunk_task.block_hashes {
// grep `dump_block_witness` in sbv here,
// TODO: we do not need to do that
// if we have block number or `dump_block_witness` support block hashes
let block = self
.provider
.get_block_by_hash(block_hash, BlockTransactionsKind::Full)
.await?
.ok_or_else(|| anyhow::anyhow!("Block not found"))?;
let number = block.header.number;
if number == 0 {
anyhow::bail!("no number in header or use block 0");
}
let builder = WitnessBuilder::new()
.block(block)
.chain_id(chain_id)
.execution_witness(self.provider.debug_execution_witness(number.into()).await?);
let builder = builder
.state_root(
self.provider
.scroll_disk_root(number.into())
.await?
.disk_root,
)
.unwrap()
.prev_state_root(
self.provider
.scroll_disk_root((number - 1).into())
.await?
.disk_root,
);
block_witnesses.push(builder.build()?);
}
let input_repack = ChunkProvingTask {
fork_name: chunk_task.fork_name,
prev_msg_queue_hash: chunk_task.prev_msg_queue_hash,
block_witnesses,
};
// self.provider.dump_block_witness(number)
Ok(serde_json::to_string(&input_repack)?)
}
pub async fn on_request(&self, mut prove_request: ProveRequest) -> Result<ProveRequest> {
match prove_request.proof_type {
ProofType::Chunk => {
prove_request.input = self.on_chunk_request(prove_request.input).await?;
}
_ => (),
}
Ok(prove_request)
}
}

View File

@@ -1,12 +1,12 @@
use std::{path::Path, sync::Arc};
use super::CircuitsHandler;
use anyhow::{anyhow, Result};
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use scroll_proving_sdk::prover::{proving_service::ProveRequest, ProofType};
use scroll_zkvm_prover_euclid::{
task::{batch::BatchProvingTask, bundle::BundleProvingTask, chunk::ChunkProvingTask},
use scroll_proving_sdk::prover::{ProofType, proving_service::ProveRequest};
use scroll_zkvm_prover::{
BatchProver, BundleProverEuclidV1, ChunkProver, ProverConfig,
task::{batch::BatchProvingTask, bundle::BundleProvingTask, chunk::ChunkProvingTask},
};
use tokio::sync::Mutex;
pub struct EuclidHandler {

View File

@@ -1,12 +1,12 @@
use std::{path::Path, sync::Arc};
use super::{euclid::Phase, CircuitsHandler};
use anyhow::{anyhow, Result};
use super::{CircuitsHandler, euclid::Phase};
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use scroll_proving_sdk::prover::{proving_service::ProveRequest, ProofType};
use scroll_zkvm_prover_euclid::{
task::{batch::BatchProvingTask, bundle::BundleProvingTask, chunk::ChunkProvingTask},
use scroll_proving_sdk::prover::{ProofType, proving_service::ProveRequest};
use scroll_zkvm_prover::{
BatchProver, BundleProverEuclidV2, ChunkProver,
task::{batch::BatchProvingTask, bundle::BundleProvingTask, chunk::ChunkProvingTask},
};
use tokio::sync::Mutex;
pub struct EuclidV2Handler {

View File

@@ -0,0 +1,4 @@
#!/bin/bash
# run crates/build-guest
cargo run --release -p scroll-zkvm-build-guest

69
zkvm-prover/build-guest.sh Executable file
View File

@@ -0,0 +1,69 @@
#!/usr/bin/env bash
set -ex
[ -f "crates/build-guest/.env" ] && . crates/build-guest/.env
# if BUILD_STAGES if empty, set it to stage1,stage2,stage3
if [ -z "${BUILD_STAGES}" ]; then
BUILD_STAGES="stage1,stage2,stage3"
fi
# build docker image
docker build --platform linux/amd64 -t build-guest:local -f ./Dockerfile ../
# run docker image
docker run --cidfile ./build-guest.cid --platform linux/amd64 -e FEATURE=${FEATURE} build-guest:local
container_id=$(cat ./build-guest.cid)
# copy staffs from sources in container to local
for f in chunk-circuit \
batch-circuit \
bundle-circuit; do
docker cp ${container_id}:/app/zkvm-circuits/crates/circuits/${f}/openvm.toml build/${f}/
done
if [ -n "$(echo ${BUILD_STAGES} | grep stage1)" ]; then
# copy leaf commitments from container to local
for f in chunk-circuit/chunk_leaf_commit.rs \
batch-circuit/batch_leaf_commit.rs \
bundle-circuit/bundle_leaf_commit.rs; do
docker cp ${container_id}:/app/zkvm-circuits/crates/circuits/${f} build/${f}
done
docker cp ${container_id}:/app/zkvm-circuits/crates/circuits/bundle-circuit/digest_2 build/bundle-circuit/digest_2
fi
if [ -n "$(echo ${BUILD_STAGES} | grep stage2)" ]; then
# copy root verifier
docker cp ${container_id}:/app/zkvm-circuits/crates/build-guest/root_verifier.asm build/root_verifier.asm
fi
if [ -n "$(echo ${BUILD_STAGES} | grep stage3)" ]; then
# copy exe commitments from container to local
for f in chunk-circuit/chunk_exe_commit.rs \
chunk-circuit/chunk_exe_rv32_commit.rs \
batch-circuit/batch_exe_commit.rs \
bundle-circuit/bundle_exe_commit.rs \
bundle-circuit/bundle_exe_euclidv1_commit.rs; do
docker cp ${container_id}:/app/zkvm-circuits/crates/circuits/${f} build/${f}
done
# copy digests from container to local
docker cp ${container_id}:/app/zkvm-circuits/crates/circuits/bundle-circuit/digest_1 build/bundle-circuit/digest_1
docker cp ${container_id}:/app/zkvm-circuits/crates/circuits/bundle-circuit/digest_1_euclidv1 build/bundle-circuit/digest_1_euclidv1
# copy app.vmexe from container to local
mkdir -p build/chunk-circuit/openvm
mkdir -p build/batch-circuit/openvm
mkdir -p build/bundle-circuit/openvm
docker cp ${container_id}:/app/zkvm-circuits/crates/circuits/chunk-circuit/openvm/app.vmexe build/chunk-circuit/openvm/app.vmexe
docker cp ${container_id}:/app/zkvm-circuits/crates/circuits/chunk-circuit/openvm/app_rv32.vmexe build/chunk-circuit/openvm/app_rv32.vmexe
docker cp ${container_id}:/app/zkvm-circuits/crates/circuits/batch-circuit/openvm/app.vmexe build/batch-circuit/openvm/app.vmexe
docker cp ${container_id}:/app/zkvm-circuits/crates/circuits/bundle-circuit/openvm/app.vmexe build/bundle-circuit/openvm/app.vmexe
docker cp ${container_id}:/app/zkvm-circuits/crates/circuits/bundle-circuit/openvm/app_euclidv1.vmexe build/bundle-circuit/openvm/app_euclidv1.vmexe
fi
# remove docker container
docker rm ${container_id}
rm ./build-guest.cid

3
zkvm-prover/build/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
*.vmexe
root_verifier.asm
bundle-circuit/digest_*

View File

@@ -0,0 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!
pub const COMMIT: [u32; 8] = [1660301305, 168653291, 489155857, 877082518, 574909420, 273656918, 548174885, 1859120536];

View File

@@ -0,0 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!
pub const COMMIT: [u32; 8] = [1583597683, 1891396505, 1658359221, 937358707, 1082503370, 1627277738, 322886937, 1126664665];

View File

@@ -0,0 +1,34 @@
[app_fri_params.fri_params]
log_blowup = 1
log_final_poly_len = 0
num_queries = 100
proof_of_work_bits = 16
[app_vm_config.rv32i]
[app_vm_config.rv32m]
[app_vm_config.io]
[app_vm_config.keccak]
[app_vm_config.castf]
[app_vm_config.modular]
supported_modulus = [
"4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787",
"52435875175126190479447740508185965837690552500527637822603658699938581184513",
]
[app_vm_config.native]
[app_vm_config.pairing]
supported_curves = ["Bls12_381"]
[app_vm_config.sha256]
[app_vm_config.fp2]
supported_modulus = [
"4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787",
]
[[app_vm_config.ecc.supported_curves]]
modulus = "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787"
scalar = "52435875175126190479447740508185965837690552500527637822603658699938581184513"
a = "0"
b = "4"

View File

@@ -0,0 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!
pub const COMMIT: [u32; 8] = [649838675, 786832254, 1395878687, 1458584106, 1291473044, 419188889, 113604861, 1633400343];

View File

@@ -0,0 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!
pub const COMMIT: [u32; 8] = [486184220, 544209032, 1886689311, 570463891, 461318186, 106392984, 525080597, 1270661183];

View File

@@ -0,0 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!
pub const COMMIT: [u32; 8] = [1227657215, 852763880, 97050317, 868662534, 861316317, 1072133630, 767460211, 493765067];

View File

@@ -0,0 +1,17 @@
[app_fri_params.fri_params]
log_blowup = 1
log_final_poly_len = 0
num_queries = 100
proof_of_work_bits = 16
[app_vm_config.rv32i]
[app_vm_config.rv32m]
[app_vm_config.io]
[app_vm_config.keccak]
[app_vm_config.castf]
[app_vm_config.native]

View File

@@ -0,0 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!
pub const COMMIT: [u32; 8] = [1098565535, 1693373045, 331673897, 142368506, 1824595882, 1020436445, 4334643, 929005758];

View File

@@ -0,0 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!
pub const COMMIT: [u32; 8] = [1876028461, 1806970388, 1493987331, 600051485, 803969577, 1396836730, 826662536, 1405122028];

View File

@@ -0,0 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!
pub const COMMIT: [u32; 8] = [1443243841, 1004366406, 2003092941, 1325139171, 1093586017, 1842955945, 1478923252, 128760176];

View File

@@ -0,0 +1,53 @@
[app_fri_params.fri_params]
log_blowup = 1
log_final_poly_len = 0
num_queries = 100
proof_of_work_bits = 16
[app_vm_config.rv32i]
[app_vm_config.io]
[app_vm_config.keccak]
[app_vm_config.rv32m]
range_tuple_checker_sizes = [256, 8192]
[app_vm_config.bigint]
range_tuple_checker_sizes = [256, 8192]
[app_vm_config.modular]
supported_modulus = [
"21888242871839275222246405745257275088696311157297823662689037894645226208583",
"21888242871839275222246405745257275088548364400416034343698204186575808495617",
"115792089237316195423570985008687907853269984665640564039457584007908834671663",
"115792089237316195423570985008687907852837564279074904382605163141518161494337",
"115792089210356248762697446949407573530086143415290314195533631308867097853951",
"115792089210356248762697446949407573529996955224135760342422259061068512044369"
]
[app_vm_config.fp2]
supported_modulus = ["21888242871839275222246405745257275088696311157297823662689037894645226208583"]
[app_vm_config.pairing]
supported_curves = ["Bn254"]
[app_vm_config.sha256]
[[app_vm_config.ecc.supported_curves]]
modulus = "115792089237316195423570985008687907853269984665640564039457584007908834671663"
scalar = "115792089237316195423570985008687907852837564279074904382605163141518161494337"
a = "0"
b = "7"
[[app_vm_config.ecc.supported_curves]]
modulus = "115792089210356248762697446949407573530086143415290314195533631308867097853951"
scalar = "115792089210356248762697446949407573529996955224135760342422259061068512044369"
a = "115792089210356248762697446949407573530086143415290314195533631308867097853948"
b = "41058363725152142129326129780047268409114441015993725554835256314039467401291"
[[app_vm_config.ecc.supported_curves]]
modulus = "21888242871839275222246405745257275088696311157297823662689037894645226208583"
scalar = "21888242871839275222246405745257275088548364400416034343698204186575808495617"
a = "0"
b = "3"

View File

@@ -8,9 +8,6 @@
"retry_wait_time_sec": 10,
"connection_timeout_sec": 30
},
"l2geth": {
"endpoint": "http://localhost:9999"
},
"prover": {
"circuit_type": 2,
"supported_proof_types": [
@@ -22,6 +19,9 @@
},
"db_path": "unique-db-path-for-prover-1"
},
"l2geth": {
"endpoint": "http://localhost:9999"
},
"circuits": {
"euclidV2": {
"hard_fork_name": "euclidV2",

View File

@@ -0,0 +1,24 @@
#!/bin/bash
# release version
if [ -z "${SCROLL_ZKVM_VERSION}" ]; then
echo "SCROLL_ZKVM_VERSION not set"
exit 1
fi
# chunk-circuit exe
wget https://circuit-release.s3.us-west-2.amazonaws.com/scroll-zkvm/releases/$SCROLL_ZKVM_VERSION/chunk/app.vmexe -O crates/circuits/chunk-circuit/openvm/app.vmexe
# batch-circuit exe
wget https://circuit-release.s3.us-west-2.amazonaws.com/scroll-zkvm/releases/$SCROLL_ZKVM_VERSION/batch/app.vmexe -O crates/circuits/batch-circuit/openvm/app.vmexe
# bundle-circuit exe
wget https://circuit-release.s3.us-west-2.amazonaws.com/scroll-zkvm/releases/$SCROLL_ZKVM_VERSION/bundle/app.vmexe -O crates/circuits/bundle-circuit/openvm/app.vmexe
# bundle-circuit exe, legacy version, may not exist
wget https://circuit-release.s3.us-west-2.amazonaws.com/scroll-zkvm/releases/$SCROLL_ZKVM_VERSION/bundle/app_euclidv1.vmexe -O crates/circuits/bundle-circuit/openvm/app_euclidv1.vmexe || echo "legacy app not exist for $SCROLL_ZKVM_VERSION"
# assets for verifier-only mode
wget https://circuit-release.s3.us-west-2.amazonaws.com/scroll-zkvm/releases/$SCROLL_ZKVM_VERSION/verifier/root-verifier-vm-config -O crates/verifier/testdata/root-verifier-vm-config
wget https://circuit-release.s3.us-west-2.amazonaws.com/scroll-zkvm/releases/$SCROLL_ZKVM_VERSION/verifier/root-verifier-committed-exe -O crates/verifier/testdata/root-verifier-committed-exe
wget https://circuit-release.s3.us-west-2.amazonaws.com/scroll-zkvm/releases/$SCROLL_ZKVM_VERSION/verifier/verifier.bin -O crates/verifier/testdata/verifier.bin

1
zkvm-prover/integration/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.txt

View File

@@ -0,0 +1,43 @@
[package]
name = "scroll-zkvm-integration"
version.workspace = true
edition.workspace = true
[dependencies]
scroll-zkvm-circuit-input-types.workspace = true
scroll-zkvm-prover.workspace = true
scroll-zkvm-verifier.workspace = true
sbv-primitives = { workspace = true }
tracing.workspace = true
metrics-tracing-context.workspace = true
openvm-build = { workspace = true, default-features = false }
openvm-circuit.workspace = true
openvm-sdk = { workspace = true, default-features = false }
openvm-native-circuit = { workspace = true, default-features = false }
openvm-native-compiler = { workspace = true, default-features = false }
openvm-native-recursion = { workspace = true, default-features = false }
openvm-native-transpiler = { workspace = true, default-features = false }
openvm-transpiler = { workspace = true, default-features = false }
alloy-primitives.workspace = true
eyre.workspace = true
serde_json.workspace = true
tiny-keccak.workspace = true
vm-zstd = { workspace = true, features = ["zstd"] }
once_cell.workspace = true
rayon = "1"
chrono = "0.4"
#ff = "0.13"
glob = "0.3"
#sha2 = "0.10"
tracing-subscriber = "0.3"
[dev-dependencies]
halo2curves-axiom = "0.7.0"
[features]
default = ["euclidv2"]
euclidv2 = []
limit-logs = []

View File

@@ -0,0 +1,351 @@
use std::{
path::{Path, PathBuf},
process,
};
use once_cell::sync::OnceCell;
use openvm_sdk::{
F, Sdk,
config::{AppConfig, SdkVmConfig},
};
use scroll_zkvm_prover::{
ProverType, WrappedProof,
setup::{read_app_config, read_app_exe},
task::ProvingTask,
};
use tracing::instrument;
use tracing_subscriber::{fmt::format::FmtSpan, layer::SubscriberExt, util::SubscriberInitExt};
pub mod testers;
pub mod utils;
/// Path to store release assets, root directory of zkvm-prover repository.
const DIR_OUTPUT: &str = "./../../.output";
/// Directory to store proofs on disc.
const DIR_PROOFS: &str = "proofs";
/// File descriptor for app openvm config.
const FD_APP_CONFIG: &str = "openvm.toml";
/// File descriptor for app exe.
const FD_APP_EXE: &str = "app.vmexe";
/// Environment variable used to set the test-run's output directory for assets.
const ENV_OUTPUT_DIR: &str = "OUTPUT_DIR";
/// Every test run will write assets to a new directory.
///
/// Possibly one of the following:
/// - <DIR_OUTPUT>/chunk-tests-{timestamp}
/// - <DIR_OUTPUT>/batch-tests-{timestamp}
/// - <DIR_OUTPUT>/bundle-tests-{timestamp}
static DIR_TESTRUN: OnceCell<PathBuf> = OnceCell::new();
/// Circuit that implements functionality required to run e2e tests.
pub trait ProverTester {
/// Prover type that is being tested.
type Prover: ProverType;
/// Path to the corresponding circuit's project directory.
const PATH_PROJECT_ROOT: &str;
/// Prefix to use while naming app-specific data like app exe, app pk, etc.
const DIR_ASSETS: &str;
/// Setup directory structure for the test suite.
fn setup() -> eyre::Result<()> {
// Setup tracing subscriber.
setup_logger()?;
// If user has set an output directory, use it.
let dir_testrun = if let Ok(env_dir) = std::env::var(ENV_OUTPUT_DIR) {
let dir = Path::new(&env_dir);
if std::fs::exists(dir).is_err() {
tracing::error!("OUTPUT_DIR={dir:?} not found");
process::exit(1);
}
dir.into()
} else {
// Create the <OUTPUT>/{DIR_ASSETS}-test-{timestamp} for test-run.
let testrun = format!(
"{}-tests-{}",
Self::DIR_ASSETS,
chrono::Utc::now().format("%Y%m%d_%H%M%S"),
);
Path::new(DIR_OUTPUT).join(testrun)
};
// Set the path for the current test-run.
DIR_TESTRUN
.set(dir_testrun)
.map_err(|dir| eyre::eyre!("could not set test-run dir: {dir:?}"))?;
Ok(())
}
/// Load the app config.
fn load_with_exe_fd(
app_exe_fd: &str,
) -> eyre::Result<(PathBuf, AppConfig<SdkVmConfig>, PathBuf)> {
let path_app_config = Path::new(Self::PATH_PROJECT_ROOT).join(FD_APP_CONFIG);
let app_config = read_app_config(&path_app_config)?;
let path_assets = Path::new(Self::PATH_PROJECT_ROOT).join("openvm");
let path_app_exe = path_assets.join(app_exe_fd);
Ok((path_app_config, app_config, path_app_exe))
}
/// Load the app config.
fn load() -> eyre::Result<(PathBuf, AppConfig<SdkVmConfig>, PathBuf)> {
Self::load_with_exe_fd(&Self::fd_app_exe())
}
/// Get the path to the app exe.
fn fd_app_exe() -> String {
FD_APP_EXE.to_string()
}
/// Generate proving task for test purposes.
fn gen_proving_task() -> eyre::Result<<Self::Prover as ProverType>::ProvingTask>;
/// Generate multiple proving tasks for test purposes.
fn gen_multi_proving_tasks() -> eyre::Result<Vec<<Self::Prover as ProverType>::ProvingTask>> {
unimplemented!("must be implemented by MultiTester");
}
/// Light weight testing to simply execute the vm program for test
#[instrument("ProverTester::execute", skip_all, fields(task_id))]
fn execute(
app_config: AppConfig<SdkVmConfig>,
task: &<Self::Prover as ProverType>::ProvingTask,
exe_path: impl AsRef<Path>,
) -> eyre::Result<Vec<F>> {
let stdin = task.build_guest_input()?;
Ok(Sdk::new().execute(read_app_exe(exe_path)?, app_config.app_vm_config, stdin)?)
}
fn execute_with_proving_task(
app_config: AppConfig<SdkVmConfig>,
exe_path: impl AsRef<Path>,
) -> eyre::Result<Vec<F>> {
Self::execute(app_config, &Self::gen_proving_task()?, exe_path)
}
}
/// The outcome of a successful prove-verify run.
pub struct ProveVerifyOutcome<T, P> {
/// Single or multiple proving tasks.
pub tasks: Vec<T>,
/// Verified proofs for the proving tasks.
pub proofs: Vec<P>,
}
impl<T: Clone, P: Clone> ProveVerifyOutcome<T, P> {
pub fn single(task: T, proof: P) -> Self {
Self {
tasks: vec![task],
proofs: vec![proof],
}
}
pub fn multi(tasks: &[T], proofs: &[P]) -> Self {
Self {
tasks: tasks.to_vec(),
proofs: proofs.to_vec(),
}
}
}
/// Setup test environment
fn setup_logger() -> eyre::Result<()> {
let fmt_layer = tracing_subscriber::fmt::layer()
.pretty()
.with_span_events(FmtSpan::CLOSE);
#[cfg(feature = "limit-logs")]
{
let filters = tracing_subscriber::filter::Targets::new()
.with_target("scroll_zkvm_prover", tracing::Level::INFO)
.with_target("scroll_zkvm_integration", tracing::Level::DEBUG);
tracing_subscriber::registry()
.with(fmt_layer)
.with(filters)
.try_init()?;
}
#[cfg(not(feature = "limit-logs"))]
{
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::from_default_env())
.with(fmt_layer)
.with(metrics_tracing_context::MetricsLayer::new())
.try_init()?;
}
Ok(())
}
/// Alias for convenience.
type ProveVerifyRes<T> = eyre::Result<
ProveVerifyOutcome<
<<T as ProverTester>::Prover as ProverType>::ProvingTask,
WrappedProof<<<T as ProverTester>::Prover as ProverType>::ProofMetadata>,
>,
>;
/// Alias for convenience.
type ProveVerifyEvmRes<T> = eyre::Result<(
ProveVerifyOutcome<
<<T as ProverTester>::Prover as ProverType>::ProvingTask,
WrappedProof<<<T as ProverTester>::Prover as ProverType>::ProofMetadata>,
>,
scroll_zkvm_verifier::verifier::Verifier<scroll_zkvm_verifier::verifier::AnyVerifier>,
PathBuf,
)>;
/// End-to-end test for a single proving task.
#[instrument(name = "prove_verify_single", skip_all)]
pub fn prove_verify_single<T>(
task: Option<<T::Prover as ProverType>::ProvingTask>,
) -> ProveVerifyRes<T>
where
T: ProverTester,
<T::Prover as ProverType>::ProvingTask: Clone,
<T::Prover as ProverType>::ProofMetadata: Clone,
<T::Prover as ProverType>::ProofType: Clone,
{
let (path_app_config, _, path_app_exe) = T::load()?;
let cache_dir = DIR_TESTRUN
.get()
.ok_or(eyre::eyre!("missing assets dir"))?
.join(T::DIR_ASSETS)
.join(DIR_PROOFS);
std::fs::create_dir_all(&cache_dir)?;
// Generate proving task for the circuit.
let task = if let Some(t) = task {
t
} else {
T::gen_proving_task()?
};
// Setup prover.
let config = scroll_zkvm_prover::ProverConfig {
path_app_exe,
path_app_config,
dir_cache: Some(cache_dir),
..Default::default()
};
let prover = scroll_zkvm_prover::Prover::<T::Prover>::setup(config)?;
// Construct root proof for the circuit.
let proof = prover.gen_proof(&task)?;
// Verify proof.
prover.verify_proof(&proof)?;
Ok(ProveVerifyOutcome::single(task, proof))
}
/// End-to-end test for multiple proving tasks of the same prover.
#[instrument(name = "prove_verify_multi", skip_all)]
pub fn prove_verify_multi<T>(
tasks: Option<&[<T::Prover as ProverType>::ProvingTask]>,
) -> ProveVerifyRes<T>
where
T: ProverTester,
<T::Prover as ProverType>::ProvingTask: Clone,
<T::Prover as ProverType>::ProofMetadata: Clone,
<T::Prover as ProverType>::ProofType: Clone,
{
let (path_app_config, _, path_app_exe) = T::load()?;
// Setup prover.
let cache_dir = DIR_TESTRUN
.get()
.ok_or(eyre::eyre!("missing assets dir"))?
.join(T::DIR_ASSETS)
.join(DIR_PROOFS);
std::fs::create_dir_all(&cache_dir)?;
let config = scroll_zkvm_prover::ProverConfig {
path_app_exe,
path_app_config,
dir_cache: Some(cache_dir),
..Default::default()
};
let prover = scroll_zkvm_prover::Prover::<T::Prover>::setup(config)?;
// Generate proving task for the circuit.
let tasks = tasks.map_or_else(|| T::gen_multi_proving_tasks(), |tasks| Ok(tasks.to_vec()))?;
// For each of the tasks, generate and verify proof.
let proofs = tasks
.iter()
.map(|task| {
let proof = prover.gen_proof(task)?;
prover.verify_proof(&proof)?;
Ok(proof)
})
.collect::<eyre::Result<Vec<WrappedProof<<T::Prover as ProverType>::ProofMetadata>>>>()?;
Ok(ProveVerifyOutcome::multi(&tasks, &proofs))
}
/// End-to-end test for a single proving task to generate an EVM-verifiable SNARK proof.
#[instrument(name = "prove_verify_single_evm", skip_all)]
pub fn prove_verify_single_evm<T>(
task: Option<<T::Prover as ProverType>::ProvingTask>,
) -> ProveVerifyEvmRes<T>
where
T: ProverTester,
<T::Prover as ProverType>::ProvingTask: Clone,
<T::Prover as ProverType>::ProofMetadata: Clone,
<T::Prover as ProverType>::ProofType: Clone,
{
let (path_app_config, _, path_app_exe) = T::load()?;
// Setup prover.
let path_assets = DIR_TESTRUN
.get()
.ok_or(eyre::eyre!("missing testrun dir"))?
.join(T::DIR_ASSETS);
let cache_dir = path_assets.join(DIR_PROOFS);
std::fs::create_dir_all(&cache_dir)?;
let config = scroll_zkvm_prover::ProverConfig {
path_app_exe,
path_app_config,
dir_cache: Some(cache_dir),
..Default::default()
};
let prover = scroll_zkvm_prover::Prover::<T::Prover>::setup(config)?;
// Dump verifier-only assets to disk.
let (path_vm_config, path_root_committed_exe) = prover.dump_verifier(&path_assets)?;
let path_verifier_code = Path::new(T::PATH_PROJECT_ROOT)
.join("openvm")
.join("verifier.bin");
let verifier = scroll_zkvm_verifier::verifier::Verifier::setup(
&path_vm_config,
&path_root_committed_exe,
&path_verifier_code,
)?;
// Generate proving task for the circuit.
let task = task.map_or_else(|| T::gen_proving_task(), Ok)?;
// Construct root proof for the circuit.
let proof = prover.gen_proof_evm(&task)?;
// Verify proof.
prover.verify_proof_evm(&proof)?;
Ok((
ProveVerifyOutcome::single(task, proof),
verifier,
path_assets,
))
}

View File

@@ -0,0 +1,64 @@
use std::path::Path;
use scroll_zkvm_prover::{
BatchProverType, ChunkProof, ProverType, task::ProvingTask, utils::read_json_deep,
};
use crate::{
ProverTester,
testers::{PATH_TESTDATA, chunk::ChunkProverTester},
utils::{build_batch_task, phase_base_directory},
};
pub struct BatchProverTester;
impl ProverTester for BatchProverTester {
type Prover = BatchProverType;
const PATH_PROJECT_ROOT: &str = "./../build/batch-circuit";
const DIR_ASSETS: &str = "batch";
fn gen_proving_task() -> eyre::Result<<Self::Prover as ProverType>::ProvingTask> {
Ok(read_json_deep(
Path::new(PATH_TESTDATA)
.join(phase_base_directory())
.join("tasks")
.join("batch-task.json"),
)?)
}
}
pub struct BatchTaskBuildingTester;
impl ProverTester for BatchTaskBuildingTester {
type Prover = BatchProverType;
const PATH_PROJECT_ROOT: &str = "./../build/batch-circuit";
const DIR_ASSETS: &str = "batch";
fn gen_proving_task() -> eyre::Result<<Self::Prover as ProverType>::ProvingTask> {
let chunk_task = ChunkProverTester::gen_proving_task()?;
let proof_path = Path::new(PATH_TESTDATA)
.join(phase_base_directory())
.join("proofs")
.join(format!("chunk-{}.json", chunk_task.identifier()));
println!("proof_path: {:?}", proof_path);
let chunk_proof = read_json_deep::<_, ChunkProof>(&proof_path)?;
let task = build_batch_task(&[chunk_task], &[chunk_proof], Default::default());
Ok(task)
}
}
#[test]
fn batch_task_parsing() {
use scroll_zkvm_prover::task::ProvingTask;
let task = BatchProverTester::gen_proving_task().unwrap();
let _ = task.build_guest_input().unwrap();
}

View File

@@ -0,0 +1,66 @@
use std::path::Path;
use scroll_zkvm_prover::{ProverType, task::bundle::BundleProvingTask, utils::read_json_deep};
#[cfg(not(feature = "euclidv2"))]
use scroll_zkvm_prover::BundleProverTypeEuclidV1 as BundleProverType;
#[cfg(feature = "euclidv2")]
use scroll_zkvm_prover::BundleProverTypeEuclidV2 as BundleProverType;
use crate::{ProverTester, testers::PATH_TESTDATA};
#[cfg(not(feature = "euclidv2"))]
use openvm_sdk::config::{AppConfig, SdkVmConfig};
#[cfg(not(feature = "euclidv2"))]
use std::path::PathBuf;
pub struct BundleProverTester;
impl ProverTester for BundleProverTester {
type Prover = BundleProverType;
const PATH_PROJECT_ROOT: &str = "./../build/bundle-circuit";
const DIR_ASSETS: &str = "bundle";
fn gen_proving_task() -> eyre::Result<<Self::Prover as ProverType>::ProvingTask> {
Ok(BundleProvingTask {
batch_proofs: vec![
read_json_deep(Path::new(PATH_TESTDATA).join("proofs").join(
"batch-0x6a2d14504ccc86a2d1a3fb00f95e50cf2de80230fc51306d16b5f4ccc17b8e73.json",
))?,
read_json_deep(Path::new(PATH_TESTDATA).join("proofs").join(
"batch-0x5f769da6d14efecf756c2a82c164416f31b3986d6c701479107acb1bcd421b21.json",
))?,
],
bundle_info: None,
fork_name: "euclidv1".to_string(),
})
}
#[cfg(not(feature = "euclidv2"))]
fn load() -> eyre::Result<(PathBuf, AppConfig<SdkVmConfig>, PathBuf)> {
Self::load_with_exe_fd("app_euclidv1.vmexe")
}
}
pub struct BundleLocalTaskTester;
impl ProverTester for BundleLocalTaskTester {
type Prover = BundleProverType;
const PATH_PROJECT_ROOT: &str = "./../build/bundle-circuit";
const DIR_ASSETS: &str = "bundle";
fn gen_proving_task() -> eyre::Result<<Self::Prover as ProverType>::ProvingTask> {
Ok(read_json_deep(
Path::new(PATH_TESTDATA).join("bundle-task.json"),
)?)
}
#[cfg(not(feature = "euclidv2"))]
fn load() -> eyre::Result<(PathBuf, AppConfig<SdkVmConfig>, PathBuf)> {
Self::load_with_exe_fd("app_euclidv1.vmexe")
}
}

View File

@@ -0,0 +1,184 @@
use std::{
fs::File,
path::{Path, PathBuf},
};
use sbv_primitives::{B256, types::BlockWitness};
use scroll_zkvm_prover::{
ChunkProverType, ChunkProverTypeRv32, ProverType, task::chunk::ChunkProvingTask,
};
use crate::{ProverTester, testers::PATH_TESTDATA, utils::phase_base_directory};
/// Load a file <block_n>.json in the <PATH_BLOCK_WITNESS> directory.
pub fn read_block_witness_from_testdata(block_n: usize) -> eyre::Result<BlockWitness> {
read_block_witness(
Path::new(PATH_TESTDATA)
.join(phase_base_directory())
.join("witnesses")
.join(format!("{}.json", block_n)),
)
}
/// Utility function to read and deserialize block witness given the block number.
pub fn read_block_witness<P>(path_witness: P) -> eyre::Result<BlockWitness>
where
P: AsRef<Path>,
{
if !path_witness.as_ref().exists() {
println!("File not found: {:?}", path_witness.as_ref());
return Err(eyre::eyre!("File not found: {:?}", path_witness.as_ref()));
}
let witness = File::open(path_witness)?;
Ok(serde_json::from_reader::<_, BlockWitness>(witness)?)
}
pub struct ChunkProverTester;
impl ProverTester for ChunkProverTester {
type Prover = ChunkProverType;
const PATH_PROJECT_ROOT: &str = "./../build/chunk-circuit";
const DIR_ASSETS: &str = "chunk";
/// [block-1, block-2, block-3, block-4]
fn gen_proving_task() -> eyre::Result<<Self::Prover as ProverType>::ProvingTask> {
let paths: Vec<PathBuf> = match std::env::var("TRACE_PATH") {
Ok(paths) => {
let paths: Vec<_> = glob::glob(&paths)?.filter_map(|entry| entry.ok()).collect();
if paths.is_empty() {
return Err(eyre::eyre!("No files found in the given path"));
}
paths
}
Err(_) => {
#[cfg(not(feature = "euclidv2"))]
let blocks = 12508460usize..=12508463usize;
#[cfg(feature = "euclidv2")]
let blocks = 1usize..=4usize;
blocks
.into_iter()
.map(|block_n| {
Path::new(PATH_TESTDATA)
.join(phase_base_directory())
.join("witnesses")
.join(format!("{}.json", block_n))
})
.collect()
}
};
Ok(ChunkProvingTask {
block_witnesses: paths
.iter()
.map(read_block_witness)
.collect::<eyre::Result<Vec<BlockWitness>>>()?,
prev_msg_queue_hash: if cfg!(feature = "euclidv2") {
B256::repeat_byte(1u8)
} else {
B256::ZERO
},
fork_name: if cfg!(feature = "euclidv2") {
String::from("euclidv2")
} else {
String::from("euclidv1")
},
})
}
}
pub struct ChunkProverRv32Tester;
impl ProverTester for ChunkProverRv32Tester {
type Prover = ChunkProverTypeRv32;
const PATH_PROJECT_ROOT: &str = "./../build/chunk-circuit";
const DIR_ASSETS: &str = "chunk";
fn fd_app_exe() -> String {
"app_rv32.vmexe".to_string()
}
fn gen_proving_task() -> eyre::Result<<Self::Prover as ProverType>::ProvingTask> {
// Reuse the same implementation as ChunkProverTester
ChunkProverTester::gen_proving_task()
}
}
/// helper func to gen a series of proving tasks, specified by the block number
pub fn gen_multi_tasks(
blocks: impl IntoIterator<Item = Vec<i32>>,
) -> eyre::Result<Vec<<ChunkProverType as ProverType>::ProvingTask>> {
let paths: Vec<Vec<PathBuf>> = match std::env::var("TRACE_PATH") {
Ok(paths) => glob::glob(&paths)?
.filter_map(|entry| entry.ok())
.map(|p| vec![p])
.collect(),
Err(_) => blocks
.into_iter()
.map(|block_group| {
block_group
.into_iter()
.map(|block_n| {
Path::new(PATH_TESTDATA)
.join(phase_base_directory())
.join("witnesses")
.join(format!("{}.json", block_n))
})
.collect()
})
.collect(),
};
let tasks = paths
.into_iter()
.map(|block_group| -> eyre::Result<_> {
let block_witnesses = block_group
.iter()
.map(read_block_witness)
.collect::<eyre::Result<Vec<BlockWitness>>>()?;
Ok(ChunkProvingTask {
block_witnesses,
prev_msg_queue_hash: if cfg!(feature = "euclidv2") {
B256::repeat_byte(1u8)
} else {
B256::ZERO
},
fork_name: if cfg!(feature = "euclidv2") {
String::from("euclidv2")
} else {
String::from("euclidv1")
},
})
})
.collect::<eyre::Result<Vec<ChunkProvingTask>>>()?;
Ok(tasks)
}
pub struct MultiChunkProverTester;
impl ProverTester for MultiChunkProverTester {
type Prover = ChunkProverType;
const PATH_PROJECT_ROOT: &str = "./../build/chunk-circuit";
const DIR_ASSETS: &str = "chunk";
fn gen_proving_task() -> eyre::Result<<Self::Prover as ProverType>::ProvingTask> {
unreachable!("Use gen_multi_proving_tasks");
}
/// [block-1]
/// [block-2]
/// [block-3, block-4]
fn gen_multi_proving_tasks() -> eyre::Result<Vec<<Self::Prover as ProverType>::ProvingTask>> {
#[cfg(not(feature = "euclidv2"))]
let blocks = [vec![12508460], vec![12508461], vec![12508462, 12508463]];
#[cfg(feature = "euclidv2")]
let blocks = [vec![1], vec![2], vec![3, 4]];
gen_multi_tasks(blocks)
}
}

View File

@@ -0,0 +1,8 @@
pub mod batch;
pub mod bundle;
pub mod chunk;
/// Path to the testdata directory.
pub const PATH_TESTDATA: &str = "./testdata";

View File

@@ -0,0 +1,420 @@
use sbv_primitives::{
B256, U256,
types::{BlockWitness, Transaction, eips::Encodable2718, reth::TransactionSigned},
};
use scroll_zkvm_circuit_input_types::{
batch::{BatchHeader, BatchHeaderV6, BatchHeaderV7},
utils::keccak256,
};
use scroll_zkvm_prover::{
ChunkProof,
task::{
batch::{BatchHeaderV, BatchProvingTask},
chunk::ChunkProvingTask,
},
utils::point_eval,
};
use vm_zstd::zstd_encode;
fn is_l1_tx(tx: &Transaction) -> bool {
// 0x7e is l1 tx
tx.transaction_type == 0x7e
}
#[allow(dead_code)]
fn final_l1_index(blk: &BlockWitness) -> u64 {
// Get number of l1 txs. L1 txs can be skipped, so just counting is not enough
// (The comment copied from scroll-prover, but why the max l1 queue index is always
// the last one for a chunk, or, is the last l1 never being skipped?)
blk.transaction
.iter()
.filter(|tx| is_l1_tx(tx))
.map(|tx| tx.queue_index.expect("l1 msg should has queue index"))
.max()
.unwrap_or_default()
}
fn blks_tx_bytes<'a>(blks: impl Iterator<Item = &'a BlockWitness>) -> Vec<u8> {
blks.flat_map(|blk| &blk.transaction)
.filter(|tx| !is_l1_tx(tx))
.fold(Vec::new(), |mut tx_bytes, tx| {
TransactionSigned::try_from(tx)
.unwrap()
.encode_2718(&mut tx_bytes);
tx_bytes
})
}
pub fn phase_base_directory() -> &'static str {
if cfg!(feature = "euclidv2") {
"phase2"
} else {
"phase1"
}
}
#[derive(Debug)]
pub struct LastHeader {
pub batch_index: u64,
pub batch_hash: B256,
pub version: u8,
/// legacy field
pub l1_message_index: u64,
}
impl Default for LastHeader {
fn default() -> Self {
// create a default LastHeader according to the dummy value
// being set in the e2e test in scroll-prover:
// https://github.com/scroll-tech/scroll-prover/blob/82f8ed3fabee5c3001b0b900cda1608413e621f8/integration/tests/e2e_tests.rs#L203C1-L207C8
Self {
batch_index: 123,
version: if cfg!(feature = "euclidv2") { 7 } else { 6 },
batch_hash: B256::new([
0xab, 0xac, 0xad, 0xae, 0xaf, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
]),
l1_message_index: 0,
}
}
}
impl From<&BatchHeaderV> for LastHeader {
fn from(value: &BatchHeaderV) -> Self {
match value {
BatchHeaderV::V6(h) => h.into(),
BatchHeaderV::V7(h) => h.into(),
}
}
}
impl From<&BatchHeaderV6> for LastHeader {
fn from(h: &BatchHeaderV6) -> Self {
Self {
batch_index: h.batch_index,
version: h.version,
batch_hash: h.batch_hash(),
l1_message_index: h.total_l1_message_popped,
}
}
}
impl From<&BatchHeaderV7> for LastHeader {
fn from(h: &BatchHeaderV7) -> Self {
Self {
batch_index: h.batch_index,
version: h.version,
batch_hash: h.batch_hash(),
l1_message_index: 0,
}
}
}
pub fn build_batch_task(
chunk_tasks: &[ChunkProvingTask],
chunk_proofs: &[ChunkProof],
last_header: LastHeader,
) -> BatchProvingTask {
// Sanity check.
assert_eq!(chunk_tasks.len(), chunk_proofs.len());
// collect tx bytes from chunk tasks
let (meta_chunk_sizes, chunk_digests, chunk_tx_bytes) = chunk_tasks.iter().fold(
(Vec::new(), Vec::new(), Vec::new()),
|(mut meta_chunk_sizes, mut chunk_digests, mut payload_bytes), task| {
let tx_bytes = blks_tx_bytes(task.block_witnesses.iter());
meta_chunk_sizes.push(tx_bytes.len());
chunk_digests.push(keccak256(&tx_bytes));
payload_bytes.extend(tx_bytes);
(meta_chunk_sizes, chunk_digests, payload_bytes)
},
);
// sanity check
for (digest, proof) in chunk_digests.iter().zip(chunk_proofs.iter()) {
assert_eq!(digest, &proof.metadata.chunk_info.tx_data_digest);
}
const LEGACY_MAX_CHUNKS: usize = 45;
let meta_chunk_bytes = {
let valid_chunk_size = chunk_proofs.len() as u16;
meta_chunk_sizes
.into_iter()
.chain(std::iter::repeat(0))
.take(LEGACY_MAX_CHUNKS)
.fold(
Vec::from(valid_chunk_size.to_be_bytes()),
|mut bytes, len| {
bytes.extend_from_slice(&(len as u32).to_be_bytes());
bytes
},
)
};
// collect all data together for payload
let mut payload = if cfg!(feature = "euclidv2") {
Vec::new()
} else {
meta_chunk_bytes.clone()
};
#[cfg(feature = "euclidv2")]
{
let num_blocks = chunk_tasks
.iter()
.map(|t| t.block_witnesses.len())
.sum::<usize>() as u16;
let (prev_msg_queue_hash, initial_block_number) = {
let first_chunk = &chunk_proofs
.first()
.expect("at least one chunk")
.metadata
.chunk_info;
(
first_chunk.prev_msg_queue_hash,
first_chunk.initial_block_number,
)
};
let post_msg_queue_hash = chunk_proofs
.last()
.expect("at least one chunk")
.metadata
.chunk_info
.post_msg_queue_hash;
payload.extend_from_slice(prev_msg_queue_hash.as_slice());
payload.extend_from_slice(post_msg_queue_hash.as_slice());
payload.extend(initial_block_number.to_be_bytes());
payload.extend(num_blocks.to_be_bytes());
assert_eq!(payload.len(), 74);
for proof in chunk_proofs {
for ctx in &proof.metadata.chunk_info.block_ctxs {
payload.extend(ctx.to_bytes());
}
}
assert_eq!(payload.len(), 74 + 52 * num_blocks as usize);
}
payload.extend(chunk_tx_bytes);
// compress ...
let compressed_payload = zstd_encode(&payload);
let version = 7u32;
let heading = compressed_payload.len() as u32 + (version << 24);
let blob_bytes = if cfg!(feature = "euclidv2") {
let mut blob_bytes = Vec::from(heading.to_be_bytes());
blob_bytes.push(1u8); // compressed flag
blob_bytes.extend(compressed_payload);
blob_bytes.resize(4096 * 31, 0);
blob_bytes
} else {
let mut blob_bytes = vec![1];
blob_bytes.extend(compressed_payload);
blob_bytes
};
let kzg_blob = point_eval::to_blob(&blob_bytes);
let kzg_commitment = point_eval::blob_to_kzg_commitment(&kzg_blob);
let blob_versioned_hash = point_eval::get_versioned_hash(&kzg_commitment);
// primage = keccak(payload) + blob_versioned_hash
let chg_preimage = if cfg!(feature = "euclidv2") {
let mut chg_preimage = keccak256(&blob_bytes).to_vec();
chg_preimage.extend(blob_versioned_hash.0);
chg_preimage
} else {
let mut chg_preimage = Vec::from(keccak256(&meta_chunk_bytes).0);
let last_digest = chunk_digests.last().expect("at least we have one");
chg_preimage.extend(
chunk_digests
.iter()
.chain(std::iter::repeat(last_digest))
.take(LEGACY_MAX_CHUNKS)
.fold(Vec::new(), |mut ret, digest| {
ret.extend_from_slice(&digest.0);
ret
}),
);
chg_preimage.extend_from_slice(blob_versioned_hash.as_slice());
chg_preimage
};
let challenge_digest = keccak256(&chg_preimage);
let x = point_eval::get_x_from_challenge(challenge_digest);
let (kzg_proof, z) = point_eval::get_kzg_proof(&kzg_blob, challenge_digest);
#[cfg(feature = "euclidv2")]
let batch_header = {
// avoid unused variant warning
let _ = x + z;
BatchHeaderV::V7(BatchHeaderV7 {
version: last_header.version,
batch_index: last_header.batch_index + 1,
parent_batch_hash: last_header.batch_hash,
blob_versioned_hash,
})
};
#[cfg(not(feature = "euclidv2"))]
let batch_header = {
// collect required fields for batch header
let last_l1_message_index: u64 = chunk_tasks
.iter()
.flat_map(|t| &t.block_witnesses)
.map(final_l1_index)
.reduce(|last, cur| if cur == 0 { last } else { cur })
.expect("at least one chunk");
let last_l1_message_index = if last_l1_message_index == 0 {
last_header.l1_message_index
} else {
last_l1_message_index
};
let last_block_timestamp = chunk_tasks.last().map_or(0u64, |t| {
t.block_witnesses
.last()
.map_or(0, |trace| trace.header.timestamp)
});
let point_evaluations = [x, z];
let data_hash = keccak256(
chunk_proofs
.iter()
.map(|proof| &proof.metadata.chunk_info.data_hash)
.fold(Vec::new(), |mut bytes, h| {
bytes.extend_from_slice(&h.0);
bytes
}),
);
BatchHeaderV::V6(BatchHeaderV6 {
version: last_header.version,
batch_index: last_header.batch_index + 1,
l1_message_popped: last_l1_message_index - last_header.l1_message_index,
last_block_timestamp,
total_l1_message_popped: last_l1_message_index,
parent_batch_hash: last_header.batch_hash,
data_hash,
blob_versioned_hash,
blob_data_proof: point_evaluations.map(|u| B256::new(u.to_be_bytes())),
})
};
BatchProvingTask {
chunk_proofs: Vec::from(chunk_proofs),
batch_header,
blob_bytes,
challenge_digest: Some(U256::from_be_bytes(challenge_digest.0)),
kzg_commitment: Some(kzg_commitment.to_bytes()),
kzg_proof: Some(kzg_proof.to_bytes()),
fork_name: if cfg!(feature = "euclidv2") {
String::from("euclidv2")
} else {
String::from("euclidv1")
},
}
}
#[test]
fn test_build_and_parse_batch_task() -> eyre::Result<()> {
#[cfg(not(feature = "euclidv2"))]
use scroll_zkvm_circuit_input_types::batch::{EnvelopeV6 as Envelope, PayloadV6 as Payload};
#[cfg(feature = "euclidv2")]
use scroll_zkvm_circuit_input_types::batch::{EnvelopeV7 as Envelope, PayloadV7 as Payload};
use scroll_zkvm_prover::utils::{read_json, read_json_deep, write_json};
// ./testdata/
let path_testdata = std::path::Path::new("testdata");
// read block witnesses.
let paths_block_witnesses = if cfg!(feature = "euclidv2") {
[
path_testdata.join("1.json"),
path_testdata.join("2.json"),
path_testdata.join("3.json"),
path_testdata.join("4.json"),
]
} else {
[
path_testdata.join("12508460.json"),
path_testdata.join("12508461.json"),
path_testdata.join("12508462.json"),
path_testdata.join("12508463.json"),
]
};
let read_block_witness = |path| Ok(read_json::<_, BlockWitness>(path)?);
let chunk_task = ChunkProvingTask {
block_witnesses: paths_block_witnesses
.iter()
.map(read_block_witness)
.collect::<eyre::Result<Vec<BlockWitness>>>()?,
prev_msg_queue_hash: Default::default(),
fork_name: if cfg!(feature = "euclidv2") {
String::from("euclidv2")
} else {
String::from("euclidv1")
},
};
// read chunk proof.
let path_chunk_proof = path_testdata
.join("proofs")
.join(if cfg!(feature = "euclidv2") {
"chunk-1-4.json"
} else {
"chunk-12508460-12508463.json"
});
let chunk_proof = read_json_deep::<_, ChunkProof>(&path_chunk_proof)?;
let task = build_batch_task(&[chunk_task], &[chunk_proof], Default::default());
let chunk_infos = task
.chunk_proofs
.iter()
.map(|proof| proof.metadata.chunk_info.clone())
.collect::<Vec<_>>();
let enveloped = Envelope::from(task.blob_bytes.as_slice());
#[cfg(feature = "euclidv2")]
let header = task.batch_header.must_v7_header();
#[cfg(not(feature = "euclidv2"))]
let header = task.batch_header.must_v6_header();
Payload::from(&enveloped).validate(header, &chunk_infos);
// depressed task output for pre-v2
#[cfg(feature = "euclidv2")]
write_json(path_testdata.join("batch-task-test-out.json"), &task).unwrap();
#[cfg(not(feature = "euclidv2"))]
write_json(path_testdata.join("batch-task-legacy-test-out.json"), &task).unwrap();
Ok(())
}
#[cfg(feature = "euclidv2")]
#[test]
fn test_batch_task_payload() -> eyre::Result<()> {
use scroll_zkvm_circuit_input_types::batch::{EnvelopeV7, PayloadV7};
use scroll_zkvm_prover::utils::read_json_deep;
// ./testdata/
let path_testdata = std::path::Path::new("testdata");
let task =
read_json_deep::<_, BatchProvingTask>(path_testdata.join("batch-task-test-out.json"))
.unwrap();
println!("blob {:?}", &task.blob_bytes[..32]);
let enveloped = EnvelopeV7::from(task.blob_bytes.as_slice());
let chunk_infos = task
.chunk_proofs
.iter()
.map(|proof| proof.metadata.chunk_info.clone())
.collect::<Vec<_>>();
PayloadV7::from(&enveloped).validate(task.batch_header.must_v7_header(), &chunk_infos);
Ok(())
}

File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show More