use crypto::sample
use crypto::fieldq

fn main() {
    // Test 1: CBD self-test
    println("=== CBD Noise Sampling ===")
    let errors = sample_self_test()
    if errors == 0 {
        println("CBD: all 256 coefficients in [-2, 2] with variation. PASS")
    } else {
        println("CBD: " + int_to_str(errors) + " errors")
    }

    // Show distribution of a noise polynomial
    let seed = secure_random_bytes(32)
    let noise = sample_noise_cbd2(seed, 0)
    let mut counts = t_zeros(5)
    let mut i = 0
    while i < 256 {
        let v = t_index_get(noise, i)
        let idx = v + 2.0
        let old = t_index_get(counts, idx)
        counts = t_index_set(counts, idx, old + 1.0)
        i = i + 1
    }
    println("  Distribution: -2:" + float_to_str(t_index_get(counts, 0)) + " -1:" + float_to_str(t_index_get(counts, 1)) + " 0:" + float_to_str(t_index_get(counts, 2)) + " +1:" + float_to_str(t_index_get(counts, 3)) + " +2:" + float_to_str(t_index_get(counts, 4)))

    // Test 2: Matrix A sampling
    println("")
    println("=== Matrix A Rejection Sampling ===")
    let rho = secure_random_bytes(32)
    let a00 = sample_ntt(rho, 0, 0)
    // Check all coefficients are in [0, q)
    let mut j = 0
    let mut bad = 0
    while j < 256 {
        let v = t_index_get(a00, j)
        if v < 0.0 || v >= 3329.0 {
            bad = bad + 1
        }
        j = j + 1
    }
    if bad == 0 {
        println("A[0,0]: all 256 coefficients in [0, q). PASS")
    } else {
        println("A[0,0]: " + int_to_str(bad) + " out-of-range coefficients")
    }
    println("  First 8: " + float_to_str(t_index_get(a00, 0)) + " " + float_to_str(t_index_get(a00, 1)) + " " + float_to_str(t_index_get(a00, 2)) + " " + float_to_str(t_index_get(a00, 3)) + " " + float_to_str(t_index_get(a00, 4)) + " " + float_to_str(t_index_get(a00, 5)) + " " + float_to_str(t_index_get(a00, 6)) + " " + float_to_str(t_index_get(a00, 7)))
}
