<commit_before># Secure Protocol

## Encrypt

Encrypt module provides some encryption methods for data. It contains
[Paillier](https://en.wikipedia.org/wiki/Paillier_cryptosystem),
[RSA](https://en.wikipedia.org/wiki/RSA_\(cryptosystem\)), and Fake
method.

## Paillier encryption

Paillier encryption is a kind of addition homomorphic encryption which
belongs to probabilistic asymmetric algorithm.


  - Generate key pairs  
  
    To generate key pairs for Paillier, two big prime numbers are
    needed. The generator will randomly pick two prime numbers, \(p\)
    and \(q\), whose bit length has been set. Then, p and q will be used
    as private keys to generate PrivateKey object.
    
    Then use \(n = pq\) as public key to generate PublicKey object.

  - Encrypt by PublicKey  
    
    Encryption of Paillier follows the steps below.

    1.  Encode: Paillier algorithm is applicable only to integer. Thus the
        input number will be encoded as a integer.
    
    2.  Encrypt: The principle of encryption can be referred to
        [here](https://en.wikipedia.org/wiki/Paillier_cryptosystem) or
        [this paper](http://www.cs.tau.ac.il/~fiat/crypt07/papers/Pai99pai.pdf)
    
    3.  Apply Obfuscator: Apply an obfuscator so that every encrypted
        number is different even if the plaintext is the same.

  - Decrypt by PrivateKey

    1.  Decrypt: Same principle introduction with encrypt above.
    
    2.  Decode: decode back as what it is.

  - Addition and Scalar Multiplication  
    
    Please refer to the links above with encrypt step for details.

## Affine Homomorphic Encryption

Affine homomorphic encryption is another kind of addition homomorphic
encryption.

  - keygen
      
      First generate a big integer $n$, then generate another three big
      integers $$ a, a^{-1}, b, $$
      where $(a, n) = 1$, and $a * a^{-1}\equiv 1\pmod{n}$.

  - Encrypt
    
    $$ E(x) = (a * x + b)\pmod{n}, $$
    recorded as pair $(E(x), 1)$, $E(x)$ is ciphertext, 1 means the bias $b$ is 1.
  
  - Addition and Scalar Multiplication
    $$ E(x + y) = (E(x), 1) + (E(y), 1) = ((a * x + b) + (a * y + b), 1 + 1) = ((a * (x + y) + 2 * b), 2) = (E(x + y), 2) $$
    $$ Scalar * E(x) = Scalar * (E(x), 1) = (E(scalar * x), scalar * 1) $$

  - Decrypt  
    Decrypt $(E(x), k)$:
    remember that
    $$ (E(x), k) = a * x + k * b $$
    $$ \begin{align} Dec((E(x), k) &= a^{-1} * (E(x) - k * b) \pmod{n} \\
                                   &= a^{-1} * (a * x) \pmod{n}\\
                                   &= x \pmod{n} \end{align}$$

## IterativeAffine Homomorphic Encryption

Iterative Affine homomorphic encryption is another kind of addition
homomorphic encryption.

  - keygen  
    Generate an key-tuple array, the element in the array is a tuple
    $$ (a, a^{-1}, n), $$
    where $(a, n) = 1$, $a^{-1} * a \equiv 1\pmod{n}$. The array is sorted by $n$.

  - Encrypt  
    $$ E(x) = (Enc_n \circ\dots\circ Enc_1)(x) $$
    
    where $Enc_r(x) = a_r * x % n_r. a_r$, $n_r$ is the r-th element
    of key-tuple array.

  - Addition and Scalar Multiplication
    
    $$ E(x + y) = E(x) + E(y) = (Enc_n \circ\dots\circ Enc_1)(x + y) $$

    $$ \begin{align} scalar * E(x) &= scalar * ((Enc_n\circ\dots\circ Enc_1)(x))\\
                                   &= (Enc_n\circ\dots\circ Enc_1)(scalar * x) \end{align}$$

  - Decrypt  
    $$ Dec(E(x)) = (Dec_1 \circ Dec_2 \circ \dots \circ Dec_n)(x) $$
    
    where $Dec_r(x) = a_r^{-1} * (a_r * x) \pmod{n_r} = x \pmod{n_r}$

## RSA encryption

This encryption method generates three very large positive integers
$e$, $d$ and $n$. Let $e$, $n$ as the public-key and $d$ as the
privacy-key. While giving data $v$, the encrypt operator will do
$$ enc(v) = v^e \pmod{n}, $$
and the decrypt operator will do
$$ dec(v) = enc(v) ^ d \pmod{n} $$

## Fake encryption

It will do nothing and return input data during encryption and
decryption.

# Hash Factory

Hash factory provides following data encoding methods: "md5", "sha1", "sha224",
"sha256", "sha384", "sha512", "sm3". This module is meant to make hashing operation with more convenient. 
It also supports adding salt and outputing results to base64 format.

# Diffne Hellman Key Exchange

Diffie–Hellman key exchange is a method to exchange cryptographic keys
over a public channel securely

## Protocol

1.  keygen: generate big prime number $p$ and $g$, where $g$ is a
    primitive root modulo $p$
2.  Alice generates random number $r_1$; Bob generates random number
    $r_2$.
3.  Alice calculates $g^{r_1}\pmod{p}$ then send to Bob; Bob
    calculates $g^{r_2}\pmod{p}$ then sends to Alice.
4.  Alice calculates
    $(g^{r_2})^{r_1}\pmod{p}) = g^{r_1 r_2} \pmod{p}$; Bob calculates
    $(g^{r_1})^{r_2}\pmod{p} = g^{r_1 r_2} \pmod{p}$;
    $g^{r_1 r_2}\pmod{p}$ is the share key.

## How to use
<commit_msg>edit doc<commit_after># Secure Protocol

## Encrypt

Encrypt module provides some encryption methods for data. It contains
[Paillier](https://en.wikipedia.org/wiki/Paillier_cryptosystem),
[RSA](https://en.wikipedia.org/wiki/RSA_\(cryptosystem\)), and Fake
method.

## Paillier encryption

Paillier encryption is a kind of addition homomorphic encryption which
belongs to probabilistic asymmetric algorithm.


  - Generate key pairs  
  
    To generate key pairs for Paillier, two big prime numbers are
    needed. The generator will randomly pick two prime numbers, \(p\)
    and \(q\), whose bit length has been set. Then, p and q will be used
    as private keys to generate PrivateKey object.
    
    Then use \(n = pq\) as public key to generate PublicKey object.

  - Encrypt by PublicKey  
    
    Encryption of Paillier follows the steps below.

    1.  Encode: Paillier algorithm is applicable only to integer. Thus the
        input number will be encoded as a integer.
    
    2.  Encrypt: The principle of encryption can be referred to
        [here](https://en.wikipedia.org/wiki/Paillier_cryptosystem) or
        [this paper](http://www.cs.tau.ac.il/~fiat/crypt07/papers/Pai99pai.pdf)
    
    3.  Apply Obfuscator: Apply an obfuscator so that every encrypted
        number is different even if the plaintext is the same.

  - Decrypt by PrivateKey

    1.  Decrypt: Same principle introduction with encrypt above.
    
    2.  Decode: decode back as what it is.

  - Addition and Scalar Multiplication  
    
    Please refer to the links above with encrypt step for details.

## Affine Homomorphic Encryption

Affine homomorphic encryption is another kind of addition homomorphic
encryption.

  - keygen
      
      First generate a big integer $n$, then generate another three big
      integers $$ a, a^{-1}, b, $$
      where $(a, n) = 1$, and $a * a^{-1}\equiv 1\pmod{n}$.

  - Encrypt
    
    $$ E(x) = (a * x + b)\pmod{n}, $$
    recorded as pair $(E(x), 1)$, $E(x)$ is ciphertext, 1 means the bias $b$ is 1.
  
  - Addition and Scalar Multiplication
    $$ E(x + y) = (E(x), 1) + (E(y), 1) = ((a * x + b) + (a * y + b), 1 + 1) = ((a * (x + y) + 2 * b), 2) = (E(x + y), 2) $$
    $$ Scalar * E(x) = Scalar * (E(x), 1) = (E(scalar * x), scalar * 1) $$

  - Decrypt  
    Decrypt $(E(x), k)$:
    remember that
    $$ (E(x), k) = a * x + k * b $$
    $$ \begin{align} Dec((E(x), k) &= a^{-1} * (E(x) - k * b) \pmod{n} \\
                                   &= a^{-1} * (a * x) \pmod{n}\\
                                   &= x \pmod{n} \end{align}$$

## IterativeAffine Homomorphic Encryption

Iterative Affine homomorphic encryption is another kind of addition
homomorphic encryption.

  - keygen  
    Generate an key-tuple array, the element in the array is a tuple
    $$ (a, a^{-1}, n), $$
    where $(a, n) = 1$, $a^{-1} * a \equiv 1\pmod{n}$. The array is sorted by $n$.

  - Encrypt  
    $$ E(x) = (Enc_n \circ\dots\circ Enc_1)(x) $$
    
    where $Enc_r(x) = a_r * x % n_r. a_r$, $n_r$ is the r-th element
    of key-tuple array.

  - Addition and Scalar Multiplication
    
    $$ E(x + y) = E(x) + E(y) = (Enc_n \circ\dots\circ Enc_1)(x + y) $$

    $$ \begin{align} scalar * E(x) &= scalar * ((Enc_n\circ\dots\circ Enc_1)(x))\\
                                   &= (Enc_n\circ\dots\circ Enc_1)(scalar * x) \end{align}$$

  - Decrypt  
    $$ Dec(E(x)) = (Dec_1 \circ Dec_2 \circ \dots \circ Dec_n)(x) $$
    
    where $Dec_r(x) = a_r^{-1} * (a_r * x) \pmod{n_r} = x \pmod{n_r}$

## RSA encryption

This encryption method generates three very large positive integers
$e$, $d$ and $n$. Let $e$, $n$ as the public-key and $d$ as the
privacy-key. While giving data $v$, the encrypt operator will do
$$ enc(v) = v^e \pmod{n}, $$
and the decrypt operator will do
$$ dec(v) = enc(v) ^ d \pmod{n} $$

## Fake encryption

It will do nothing and return input data during encryption and
decryption.

## Hash Factory

Hash factory provides following data encoding methods: "md5", "sha1", "sha224",
"sha256", "sha384", "sha512", "sm3". This module is meant to make hashing operation with more convenient. 
It also supports adding salt and outputing results to base64 format.

## Diffne Hellman Key Exchange

Diffie–Hellman key exchange is a method to exchange cryptographic keys
over a public channel securely

## Protocol

1.  keygen: generate big prime number $p$ and $g$, where $g$ is a
    primitive root modulo $p$
2.  Alice generates random number $r_1$; Bob generates random number
    $r_2$.
3.  Alice calculates $g^{r_1}\pmod{p}$ then send to Bob; Bob
    calculates $g^{r_2}\pmod{p}$ then sends to Alice.
4.  Alice calculates
    $(g^{r_2})^{r_1}\pmod{p}) = g^{r_1 r_2} \pmod{p}$; Bob calculates
    $(g^{r_1})^{r_2}\pmod{p} = g^{r_1 r_2} \pmod{p}$;
    $g^{r_1 r_2}\pmod{p}$ is the share key.

## How to use

{'fraction_non_alphanumeric': 0.1381585730487679, 'fraction_numerical': 0.015053098257552326, 'mean_word_length': 2.7221795855717574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5888047', 'n_tokens_mistral': 3454, 'n_tokens_neox': 3255, 'n_words': 1195}