Skip to content

Commit dadb5b0

Browse files
abstract compress/decompress
1 parent f063653 commit dadb5b0

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

src/decrypt.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use polynomial_ring::Polynomial;
22
use ring_lwe::utils::{polysub,nearest_int};
3-
use crate::utils::{Parameters,mul_vec_simple};
4-
use base64::{engine::general_purpose, Engine as _};
5-
use bincode;
3+
use crate::utils::{Parameters,mul_vec_simple,decompress};
64

75
/// Decrypt a ciphertext
86
/// # Arguments
@@ -53,21 +51,15 @@ pub fn decrypt_string(sk_string: &String, ciphertext_base64: &String, params: &P
5351
let (n, k) = (params.n, params.k);
5452

5553
// Base64 decode the secret key string
56-
let sk_bytes = general_purpose::STANDARD.decode(sk_string).expect("Failed to decode base64 secret key");
57-
58-
// Deserialize the secret key from bytes (it was serialized with bincode)
59-
let sk_array: Vec<i64> = bincode::deserialize(&sk_bytes).expect("Failed to deserialize secret key");
54+
let sk_array: Vec<i64> = decompress(sk_string);
6055

6156
// Convert the secret key into a Vec<Polynomial<i64>>
6257
let sk: Vec<Polynomial<i64>> = sk_array.chunks(n)
6358
.map(|chunk| Polynomial::new(chunk.to_vec()))
6459
.collect();
6560

66-
// Base64 decode the ciphertext string
67-
let ciphertext_bytes = general_purpose::STANDARD.decode(ciphertext_base64).expect("Failed to decode ciphertext");
68-
69-
// Deserialize the ciphertext list from the decoded bytes
70-
let ciphertext_list: Vec<i64> = bincode::deserialize(&ciphertext_bytes).expect("Failed to deserialize ciphertext");
61+
// Base64 decode and deserialize the ciphertext string
62+
let ciphertext_list: Vec<i64> = decompress(ciphertext_base64);
7163

7264
let block_size = (k + 1) * n;
7365
let num_blocks = ciphertext_list.len() / block_size;

src/encrypt.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use polynomial_ring::Polynomial;
22
use ring_lwe::utils::{polyadd,polysub,nearest_int};
3-
use crate::utils::{Parameters, add_vec, mul_mat_vec_simple, transpose, mul_vec_simple, gen_small_vector};
4-
use base64::{engine::general_purpose, Engine as _};
5-
use bincode;
3+
use crate::utils::{Parameters, add_vec, mul_mat_vec_simple, transpose, mul_vec_simple, gen_small_vector, compress, decompress};
64

75
/// Encrypt a message using the ring-LWE cryptosystem
86
/// # Arguments
@@ -72,11 +70,8 @@ pub fn encrypt_string(pk_string: &String, message_string: &String, params: &Para
7270
// Get parameters
7371
let (n, k) = (params.n, params.k);
7472

75-
// Decode the base64-encoded public key string
76-
let pk_bytes = general_purpose::STANDARD.decode(pk_string).expect("Failed to decode base64 public key");
77-
78-
// Deserialize the public key from bytes (it was serialized with bincode)
79-
let pk_list: Vec<i64> = bincode::deserialize(&pk_bytes).expect("Failed to deserialize public key");
73+
// Decode and deserialize the base64-encoded public key string
74+
let pk_list: Vec<i64> = decompress(pk_string);
8075

8176
// Parse the public key
8277
let a: Vec<Vec<Polynomial<i64>>> = pk_list[..k * k * n]
@@ -120,9 +115,6 @@ pub fn encrypt_string(pk_string: &String, message_string: &String, params: &Para
120115
ciphertext_list.extend(v_flattened);
121116
}
122117

123-
// Serialize and Base64 encode the ciphertext
124-
let ciphertext_bytes = bincode::serialize(&ciphertext_list).expect("Failed to serialize ciphertext");
125-
let ciphertext_base64 = general_purpose::STANDARD.encode(ciphertext_bytes);
126-
127-
ciphertext_base64
118+
// Serialize and Base64 encode the ciphertext coefficient list
119+
compress(&ciphertext_list)
128120
}

src/keygen.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use polynomial_ring::Polynomial;
22
use std::collections::HashMap;
3-
use crate::utils::{Parameters, add_vec, mul_mat_vec_simple, gen_small_vector, gen_uniform_matrix};
4-
use base64::{engine::general_purpose, Engine as _};
5-
use bincode;
3+
use crate::utils::{Parameters, add_vec, mul_mat_vec_simple, gen_small_vector, gen_uniform_matrix,compress};
64

75
/// Generate public and secret keys for the ring-LWE cryptosystem
86
/// # Arguments
@@ -77,18 +75,10 @@ pub fn keygen_string(params: &Parameters, seed: Option<u64>) -> HashMap<String,
7775
})
7876
.collect();
7977

80-
// Serialize the public and secret key coefficients
81-
let pk_bytes = bincode::serialize(&pk_coeffs).expect("Failed to serialize public key coefficients");
82-
let sk_bytes = bincode::serialize(&sk_coeffs).expect("Failed to serialize secret key coefficients");
83-
84-
// Base64 encode the serialized keys
85-
let pk_base64 = general_purpose::STANDARD.encode(pk_bytes);
86-
let sk_base64 = general_purpose::STANDARD.encode(sk_bytes);
87-
8878
// Store the Base64 encoded keys in a HashMap
8979
let mut keys: HashMap<String, String> = HashMap::new();
90-
keys.insert(String::from("secret"), sk_base64);
91-
keys.insert(String::from("public"), pk_base64);
80+
keys.insert(String::from("secret"), compress(&sk_coeffs));
81+
keys.insert(String::from("public"), compress(&pk_coeffs));
9282

9383
keys
9484
}

src/utils.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use rand::SeedableRng;
44
use rand::rngs::StdRng;
55
use ring_lwe::utils::{polyadd, polymul_fast, gen_uniform_poly};
66
use ntt::omega;
7+
use base64::{engine::general_purpose, Engine as _};
8+
use bincode;
79

810
#[derive(Debug)]
911
/// default parameters for module-LWE
@@ -143,4 +145,24 @@ pub fn gen_uniform_matrix(size : usize, rank: usize, modulus: i64, seed: Option<
143145
}
144146
}
145147
m
148+
}
149+
150+
/// seralize and encode a vector of i64 to a base64 encoded string
151+
/// # Arguments
152+
/// * `data` - vector of i64
153+
/// # Returns
154+
/// * `encoded` - base64 encoded string
155+
pub fn compress(data: &Vec<i64>) -> String {
156+
let serialized_data = bincode::serialize(data).expect("Failed to serialize data");
157+
general_purpose::STANDARD.encode(&serialized_data)
158+
}
159+
160+
/// decode and deserialize a base64 encoded string to a vector of i64
161+
/// # Arguments
162+
/// * `base64_str` - base64 encoded string
163+
/// # Returns
164+
/// * `decoded_data` - vector of i64
165+
pub fn decompress(base64_str: &str) -> Vec<i64> {
166+
let decoded_bytes = general_purpose::STANDARD.decode(base64_str).expect("Failed to decode base64 string");
167+
bincode::deserialize(&decoded_bytes).expect("Failed to deserialize data")
146168
}

0 commit comments

Comments
 (0)