use amaters_core::{
compute::{CircuitBuilder, CircuitValue, EncryptedType, FheExecutor},
storage::MemoryStorage,
traits::StorageEngine,
types::{CipherBlob, Key},
};
fn encrypt_u8(value: u8) -> CipherBlob {
CipherBlob::new(vec![value])
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
println!("=== Supply Chain Transparency: Privacy-Preserving ESG ===\n");
let company_a = MemoryStorage::new();
let company_b = MemoryStorage::new();
let suppliers_a: &[(&str, u8, u8, u8, u8)] = &[
("S-A01", 45, 92, 120, 1),
("S-A02", 78, 85, 95, 1),
("S-A03", 32, 98, 150, 1),
];
for (id, co2, reliability, cost, compliance) in suppliers_a {
let prefix = format!("company_a:supplier:{id}");
company_a
.put(&Key::from_str(&format!("{prefix}:co2")), &encrypt_u8(*co2))
.await?;
company_a
.put(
&Key::from_str(&format!("{prefix}:reliability")),
&encrypt_u8(*reliability),
)
.await?;
company_a
.put(
&Key::from_str(&format!("{prefix}:cost")),
&encrypt_u8(*cost),
)
.await?;
company_a
.put(
&Key::from_str(&format!("{prefix}:compliance")),
&encrypt_u8(*compliance),
)
.await?;
println!("[Company A] Stored encrypted data for supplier {id}");
}
let suppliers_b: &[(&str, u8, u8, u8, u8)] = &[
("S-B01", 60, 88, 80, 1),
("S-B02", 25, 95, 110, 0), ];
for (id, co2, reliability, cost, compliance) in suppliers_b {
let prefix = format!("company_b:supplier:{id}");
company_b
.put(&Key::from_str(&format!("{prefix}:co2")), &encrypt_u8(*co2))
.await?;
company_b
.put(
&Key::from_str(&format!("{prefix}:reliability")),
&encrypt_u8(*reliability),
)
.await?;
company_b
.put(
&Key::from_str(&format!("{prefix}:cost")),
&encrypt_u8(*cost),
)
.await?;
company_b
.put(
&Key::from_str(&format!("{prefix}:compliance")),
&encrypt_u8(*compliance),
)
.await?;
println!("[Company B] Stored encrypted data for supplier {id}");
}
println!("\n--- Range Queries ---");
let a_start = Key::from_str("company_a:supplier:");
let a_end = Key::from_str("company_a:supplier:~");
let a_records = company_a.range(&a_start, &a_end).await?;
println!(" Company A: {} encrypted supplier fields", a_records.len());
let b_start = Key::from_str("company_b:supplier:");
let b_end = Key::from_str("company_b:supplier:~");
let b_records = company_b.range(&b_start, &b_end).await?;
println!(" Company B: {} encrypted supplier fields", b_records.len());
println!("\n--- Batch Key Listing ---");
let a_keys = company_a.keys().await?;
let b_keys = company_b.keys().await?;
println!(" Company A keys: {}", a_keys.len());
println!(" Company B keys: {}", b_keys.len());
println!("\n--- FHE Circuit: Cross-Company CO2 Footprint ---");
let mut co2_builder = CircuitBuilder::new();
co2_builder
.declare_variable("co2_a", EncryptedType::U8)
.declare_variable("co2_b", EncryptedType::U8);
let co2_a = co2_builder.load("co2_a");
let co2_b = co2_builder.load("co2_b");
let total_co2 = co2_builder.add(co2_a, co2_b);
let co2_circuit = co2_builder.build(total_co2)?;
println!(
" Circuit depth: {}, gates: {}",
co2_circuit.depth, co2_circuit.gate_count
);
println!(
" Result type: {} (remains encrypted)",
co2_circuit.result_type
);
println!("\n--- FHE Circuit: Sustainability Score ---");
let mut score_builder = CircuitBuilder::new();
score_builder
.declare_variable("reliability", EncryptedType::U8)
.declare_variable("co2", EncryptedType::U8);
let rel = score_builder.load("reliability");
let co2_val = score_builder.load("co2");
let penalty_weight = score_builder.constant(CircuitValue::U8(2));
let penalty = score_builder.mul(co2_val, penalty_weight);
let score = score_builder.sub(rel, penalty);
let score_circuit = score_builder.build(score)?;
println!(
" Circuit depth: {}, gates: {}",
score_circuit.depth, score_circuit.gate_count
);
println!("\n--- FHE Circuit: Joint Compliance Gate ---");
let mut comp_builder = CircuitBuilder::new();
comp_builder
.declare_variable("cert_a", EncryptedType::Bool)
.declare_variable("cert_b", EncryptedType::Bool);
let cert_a = comp_builder.load("cert_a");
let cert_b = comp_builder.load("cert_b");
let both = comp_builder.and(cert_a, cert_b);
let comp_circuit = comp_builder.build(both)?;
println!(
" Circuit depth: {}, gates: {}",
comp_circuit.depth, comp_circuit.gate_count
);
println!("\n--- Attempting FHE Execution ---");
let executor = FheExecutor::new();
let inputs = std::collections::HashMap::new(); match executor.execute(&co2_circuit, &inputs) {
Ok(result) => println!(" Encrypted CO2 total: {} bytes", result.len()),
Err(e) => println!(" Expected: {e} (enable `compute` feature for real FHE)"),
}
println!("\n--- Cross-Company Privacy Summary ---");
println!(
" Company A: {} encrypted fields across {} suppliers",
company_a.len(),
suppliers_a.len()
);
println!(
" Company B: {} encrypted fields across {} suppliers",
company_b.len(),
suppliers_b.len()
);
println!(" Neither company revealed costs, reliability, or CO2 data.");
println!(" Joint CO2 footprint and compliance computed on ciphertext.");
println!(" Only the final decrypted aggregate is shared (by mutual consent).");
Ok(())
}