//! X25519 Diffie-Hellman key exchange demonstration (RFC 7748).
//!
//! ```sh
//! cargo run -p arcana --release --example x25519_demo
//! ```

use arcana::ecc::x25519::{x25519_derive_public, x25519_ecdh};

fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{:02x}", b)).collect()
}

fn main() {
    // Alice and Bob each have a 32-byte secret key.
    let alice_sk = [0x77u8; 32]; // in production: random
    let bob_sk = [0x88u8; 32];

    // Derive public keys.
    let alice_pk = x25519_derive_public(&alice_sk);
    let bob_pk = x25519_derive_public(&bob_sk);
    println!("X25519 ECDH:");
    println!("  Alice pk: {}", hex(&alice_pk));
    println!("  Bob   pk: {}", hex(&bob_pk));

    // Each side computes the shared secret from their own sk and the
    // peer's pk. Both must arrive at the same 32-byte value.
    let shared_a = x25519_ecdh(&alice_sk, &bob_pk);
    let shared_b = x25519_ecdh(&bob_sk, &alice_pk);

    assert_eq!(shared_a, shared_b, "shared secrets must match");
    println!("  Shared:   {}", hex(&shared_a));
    println!("  Match:    OK");

    println!("\nAll X25519 demos passed.");
}
