use crate::{block::BLOCK, encrypt::sbox};
#[derive(Clone)]
pub struct Keys {
    pub keyBlocks: [BLOCK; 10],
}
impl Keys {
    fn new() -> Self {
        Keys {
            keyBlocks: [BLOCK::new([0_u8; 16]); 10],
        }
    }
    pub fn from(key: BLOCK) -> Self {
        let mut keys = crate::keyGen::Keys::new();
        keys.keyBlocks[0] = key;

        for i in 1..10 {
            let oldkey = keys.keyBlocks[i - 1];
            keys.keyBlocks[i] = oldkey;
            keys.keyBlocks[i].0[3].rotate_left(1);
            sbox(&mut keys.keyBlocks[i], true);
            keys.keyBlocks[i].0[3][1] += 1;
            keys.keyBlocks[i].0[0] = xor_row(keys.keyBlocks[i].0[0], oldkey.0[3]);

            for j in 1..3 {
                keys.keyBlocks[i].0[j] = xor_row(keys.keyBlocks[i].0[j], oldkey.0[j - 1]);
            }
        }
        keys
    }
}

fn xor_row(lhs: [u8; 4], rhs: [u8; 4]) -> [u8; 4] {
    [
        lhs[0] ^ rhs[0],
        lhs[1] ^ rhs[1],
        lhs[2] ^ rhs[2],
        lhs[3] ^ rhs[3],
    ]
}
