ckks_engine/
keygen.rs

1use rand::Rng;  // Importing random number generator traits
2
3// Struct to represent the public key containing two polynomials
4#[derive(Debug, Clone)]
5pub struct PublicKey {
6    pub pk_0: Vec<i64>,  // First polynomial of the public key
7    pub pk_1: Vec<i64>,  // Second polynomial of the public key
8}
9
10// Struct to represent the secret key containing a polynomial
11#[derive(Debug, Clone)]
12pub struct SecretKey {
13    pub poly: Vec<i64>,  // Polynomial of the secret key
14}
15
16// Struct for key generation
17pub struct KeyGenerator;
18
19impl KeyGenerator {
20    // Constructor to create a new KeyGenerator
21    pub fn new() -> Self {
22        KeyGenerator
23    }
24
25    // Function to generate a pair of public and secret keys
26    pub fn generate_keys(&self) -> (PublicKey, SecretKey) {
27        let mut rng = rand::thread_rng();  // Create a random number generator
28        
29        // Generate secret key (random polynomial of size 2048)
30        let sec_key_poly: Vec<i64> = (0..2048).map(|_| rng.gen_range(1..100)).collect();
31        let sec_key = SecretKey { poly: sec_key_poly.clone() };  // Create secret key using the generated polynomial
32
33        // Generate a random polynomial for public key generation
34        let random_poly: Vec<i64> = (0..2048).map(|_| rng.gen_range(1..100)).collect();
35      
36        // Create public key polynomials
37        let pk_0: Vec<i64> = sec_key_poly.iter().zip(&random_poly)
38            .map(|(&sk, &r)| -sk * r + rng.gen_range(-10..10))  // Compute pk_0 as -sk * random + noise
39            .collect();
40        let pk_1: Vec<i64> = random_poly;  // Set pk_1 to the random polynomial
41
42        // Create public key with the generated polynomials
43        let pub_key = PublicKey { pk_0, pk_1 };
44
45        (pub_key, sec_key)  // Return the generated public and secret keys
46    }
47
48
49}
50
51
52