1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
//! rlwe_params module describing the RLWEParams structure
use super::{read_from_file, write_to_file};
use crate::error::CryptoAPIError;
use backtrace::Backtrace;
use colored::Colorize;
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fmt;
/// Structure describing the security parameters for encryption with RLWE ciphertexts
/// # Attributes
/// - `polynomial_size`: the number of coefficients in a polynomial
/// - `dimension`: the size of an RLWE mask
/// - `log2_std_dev`: the log2 of the standard deviation used for the error normal distribution
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RLWEParams {
pub polynomial_size: usize,
pub dimension: usize,
pub log2_std_dev: i32,
}
////////////////////////////////////////
// 128 bits of security - dimension 1 //
////////////////////////////////////////
/// 128 bits of security with a polynomial_size of 1 and a polynomial size of 256 (LWE estimator, September 15th 2020)
pub const RLWE128_256_1: RLWEParams = RLWEParams {
dimension: 1,
polynomial_size: 256,
log2_std_dev: -5,
};
/// 128 bits of security with a polynomial_size of 1 and a polynomial size of 512 (LWE estimator, September 15th 2020)
pub const RLWE128_512_1: RLWEParams = RLWEParams {
dimension: 1,
polynomial_size: 512,
log2_std_dev: -11,
};
/// 128 bits of security with a polynomial_size of 1 and a polynomial size of 1024 (LWE estimator, September 15th 2020)
pub const RLWE128_1024_1: RLWEParams = RLWEParams {
dimension: 1,
polynomial_size: 1024,
log2_std_dev: -25,
};
/// 128 bits of security with a polynomial_size of 1 and a polynomial size of 2048 (LWE estimator, September 15th 2020)
pub const RLWE128_2048_1: RLWEParams = RLWEParams {
dimension: 1,
polynomial_size: 2048,
log2_std_dev: -52, // warning u32
};
/// 128 bits of security with a polynomial_size of 1 and a polynomial size of 4096 (LWE estimator, September 15th 2020)
pub const RLWE128_4096_1: RLWEParams = RLWEParams {
dimension: 1,
polynomial_size: 4096,
log2_std_dev: -105, // warning u64
};
////////////////////////////////////////
// 128 bits of security - dimension 2 //
////////////////////////////////////////
/// 128 bits of security with a polynomial_size of 2 and a polynomial size of 256 (LWE estimator, September 15th 2020)
pub const RLWE128_256_2: RLWEParams = RLWEParams {
dimension: 2,
polynomial_size: 256,
log2_std_dev: -11,
};
/// 128 bits of security with a polynomial_size of 2 and a polynomial size of 512 (LWE estimator, September 15th 2020)
pub const RLWE128_512_2: RLWEParams = RLWEParams {
dimension: 2,
polynomial_size: 512,
log2_std_dev: -25,
};
////////////////////////////////////////
// 128 bits of security - dimension 4 //
////////////////////////////////////////
/// 128 bits of security with a polynomial_size of 4 and a polynomial size of 256 (LWE estimator, September 15th 2020)
pub const RLWE128_256_4: RLWEParams = RLWEParams {
dimension: 4,
polynomial_size: 256,
log2_std_dev: -25,
};
///////////////////////////////////////
// 80 bits of security - dimension 1 //
///////////////////////////////////////
/// 80 bits of security with a polynomial_size of 1 and a polynomial size of 256 (LWE estimator, September 15th 2020)
pub const RLWE80_256_1: RLWEParams = RLWEParams {
dimension: 1,
polynomial_size: 256,
log2_std_dev: -9,
};
/// 80 bits of security with a polynomial_size of 1 and a polynomial size of 512 (LWE estimator, September 15th 2020)
pub const RLWE80_512_1: RLWEParams = RLWEParams {
dimension: 1,
polynomial_size: 512,
log2_std_dev: -19,
};
/// 80 bits of security with a polynomial_size of 1 and a polynomial size of 1024 (LWE estimator, September 15th 2020)
pub const RLWE80_1024_1: RLWEParams = RLWEParams {
dimension: 1,
polynomial_size: 1024,
log2_std_dev: -40, // warning u32
};
/// 80 bits of security with a polynomial_size of 1 and a polynomial size of 2048 (LWE estimator, September 15th 2020)
pub const RLWE80_2048_1: RLWEParams = RLWEParams {
dimension: 1,
polynomial_size: 2048,
log2_std_dev: -82, // warning u64
};
///////////////////////////////////////
// 80 bits of security - dimension 2 //
///////////////////////////////////////
/// 80 bits of security with a polynomial_size of 2 and a polynomial size of 256 (LWE estimator, September 15th 2020)
pub const RLWE80_256_2: RLWEParams = RLWEParams {
dimension: 2,
polynomial_size: 256,
log2_std_dev: -19,
};
/// 80 bits of security with a polynomial_size of 2 and a polynomial size of 512 (LWE estimator, September 15th 2020)
pub const RLWE80_512_2: RLWEParams = RLWEParams {
dimension: 2,
polynomial_size: 512,
log2_std_dev: -40, // warning u32
};
///////////////////////////////////////
// 80 bits of security - dimension 4 //
///////////////////////////////////////
/// 80 bits of security with a polynomial_size of 4 and a polynomial size of 256 (LWE estimator, September 15th 2020)
pub const RLWE80_256_4: RLWEParams = RLWEParams {
dimension: 4,
polynomial_size: 256,
log2_std_dev: -40, // warning u32
};
impl RLWEParams {
/// Instantiate a new RLWEParams with the provided dimension and standard deviation
/// # Arguments
/// * `polynomial_size` - the number of coefficients in a polynomial
/// * `dimension` - the size of an RLWE mask
/// * `std_dev` - the standard deviation used for the error normal distribution
/// # Output
/// * a new instantiation of an RLWEParams
/// * NotPowerOfTwoError if `polynomial_size` is not a power of 2
pub fn new(
polynomial_size: usize,
dimension: usize,
log2_std_dev: i32,
) -> Result<RLWEParams, CryptoAPIError> {
if (polynomial_size as f64 - f64::powi(2., (polynomial_size as f64).log2().round() as i32))
.abs()
> f64::EPSILON
{
return Err(NotPowerOfTwoError!(polynomial_size));
}
Ok(RLWEParams {
polynomial_size,
dimension,
log2_std_dev,
})
}
pub fn get_std_dev(&self) -> f64 {
f64::powi(2., self.log2_std_dev)
}
pub fn save(&self, path: &str) -> Result<(), Box<dyn Error>> {
write_to_file(path, self)
}
pub fn load(path: &str) -> Result<RLWEParams, Box<dyn Error>> {
read_from_file(path)
}
}
impl fmt::Display for RLWEParams {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut to_be_print: String = "".to_string();
to_be_print = to_be_print
+ &format!(
" RLWEParams {{\n -> dimension = {}\n -> std_dev = {}\n -> log2_std_dev = {}\n -> polynomial_size = {}\n",
self.dimension, self.get_std_dev(),self.log2_std_dev, self.polynomial_size
);
to_be_print += " }";
writeln!(f, "{}", to_be_print)
}
}