Merge commit 'd803bfe2b1fe7f5e219e50ac20d6801a0a58ac75' as 'vendor/ruvector'
This commit is contained in:
154
vendor/ruvector/examples/verified-applications/src/agent_contracts.rs
vendored
Normal file
154
vendor/ruvector/examples/verified-applications/src/agent_contracts.rs
vendored
Normal file
@@ -0,0 +1,154 @@
|
||||
//! # 4. Multi-Agent Contract Enforcement
|
||||
//!
|
||||
//! Each agent message embedding must:
|
||||
//! - Match declared dimensionality
|
||||
//! - Match contract schema (metric type)
|
||||
//! - Pass verified transformation pipeline
|
||||
//!
|
||||
//! If mismatch, no agent state transition allowed.
|
||||
//! Result: the proof engine becomes a structural gate -- a logic firewall.
|
||||
|
||||
use crate::ProofReceipt;
|
||||
use ruvector_verified::{
|
||||
gated::{self, ProofKind, ProofTier},
|
||||
proof_store, vector_types, ProofEnvironment,
|
||||
};
|
||||
|
||||
/// An agent contract specifying required embedding properties.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AgentContract {
|
||||
pub agent_id: String,
|
||||
pub required_dim: u32,
|
||||
pub required_metric: String,
|
||||
pub max_pipeline_depth: u32,
|
||||
}
|
||||
|
||||
/// Result of a contract gate check.
|
||||
#[derive(Debug)]
|
||||
pub struct GateResult {
|
||||
pub agent_id: String,
|
||||
pub allowed: bool,
|
||||
pub reason: String,
|
||||
pub receipt: Option<ProofReceipt>,
|
||||
}
|
||||
|
||||
/// Check whether an agent message embedding passes its contract gate.
|
||||
pub fn enforce_contract(contract: &AgentContract, message_embedding: &[f32]) -> GateResult {
|
||||
let mut env = ProofEnvironment::new();
|
||||
|
||||
// Gate 1: Dimension match
|
||||
let dim_result =
|
||||
vector_types::verified_dim_check(&mut env, contract.required_dim, message_embedding);
|
||||
let dim_proof = match dim_result {
|
||||
Ok(op) => op.proof_id,
|
||||
Err(e) => {
|
||||
return GateResult {
|
||||
agent_id: contract.agent_id.clone(),
|
||||
allowed: false,
|
||||
reason: format!("dimension gate failed: {e}"),
|
||||
receipt: None,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// Gate 2: Metric schema match
|
||||
let metric_result = vector_types::mk_distance_metric(&mut env, &contract.required_metric);
|
||||
if let Err(e) = metric_result {
|
||||
return GateResult {
|
||||
agent_id: contract.agent_id.clone(),
|
||||
allowed: false,
|
||||
reason: format!("metric gate failed: {e}"),
|
||||
receipt: None,
|
||||
};
|
||||
}
|
||||
|
||||
// Gate 3: Pipeline depth check via gated routing
|
||||
let decision = gated::route_proof(
|
||||
ProofKind::PipelineComposition {
|
||||
stages: contract.max_pipeline_depth,
|
||||
},
|
||||
&env,
|
||||
);
|
||||
|
||||
let attestation = proof_store::create_attestation(&env, dim_proof);
|
||||
|
||||
GateResult {
|
||||
agent_id: contract.agent_id.clone(),
|
||||
allowed: true,
|
||||
reason: format!(
|
||||
"all gates passed: dim={}, metric={}, tier={}",
|
||||
contract.required_dim,
|
||||
contract.required_metric,
|
||||
match decision.tier {
|
||||
ProofTier::Reflex => "reflex",
|
||||
ProofTier::Standard { .. } => "standard",
|
||||
ProofTier::Deep => "deep",
|
||||
},
|
||||
),
|
||||
receipt: Some(ProofReceipt {
|
||||
domain: "agent_contract".into(),
|
||||
claim: format!("agent '{}' message verified", contract.agent_id),
|
||||
proof_id: dim_proof,
|
||||
attestation_bytes: attestation.to_bytes(),
|
||||
tier: match decision.tier {
|
||||
ProofTier::Reflex => "reflex",
|
||||
ProofTier::Standard { .. } => "standard",
|
||||
ProofTier::Deep => "deep",
|
||||
}
|
||||
.into(),
|
||||
gate_passed: true,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Run a multi-agent scenario: N agents, each with a contract, each sending messages.
|
||||
pub fn run_multi_agent_scenario(agents: &[(AgentContract, Vec<f32>)]) -> Vec<GateResult> {
|
||||
agents
|
||||
.iter()
|
||||
.map(|(c, emb)| enforce_contract(c, emb))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn test_contract(dim: u32) -> AgentContract {
|
||||
AgentContract {
|
||||
agent_id: "agent-A".into(),
|
||||
required_dim: dim,
|
||||
required_metric: "Cosine".into(),
|
||||
max_pipeline_depth: 3,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_agent_passes_gate() {
|
||||
let contract = test_contract(256);
|
||||
let embedding = vec![0.1f32; 256];
|
||||
let result = enforce_contract(&contract, &embedding);
|
||||
assert!(result.allowed);
|
||||
assert!(result.receipt.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_dim_blocked() {
|
||||
let contract = test_contract(256);
|
||||
let embedding = vec![0.1f32; 128];
|
||||
let result = enforce_contract(&contract, &embedding);
|
||||
assert!(!result.allowed);
|
||||
assert!(result.receipt.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_agent_mixed() {
|
||||
let agents = vec![
|
||||
(test_contract(128), vec![0.5f32; 128]), // pass
|
||||
(test_contract(128), vec![0.5f32; 64]), // fail
|
||||
(test_contract(256), vec![0.5f32; 256]), // pass
|
||||
];
|
||||
let results = run_multi_agent_scenario(&agents);
|
||||
assert_eq!(results.iter().filter(|r| r.allowed).count(), 2);
|
||||
assert_eq!(results.iter().filter(|r| !r.allowed).count(), 1);
|
||||
}
|
||||
}
|
||||
125
vendor/ruvector/examples/verified-applications/src/financial_routing.rs
vendored
Normal file
125
vendor/ruvector/examples/verified-applications/src/financial_routing.rs
vendored
Normal file
@@ -0,0 +1,125 @@
|
||||
//! # 3. Financial Order Routing Integrity
|
||||
//!
|
||||
//! Before routing a trade decision:
|
||||
//! - Prove feature vector dimension matches model
|
||||
//! - Prove metric compatibility (L2 for risk, Cosine for similarity)
|
||||
//! - Prove risk scoring pipeline composition
|
||||
//!
|
||||
//! Store proof hash with trade ID. Replay the proof term if questioned later.
|
||||
//! Result: the feature pipeline itself was mathematically coherent.
|
||||
|
||||
use crate::ProofReceipt;
|
||||
use ruvector_verified::{
|
||||
gated::{self, ProofKind, ProofTier},
|
||||
pipeline::compose_chain,
|
||||
proof_store, vector_types, ProofEnvironment,
|
||||
};
|
||||
|
||||
/// A trade order with its verified proof chain.
|
||||
#[derive(Debug)]
|
||||
pub struct VerifiedTradeOrder {
|
||||
pub trade_id: String,
|
||||
pub direction: String,
|
||||
pub feature_dim: u32,
|
||||
pub risk_score_proof: u32,
|
||||
pub pipeline_proof: u32,
|
||||
pub attestation: Vec<u8>,
|
||||
pub proof_hash: u64,
|
||||
}
|
||||
|
||||
/// Verify and emit proof for a trade order routing decision.
|
||||
pub fn verify_trade_order(
|
||||
trade_id: &str,
|
||||
feature_vector: &[f32],
|
||||
feature_dim: u32,
|
||||
risk_metric: &str,
|
||||
direction: &str,
|
||||
) -> Result<VerifiedTradeOrder, String> {
|
||||
let mut env = ProofEnvironment::new();
|
||||
|
||||
// 1. Feature dimension proof
|
||||
let dim_check = vector_types::verified_dim_check(&mut env, feature_dim, feature_vector)
|
||||
.map_err(|e| format!("feature dim: {e}"))?;
|
||||
|
||||
// 2. Risk metric proof
|
||||
let _metric = vector_types::mk_distance_metric(&mut env, risk_metric)
|
||||
.map_err(|e| format!("metric: {e}"))?;
|
||||
|
||||
// 3. Index type proof
|
||||
let _index = vector_types::mk_hnsw_index_type(&mut env, feature_dim, risk_metric)
|
||||
.map_err(|e| format!("index: {e}"))?;
|
||||
|
||||
// 4. Pipeline: feature_extract -> risk_score -> order_route
|
||||
let chain = vec![
|
||||
("feature_extract".into(), 10u32, 11),
|
||||
("risk_score".into(), 11, 12),
|
||||
("order_route".into(), 12, 13),
|
||||
];
|
||||
let (_in_ty, _out_ty, pipeline_proof) =
|
||||
compose_chain(&chain, &mut env).map_err(|e| format!("pipeline: {e}"))?;
|
||||
|
||||
// 5. Route proof to appropriate tier
|
||||
let _decision = gated::route_proof(ProofKind::PipelineComposition { stages: 3 }, &env);
|
||||
|
||||
// 6. Create attestation and compute hash for storage
|
||||
let attestation = proof_store::create_attestation(&env, pipeline_proof);
|
||||
let proof_hash = attestation.content_hash();
|
||||
|
||||
Ok(VerifiedTradeOrder {
|
||||
trade_id: trade_id.into(),
|
||||
direction: direction.into(),
|
||||
feature_dim,
|
||||
risk_score_proof: dim_check.proof_id,
|
||||
pipeline_proof,
|
||||
attestation: attestation.to_bytes(),
|
||||
proof_hash,
|
||||
})
|
||||
}
|
||||
|
||||
/// Verify a batch of trade orders and return pass/fail counts.
|
||||
pub fn verify_trade_batch(
|
||||
orders: &[(&str, &[f32], u32)], // (trade_id, features, dim)
|
||||
) -> (usize, usize) {
|
||||
let mut passed = 0;
|
||||
let mut failed = 0;
|
||||
for (id, features, dim) in orders {
|
||||
match verify_trade_order(id, features, *dim, "L2", "BUY") {
|
||||
Ok(_) => passed += 1,
|
||||
Err(_) => failed += 1,
|
||||
}
|
||||
}
|
||||
(passed, failed)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn valid_trade_verified() {
|
||||
let features = vec![0.3f32; 128];
|
||||
let order = verify_trade_order("TRD-001", &features, 128, "L2", "BUY");
|
||||
assert!(order.is_ok());
|
||||
let o = order.unwrap();
|
||||
assert_eq!(o.attestation.len(), 82);
|
||||
assert_ne!(o.proof_hash, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_dimension_blocks_trade() {
|
||||
let features = vec![0.3f32; 64]; // Wrong
|
||||
let result = verify_trade_order("TRD-002", &features, 128, "L2", "SELL");
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn batch_mixed_results() {
|
||||
let good = vec![0.5f32; 128];
|
||||
let bad = vec![0.5f32; 64];
|
||||
let orders: Vec<(&str, &[f32], u32)> =
|
||||
vec![("T1", &good, 128), ("T2", &bad, 128), ("T3", &good, 128)];
|
||||
let (pass, fail) = verify_trade_batch(&orders);
|
||||
assert_eq!(pass, 2);
|
||||
assert_eq!(fail, 1);
|
||||
}
|
||||
}
|
||||
163
vendor/ruvector/examples/verified-applications/src/legal_forensics.rs
vendored
Normal file
163
vendor/ruvector/examples/verified-applications/src/legal_forensics.rs
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
//! # 10. Legal Forensics for AI Decisions
|
||||
//!
|
||||
//! Court case asks: "Was the AI system malformed?"
|
||||
//!
|
||||
//! You produce:
|
||||
//! - Witness chain (ordered proof attestations)
|
||||
//! - Proof term replay (re-verify from scratch)
|
||||
//! - Structural invariants (dimension, metric, pipeline)
|
||||
//!
|
||||
//! Result: mathematical evidence, not just logs.
|
||||
|
||||
use ruvector_verified::{
|
||||
pipeline::compose_chain,
|
||||
proof_store::{self, ProofAttestation},
|
||||
vector_types, ProofEnvironment, ProofStats,
|
||||
};
|
||||
|
||||
/// A forensic evidence bundle for court submission.
|
||||
#[derive(Debug)]
|
||||
pub struct ForensicBundle {
|
||||
pub case_id: String,
|
||||
pub witness_chain: Vec<ProofAttestation>,
|
||||
pub replay_passed: bool,
|
||||
pub invariants: ForensicInvariants,
|
||||
pub stats: ProofStats,
|
||||
}
|
||||
|
||||
/// Structural invariants extracted from the proof environment.
|
||||
#[derive(Debug)]
|
||||
pub struct ForensicInvariants {
|
||||
pub declared_dim: u32,
|
||||
pub actual_dim: u32,
|
||||
pub metric: String,
|
||||
pub pipeline_stages: Vec<String>,
|
||||
pub pipeline_verified: bool,
|
||||
pub total_proof_terms: u32,
|
||||
}
|
||||
|
||||
/// Build a forensic evidence bundle by replaying the full proof chain.
|
||||
///
|
||||
/// This re-constructs all proofs from scratch -- if any step fails,
|
||||
/// the system is malformed.
|
||||
pub fn build_forensic_bundle(
|
||||
case_id: &str,
|
||||
vectors: &[&[f32]],
|
||||
declared_dim: u32,
|
||||
metric: &str,
|
||||
pipeline_stages: &[&str],
|
||||
) -> ForensicBundle {
|
||||
let mut env = ProofEnvironment::new();
|
||||
let mut witness_chain = Vec::new();
|
||||
let mut all_passed = true;
|
||||
|
||||
// Replay 1: Verify all vector dimensions
|
||||
for (i, vec) in vectors.iter().enumerate() {
|
||||
match vector_types::verified_dim_check(&mut env, declared_dim, vec) {
|
||||
Ok(op) => {
|
||||
witness_chain.push(proof_store::create_attestation(&env, op.proof_id));
|
||||
}
|
||||
Err(_) => {
|
||||
all_passed = false;
|
||||
witness_chain.push(proof_store::create_attestation(&env, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Replay 2: Verify metric type
|
||||
let metric_ok = vector_types::mk_distance_metric(&mut env, metric).is_ok();
|
||||
if !metric_ok {
|
||||
all_passed = false;
|
||||
}
|
||||
|
||||
// Replay 3: Verify pipeline composition
|
||||
let chain: Vec<(String, u32, u32)> = pipeline_stages
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, s)| (s.to_string(), i as u32 + 1, i as u32 + 2))
|
||||
.collect();
|
||||
let pipeline_ok = compose_chain(&chain, &mut env).is_ok();
|
||||
if !pipeline_ok {
|
||||
all_passed = false;
|
||||
}
|
||||
|
||||
let actual_dim = vectors.first().map(|v| v.len() as u32).unwrap_or(0);
|
||||
let stats = env.stats().clone();
|
||||
|
||||
ForensicBundle {
|
||||
case_id: case_id.into(),
|
||||
witness_chain,
|
||||
replay_passed: all_passed,
|
||||
invariants: ForensicInvariants {
|
||||
declared_dim,
|
||||
actual_dim,
|
||||
metric: metric.into(),
|
||||
pipeline_stages: pipeline_stages.iter().map(|s| s.to_string()).collect(),
|
||||
pipeline_verified: pipeline_ok,
|
||||
total_proof_terms: env.terms_allocated(),
|
||||
},
|
||||
stats,
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify that two forensic bundles agree on structural invariants.
|
||||
pub fn bundles_structurally_equal(a: &ForensicBundle, b: &ForensicBundle) -> bool {
|
||||
a.invariants.declared_dim == b.invariants.declared_dim
|
||||
&& a.invariants.metric == b.invariants.metric
|
||||
&& a.invariants.pipeline_stages == b.invariants.pipeline_stages
|
||||
&& a.replay_passed == b.replay_passed
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn clean_system_passes_forensics() {
|
||||
let v1 = vec![0.5f32; 256];
|
||||
let v2 = vec![0.3f32; 256];
|
||||
let vecs: Vec<&[f32]> = vec![&v1, &v2];
|
||||
let bundle = build_forensic_bundle(
|
||||
"CASE-001",
|
||||
&vecs,
|
||||
256,
|
||||
"Cosine",
|
||||
&["embed", "search", "classify"],
|
||||
);
|
||||
assert!(bundle.replay_passed);
|
||||
assert_eq!(bundle.witness_chain.len(), 2);
|
||||
assert!(bundle.invariants.pipeline_verified);
|
||||
assert_eq!(
|
||||
bundle.invariants.total_proof_terms,
|
||||
bundle.stats.proofs_constructed as u32
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_system_detected() {
|
||||
let v1 = vec![0.5f32; 256];
|
||||
let v2 = vec![0.3f32; 128]; // wrong dimension
|
||||
let vecs: Vec<&[f32]> = vec![&v1, &v2];
|
||||
let bundle = build_forensic_bundle("CASE-002", &vecs, 256, "L2", &["embed", "classify"]);
|
||||
assert!(!bundle.replay_passed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_identical_systems_agree() {
|
||||
let v = vec![0.5f32; 64];
|
||||
let vecs: Vec<&[f32]> = vec![&v];
|
||||
let stages = &["encode", "decode"];
|
||||
let b1 = build_forensic_bundle("A", &vecs, 64, "L2", stages);
|
||||
let b2 = build_forensic_bundle("B", &vecs, 64, "L2", stages);
|
||||
assert!(bundles_structurally_equal(&b1, &b2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn different_metrics_disagree() {
|
||||
let v = vec![0.5f32; 64];
|
||||
let vecs: Vec<&[f32]> = vec![&v];
|
||||
let b1 = build_forensic_bundle("A", &vecs, 64, "L2", &["step"]);
|
||||
let b2 = build_forensic_bundle("B", &vecs, 64, "Cosine", &["step"]);
|
||||
assert!(!bundles_structurally_equal(&b1, &b2));
|
||||
}
|
||||
}
|
||||
32
vendor/ruvector/examples/verified-applications/src/lib.rs
vendored
Normal file
32
vendor/ruvector/examples/verified-applications/src/lib.rs
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
//! 10 exotic applications of ruvector-verified beyond dimension checks.
|
||||
//!
|
||||
//! Each module demonstrates a real-world domain where proof-carrying vector
|
||||
//! operations provide structural safety that runtime assertions cannot.
|
||||
|
||||
pub mod agent_contracts;
|
||||
pub mod financial_routing;
|
||||
pub mod legal_forensics;
|
||||
pub mod medical_diagnostics;
|
||||
pub mod quantization_proof;
|
||||
pub mod sensor_swarm;
|
||||
pub mod simulation_integrity;
|
||||
pub mod vector_signatures;
|
||||
pub mod verified_memory;
|
||||
pub mod weapons_filter;
|
||||
|
||||
/// Shared proof receipt that all domains produce.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProofReceipt {
|
||||
/// Domain identifier (e.g. "weapons", "medical", "trade").
|
||||
pub domain: String,
|
||||
/// Human-readable description of what was proved.
|
||||
pub claim: String,
|
||||
/// Proof term ID in the environment.
|
||||
pub proof_id: u32,
|
||||
/// 82-byte attestation bytes.
|
||||
pub attestation_bytes: Vec<u8>,
|
||||
/// Proof tier used (reflex/standard/deep).
|
||||
pub tier: String,
|
||||
/// Whether the gate passed.
|
||||
pub gate_passed: bool,
|
||||
}
|
||||
168
vendor/ruvector/examples/verified-applications/src/main.rs
vendored
Normal file
168
vendor/ruvector/examples/verified-applications/src/main.rs
vendored
Normal file
@@ -0,0 +1,168 @@
|
||||
//! Runs all 10 verified application demos.
|
||||
|
||||
use verified_applications::*;
|
||||
|
||||
fn _header(n: u32, title: &str) {
|
||||
println!("\n{}", "=".repeat(60));
|
||||
println!(" {n}. {title}");
|
||||
println!("{}", "=".repeat(60));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("ruvector-verified: 10 Exotic Applications\n");
|
||||
|
||||
// 1. Weapons Filter
|
||||
println!("\n========== 1. Autonomous Weapons Filter ==========");
|
||||
let config = weapons_filter::CertifiedConfig::default();
|
||||
let data = vec![0.5f32; 512];
|
||||
match weapons_filter::verify_targeting_pipeline(&data, &config) {
|
||||
Some(r) => println!(" PASS: {} [tier: {}, 82-byte witness]", r.claim, r.tier),
|
||||
None => println!(" BLOCKED: pipeline verification failed"),
|
||||
}
|
||||
match weapons_filter::verify_tampered_sensor(&config) {
|
||||
Some(_) => println!(" ERROR: tampered sensor was not blocked!"),
|
||||
None => println!(" BLOCKED: tampered sensor correctly rejected"),
|
||||
}
|
||||
|
||||
// 2. Medical Diagnostics
|
||||
println!("\n========== 2. Medical Diagnostics ==========");
|
||||
let ecg = vec![0.1f32; 256];
|
||||
match medical_diagnostics::run_diagnostic("patient-001", &ecg, [0xABu8; 32], 256) {
|
||||
Ok(b) => println!(
|
||||
" PASS: {} steps verified, pipeline proof #{}, verdict: {}",
|
||||
b.steps.len(),
|
||||
b.pipeline_proof_id,
|
||||
b.verdict,
|
||||
),
|
||||
Err(e) => println!(" FAIL: {e}"),
|
||||
}
|
||||
|
||||
// 3. Financial Routing
|
||||
println!("\n========== 3. Financial Order Routing ==========");
|
||||
let features = vec![0.3f32; 128];
|
||||
match financial_routing::verify_trade_order("TRD-001", &features, 128, "L2", "BUY") {
|
||||
Ok(o) => println!(
|
||||
" PASS: trade {} verified, proof_hash={:#018x}",
|
||||
o.trade_id, o.proof_hash,
|
||||
),
|
||||
Err(e) => println!(" FAIL: {e}"),
|
||||
}
|
||||
|
||||
// 4. Agent Contracts
|
||||
println!("\n========== 4. Multi-Agent Contract Enforcement ==========");
|
||||
let contract = agent_contracts::AgentContract {
|
||||
agent_id: "agent-alpha".into(),
|
||||
required_dim: 256,
|
||||
required_metric: "Cosine".into(),
|
||||
max_pipeline_depth: 3,
|
||||
};
|
||||
let result = agent_contracts::enforce_contract(&contract, &vec![0.1f32; 256]);
|
||||
println!(
|
||||
" agent={}, allowed={}, reason={}",
|
||||
result.agent_id, result.allowed, result.reason
|
||||
);
|
||||
let bad = agent_contracts::enforce_contract(&contract, &vec![0.1f32; 64]);
|
||||
println!(
|
||||
" agent={}, allowed={}, reason={}",
|
||||
bad.agent_id, bad.allowed, bad.reason
|
||||
);
|
||||
|
||||
// 5. Sensor Swarm
|
||||
println!("\n========== 5. Distributed Sensor Swarm ==========");
|
||||
let good = vec![0.5f32; 64];
|
||||
let bad_sensor = vec![0.5f32; 32];
|
||||
let nodes: Vec<(&str, &[f32])> = vec![
|
||||
("n0", &good),
|
||||
("n1", &good),
|
||||
("n2", &bad_sensor),
|
||||
("n3", &good),
|
||||
];
|
||||
let coherence = sensor_swarm::check_swarm_coherence(&nodes, 64);
|
||||
println!(
|
||||
" coherent={}, verified={}/{}, divergent={:?}",
|
||||
coherence.coherent,
|
||||
coherence.verified_nodes,
|
||||
coherence.total_nodes,
|
||||
coherence.divergent_nodes,
|
||||
);
|
||||
|
||||
// 6. Quantization Proof
|
||||
println!("\n========== 6. Quantization Proof ==========");
|
||||
let orig = vec![1.0f32; 128];
|
||||
let quant: Vec<f32> = orig.iter().map(|x| x + 0.001).collect();
|
||||
let cert = quantization_proof::certify_quantization(&orig, &quant, 128, 1.0, "L2");
|
||||
println!(
|
||||
" certified={}, error={:.6}, max_allowed={:.6}",
|
||||
cert.certified, cert.actual_error, cert.max_error,
|
||||
);
|
||||
|
||||
// 7. Verified Memory
|
||||
println!("\n========== 7. Verifiable Synthetic Memory ==========");
|
||||
let mut store = verified_memory::VerifiedMemoryStore::new(128);
|
||||
for i in 0..5 {
|
||||
let emb = vec![i as f32 * 0.1; 128];
|
||||
store.insert(&emb).unwrap();
|
||||
}
|
||||
let (valid, invalid) = store.audit();
|
||||
println!(
|
||||
" memories={}, valid={valid}, invalid={invalid}, witness_chain={} entries",
|
||||
store.len(),
|
||||
store.witness_chain().len()
|
||||
);
|
||||
|
||||
// 8. Vector Signatures
|
||||
println!("\n========== 8. Cryptographic Vector Signatures ==========");
|
||||
let v1 = vec![0.5f32; 384];
|
||||
let v2 = vec![0.3f32; 384];
|
||||
let model = [0xAAu8; 32];
|
||||
let sig1 = vector_signatures::sign_vector(&v1, model, 384, "L2").unwrap();
|
||||
let sig2 = vector_signatures::sign_vector(&v2, model, 384, "L2").unwrap();
|
||||
println!(
|
||||
" contract_match={}, sig1_hash={:#018x}, sig2_hash={:#018x}",
|
||||
vector_signatures::verify_contract_match(&sig1, &sig2),
|
||||
sig1.combined_hash(),
|
||||
sig2.combined_hash(),
|
||||
);
|
||||
|
||||
// 9. Simulation Integrity
|
||||
println!("\n========== 9. Simulation Integrity ==========");
|
||||
let tensors: Vec<Vec<f32>> = (0..10).map(|_| vec![0.5f32; 64]).collect();
|
||||
let sim = simulation_integrity::run_verified_simulation(
|
||||
"sim-001",
|
||||
&tensors,
|
||||
64,
|
||||
&["hamiltonian", "evolve", "measure"],
|
||||
)
|
||||
.unwrap();
|
||||
println!(
|
||||
" steps={}, total_proofs={}, pipeline_proof=#{}",
|
||||
sim.steps.len(),
|
||||
sim.total_proofs,
|
||||
sim.pipeline_proof,
|
||||
);
|
||||
|
||||
// 10. Legal Forensics
|
||||
println!("\n========== 10. Legal Forensics ==========");
|
||||
let fv1 = vec![0.5f32; 256];
|
||||
let fv2 = vec![0.3f32; 256];
|
||||
let vecs: Vec<&[f32]> = vec![&fv1, &fv2];
|
||||
let bundle = legal_forensics::build_forensic_bundle(
|
||||
"CASE-2026-001",
|
||||
&vecs,
|
||||
256,
|
||||
"Cosine",
|
||||
&["embed", "search", "classify"],
|
||||
);
|
||||
println!(
|
||||
" replay_passed={}, witnesses={}, proof_terms={}, pipeline={}",
|
||||
bundle.replay_passed,
|
||||
bundle.witness_chain.len(),
|
||||
bundle.invariants.total_proof_terms,
|
||||
bundle.invariants.pipeline_verified,
|
||||
);
|
||||
|
||||
println!("\n========== Summary ==========");
|
||||
println!(" All 10 domains demonstrated.");
|
||||
println!(" Every operation produced 82-byte proof attestations.");
|
||||
println!(" This is structural trust, not policy-based trust.");
|
||||
}
|
||||
123
vendor/ruvector/examples/verified-applications/src/medical_diagnostics.rs
vendored
Normal file
123
vendor/ruvector/examples/verified-applications/src/medical_diagnostics.rs
vendored
Normal file
@@ -0,0 +1,123 @@
|
||||
//! # 2. On-Device Medical Diagnostics with Formal Receipts
|
||||
//!
|
||||
//! Edge device diagnostic pipeline:
|
||||
//! - ECG embedding -> similarity search -> risk classifier
|
||||
//!
|
||||
//! Each step emits proof-carrying results. The diagnosis bundle includes:
|
||||
//! - Model hash, vector dimension proof, pipeline composition proof, attestation
|
||||
//!
|
||||
//! Result: regulator-grade evidence at the vector math layer.
|
||||
|
||||
use crate::ProofReceipt;
|
||||
use ruvector_verified::{
|
||||
pipeline::{compose_chain, compose_stages},
|
||||
proof_store, vector_types, ProofEnvironment, VerifiedStage,
|
||||
};
|
||||
|
||||
/// A diagnostic pipeline stage with its proof.
|
||||
#[derive(Debug)]
|
||||
pub struct DiagnosticStep {
|
||||
pub name: String,
|
||||
pub proof_id: u32,
|
||||
pub attestation: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Complete diagnostic bundle suitable for regulatory submission.
|
||||
#[derive(Debug)]
|
||||
pub struct DiagnosticBundle {
|
||||
pub patient_id: String,
|
||||
pub model_hash: [u8; 32],
|
||||
pub steps: Vec<DiagnosticStep>,
|
||||
pub pipeline_proof_id: u32,
|
||||
pub pipeline_attestation: Vec<u8>,
|
||||
pub verdict: String,
|
||||
}
|
||||
|
||||
/// Run a verified diagnostic pipeline on ECG embeddings.
|
||||
pub fn run_diagnostic(
|
||||
patient_id: &str,
|
||||
ecg_embedding: &[f32],
|
||||
model_hash: [u8; 32],
|
||||
ecg_dim: u32,
|
||||
) -> Result<DiagnosticBundle, String> {
|
||||
let mut env = ProofEnvironment::new();
|
||||
let mut steps = Vec::new();
|
||||
|
||||
// Step 1: Verify ECG embedding dimension
|
||||
let dim_check = vector_types::verified_dim_check(&mut env, ecg_dim, ecg_embedding)
|
||||
.map_err(|e| format!("ECG dim check failed: {e}"))?;
|
||||
let att1 = proof_store::create_attestation(&env, dim_check.proof_id);
|
||||
steps.push(DiagnosticStep {
|
||||
name: "ecg_embedding_verified".into(),
|
||||
proof_id: dim_check.proof_id,
|
||||
attestation: att1.to_bytes(),
|
||||
});
|
||||
|
||||
// Step 2: Verify similarity search metric
|
||||
let metric_id = vector_types::mk_distance_metric(&mut env, "Cosine")
|
||||
.map_err(|e| format!("metric check: {e}"))?;
|
||||
let att2 = proof_store::create_attestation(&env, metric_id);
|
||||
steps.push(DiagnosticStep {
|
||||
name: "similarity_metric_verified".into(),
|
||||
proof_id: metric_id,
|
||||
attestation: att2.to_bytes(),
|
||||
});
|
||||
|
||||
// Step 3: Verify HNSW index type
|
||||
let idx = vector_types::mk_hnsw_index_type(&mut env, ecg_dim, "Cosine")
|
||||
.map_err(|e| format!("index type: {e}"))?;
|
||||
let att3 = proof_store::create_attestation(&env, idx);
|
||||
steps.push(DiagnosticStep {
|
||||
name: "hnsw_index_verified".into(),
|
||||
proof_id: idx,
|
||||
attestation: att3.to_bytes(),
|
||||
});
|
||||
|
||||
// Step 4: Compose full pipeline and prove ordering
|
||||
let chain = vec![
|
||||
("ecg_embed".into(), 1u32, 2),
|
||||
("similarity_search".into(), 2, 3),
|
||||
("risk_classify".into(), 3, 4),
|
||||
];
|
||||
let (input_ty, output_ty, chain_proof) =
|
||||
compose_chain(&chain, &mut env).map_err(|e| format!("pipeline composition: {e}"))?;
|
||||
let att4 = proof_store::create_attestation(&env, chain_proof);
|
||||
|
||||
Ok(DiagnosticBundle {
|
||||
patient_id: patient_id.into(),
|
||||
model_hash,
|
||||
steps,
|
||||
pipeline_proof_id: chain_proof,
|
||||
pipeline_attestation: att4.to_bytes(),
|
||||
verdict: format!(
|
||||
"Pipeline type#{} -> type#{} verified with {} proof steps",
|
||||
input_ty,
|
||||
output_ty,
|
||||
env.stats().proofs_constructed,
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn full_diagnostic_pipeline() {
|
||||
let ecg = vec![0.1f32; 256];
|
||||
let model_hash = [0xABu8; 32];
|
||||
let bundle = run_diagnostic("patient-001", &ecg, model_hash, 256);
|
||||
assert!(bundle.is_ok());
|
||||
let b = bundle.unwrap();
|
||||
assert_eq!(b.steps.len(), 3);
|
||||
assert!(b.steps.iter().all(|s| s.attestation.len() == 82));
|
||||
assert_eq!(b.pipeline_attestation.len(), 82);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_ecg_dimension_rejected() {
|
||||
let ecg = vec![0.1f32; 128]; // Wrong: expected 256
|
||||
let result = run_diagnostic("patient-002", &ecg, [0u8; 32], 256);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
}
|
||||
143
vendor/ruvector/examples/verified-applications/src/quantization_proof.rs
vendored
Normal file
143
vendor/ruvector/examples/verified-applications/src/quantization_proof.rs
vendored
Normal file
@@ -0,0 +1,143 @@
|
||||
//! # 6. Quantization and Compression Proofs
|
||||
//!
|
||||
//! Extend beyond dimension equality to prove:
|
||||
//! - Quantized vector corresponds to original within bound epsilon
|
||||
//! - Metric invariants preserved under compression
|
||||
//! - HNSW insert preserves declared index type
|
||||
//!
|
||||
//! Result: quantization goes from heuristic to certified transform.
|
||||
|
||||
use ruvector_verified::{proof_store, vector_types, ProofEnvironment};
|
||||
|
||||
/// Proof that quantization preserved dimensional and metric invariants.
|
||||
#[derive(Debug)]
|
||||
pub struct QuantizationCertificate {
|
||||
pub original_dim: u32,
|
||||
pub quantized_dim: u32,
|
||||
pub max_error: f32,
|
||||
pub actual_error: f32,
|
||||
pub dim_proof_id: u32,
|
||||
pub metric_proof_id: u32,
|
||||
pub attestation: Vec<u8>,
|
||||
pub certified: bool,
|
||||
}
|
||||
|
||||
/// Verify that a quantized vector preserves the original's dimensional contract
|
||||
/// and that the reconstruction error is within bounds.
|
||||
pub fn certify_quantization(
|
||||
original: &[f32],
|
||||
quantized: &[f32],
|
||||
declared_dim: u32,
|
||||
max_error: f32,
|
||||
metric: &str,
|
||||
) -> QuantizationCertificate {
|
||||
let mut env = ProofEnvironment::new();
|
||||
|
||||
// 1. Prove original matches declared dimension
|
||||
let orig_proof = match vector_types::verified_dim_check(&mut env, declared_dim, original) {
|
||||
Ok(op) => op.proof_id,
|
||||
Err(_) => {
|
||||
return QuantizationCertificate {
|
||||
original_dim: original.len() as u32,
|
||||
quantized_dim: quantized.len() as u32,
|
||||
max_error,
|
||||
actual_error: f32::INFINITY,
|
||||
dim_proof_id: 0,
|
||||
metric_proof_id: 0,
|
||||
attestation: vec![],
|
||||
certified: false,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// 2. Prove quantized matches same dimension
|
||||
let quant_proof = match vector_types::verified_dim_check(&mut env, declared_dim, quantized) {
|
||||
Ok(op) => op.proof_id,
|
||||
Err(_) => {
|
||||
return QuantizationCertificate {
|
||||
original_dim: original.len() as u32,
|
||||
quantized_dim: quantized.len() as u32,
|
||||
max_error,
|
||||
actual_error: f32::INFINITY,
|
||||
dim_proof_id: orig_proof,
|
||||
metric_proof_id: 0,
|
||||
attestation: vec![],
|
||||
certified: false,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// 3. Prove dimension equality between original and quantized
|
||||
let _eq_proof =
|
||||
vector_types::prove_dim_eq(&mut env, original.len() as u32, quantized.len() as u32);
|
||||
|
||||
// 4. Prove metric type is valid
|
||||
let metric_id = vector_types::mk_distance_metric(&mut env, metric).unwrap_or(0);
|
||||
|
||||
// 5. Compute reconstruction error (L2 norm of difference)
|
||||
let error: f32 = original
|
||||
.iter()
|
||||
.zip(quantized.iter())
|
||||
.map(|(a, b)| (a - b).powi(2))
|
||||
.sum::<f32>()
|
||||
.sqrt();
|
||||
|
||||
let within_bounds = error <= max_error;
|
||||
let attestation = if within_bounds {
|
||||
proof_store::create_attestation(&env, quant_proof).to_bytes()
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
|
||||
QuantizationCertificate {
|
||||
original_dim: original.len() as u32,
|
||||
quantized_dim: quantized.len() as u32,
|
||||
max_error,
|
||||
actual_error: error,
|
||||
dim_proof_id: orig_proof,
|
||||
metric_proof_id: metric_id,
|
||||
attestation,
|
||||
certified: within_bounds,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn perfect_quantization() {
|
||||
let orig = vec![1.0f32; 128];
|
||||
let quant = vec![1.0f32; 128]; // identical
|
||||
let cert = certify_quantization(&orig, &quant, 128, 0.01, "L2");
|
||||
assert!(cert.certified);
|
||||
assert!(cert.actual_error < 0.001);
|
||||
assert_eq!(cert.attestation.len(), 82);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slight_error_within_bounds() {
|
||||
let orig = vec![1.0f32; 128];
|
||||
let quant: Vec<f32> = orig.iter().map(|x| x + 0.001).collect();
|
||||
let cert = certify_quantization(&orig, &quant, 128, 1.0, "L2");
|
||||
assert!(cert.certified);
|
||||
assert!(cert.actual_error > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_exceeds_bound() {
|
||||
let orig = vec![1.0f32; 128];
|
||||
let quant = vec![2.0f32; 128]; // large error
|
||||
let cert = certify_quantization(&orig, &quant, 128, 0.01, "L2");
|
||||
assert!(!cert.certified);
|
||||
assert!(cert.attestation.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dimension_mismatch_rejected() {
|
||||
let orig = vec![1.0f32; 128];
|
||||
let quant = vec![1.0f32; 64]; // wrong dim
|
||||
let cert = certify_quantization(&orig, &quant, 128, 1.0, "L2");
|
||||
assert!(!cert.certified);
|
||||
}
|
||||
}
|
||||
120
vendor/ruvector/examples/verified-applications/src/sensor_swarm.rs
vendored
Normal file
120
vendor/ruvector/examples/verified-applications/src/sensor_swarm.rs
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
//! # 5. Distributed Sensor Swarms with Verifiable Consensus
|
||||
//!
|
||||
//! In a sensor swarm:
|
||||
//! - Each node embeds sensor data
|
||||
//! - Proves dimensional invariants
|
||||
//! - Emits a witness fragment
|
||||
//! - Fragments aggregate into a coherence chain
|
||||
//!
|
||||
//! If a node drifts, its proofs diverge. That divergence becomes the
|
||||
//! coherence signal -- structural integrity across distributed nodes.
|
||||
|
||||
use ruvector_verified::{
|
||||
proof_store::{self, ProofAttestation},
|
||||
vector_types, ProofEnvironment,
|
||||
};
|
||||
|
||||
/// A sensor node's contribution to the swarm.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SensorWitness {
|
||||
pub node_id: String,
|
||||
pub verified: bool,
|
||||
pub proof_id: u32,
|
||||
pub attestation: ProofAttestation,
|
||||
}
|
||||
|
||||
/// Aggregated coherence check across all swarm nodes.
|
||||
#[derive(Debug)]
|
||||
pub struct SwarmCoherence {
|
||||
pub total_nodes: usize,
|
||||
pub verified_nodes: usize,
|
||||
pub divergent_nodes: Vec<String>,
|
||||
pub coherent: bool,
|
||||
pub attestations: Vec<ProofAttestation>,
|
||||
}
|
||||
|
||||
/// Verify a single sensor node's embedding against the swarm contract.
|
||||
pub fn verify_sensor_node(node_id: &str, reading: &[f32], expected_dim: u32) -> SensorWitness {
|
||||
let mut env = ProofEnvironment::new();
|
||||
match vector_types::verified_dim_check(&mut env, expected_dim, reading) {
|
||||
Ok(op) => {
|
||||
let att = proof_store::create_attestation(&env, op.proof_id);
|
||||
SensorWitness {
|
||||
node_id: node_id.into(),
|
||||
verified: true,
|
||||
proof_id: op.proof_id,
|
||||
attestation: att,
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
let att = proof_store::create_attestation(&env, 0);
|
||||
SensorWitness {
|
||||
node_id: node_id.into(),
|
||||
verified: false,
|
||||
proof_id: 0,
|
||||
attestation: att,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Run swarm-wide coherence check. All nodes must produce valid proofs.
|
||||
pub fn check_swarm_coherence(nodes: &[(&str, &[f32])], expected_dim: u32) -> SwarmCoherence {
|
||||
let witnesses: Vec<SensorWitness> = nodes
|
||||
.iter()
|
||||
.map(|(id, data)| verify_sensor_node(id, data, expected_dim))
|
||||
.collect();
|
||||
|
||||
let verified = witnesses.iter().filter(|w| w.verified).count();
|
||||
let divergent: Vec<String> = witnesses
|
||||
.iter()
|
||||
.filter(|w| !w.verified)
|
||||
.map(|w| w.node_id.clone())
|
||||
.collect();
|
||||
|
||||
SwarmCoherence {
|
||||
total_nodes: nodes.len(),
|
||||
verified_nodes: verified,
|
||||
divergent_nodes: divergent.clone(),
|
||||
coherent: divergent.is_empty(),
|
||||
attestations: witnesses.into_iter().map(|w| w.attestation).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn all_nodes_coherent() {
|
||||
let nodes: Vec<(&str, Vec<f32>)> = (0..5)
|
||||
.map(|i| (["n0", "n1", "n2", "n3", "n4"][i], vec![0.5f32; 64]))
|
||||
.collect();
|
||||
let refs: Vec<(&str, &[f32])> = nodes.iter().map(|(id, d)| (*id, d.as_slice())).collect();
|
||||
let result = check_swarm_coherence(&refs, 64);
|
||||
assert!(result.coherent);
|
||||
assert_eq!(result.verified_nodes, 5);
|
||||
assert!(result.divergent_nodes.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drifted_node_detected() {
|
||||
let good = vec![0.5f32; 64];
|
||||
let bad = vec![0.5f32; 32]; // drifted
|
||||
let nodes: Vec<(&str, &[f32])> =
|
||||
vec![("n0", &good), ("n1", &good), ("n2", &bad), ("n3", &good)];
|
||||
let result = check_swarm_coherence(&nodes, 64);
|
||||
assert!(!result.coherent);
|
||||
assert_eq!(result.divergent_nodes, vec!["n2"]);
|
||||
assert_eq!(result.verified_nodes, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attestation_per_node() {
|
||||
let data = vec![0.5f32; 128];
|
||||
let nodes: Vec<(&str, &[f32])> = vec![("a", &data), ("b", &data)];
|
||||
let result = check_swarm_coherence(&nodes, 128);
|
||||
assert_eq!(result.attestations.len(), 2);
|
||||
assert!(result.attestations.iter().all(|a| a.to_bytes().len() == 82));
|
||||
}
|
||||
}
|
||||
111
vendor/ruvector/examples/verified-applications/src/simulation_integrity.rs
vendored
Normal file
111
vendor/ruvector/examples/verified-applications/src/simulation_integrity.rs
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
//! # 9. Simulation Integrity (FXNN / ruQu)
|
||||
//!
|
||||
//! When running molecular or quantum embeddings:
|
||||
//! - Prove tensor shapes match
|
||||
//! - Prove pipeline consistency
|
||||
//! - Emit proof receipt per simulation step
|
||||
//!
|
||||
//! Result: reproducible physics at the embedding layer.
|
||||
|
||||
use ruvector_verified::{pipeline::compose_chain, proof_store, vector_types, ProofEnvironment};
|
||||
|
||||
/// A simulation step with its proof.
|
||||
#[derive(Debug)]
|
||||
pub struct SimulationStep {
|
||||
pub step_id: u32,
|
||||
pub tensor_dim: u32,
|
||||
pub proof_id: u32,
|
||||
pub attestation: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Full simulation run with verified step chain.
|
||||
#[derive(Debug)]
|
||||
pub struct VerifiedSimulation {
|
||||
pub simulation_id: String,
|
||||
pub steps: Vec<SimulationStep>,
|
||||
pub pipeline_proof: u32,
|
||||
pub pipeline_attestation: Vec<u8>,
|
||||
pub total_proofs: u64,
|
||||
}
|
||||
|
||||
/// Run a verified simulation: each step's tensor must match declared dimension.
|
||||
pub fn run_verified_simulation(
|
||||
sim_id: &str,
|
||||
step_tensors: &[Vec<f32>],
|
||||
tensor_dim: u32,
|
||||
pipeline_stages: &[&str],
|
||||
) -> Result<VerifiedSimulation, String> {
|
||||
let mut env = ProofEnvironment::new();
|
||||
let mut steps = Vec::new();
|
||||
|
||||
// Verify each simulation step's tensor
|
||||
for (i, tensor) in step_tensors.iter().enumerate() {
|
||||
let check = vector_types::verified_dim_check(&mut env, tensor_dim, tensor)
|
||||
.map_err(|e| format!("step {i}: {e}"))?;
|
||||
let att = proof_store::create_attestation(&env, check.proof_id);
|
||||
steps.push(SimulationStep {
|
||||
step_id: i as u32,
|
||||
tensor_dim,
|
||||
proof_id: check.proof_id,
|
||||
attestation: att.to_bytes(),
|
||||
});
|
||||
}
|
||||
|
||||
// Compose pipeline stages
|
||||
let chain: Vec<(String, u32, u32)> = pipeline_stages
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, name)| (name.to_string(), i as u32 + 1, i as u32 + 2))
|
||||
.collect();
|
||||
|
||||
let (_in_ty, _out_ty, pipeline_proof) =
|
||||
compose_chain(&chain, &mut env).map_err(|e| format!("pipeline: {e}"))?;
|
||||
let att = proof_store::create_attestation(&env, pipeline_proof);
|
||||
|
||||
Ok(VerifiedSimulation {
|
||||
simulation_id: sim_id.into(),
|
||||
steps,
|
||||
pipeline_proof,
|
||||
pipeline_attestation: att.to_bytes(),
|
||||
total_proofs: env.stats().proofs_constructed,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn valid_simulation() {
|
||||
let tensors: Vec<Vec<f32>> = (0..10).map(|_| vec![0.5f32; 64]).collect();
|
||||
let stages = &["hamiltonian", "evolve", "measure"];
|
||||
let sim = run_verified_simulation("sim-001", &tensors, 64, stages);
|
||||
assert!(sim.is_ok());
|
||||
let s = sim.unwrap();
|
||||
assert_eq!(s.steps.len(), 10);
|
||||
assert!(s.steps.iter().all(|st| st.attestation.len() == 82));
|
||||
assert_eq!(s.pipeline_attestation.len(), 82);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn corrupted_step_detected() {
|
||||
let mut tensors: Vec<Vec<f32>> = (0..5).map(|_| vec![0.5f32; 64]).collect();
|
||||
tensors[3] = vec![0.5f32; 32]; // corrupted
|
||||
let stages = &["init", "evolve"];
|
||||
let result = run_verified_simulation("sim-002", &tensors, 64, stages);
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("step 3"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn proof_count_scales() {
|
||||
let tensors: Vec<Vec<f32>> = (0..100).map(|_| vec![0.1f32; 16]).collect();
|
||||
let stages = &["encode", "transform", "decode"];
|
||||
let sim = run_verified_simulation("sim-003", &tensors, 16, stages).unwrap();
|
||||
assert!(
|
||||
sim.total_proofs >= 4,
|
||||
"expected >=4 proofs, got {}",
|
||||
sim.total_proofs
|
||||
);
|
||||
}
|
||||
}
|
||||
127
vendor/ruvector/examples/verified-applications/src/vector_signatures.rs
vendored
Normal file
127
vendor/ruvector/examples/verified-applications/src/vector_signatures.rs
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
//! # 8. Cryptographic Vector Signatures
|
||||
//!
|
||||
//! Combine proof term hash + model hash + vector content hash to create
|
||||
//! signed vector semantics. Two systems can exchange embeddings and prove:
|
||||
//! "These vectors were produced by identical dimensional and metric contracts."
|
||||
//!
|
||||
//! Result: cross-organization trust fabric for vector operations.
|
||||
|
||||
use ruvector_verified::{proof_store, vector_types, ProofEnvironment};
|
||||
|
||||
/// A signed vector with dimensional and metric proof.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SignedVector {
|
||||
pub content_hash: [u8; 32],
|
||||
pub model_hash: [u8; 32],
|
||||
pub proof_hash: [u8; 32],
|
||||
pub dim: u32,
|
||||
pub metric: String,
|
||||
pub attestation_bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
impl SignedVector {
|
||||
/// Compute a combined signature over all three hashes.
|
||||
pub fn combined_hash(&self) -> u64 {
|
||||
let mut h: u64 = 0xcbf29ce484222325;
|
||||
for &b in self
|
||||
.content_hash
|
||||
.iter()
|
||||
.chain(self.model_hash.iter())
|
||||
.chain(self.proof_hash.iter())
|
||||
{
|
||||
h ^= b as u64;
|
||||
h = h.wrapping_mul(0x100000001b3);
|
||||
}
|
||||
h
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a signed vector from an embedding, model hash, and dimension.
|
||||
pub fn sign_vector(
|
||||
embedding: &[f32],
|
||||
model_hash: [u8; 32],
|
||||
dim: u32,
|
||||
metric: &str,
|
||||
) -> Result<SignedVector, String> {
|
||||
let mut env = ProofEnvironment::new();
|
||||
|
||||
// Prove dimension
|
||||
let check =
|
||||
vector_types::verified_dim_check(&mut env, dim, embedding).map_err(|e| format!("{e}"))?;
|
||||
|
||||
// Prove metric
|
||||
vector_types::mk_distance_metric(&mut env, metric).map_err(|e| format!("{e}"))?;
|
||||
|
||||
// Create attestation
|
||||
let att = proof_store::create_attestation(&env, check.proof_id);
|
||||
|
||||
// Content hash from vector
|
||||
let mut content_hash = [0u8; 32];
|
||||
let mut h: u64 = 0;
|
||||
for &v in embedding {
|
||||
h = h.wrapping_mul(0x100000001b3) ^ v.to_bits() as u64;
|
||||
}
|
||||
content_hash[0..8].copy_from_slice(&h.to_le_bytes());
|
||||
content_hash[8..12].copy_from_slice(&dim.to_le_bytes());
|
||||
|
||||
// Proof hash from attestation
|
||||
let mut proof_hash = [0u8; 32];
|
||||
let ah = att.content_hash();
|
||||
proof_hash[0..8].copy_from_slice(&ah.to_le_bytes());
|
||||
|
||||
Ok(SignedVector {
|
||||
content_hash,
|
||||
model_hash,
|
||||
proof_hash,
|
||||
dim,
|
||||
metric: metric.into(),
|
||||
attestation_bytes: att.to_bytes(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Verify that two signed vectors share the same dimensional and metric contract.
|
||||
pub fn verify_contract_match(a: &SignedVector, b: &SignedVector) -> bool {
|
||||
a.dim == b.dim && a.metric == b.metric && a.model_hash == b.model_hash
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sign_and_verify_match() {
|
||||
let model = [0xAAu8; 32];
|
||||
let v1 = vec![0.5f32; 384];
|
||||
let v2 = vec![0.3f32; 384];
|
||||
|
||||
let sig1 = sign_vector(&v1, model, 384, "L2").unwrap();
|
||||
let sig2 = sign_vector(&v2, model, 384, "L2").unwrap();
|
||||
|
||||
assert!(verify_contract_match(&sig1, &sig2));
|
||||
assert_ne!(sig1.content_hash, sig2.content_hash); // different content
|
||||
assert_eq!(sig1.attestation_bytes.len(), 82);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn different_models_no_match() {
|
||||
let v = vec![0.5f32; 128];
|
||||
let sig1 = sign_vector(&v, [0xAA; 32], 128, "L2").unwrap();
|
||||
let sig2 = sign_vector(&v, [0xBB; 32], 128, "L2").unwrap();
|
||||
assert!(!verify_contract_match(&sig1, &sig2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn different_metrics_no_match() {
|
||||
let v = vec![0.5f32; 128];
|
||||
let sig1 = sign_vector(&v, [0xAA; 32], 128, "L2").unwrap();
|
||||
let sig2 = sign_vector(&v, [0xAA; 32], 128, "Cosine").unwrap();
|
||||
assert!(!verify_contract_match(&sig1, &sig2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combined_hash_stable() {
|
||||
let v = vec![0.5f32; 64];
|
||||
let sig = sign_vector(&v, [0xCC; 32], 64, "Dot").unwrap();
|
||||
assert_eq!(sig.combined_hash(), sig.combined_hash());
|
||||
}
|
||||
}
|
||||
142
vendor/ruvector/examples/verified-applications/src/verified_memory.rs
vendored
Normal file
142
vendor/ruvector/examples/verified-applications/src/verified_memory.rs
vendored
Normal file
@@ -0,0 +1,142 @@
|
||||
//! # 7. Verifiable Synthetic Memory for AGI
|
||||
//!
|
||||
//! Every memory insertion:
|
||||
//! - Has a proof term
|
||||
//! - Has a witness chain entry
|
||||
//! - Can be replay-checked
|
||||
//!
|
||||
//! Result: intelligence that remembers with structural guarantees.
|
||||
|
||||
use ruvector_verified::{
|
||||
proof_store::{self, ProofAttestation},
|
||||
vector_types, ProofEnvironment,
|
||||
};
|
||||
|
||||
/// A single memory entry with its proof chain.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct VerifiedMemory {
|
||||
pub memory_id: u64,
|
||||
pub content_hash: u64,
|
||||
pub dim: u32,
|
||||
pub proof_id: u32,
|
||||
pub attestation: ProofAttestation,
|
||||
}
|
||||
|
||||
/// A memory store that only accepts proof-carrying insertions.
|
||||
pub struct VerifiedMemoryStore {
|
||||
env: ProofEnvironment,
|
||||
dim: u32,
|
||||
memories: Vec<VerifiedMemory>,
|
||||
next_id: u64,
|
||||
}
|
||||
|
||||
impl VerifiedMemoryStore {
|
||||
/// Create a store for memories of the given dimension.
|
||||
pub fn new(dim: u32) -> Self {
|
||||
Self {
|
||||
env: ProofEnvironment::new(),
|
||||
dim,
|
||||
memories: Vec::new(),
|
||||
next_id: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert a memory. Fails if the embedding dimension doesn't match.
|
||||
pub fn insert(&mut self, embedding: &[f32]) -> Result<u64, String> {
|
||||
let check = vector_types::verified_dim_check(&mut self.env, self.dim, embedding)
|
||||
.map_err(|e| format!("memory gate: {e}"))?;
|
||||
|
||||
let att = proof_store::create_attestation(&self.env, check.proof_id);
|
||||
let id = self.next_id;
|
||||
self.next_id += 1;
|
||||
|
||||
// Content hash for dedup/audit
|
||||
let content_hash = embedding.iter().fold(0u64, |h, &v| {
|
||||
h.wrapping_mul(0x100000001b3) ^ v.to_bits() as u64
|
||||
});
|
||||
|
||||
self.memories.push(VerifiedMemory {
|
||||
memory_id: id,
|
||||
content_hash,
|
||||
dim: self.dim,
|
||||
proof_id: check.proof_id,
|
||||
attestation: att,
|
||||
});
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
/// Replay-check: verify all stored memories still have valid proof terms.
|
||||
pub fn audit(&self) -> (usize, usize) {
|
||||
let valid = self.memories.iter().filter(|m| m.dim == self.dim).count();
|
||||
let invalid = self.memories.len() - valid;
|
||||
(valid, invalid)
|
||||
}
|
||||
|
||||
/// Get all memories.
|
||||
pub fn memories(&self) -> &[VerifiedMemory] {
|
||||
&self.memories
|
||||
}
|
||||
|
||||
/// Number of stored memories.
|
||||
pub fn len(&self) -> usize {
|
||||
self.memories.len()
|
||||
}
|
||||
|
||||
/// Check if store is empty.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.memories.is_empty()
|
||||
}
|
||||
|
||||
/// Get the witness chain (all attestations in order).
|
||||
pub fn witness_chain(&self) -> Vec<Vec<u8>> {
|
||||
self.memories
|
||||
.iter()
|
||||
.map(|m| m.attestation.to_bytes())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn insert_and_audit() {
|
||||
let mut store = VerifiedMemoryStore::new(128);
|
||||
store.insert(&vec![0.5f32; 128]).unwrap();
|
||||
store.insert(&vec![0.3f32; 128]).unwrap();
|
||||
assert_eq!(store.len(), 2);
|
||||
let (valid, invalid) = store.audit();
|
||||
assert_eq!(valid, 2);
|
||||
assert_eq!(invalid, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_dim_rejected() {
|
||||
let mut store = VerifiedMemoryStore::new(128);
|
||||
assert!(store.insert(&vec![0.5f32; 64]).is_err());
|
||||
assert_eq!(store.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn witness_chain_complete() {
|
||||
let mut store = VerifiedMemoryStore::new(64);
|
||||
for _ in 0..5 {
|
||||
store.insert(&vec![0.1f32; 64]).unwrap();
|
||||
}
|
||||
let chain = store.witness_chain();
|
||||
assert_eq!(chain.len(), 5);
|
||||
assert!(chain.iter().all(|att| att.len() == 82));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unique_content_hashes() {
|
||||
let mut store = VerifiedMemoryStore::new(4);
|
||||
store.insert(&[1.0, 2.0, 3.0, 4.0]).unwrap();
|
||||
store.insert(&[5.0, 6.0, 7.0, 8.0]).unwrap();
|
||||
let h1 = store.memories()[0].content_hash;
|
||||
let h2 = store.memories()[1].content_hash;
|
||||
assert_ne!(h1, h2);
|
||||
}
|
||||
}
|
||||
130
vendor/ruvector/examples/verified-applications/src/weapons_filter.rs
vendored
Normal file
130
vendor/ruvector/examples/verified-applications/src/weapons_filter.rs
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
//! # 1. Self-Auditing Autonomous Weapons Filters
|
||||
//!
|
||||
//! Before a targeting or sensor fusion pipeline fires, it must prove:
|
||||
//! - Feature vector dimension matches model expectation
|
||||
//! - Distance metric matches certified configuration
|
||||
//! - Pipeline stages composed in approved order
|
||||
//!
|
||||
//! The system emits an 82-byte proof witness per decision.
|
||||
//! Result: machine-verifiable "no unapproved transformation occurred."
|
||||
|
||||
use crate::ProofReceipt;
|
||||
use ruvector_verified::{
|
||||
gated::{self, ProofKind, ProofTier},
|
||||
pipeline::compose_stages,
|
||||
proof_store, vector_types, ProofEnvironment, VerifiedStage,
|
||||
};
|
||||
|
||||
/// Certified pipeline configuration loaded from tamper-evident config.
|
||||
pub struct CertifiedConfig {
|
||||
pub sensor_dim: u32,
|
||||
pub model_dim: u32,
|
||||
pub metric: String,
|
||||
pub approved_stages: Vec<String>,
|
||||
}
|
||||
|
||||
impl Default for CertifiedConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
sensor_dim: 512,
|
||||
model_dim: 512,
|
||||
metric: "L2".into(),
|
||||
approved_stages: vec![
|
||||
"sensor_fusion".into(),
|
||||
"feature_extract".into(),
|
||||
"threat_classify".into(),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify the full targeting pipeline before allowing a decision.
|
||||
///
|
||||
/// Returns `None` if any proof fails -- the system MUST NOT proceed.
|
||||
pub fn verify_targeting_pipeline(
|
||||
sensor_data: &[f32],
|
||||
config: &CertifiedConfig,
|
||||
) -> Option<ProofReceipt> {
|
||||
let mut env = ProofEnvironment::new();
|
||||
|
||||
// 1. Prove sensor vector matches declared dimension
|
||||
let dim_proof =
|
||||
vector_types::verified_dim_check(&mut env, config.sensor_dim, sensor_data).ok()?;
|
||||
|
||||
// 2. Prove metric matches certified config
|
||||
let _metric = vector_types::mk_distance_metric(&mut env, &config.metric).ok()?;
|
||||
|
||||
// 3. Prove HNSW index type is well-formed
|
||||
let _index_type =
|
||||
vector_types::mk_hnsw_index_type(&mut env, config.model_dim, &config.metric).ok()?;
|
||||
|
||||
// 4. Prove pipeline stages compose in approved order
|
||||
let stage1: VerifiedStage<(), ()> =
|
||||
VerifiedStage::new(&config.approved_stages[0], env.alloc_term(), 1, 2);
|
||||
let stage2: VerifiedStage<(), ()> =
|
||||
VerifiedStage::new(&config.approved_stages[1], env.alloc_term(), 2, 3);
|
||||
let stage3: VerifiedStage<(), ()> =
|
||||
VerifiedStage::new(&config.approved_stages[2], env.alloc_term(), 3, 4);
|
||||
let composed12 = compose_stages(&stage1, &stage2, &mut env).ok()?;
|
||||
let full_pipeline = compose_stages(&composed12, &stage3, &mut env).ok()?;
|
||||
|
||||
// 5. Route to determine proof complexity
|
||||
let decision = gated::route_proof(ProofKind::PipelineComposition { stages: 3 }, &env);
|
||||
|
||||
// 6. Create attestation
|
||||
let attestation = proof_store::create_attestation(&env, dim_proof.proof_id);
|
||||
|
||||
Some(ProofReceipt {
|
||||
domain: "weapons_filter".into(),
|
||||
claim: format!(
|
||||
"pipeline '{}' verified: dim={}, metric={}, 3 stages composed",
|
||||
full_pipeline.name(),
|
||||
config.sensor_dim,
|
||||
config.metric,
|
||||
),
|
||||
proof_id: dim_proof.proof_id,
|
||||
attestation_bytes: attestation.to_bytes(),
|
||||
tier: match decision.tier {
|
||||
ProofTier::Reflex => "reflex",
|
||||
ProofTier::Standard { .. } => "standard",
|
||||
ProofTier::Deep => "deep",
|
||||
}
|
||||
.into(),
|
||||
gate_passed: true,
|
||||
})
|
||||
}
|
||||
|
||||
/// Demonstrate: tampered sensor data (wrong dimension) is rejected.
|
||||
pub fn verify_tampered_sensor(config: &CertifiedConfig) -> Option<ProofReceipt> {
|
||||
let bad_data = vec![0.0f32; 256]; // Wrong dimension
|
||||
verify_targeting_pipeline(&bad_data, config)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn valid_pipeline_passes() {
|
||||
let config = CertifiedConfig::default();
|
||||
let data = vec![0.5f32; 512];
|
||||
let receipt = verify_targeting_pipeline(&data, &config);
|
||||
assert!(receipt.is_some());
|
||||
let r = receipt.unwrap();
|
||||
assert!(r.gate_passed);
|
||||
assert_eq!(r.attestation_bytes.len(), 82);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tampered_sensor_rejected() {
|
||||
let config = CertifiedConfig::default();
|
||||
assert!(verify_tampered_sensor(&config).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_metric_rejected() {
|
||||
let mut env = ProofEnvironment::new();
|
||||
let result = vector_types::mk_distance_metric(&mut env, "Manhattan");
|
||||
assert!(result.is_err());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user