/*
 * Copyright (C) 2025 - 2026, Stephan Mueller <smueller@chronox.de>
 *
 * License: see LICENSE file in root directory
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
 * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

use leancrypto_sys::lcr_kyber_x25519::lcr_kyber_x25519;
use leancrypto_sys::lcr_kyber_x25519::lcr_kyber_x25519_type;

fn lc_rust_kyber_x25519_one(kyber_x25519_type: lcr_kyber_x25519_type) {
    let mut kyber_x25519 = lcr_kyber_x25519::new();

    let result = kyber_x25519.keypair(kyber_x25519_type);
    assert_eq!(result, Ok(()));

    let result = kyber_x25519.encapsulate();
    assert_eq!(result, Ok(()));

    let (ct_kyber_slice, ct_x25519_slice) =
        kyber_x25519.get_ct().expect("get_ct");
    let ct_kyber = ct_kyber_slice.to_vec();
    let ct_x25519 = ct_x25519_slice.to_vec();
    let (sk_kyber_slice, sk_x25519_slice) =
        kyber_x25519.get_sk().expect("get_sk");
    let sk_kyber = sk_kyber_slice.to_vec();
    let sk_x25519 = sk_x25519_slice.to_vec();

    let mut kyber_x255192 = lcr_kyber_x25519::new();
    let result = kyber_x255192.sk_load(&sk_kyber, &sk_x25519);
    assert_eq!(result, Ok(()));
    assert_eq!(
        kyber_x25519.get_sk().expect("get_sk").0,
        kyber_x255192.get_sk().expect("get_sk").0
    );
    assert_eq!(
        kyber_x25519.get_sk().expect("get_sk").1,
        kyber_x255192.get_sk().expect("get_sk").1
    );

    let result = kyber_x255192.ct_load(&ct_kyber, &ct_x25519);
    assert_eq!(result, Ok(()));
    assert_eq!(
        kyber_x25519.get_ct().expect("get_ct").0,
        kyber_x255192.get_ct().expect("get_ct").0
    );
    assert_eq!(
        kyber_x25519.get_ct().expect("get_ct").1,
        kyber_x255192.get_ct().expect("get_ct").1
    );

    let result = kyber_x255192.decapsulate();
    assert_eq!(result, Ok(()));
    assert_eq!(
        kyber_x25519.get_ss().expect("get_ss").0,
        kyber_x255192.get_ss().expect("get_ss").0
    );
    assert_eq!(
        kyber_x25519.get_ss().expect("get_ss").1,
        kyber_x255192.get_ss().expect("get_ss").1
    );
    //println!("ct {:x?}",  kyber_x255192.ct().to_vec().chunks(10).next());
}

#[test]
fn lc_rust_kyber_x25519_512() {
    lc_rust_kyber_x25519_one(lcr_kyber_x25519_type::lcr_kyber_512);
}

#[test]
fn lc_rust_kyber_x25519_768() {
    lc_rust_kyber_x25519_one(lcr_kyber_x25519_type::lcr_kyber_768);
}

#[test]
fn lc_rust_kyber_x25519_1024() {
    lc_rust_kyber_x25519_one(lcr_kyber_x25519_type::lcr_kyber_1024);
}
