use ed25519_dalek::SigningKey;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
pub const TRANSFER_SCHEMA: &str = "vela.transfer.v0.1";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TransferKind {
LeanHomomorphism,
FrozenVerifier,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HomomorphismDescriptor {
pub kind: TransferKind,
pub map_decl: String,
pub source_type: String,
pub target_type: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub theorem_verification: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub theorem_id: Option<u32>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Transfer {
pub schema: String,
pub transfer_id: String,
pub source_claim: String,
pub source_claim_digest: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub source_gate_status_claimed: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub source_attachments: Vec<String>,
pub target_claim: String,
pub target_premise_digest: String,
pub target_attachments: Vec<String>,
pub homomorphism: HomomorphismDescriptor,
#[serde(default, skip_serializing_if = "is_default_provenance")]
pub provenance: crate::attempt::Provenance,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub note: String,
pub signature: String,
pub signer_pubkey_hex: String,
}
fn is_default_provenance(p: &crate::attempt::Provenance) -> bool {
*p == crate::attempt::Provenance::default()
}
#[derive(Deserialize)]
pub struct TransferDraft {
pub source_claim: String,
pub source_claim_digest: String,
#[serde(default)]
pub source_gate_status_claimed: String,
#[serde(default)]
pub source_attachments: Vec<String>,
pub target_claim: String,
pub target_premise_digest: String,
#[serde(default)]
pub target_attachments: Vec<String>,
pub homomorphism: HomomorphismDescriptor,
#[serde(default)]
pub provenance: crate::attempt::Provenance,
#[serde(default)]
pub note: String,
}
impl Transfer {
pub fn build(draft: TransferDraft, key: &SigningKey) -> Result<Self, String> {
if draft.source_claim.trim().is_empty() {
return Err("transfer.source_claim cannot be empty".to_string());
}
if draft.target_claim.trim().is_empty() {
return Err("transfer.target_claim cannot be empty".to_string());
}
if draft.target_premise_digest.trim().is_empty() {
return Err(
"transfer.target_premise_digest cannot be empty (T5: a transfer must \
discharge a specific premise, not all of B)"
.to_string(),
);
}
let h = &draft.homomorphism;
if h.map_decl.trim().is_empty() {
return Err("transfer.homomorphism.map_decl cannot be empty".to_string());
}
if h.source_type.trim().is_empty() || h.target_type.trim().is_empty() {
return Err(
"transfer.homomorphism source_type/target_type cannot be empty".to_string(),
);
}
if h.kind == TransferKind::LeanHomomorphism && !h.theorem_verification.starts_with("vlv_") {
return Err(
"a LeanHomomorphism transfer requires a `vlv_` theorem_verification".to_string(),
);
}
let mut t = Transfer {
schema: TRANSFER_SCHEMA.to_string(),
transfer_id: String::new(),
source_claim: draft.source_claim,
source_claim_digest: draft.source_claim_digest,
source_gate_status_claimed: draft.source_gate_status_claimed,
source_attachments: draft.source_attachments,
target_claim: draft.target_claim,
target_premise_digest: draft.target_premise_digest,
target_attachments: draft.target_attachments,
homomorphism: draft.homomorphism,
provenance: draft.provenance,
note: draft.note,
signature: String::new(),
signer_pubkey_hex: hex::encode(key.verifying_key().to_bytes()),
};
let preimage = t.id_preimage_bytes()?;
t.signature = hex::encode(crate::sign::sign_bytes(key, &preimage));
t.transfer_id = t.derive_id()?;
Ok(t)
}
fn id_preimage_bytes(&self) -> Result<Vec<u8>, String> {
let mut preimage = self.clone();
preimage.transfer_id = String::new();
preimage.signature = String::new();
preimage.signer_pubkey_hex = String::new();
crate::canonical::to_canonical_bytes(&preimage)
.map_err(|e| format!("canonicalize transfer preimage: {e}"))
}
pub fn derive_id(&self) -> Result<String, String> {
let bytes = self.id_preimage_bytes()?;
Ok(format!(
"vtr_{}",
&hex::encode(Sha256::digest(&bytes))[..16]
))
}
pub fn verify(&self) -> Result<(), String> {
if self.schema != TRANSFER_SCHEMA {
return Err(format!(
"transfer.schema must be `{TRANSFER_SCHEMA}`, got `{}`",
self.schema
));
}
if !self.transfer_id.starts_with("vtr_") {
return Err(format!(
"transfer id must start with `vtr_`, got `{}`",
self.transfer_id
));
}
let preimage = self.id_preimage_bytes()?;
if !crate::sign::verify_action_signature(
&preimage,
&self.signature,
&self.signer_pubkey_hex,
)? {
return Err("transfer signature does not verify under the declared pubkey".to_string());
}
let rederived = self.derive_id()?;
if rederived != self.transfer_id {
return Err(format!(
"transfer_id mismatch: declared {}, rebuilt {}",
self.transfer_id, rederived
));
}
Ok(())
}
#[must_use]
pub fn deposit_event(
&self,
actor_id: &str,
actor_type: &str,
reason: &str,
) -> crate::events::StateEvent {
let payload =
serde_json::json!({ "transfer": serde_json::to_value(self).unwrap_or_default() });
crate::events::new_transfer_deposited_event(
&self.transfer_id,
actor_id,
actor_type,
reason,
payload,
vec![
"A signed cross-domain transfer. It certifies the LINK is sound given A's \
gate-status; it does NOT re-prove A or re-run A's verifier."
.to_string(),
],
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TransferStatus {
NeedsVerification,
Admitted,
Rejected,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransferOutcome {
pub status: TransferStatus,
pub reasons: Vec<String>,
}
impl TransferOutcome {
#[must_use]
pub fn is_admitted(&self) -> bool {
self.status == TransferStatus::Admitted
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DomainTags {
pub source: String,
pub target: String,
}
const ALLOWED_FROZEN_VERIFIERS: &[&str] = &[
"vela-verify:sidon",
"vela-verify:cap",
"vela-verify:golomb",
"vela-verify:bh",
"vela-verify:constantweight",
"vela-verify:linearcode",
"vela-verify:dnacode",
"vela-verify:covering",
"vela-verify:stabilizer",
];
#[must_use]
pub fn derive_transfer_status(
transfer: &Transfer,
source_gate: &crate::verifier_attachment::GateOutcome,
target_gate: &crate::verifier_attachment::GateOutcome,
theorem_verification: Option<&crate::lean_verification::LeanVerification>,
domain_tags: &DomainTags,
) -> TransferOutcome {
use crate::verifier_attachment::{GateStatus, MethodIntegrity};
let mut reasons = Vec::new();
let mut rejected = false;
if let Err(e) = transfer.verify() {
reasons.push(format!("T4: transfer is not well-formed: {e}"));
}
match source_gate.status {
GateStatus::Verified => {}
GateStatus::Refuted => {
rejected = true;
reasons.push(
"T1: source claim A is REFUTED — the transfer cannot discharge B".to_string(),
);
}
GateStatus::NeedsVerification => {
reasons.push("T1: source claim A is not gate-verified (NeedsVerification)".to_string());
}
}
match transfer.homomorphism.kind {
TransferKind::LeanHomomorphism => match theorem_verification {
None => reasons.push(
"T2: LeanHomomorphism transfer has no resolved `vlv_` theorem verification"
.to_string(),
),
Some(v) => {
if let Err(e) = v.verify() {
reasons.push(format!("T2: transfer theorem `vlv_` does not verify: {e}"));
} else if v.status != "verified" {
reasons.push(format!(
"T2: transfer theorem status is `{}`, not `verified`",
v.status
));
}
match v.to_attachment_integrity() {
MethodIntegrity::Sound => {}
MethodIntegrity::Compromised => {
rejected = true;
reasons.push("T2: transfer theorem method integrity COMPROMISED \
(forbidden axiom — e.g. native_decide/sorry — or failed kernel re-check)"
.to_string());
}
MethodIntegrity::Unattested => reasons
.push("T2: transfer theorem has no axiom audit (Unattested)".to_string()),
}
}
},
TransferKind::FrozenVerifier => {
if ALLOWED_FROZEN_VERIFIERS.contains(&transfer.homomorphism.map_decl.as_str()) {
reasons.push(format!(
"note: FrozenVerifier transfer — trust base additionally includes the executable \
verifier `{}`, not only the Lean kernel",
transfer.homomorphism.map_decl
));
} else {
reasons.push(format!(
"T2: FrozenVerifier `{}` is not in the audited allowlist",
transfer.homomorphism.map_decl
));
}
match target_gate.status {
GateStatus::Verified => {}
GateStatus::Refuted => {
rejected = true;
reasons.push(
"T6: the target verifier REFUTED the mapped object φ(A) — the link cannot \
discharge B"
.to_string(),
);
}
GateStatus::NeedsVerification => reasons.push(
"T6: FrozenVerifier transfer requires a Verified target-verifier receipt on \
φ(A) (target_attachments); none resolved"
.to_string(),
),
}
}
}
if transfer.homomorphism.source_type != domain_tags.source {
reasons.push(format!(
"T3: homomorphism source_type `{}` != A's domain `{}`",
transfer.homomorphism.source_type, domain_tags.source
));
}
if transfer.homomorphism.target_type != domain_tags.target {
reasons.push(format!(
"T3: homomorphism target_type `{}` != B's premise domain `{}`",
transfer.homomorphism.target_type, domain_tags.target
));
}
if transfer.target_premise_digest.trim().is_empty() {
reasons.push("T5: target_premise_digest is empty (no specific premise pinned)".to_string());
}
let blocking = reasons.iter().any(|r| !r.starts_with("note:"));
let status = if rejected {
TransferStatus::Rejected
} else if blocking {
TransferStatus::NeedsVerification
} else {
TransferStatus::Admitted
};
TransferOutcome { status, reasons }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::lean_verification::{KernelRecheck, LeanVerification, VerificationDraft};
use crate::tcb_policy::AxiomVerdict;
use crate::verifier_attachment::{GateOutcome, GateStatus};
use rand::rngs::OsRng;
fn key() -> SigningKey {
SigningKey::generate(&mut OsRng)
}
fn lean_decl_descriptor(theorem_verification: &str) -> HomomorphismDescriptor {
HomomorphismDescriptor {
kind: TransferKind::LeanHomomorphism,
map_decl: "Vela.TransferCWCtoDNA.cwcToDNA".to_string(),
source_type: "constant_weight_code".to_string(),
target_type: "dna_code".to_string(),
theorem_verification: theorem_verification.to_string(),
theorem_id: Some(35),
}
}
fn draft(theorem_verification: &str) -> TransferDraft {
TransferDraft {
source_claim: "vat_aaaaaaaaaaaaaaaa".to_string(),
source_claim_digest: crate::verifier_attachment::claim_digest("A(n,d,w) >= 12"),
source_gate_status_claimed: "verified".to_string(),
source_attachments: vec!["vva_bbbbbbbbbbbbbbbb".to_string()],
target_claim: "vfr_cccccccccccccccc".to_string(),
target_premise_digest: crate::verifier_attachment::claim_digest(
"a DNA code of size >= 12 exists",
),
target_attachments: vec![],
homomorphism: lean_decl_descriptor(theorem_verification),
provenance: crate::attempt::Provenance::default(),
note: String::new(),
}
}
fn lean_v(kernel_clean: bool) -> LeanVerification {
let d = VerificationDraft {
anchor_id: "vla_dddddddddddddddd".to_string(),
theorem_id: 35,
module: "Vela/Transfer.lean".to_string(),
module_sha256: "0".repeat(64),
lean_toolchain: "leanprover/lean4:v4.29.1".to_string(),
mathlib_revision: "abc".to_string(),
verifier_output_hash: "0".repeat(64),
status: if kernel_clean {
"verified".to_string()
} else {
"failed_axiom_check".to_string()
},
verified_at: "2026-06-09T00:00:00Z".to_string(),
verifier_actor: "test".to_string(),
tcb_id: "vtcb_dddddddddddddddd".to_string(),
axioms: vec![
"propext".to_string(),
"Classical.choice".to_string(),
"Quot.sound".to_string(),
],
axiom_verdict: Some(if kernel_clean {
AxiomVerdict::KernelClean
} else {
AxiomVerdict::ForbiddenAxiom
}),
kernel_recheck: Some(KernelRecheck::Passed),
axioms_output_hash: "0".repeat(64),
};
LeanVerification::build(d, &key()).unwrap()
}
fn verified_gate() -> GateOutcome {
GateOutcome {
status: GateStatus::Verified,
reasons: vec![],
}
}
#[test]
fn build_verify_roundtrip_and_id_prefix() {
let t = Transfer::build(draft("vlv_eeeeeeeeeeeeeeee"), &key()).unwrap();
assert!(t.transfer_id.starts_with("vtr_"));
t.verify().unwrap();
}
#[test]
fn tampered_body_fails_t4() {
let mut t = Transfer::build(draft("vlv_eeeeeeeeeeeeeeee"), &key()).unwrap();
t.target_premise_digest = crate::verifier_attachment::claim_digest("a DIFFERENT premise");
assert!(t.verify().is_err());
}
#[test]
fn admits_when_all_clauses_pass() {
let v = lean_v(true);
let t = Transfer::build(draft(&v.verification_id), &key()).unwrap();
let tags = DomainTags {
source: "constant_weight_code".into(),
target: "dna_code".into(),
};
let out = derive_transfer_status(&t, &verified_gate(), &verified_gate(), Some(&v), &tags);
assert_eq!(
out.status,
TransferStatus::Admitted,
"reasons: {:?}",
out.reasons
);
}
#[test]
fn native_decide_theorem_cannot_admit() {
let v = lean_v(false); let t = Transfer::build(draft(&v.verification_id), &key()).unwrap();
let tags = DomainTags {
source: "constant_weight_code".into(),
target: "dna_code".into(),
};
let out = derive_transfer_status(&t, &verified_gate(), &verified_gate(), Some(&v), &tags);
assert_eq!(out.status, TransferStatus::Rejected);
}
#[test]
fn refuted_source_drives_rejected() {
let v = lean_v(true);
let t = Transfer::build(draft(&v.verification_id), &key()).unwrap();
let tags = DomainTags {
source: "constant_weight_code".into(),
target: "dna_code".into(),
};
let refuted = GateOutcome {
status: GateStatus::Refuted,
reasons: vec!["probe refuted".into()],
};
let out = derive_transfer_status(&t, &refuted, &verified_gate(), Some(&v), &tags);
assert_eq!(out.status, TransferStatus::Rejected);
}
#[test]
fn type_mismatch_is_not_admitted() {
let v = lean_v(true);
let t = Transfer::build(draft(&v.verification_id), &key()).unwrap();
let tags = DomainTags {
source: "sidon".into(),
target: "dna_code".into(),
}; let out = derive_transfer_status(&t, &verified_gate(), &verified_gate(), Some(&v), &tags);
assert_eq!(out.status, TransferStatus::NeedsVerification);
assert!(out.reasons.iter().any(|r| r.starts_with("T3")));
}
#[test]
fn unverified_source_needs_verification() {
let v = lean_v(true);
let t = Transfer::build(draft(&v.verification_id), &key()).unwrap();
let tags = DomainTags {
source: "constant_weight_code".into(),
target: "dna_code".into(),
};
let needs = GateOutcome {
status: GateStatus::NeedsVerification,
reasons: vec![],
};
let out = derive_transfer_status(&t, &needs, &verified_gate(), Some(&v), &tags);
assert_eq!(out.status, TransferStatus::NeedsVerification);
}
fn frozen_draft() -> TransferDraft {
TransferDraft {
source_claim: "vf_sidon_a7".to_string(),
source_claim_digest: crate::verifier_attachment::claim_digest("a Sidon set of size 7"),
source_gate_status_claimed: "verified".to_string(),
source_attachments: vec!["vva_source_sidon".to_string()],
target_claim: "vfr_golomb".to_string(),
target_premise_digest: crate::verifier_attachment::claim_digest(
"a Golomb ruler of size 7 exists",
),
target_attachments: vec!["vva_target_golomb".to_string()],
homomorphism: HomomorphismDescriptor {
kind: TransferKind::FrozenVerifier,
map_decl: "vela-verify:golomb".to_string(),
source_type: "sidon".to_string(),
target_type: "golomb".to_string(),
theorem_verification: String::new(),
theorem_id: None,
},
provenance: Default::default(),
note: String::new(),
}
}
fn golomb_tags() -> DomainTags {
DomainTags {
source: "sidon".into(),
target: "golomb".into(),
}
}
fn needs_gate() -> GateOutcome {
GateOutcome {
status: GateStatus::NeedsVerification,
reasons: vec![],
}
}
#[test]
fn frozen_admits_only_with_both_verifier_receipts() {
let t = Transfer::build(frozen_draft(), &key()).unwrap();
let out =
derive_transfer_status(&t, &verified_gate(), &verified_gate(), None, &golomb_tags());
assert_eq!(
out.status,
TransferStatus::Admitted,
"reasons: {:?}",
out.reasons
);
}
#[test]
fn frozen_without_target_receipt_is_not_admitted() {
let t = Transfer::build(frozen_draft(), &key()).unwrap();
let out = derive_transfer_status(&t, &verified_gate(), &needs_gate(), None, &golomb_tags());
assert_eq!(out.status, TransferStatus::NeedsVerification);
assert!(
out.reasons.iter().any(|r| r.starts_with("T6")),
"reasons: {:?}",
out.reasons
);
}
#[test]
fn frozen_with_refuted_target_is_rejected() {
let t = Transfer::build(frozen_draft(), &key()).unwrap();
let refuted = GateOutcome {
status: GateStatus::Refuted,
reasons: vec!["golomb verifier refuted φ(A)".into()],
};
let out = derive_transfer_status(&t, &verified_gate(), &refuted, None, &golomb_tags());
assert_eq!(out.status, TransferStatus::Rejected);
}
}