<commit_before>extern crate num;
extern crate rand;
extern crate serialize;

use bignum;
use sha1::Sha1;

pub struct DH<T: bignum::BigNumTrait> {
    p: T,
    g: T,
    a: T,
}

pub fn secret_to_key(s: &[u8]) -> Vec<u8> {
    let mut m = Sha1::new();
    m.update(s);
    m.digest().bytes()[0..16].to_vec()
}

pub fn serialize<T: bignum::BigNumTrait>(x: &T) -> Vec<u8> {
    x.to_bytes_be()
}

pub fn deserialize<T: bignum::BigNumTrait>(x: &[u8]) -> T {
    T::from_bytes_be(x)
}

impl<T: bignum::BigNumTrait> DH<T> {
    pub fn new() -> Self {
        DH {
            p: T::zero(),
            g: T::zero(),
            a: T::zero(),
        }
    }

    pub fn init(&mut self) {
        let p_hex = "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74\
                     020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f1437\
                     4fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7ed\
                     ee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf05\
                     98da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb\
                     9ed529077096966d670c354e4abc9804f1746c08ca237327ffffffffffffffff";

        let p = T::from_hex_str(p_hex).unwrap();
        let g = T::from_u32(2);
        //let p = 37.to_biguint().unwrap();
        //let g = 5.to_biguint().unwrap();
        self.p = p;
        self.g = g;
    }

    pub fn init_with_parameters(&mut self, p: Vec<u8>, g: Vec<u8>) {
        self.p = deserialize(&p);
        self.g = deserialize(&g);
    }

    pub fn parameters(&self) -> (Vec<u8>, Vec<u8>) {
        (serialize(&self.p), serialize(&self.g))
    }

    fn generate_private_key(&mut self) {
        //let mut rng = rand::thread_rng();
        let a = T::gen_below(&self.p);
        self.a = a;
    }

    pub fn public_key(&mut self) -> Vec<u8> {
        self.generate_private_key();
        serialize(&self.g.mod_exp(&self.a, &self.p))
    }

    #[allow(non_snake_case)]
    pub fn shared_key(&mut self, B: &[u8]) -> Vec<u8> {
        let B = T::from_bytes_be(B);
        let s = B.mod_exp(&self.a, &self.p);
        secret_to_key(&serialize(&s))
    }
}

impl<T: bignum::BigNumTrait> Default for DH<T> {
    fn default() -> Self {
        Self::new()
    }
}<commit_msg>Use serialization wrapper<commit_after>extern crate num;
extern crate rand;
extern crate serialize;

use bignum;
use sha1::Sha1;

pub struct DH<T: bignum::BigNumTrait> {
    p: T,
    g: T,
    a: T,
}

pub fn secret_to_key(s: &[u8]) -> Vec<u8> {
    let mut m = Sha1::new();
    m.update(s);
    m.digest().bytes()[0..16].to_vec()
}

pub fn serialize<T: bignum::BigNumTrait>(x: &T) -> Vec<u8> {
    x.to_bytes_be()
}

pub fn deserialize<T: bignum::BigNumTrait>(x: &[u8]) -> T {
    T::from_bytes_be(x)
}

impl<T: bignum::BigNumTrait> DH<T> {
    pub fn new() -> Self {
        DH {
            p: T::zero(),
            g: T::zero(),
            a: T::zero(),
        }
    }

    pub fn init(&mut self) {
        let p_hex = "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74\
                     020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f1437\
                     4fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7ed\
                     ee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf05\
                     98da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb\
                     9ed529077096966d670c354e4abc9804f1746c08ca237327ffffffffffffffff";

        let p = T::from_hex_str(p_hex).unwrap();
        let g = T::from_u32(2);
        //let p = 37.to_biguint().unwrap();
        //let g = 5.to_biguint().unwrap();
        self.p = p;
        self.g = g;
    }

    pub fn init_with_parameters(&mut self, p: Vec<u8>, g: Vec<u8>) {
        self.p = deserialize(&p);
        self.g = deserialize(&g);
    }

    pub fn parameters(&self) -> (Vec<u8>, Vec<u8>) {
        (serialize(&self.p), serialize(&self.g))
    }

    fn generate_private_key(&mut self) {
        //let mut rng = rand::thread_rng();
        let a = T::gen_below(&self.p);
        self.a = a;
    }

    pub fn public_key(&mut self) -> Vec<u8> {
        self.generate_private_key();
        serialize(&self.g.mod_exp(&self.a, &self.p))
    }

    #[allow(non_snake_case)]
    pub fn shared_key(&mut self, B: &[u8]) -> Vec<u8> {
        let B: T = deserialize(B);
        let s = B.mod_exp(&self.a, &self.p);
        secret_to_key(&serialize(&s))
    }
}

impl<T: bignum::BigNumTrait> Default for DH<T> {
    fn default() -> Self {
        Self::new()
    }
}
{'fraction_non_alphanumeric': 0.14915327833260963, 'fraction_numerical': 0.10855405992184107, 'mean_word_length': 2.3974926253687316, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3460290', 'n_tokens_mistral': 2167, 'n_tokens_neox': 1879, 'n_words': 311}