vela_protocol/domains/
proof_verification.rs1use serde::{Deserialize, Serialize};
14use sha2::{Digest, Sha256};
15
16pub const VERIFICATION_SCHEMA: &str = "vela.proof_verification.v0.1";
17
18#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
19pub struct ProofVerification {
20 pub schema: String,
21 pub verification_id: String,
22 pub proof_id: String,
23 pub tool: String,
24 pub tool_version: String,
25 pub script_locator: String,
26 #[serde(default, skip_serializing_if = "Option::is_none")]
27 pub lake_manifest_hash: Option<String>,
28 pub verifier_output_hash: String,
29 pub status: String,
30 pub verified_at: String,
31 pub verifier_actor: String,
32 pub verifier_pubkey: String,
33 pub signature: String,
34}
35
36#[derive(Debug, Clone)]
37pub struct VerificationDraft {
38 pub proof_id: String,
39 pub tool: String,
40 pub tool_version: String,
41 pub script_locator: String,
42 pub lake_manifest_hash: Option<String>,
43 pub verifier_output_hash: String,
44 pub status: String,
45 pub verified_at: String,
46 pub verifier_actor: String,
47}
48
49impl ProofVerification {
50 pub fn build(
55 draft: VerificationDraft,
56 signing_key: &ed25519_dalek::SigningKey,
57 ) -> Result<Self, String> {
58 if !draft.proof_id.starts_with("vpf_") {
59 return Err(format!(
60 "proof_id must start with `vpf_`, got `{}`",
61 draft.proof_id
62 ));
63 }
64 if !matches!(
65 draft.tool.as_str(),
66 "lean4" | "coq" | "isabelle" | "agda" | "metamath" | "rocq" | "other"
67 ) {
68 return Err(format!(
69 "tool must be one of lean4|coq|isabelle|agda|metamath|rocq|other; got `{}`",
70 draft.tool
71 ));
72 }
73 if !matches!(
74 draft.status.as_str(),
75 "verified" | "failed" | "toolchain_mismatch"
76 ) {
77 return Err(format!(
78 "status must be one of verified|failed|toolchain_mismatch; got `{}`",
79 draft.status
80 ));
81 }
82 let mut record = ProofVerification {
83 schema: VERIFICATION_SCHEMA.to_string(),
84 verification_id: String::new(),
85 proof_id: draft.proof_id,
86 tool: draft.tool,
87 tool_version: draft.tool_version,
88 script_locator: draft.script_locator,
89 lake_manifest_hash: draft.lake_manifest_hash,
90 verifier_output_hash: draft.verifier_output_hash,
91 status: draft.status,
92 verified_at: draft.verified_at,
93 verifier_actor: draft.verifier_actor,
94 verifier_pubkey: hex::encode(signing_key.verifying_key().to_bytes()),
95 signature: String::new(),
96 };
97 let preimage = record.preimage_bytes()?;
98 record.signature = hex::encode(crate::sign::sign_bytes(signing_key, &preimage));
99 record.verification_id = record.derive_id()?;
100 Ok(record)
101 }
102
103 pub fn preimage_bytes(&self) -> Result<Vec<u8>, String> {
104 let mut preimage = self.clone();
105 preimage.signature = String::new();
106 preimage.verification_id = String::new();
107 crate::canonical::to_canonical_bytes(&preimage)
108 .map_err(|e| format!("canonicalize verification preimage: {e}"))
109 }
110
111 pub fn derive_id(&self) -> Result<String, String> {
112 let mut preimage = self.clone();
113 preimage.verification_id = String::new();
114 let bytes = crate::canonical::to_canonical_bytes(&preimage)
115 .map_err(|e| format!("canonicalize verification id preimage: {e}"))?;
116 let digest = Sha256::digest(&bytes);
117 Ok(format!("vpv_{}", &hex::encode(digest)[..16]))
118 }
119
120 pub fn verify(&self) -> Result<(), String> {
127 if self.schema != VERIFICATION_SCHEMA {
128 return Err(format!(
129 "verification.schema must be `{VERIFICATION_SCHEMA}`, got `{}`",
130 self.schema
131 ));
132 }
133 let derived = self.derive_id()?;
134 if derived != self.verification_id {
135 return Err(format!(
136 "verification_id mismatch: stored `{}`, derived `{}`",
137 self.verification_id, derived
138 ));
139 }
140 let preimage = self.preimage_bytes()?;
141 if !crate::sign::verify_action_signature(&preimage, &self.signature, &self.verifier_pubkey)?
142 {
143 return Err("verification signature does not verify under verifier_pubkey".to_string());
144 }
145 Ok(())
146 }
147}
148
149#[cfg(test)]
150mod tests {
151 use super::*;
152
153 fn make_key() -> ed25519_dalek::SigningKey {
154 use rand::rngs::OsRng;
155 ed25519_dalek::SigningKey::generate(&mut OsRng)
156 }
157
158 fn good_draft() -> VerificationDraft {
159 VerificationDraft {
160 proof_id: "vpf_egz_n2".to_string(),
161 tool: "lean4".to_string(),
162 tool_version: "4.29.1".to_string(),
163 script_locator:
164 "sha256:58dec20d4f8c474d222009c9d3b7cae2ef010bfac48fd5c2ad7c9c8d894428ec"
165 .to_string(),
166 lake_manifest_hash: Some("sha256:0".repeat(64)),
167 verifier_output_hash: format!("sha256:{}", "0".repeat(64)),
168 status: "verified".to_string(),
169 verified_at: "2026-05-11T00:00:00+00:00".to_string(),
170 verifier_actor: "verifier:lean-ci".to_string(),
171 }
172 }
173
174 #[test]
175 fn build_roundtrip() {
176 let sk = make_key();
177 let record = ProofVerification::build(good_draft(), &sk).unwrap();
178 assert!(record.verification_id.starts_with("vpv_"));
179 record.verify().unwrap();
180 }
181
182 #[test]
183 fn id_changes_when_status_changes() {
184 let sk = make_key();
185 let a = ProofVerification::build(good_draft(), &sk).unwrap();
186 let mut draft = good_draft();
187 draft.status = "failed".to_string();
188 let b = ProofVerification::build(draft, &sk).unwrap();
189 assert_ne!(a.verification_id, b.verification_id);
190 }
191
192 #[test]
193 fn tampered_signature_rejected() {
194 let sk = make_key();
195 let mut record = ProofVerification::build(good_draft(), &sk).unwrap();
196 record.signature = "0".repeat(128);
197 let err = record.verify().unwrap_err();
198 assert!(
199 err.contains("mismatch") || err.contains("does not verify"),
200 "got: {err}"
201 );
202 }
203
204 #[test]
205 fn invalid_status_rejected() {
206 let sk = make_key();
207 let mut draft = good_draft();
208 draft.status = "bogus".to_string();
209 let err = ProofVerification::build(draft, &sk).unwrap_err();
210 assert!(err.contains("status must be"), "got: {err}");
211 }
212}