Skip to content

Commit b63e247

Browse files
committed
init verifier with version, fix pi hash checking in validium mode
1 parent 76e9283 commit b63e247

File tree

8 files changed

+69
-16
lines changed

8 files changed

+69
-16
lines changed

coordinator/cmd/tool/verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func verify(cCtx *cli.Context) error {
3636
return fmt.Errorf("error reading file: %w", err)
3737
}
3838

39-
vf, err := verifier.NewVerifier(cfg.ProverManager.Verifier)
39+
vf, err := verifier.NewVerifier(cfg.ProverManager.Verifier, cfg.L2.ValidiumMode)
4040
if err != nil {
4141
return err
4242
}

coordinator/internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Config struct {
6565
// AssetConfig contain assets configurated for each fork, the defaul vkfile name is "OpenVmVk.json".
6666
type AssetConfig struct {
6767
AssetsPath string `json:"assets_path"`
68+
Version uint8 `json:"version,omitempty"`
6869
ForkName string `json:"fork_name"`
6970
Vkfile string `json:"vk_file,omitempty"`
7071
MinProverVersion string `json:"min_prover_version,omitempty"`

coordinator/internal/controller/api/controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ var (
2424

2525
// InitController inits Controller with database
2626
func InitController(cfg *config.Config, chainCfg *params.ChainConfig, db *gorm.DB, reg prometheus.Registerer) {
27-
vf, err := verifier.NewVerifier(cfg.ProverManager.Verifier)
27+
validiumMode := cfg.L2.ValidiumMode
28+
29+
vf, err := verifier.NewVerifier(cfg.ProverManager.Verifier, validiumMode)
2830
if err != nil {
2931
panic("proof receiver new verifier failure")
3032
}

coordinator/internal/logic/verifier/verifier.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,34 @@ import (
1919

2020
"scroll-tech/coordinator/internal/config"
2121
"scroll-tech/coordinator/internal/logic/libzkp"
22+
"scroll-tech/coordinator/internal/utils"
2223
)
2324

2425
// This struct maps to `CircuitConfig` in libzkp/src/verifier.rs
2526
// Define a brand new struct here is to eliminate side effects in case fields
2627
// in `*config.CircuitConfig` being changed
2728
type rustCircuitConfig struct {
29+
Version uint `json:"version"`
2830
ForkName string `json:"fork_name"`
2931
AssetsPath string `json:"assets_path"`
3032
}
3133

34+
var validiumMode bool
35+
3236
func newRustCircuitConfig(cfg config.AssetConfig) *rustCircuitConfig {
37+
ver := cfg.Version
38+
if ver == 0 {
39+
var err error
40+
ver, err = utils.Version(cfg.ForkName, validiumMode)
41+
if err != nil {
42+
panic(err)
43+
}
44+
}
45+
3346
return &rustCircuitConfig{
34-
ForkName: cfg.ForkName,
47+
Version: uint(ver),
3548
AssetsPath: cfg.AssetsPath,
49+
ForkName: cfg.ForkName,
3650
}
3751
}
3852

@@ -60,7 +74,8 @@ type rustVkDump struct {
6074
}
6175

6276
// NewVerifier Sets up a rust ffi to call verify.
63-
func NewVerifier(cfg *config.VerifierConfig) (*Verifier, error) {
77+
func NewVerifier(cfg *config.VerifierConfig, useValidiumMode bool) (*Verifier, error) {
78+
validiumMode = useValidiumMode
6479
verifierConfig := newRustVerifierConfig(cfg)
6580
configBytes, err := json.Marshal(verifierConfig)
6681
if err != nil {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package utils
2+
3+
import (
4+
"errors"
5+
"strings"
6+
)
7+
8+
// version get the version for the chain instance
9+
//
10+
// TODO: This is not foolproof and does not cover all scenarios.
11+
func Version(hardForkName string, ValidiumMode bool) (uint8, error) {
12+
13+
var domain, stfVersion uint8
14+
15+
if ValidiumMode {
16+
domain = 1
17+
stfVersion = 1
18+
} else {
19+
domain = 0
20+
switch canonicalName := strings.ToLower(hardForkName); canonicalName {
21+
case "euclidv1":
22+
stfVersion = 6
23+
case "euclidv2":
24+
stfVersion = 7
25+
case "feynman":
26+
stfVersion = 8
27+
default:
28+
return 0, errors.New("unknown fork name " + canonicalName)
29+
}
30+
}
31+
32+
return (domain << 6) + stfVersion, nil
33+
}

crates/libzkp/src/proofs.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ use scroll_zkvm_types::{
88
bundle::BundleInfo,
99
chunk::ChunkInfo,
1010
proof::{EvmProof, OpenVmEvmProof, ProofEnum, StarkProof},
11-
public_inputs::{ForkName, MultiVersionPublicInputs},
11+
public_inputs::MultiVersionPublicInputs,
1212
types_agg::AggregationInput,
1313
utils::{serialize_vk, vec_as_base64},
14+
version,
1415
};
1516
use serde::{de::DeserializeOwned, Deserialize, Serialize};
1617

@@ -181,13 +182,13 @@ impl<Metadata: ProofMetadata> WrappedProof<Metadata> {
181182
/// Sanity checks on the wrapped proof:
182183
///
183184
/// - pi_hash computed in host does in fact match pi_hash computed in guest
184-
pub fn pi_hash_check(&self, fork_name: ForkName) -> bool {
185+
pub fn pi_hash_check(&self, ver: version::Version) -> bool {
185186
let proof_pi = self.proof.public_values();
186187

187188
let expected_pi = self
188189
.metadata
189190
.pi_hash_info()
190-
.pi_hash_by_fork(fork_name)
191+
.pi_hash_by_version(ver)
191192
.0
192193
.as_ref()
193194
.iter()

crates/libzkp/src/verifier.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub trait ProofVerifier {
4141

4242
#[derive(Debug, Serialize, Deserialize)]
4343
pub struct CircuitConfig {
44+
pub version: u8,
4445
pub fork_name: String,
4546
pub assets_path: String,
4647
}
@@ -61,14 +62,14 @@ pub fn init(config: VerifierConfig) {
6162
for cfg in &config.circuits {
6263
let canonical_fork_name = cfg.fork_name.to_lowercase();
6364

64-
let verifier = Verifier::new(&cfg.assets_path, canonical_fork_name.as_str().into());
65+
let verifier = Verifier::new(&cfg.assets_path, cfg.version);
6566
let ret = verifiers.insert(canonical_fork_name, Arc::new(Mutex::new(verifier)));
6667
assert!(
6768
ret.is_none(),
6869
"DO NOT init the same fork {} twice",
6970
cfg.fork_name
7071
);
71-
tracing::info!("load verifier config for fork {}", cfg.fork_name);
72+
tracing::info!("load verifier config for fork {} (ver {})", cfg.fork_name, cfg.version);
7273
}
7374

7475
let ret = VERIFIERS.set(verifiers).is_ok();

crates/libzkp/src/verifier/universal.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ use crate::{
66
proofs::{AsRootProof, BatchProof, BundleProof, ChunkProof, IntoEvmProof},
77
utils::panic_catch,
88
};
9-
use scroll_zkvm_types::public_inputs::ForkName;
9+
use scroll_zkvm_types::version::Version;
1010
use scroll_zkvm_verifier::verifier::UniversalVerifier;
1111
use std::path::Path;
1212

1313
pub struct Verifier {
1414
verifier: UniversalVerifier,
15-
fork: ForkName,
15+
version: Version,
1616
}
1717

1818
impl Verifier {
19-
pub fn new(assets_dir: &str, fork: ForkName) -> Self {
19+
pub fn new(assets_dir: &str, ver_n: u8) -> Self {
2020
let verifier_bin = Path::new(assets_dir);
2121

2222
Self {
2323
verifier: UniversalVerifier::setup(verifier_bin).expect("Setting up chunk verifier"),
24-
fork,
24+
version: Version::from(ver_n),
2525
}
2626
}
2727
}
@@ -31,21 +31,21 @@ impl ProofVerifier for Verifier {
3131
panic_catch(|| match task_type {
3232
TaskType::Chunk => {
3333
let proof = serde_json::from_slice::<ChunkProof>(proof).unwrap();
34-
assert!(proof.pi_hash_check(self.fork));
34+
assert!(proof.pi_hash_check(self.version));
3535
self.verifier
3636
.verify_stark_proof(proof.as_root_proof(), &proof.vk)
3737
.unwrap()
3838
}
3939
TaskType::Batch => {
4040
let proof = serde_json::from_slice::<BatchProof>(proof).unwrap();
41-
assert!(proof.pi_hash_check(self.fork));
41+
assert!(proof.pi_hash_check(self.version));
4242
self.verifier
4343
.verify_stark_proof(proof.as_root_proof(), &proof.vk)
4444
.unwrap()
4545
}
4646
TaskType::Bundle => {
4747
let proof = serde_json::from_slice::<BundleProof>(proof).unwrap();
48-
assert!(proof.pi_hash_check(self.fork));
48+
assert!(proof.pi_hash_check(self.version));
4949
let vk = proof.vk.clone();
5050
let evm_proof = proof.into_evm_proof();
5151
self.verifier.verify_evm_proof(&evm_proof, &vk).unwrap()

0 commit comments

Comments
 (0)