Formal Verification & Global Decentralized Registry
A practical, function-by-function developer guide explaining why formal contracts and decentralized package registries are needed, and how to use them to build crash-proof software.
1. Why Does This Module Exist?
🛑 The Real-World Problem
In safety-critical avionics (airplane autopilot), medical pacemakers, or banking engines moving $100 Million, a crash or buffer overflow can kill people or bankrupt companies. Meanwhile, centralized registries (like npm or PyPI) have had servers hacked to inject malware, and cannot function in air-gapped defense networks without internet access.
🛡️ The Nyx Solution
Formal Contracts (@requires, @ensures) mathematically prove that functions cannot receive bad inputs or produce bad outputs before running. Decentralized Registries use Ed25519 author signatures and SHA-256 hashes so packages can be verified 100% offline with mathematical tamper-resistance.
2. Formal Contracts (`std.formal`): Function Reference
Import std.formal to declare mathematical contracts on functions and structs.
@requires(condition: bool)
Declares a mandatory mathematical precondition that MUST be true before the function is called.
Example:
import std.formal;
// Precondition: Divisor cannot be zero, and balance must be non-negative
@requires(denominator != 0.0)
fn divide_safe(numerator: f64, denominator: f64) -> f64 {
return numerator / denominator;
}
@ensures(condition: bool)
Declares a postcondition that the function guarantees to be true upon returning. If violated, compilation fails or throws a contract fault.
Example:
import std.formal;
// Postcondition: Result must always be non-negative
@requires(amount > 0.0 && amount <= balance)
@ensures(result >= 0.0)
fn withdraw(balance: f64, amount: f64) -> f64 {
return balance - amount;
}
formal::verify_region_safety(scope: String, size: usize, has_escape: bool) -> VerificationProof
Generates an SMT-LIB2 / Lean 4 mathematical proof confirming that all pointers in a region expire before the region drops.
Example:
import std.formal;
let proof = formal::verify_region_safety("FlightControlArena", 1024 * 1024, false);
if proof.is_verified {
print("Soundness Proof # {proof.proof_id}: Q.E.D. Mathematically Sound!");
}
3. Decentralized Registry (`std.registry`): Function Reference
Import std.registry to sign, verify, and sync package artifacts without relying on a central server.
registry::sign_package(name: String, version: String, payload: *void, size: usize, key: String) -> PackageManifest
Hashes the package bytes with SHA-256 and attaches an Ed25519 digital signature generated from your private key.
Example:
import std.registry;
let manifest = registry::sign_package("@nyx/crypto", "1.0.0", payload_ptr, payload_len, "private_key.pem");
print("Signed Hash: {manifest.content_hash}");
print("Signature: {manifest.signature}");
registry::verify_package(manifest: *PackageManifest, payload: *void, size: usize) -> bool
Validates the binary payload against the author's public key and content hash. Returns true if authentic, false if tampered.
Example:
import std.registry;
let is_safe = registry::verify_package(&manifest, payload_ptr, payload_len);
if is_safe {
print("Package authentic & verified offline!");
}
registry::replicate_to_peer(manifest: *PackageManifest, peer_url: String) -> bool
Replicates the package artifact to another node in the peer-to-peer network (IPFS or Raft consensus mesh).
Example:
import std.registry;
registry::replicate_to_peer(&manifest, "ipfs://QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco");
registry::replicate_to_peer(&manifest, "raft-mesh://node-backup.internal:9443");
4. Complete Working Tutorial: Mission-Critical Aerospace Fuel Valve Controller
This complete example demonstrates how aerospace engineers use formal contracts to eliminate runtime catastrophic failure:
import std.formal;
struct ValveState {
flow_rate_liters_sec: f64,
pressure_psi: f64,
is_emergency_shutdown: bool,
}
@requires(target_pressure >= 10.0 && target_pressure <= 350.0) // Safe operating pressure band
@ensures(result.pressure_psi == target_pressure) // Guaranteed exact calibration
@ensures(result.is_emergency_shutdown == false) // Invariant: Valve is operational
fn calibrate_fuel_valve(target_pressure: f64) -> ValveState {
let mut valve = ValveState {
flow_rate_liters_sec: 14.5,
pressure_psi: target_pressure,
is_emergency_shutdown: false,
};
return valve;
}
fn main() {
print("Calibrating rocket engine fuel valves...");
let valve = calibrate_fuel_valve(185.0);
print("Calibration verified! Pressure: {valve.pressure_psi} PSI (Zero Runtime Risk)");
}