// Trace fq_mont_reduce(-571) step by step

fn main() {
    let q = 3329
    let qinv = 62209
    let a = 0 - 571

    println("a = " + int_to_str(a))

    let aq = a * qinv
    println("a * QINV = " + int_to_str(aq))

    let t_raw = bit_and(aq, 65535)
    println("bit_and(a*QINV, 65535) = " + int_to_str(t_raw))

    let mut t = t_raw
    if t >= 32768 { t = t - 65536 }
    println("signed t = " + int_to_str(t))

    let tq = t * q
    println("t * Q = " + int_to_str(tq))

    let diff = a - tq
    println("a - t*Q = " + int_to_str(diff))

    let low = bit_and(diff, 65535)
    println("(a-tQ) & 0xFFFF = " + int_to_str(low) + " (should be 0)")

    let result = shr(diff, 16)
    println("shr(diff, 16) = " + int_to_str(result))
    println("Expected ~42")
}
