//! Ed25519 demonstration: key derivation, signing, and verification.
//!
//! ```sh
//! cargo run -p arcana --release --example eddsa_demo
//! ```

use arcana::ecc::eddsa::{ed25519_keygen, ed25519_sign, ed25519_verify};

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

fn main() {
    // Ed25519 key derivation from a 32-byte seed.
    let seed: [u8; 32] = [
        0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49,
        0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60,
    ];

    let (pk, sk) = ed25519_keygen(&seed);
    println!("Ed25519 keygen:");
    println!("  pk: {}", hex(&pk.0));
    println!("  sk: {}", hex(&sk.0));

    // Sign a message
    let msg = b"Ed25519 signature demo";
    let sig = ed25519_sign(&sk, msg);
    println!("\nSign:");
    println!("  sig: {}", hex(&sig.0));

    // Verify
    let ok = ed25519_verify(&pk, msg, &sig);
    assert!(ok, "signature must verify");
    println!("  verify: OK");

    // Tamper test
    let mut sig_bad = sig.clone();
    sig_bad.0[0] ^= 0xFF;
    assert!(!ed25519_verify(&pk, msg, &sig_bad));
    println!("  tamper: correctly rejected");

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