use hacspec_dev::prelude::*;
use hacspec_lib::prelude::*;

use hacspec_chacha20::*;

#[test]
fn test_quarter_round() {
    let mut state = State::from_public_slice(&[
        0x879531e0, 0xc5ecf37d, 0x516461b1, 0xc9a62f8a, 0x44c20ef3, 0x3390af7f, 0xd9fc690b,
        0x2a5f714c, 0x53372767, 0xb00a5631, 0x974c541a, 0x359e9963, 0x5c971061, 0x3d631689,
        0x2098d9d6, 0x91dbd320,
    ]);
    let expected_state = State::from_public_slice(&[
        0x879531e0, 0xc5ecf37d, 0xbdb886dc, 0xc9a62f8a, 0x44c20ef3, 0x3390af7f, 0xd9fc690b,
        0xcfacafd2, 0xe46bea80, 0xb00a5631, 0x974c541a, 0x359e9963, 0x5c971061, 0xccc07c79,
        0x2098d9d6, 0x91dbd320,
    ]);
    state = chacha_quarter_round(2, 7, 8, 13, state);
    assert_eq!(
        state
            .iter()
            .map(|x| U32::declassify(*x))
            .collect::<Vec<_>>(),
        expected_state
            .iter()
            .map(|x| U32::declassify(*x))
            .collect::<Vec<_>>()
    );
}

#[test]
fn test_block() {
    let key = Key::from_public_slice(&[
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
        0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
        0x1e, 0x1f,
    ]);
    let iv = IV::from_public_slice(&[
        0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00,
    ]);
    let ctr = U32(1);
    let state = chacha_block_init(key, ctr, iv);
    let expected_state = State::from_public_slice(&[
        0x61707865, 0x3320646e, 0x79622d32, 0x6b206574, 0x03020100, 0x07060504, 0x0b0a0908,
        0x0f0e0d0c, 0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c, 0x00000001, 0x09000000,
        0x4a000000, 0x00000000,
    ]);
    assert_eq!(
        state
            .iter()
            .map(|x| U32::declassify(*x))
            .collect::<Vec<_>>(),
        expected_state
            .iter()
            .map(|x| U32::declassify(*x))
            .collect::<Vec<_>>()
    );

    let state = chacha_block_inner(key, ctr, iv);
    let expected_state = State::from_public_slice(&[
        0xe4e7f110, 0x15593bd1, 0x1fdd0f50, 0xc47120a3, 0xc7f4d1c7, 0x0368c033, 0x9aaa2204,
        0x4e6cd4c3, 0x466482d2, 0x09aa9f07, 0x05d7c214, 0xa2028bd9, 0xd19c12b5, 0xb94e16de,
        0xe883d0cb, 0x4e3c50a2,
    ]);
    assert_eq!(
        state
            .iter()
            .map(|x| U32::declassify(*x))
            .collect::<Vec<_>>(),
        expected_state
            .iter()
            .map(|x| U32::declassify(*x))
            .collect::<Vec<_>>()
    );

    let expected_serialised = secret_bytes!([
        0x10, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15, 0x50, 0x0f, 0xdd, 0x1f, 0xa3, 0x20, 0x71,
        0xc4, 0xc7, 0xd1, 0xf4, 0xc7, 0x33, 0xc0, 0x68, 0x03, 0x04, 0x22, 0xaa, 0x9a, 0xc3, 0xd4,
        0x6c, 0x4e, 0xd2, 0x82, 0x64, 0x46, 0x07, 0x9f, 0xaa, 0x09, 0x14, 0xc2, 0xd7, 0x05, 0xd9,
        0x8b, 0x02, 0xa2, 0xb5, 0x12, 0x9c, 0xd1, 0xde, 0x16, 0x4e, 0xb9, 0xcb, 0xd0, 0x83, 0xe8,
        0xa2, 0x50, 0x3c, 0x4e
    ]);
    let serialised = state_to_bytes(state);
    println!("{:?}", serialised.len());
    assert_eq!(
        serialised
            .iter()
            .map(|x| U8::declassify(*x))
            .collect::<Vec<_>>(),
        expected_serialised
            .iter()
            .map(|x| U8::declassify(*x))
            .collect::<Vec<_>>()
    );
}

fn enc_dec_test(m: ByteSeq, key: Key, iv: IV) {
    let c = chacha(key, iv, &m);
    let m_dec = chacha(key, iv, &c);
    assert_eq!(
        m.iter().map(|x| U8::declassify(*x)).collect::<Vec<_>>(),
        m_dec.iter().map(|x| U8::declassify(*x)).collect::<Vec<_>>()
    );
}

fn kat_test(m: ByteSeq, key: Key, iv: IV, exp_cipher: ByteSeq) {
    let enc = chacha(key, iv, &m);
    let c = enc;
    assert_eq!(
        exp_cipher
            .iter()
            .map(|x| U8::declassify(*x))
            .collect::<Vec<_>>(),
        c.iter().map(|x| U8::declassify(*x)).collect::<Vec<_>>()
    );
    let m_dec = chacha(key, iv, &c);
    assert_eq!(
        m.iter().map(|x| U8::declassify(*x)).collect::<Vec<_>>(),
        m_dec.iter().map(|x| U8::declassify(*x)).collect::<Vec<_>>()
    );
}

#[test]
fn test_enc_dec() {
    let key = Key::from_public_slice(&random_byte_vec(Key::length()));
    let iv = IV::from_public_slice(&random_byte_vec(IV::length()));
    let m = ByteSeq::from_public_slice(&random_byte_vec(40));
    enc_dec_test(m, key, iv);
}

#[test]
fn test_kat() {
    let key = Key::from_public_slice(&[
        0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
        0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d,
        0x9e, 0x9f,
    ]);
    let iv = IV::from_public_slice(&[
        0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
    ]);
    let m = ByteSeq::from_public_slice(&[
        0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74,
        0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c,
        0x61, 0x73, 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20,
        0x49, 0x20, 0x63, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79,
        0x6f, 0x75, 0x20, 0x6f, 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70,
        0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65,
        0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75,
        0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, 0x74, 0x2e,
    ]);
    let exp_cipher = ByteSeq::from_public_slice(&[
        0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb, 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e,
        0xc2, 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe, 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee,
        0x62, 0xd6, 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12, 0x82, 0xfa, 0xfb, 0x69, 0xda,
        0x92, 0x72, 0x8b, 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29, 0x05, 0xd6, 0xa5, 0xb6,
        0x7e, 0xcd, 0x3b, 0x36, 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c, 0x98, 0x03, 0xae,
        0xe3, 0x28, 0x09, 0x1b, 0x58, 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94, 0x55, 0x85,
        0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc, 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d, 0xe5,
        0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b, 0x61, 0x16,
    ]);
    kat_test(m, key, iv, exp_cipher);
}
