use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
pub const POLICY_SCHEMA: &str = "vela.policy.v1";
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PolicyRule {
pub id: String,
pub description: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct VerificationPolicy {
pub schema: String,
pub id: String,
pub name: String,
pub version: String,
pub rules: Vec<PolicyRule>,
pub canonical_digest: String,
}
impl VerificationPolicy {
#[must_use]
pub fn build(name: &str, version: &str, rules: Vec<PolicyRule>) -> Self {
let mut policy = VerificationPolicy {
schema: POLICY_SCHEMA.to_string(),
id: String::new(),
name: name.to_string(),
version: version.to_string(),
rules,
canonical_digest: String::new(),
};
policy.canonical_digest = policy.derive_digest();
policy.id = format!("vpol_{}", &policy.canonical_digest[..16]);
policy
}
fn derive_digest(&self) -> String {
let mut c = self.clone();
c.id = String::new();
c.canonical_digest = String::new();
let bytes = crate::canonical::to_canonical_bytes(&c).unwrap_or_default();
hex::encode(Sha256::digest(bytes))
}
}
#[must_use]
pub fn canonical_gate_policy() -> VerificationPolicy {
let rule = |id: &str, description: &str| PolicyRule {
id: id.to_string(),
description: description.to_string(),
};
VerificationPolicy::build(
"vela-gate",
"G1-G5/v1",
vec![
rule(
"G1",
"at least two independent matched verifier attachments; independence binds to a sibling vva_ witness id, never to the shared claim digest",
),
rule(
"G2",
"every counted attachment matches the current claim digest",
),
rule(
"G3",
"at least one surviving adversarial probe and no refuting probe",
),
rule(
"G4",
"every counted attachment's id content-addresses its body",
),
rule(
"G5",
"method integrity is sound: no forbidden axiom and no failed kernel re-check",
),
],
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn canonical_policy_is_content_addressed_and_stable() {
let a = canonical_gate_policy();
let b = canonical_gate_policy();
assert!(a.id.starts_with("vpol_"));
assert_eq!(a.canonical_digest.len(), 64);
assert_eq!(a, b);
assert_eq!(a.id[5..], a.canonical_digest[..16]);
}
}