params.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. use std::mem::size_of;
  2. use crate::{arith::*, ntt::*, number_theory::*, poly::*};
  3. pub const MAX_MODULI: usize = 4;
  4. pub static MIN_Q2_BITS: u64 = 14;
  5. pub static Q2_VALUES: [u64; 37] = [
  6. 0,
  7. 0,
  8. 0,
  9. 0,
  10. 0,
  11. 0,
  12. 0,
  13. 0,
  14. 0,
  15. 0,
  16. 0,
  17. 0,
  18. 0,
  19. 0,
  20. 12289,
  21. 12289,
  22. 61441,
  23. 65537,
  24. 65537,
  25. 520193,
  26. 786433,
  27. 786433,
  28. 3604481,
  29. 7340033,
  30. 16515073,
  31. 33292289,
  32. 67043329,
  33. 132120577,
  34. 268369921,
  35. 469762049,
  36. 1073479681,
  37. 2013265921,
  38. 4293918721,
  39. 8588886017,
  40. 17175674881,
  41. 34359214081,
  42. 68718428161,
  43. ];
  44. #[derive(Debug, PartialEq, Clone)]
  45. pub struct Params {
  46. pub poly_len: usize,
  47. pub poly_len_log2: usize,
  48. pub ntt_tables: Vec<Vec<Vec<u64>>>,
  49. pub scratch: Vec<u64>,
  50. pub crt_count: usize,
  51. pub barrett_cr_0: [u64; MAX_MODULI],
  52. pub barrett_cr_1: [u64; MAX_MODULI],
  53. pub barrett_cr_0_modulus: u64,
  54. pub barrett_cr_1_modulus: u64,
  55. pub mod0_inv_mod1: u64,
  56. pub mod1_inv_mod0: u64,
  57. pub moduli: [u64; MAX_MODULI],
  58. pub modulus: u64,
  59. pub modulus_log2: u64,
  60. pub noise_width: f64,
  61. pub n: usize,
  62. pub pt_modulus: u64,
  63. pub q2_bits: u64,
  64. pub t_conv: usize,
  65. pub t_exp_left: usize,
  66. pub t_exp_right: usize,
  67. pub t_gsw: usize,
  68. pub expand_queries: bool,
  69. pub db_dim_1: usize,
  70. pub db_dim_2: usize,
  71. pub instances: usize,
  72. pub db_item_size: usize,
  73. }
  74. impl Params {
  75. pub fn get_ntt_forward_table(&self, i: usize) -> &[u64] {
  76. self.ntt_tables[i][0].as_slice()
  77. }
  78. pub fn get_ntt_forward_prime_table(&self, i: usize) -> &[u64] {
  79. self.ntt_tables[i][1].as_slice()
  80. }
  81. pub fn get_ntt_inverse_table(&self, i: usize) -> &[u64] {
  82. self.ntt_tables[i][2].as_slice()
  83. }
  84. pub fn get_ntt_inverse_prime_table(&self, i: usize) -> &[u64] {
  85. self.ntt_tables[i][3].as_slice()
  86. }
  87. pub fn get_v_neg1(&self) -> Vec<PolyMatrixNTT> {
  88. let mut v_neg1 = Vec::new();
  89. for i in 0..self.poly_len_log2 {
  90. let idx = self.poly_len - (1 << i);
  91. let mut ng1 = PolyMatrixRaw::zero(&self, 1, 1);
  92. ng1.data[idx] = 1;
  93. v_neg1.push((-&ng1).ntt());
  94. }
  95. v_neg1
  96. }
  97. pub fn get_sk_gsw(&self) -> (usize, usize) {
  98. (self.n, 1)
  99. }
  100. pub fn get_sk_reg(&self) -> (usize, usize) {
  101. (1, 1)
  102. }
  103. pub fn num_expanded(&self) -> usize {
  104. 1 << self.db_dim_1
  105. }
  106. pub fn num_items(&self) -> usize {
  107. (1 << self.db_dim_1) * (1 << self.db_dim_2)
  108. }
  109. pub fn g(&self) -> usize {
  110. let num_bits_to_gen = self.t_gsw * self.db_dim_2 + self.num_expanded();
  111. log2_ceil_usize(num_bits_to_gen)
  112. }
  113. pub fn stop_round(&self) -> usize {
  114. log2_ceil_usize(self.t_gsw * self.db_dim_2)
  115. }
  116. pub fn factor_on_first_dim(&self) -> usize {
  117. if self.db_dim_2 == 0 { 1 } else { 2 }
  118. }
  119. pub fn setup_bytes(&self) -> usize {
  120. let mut sz_polys = 0;
  121. let packing_sz = (self.n + 1) * self.t_conv;
  122. sz_polys += self.n * packing_sz;
  123. if self.expand_queries {
  124. let expansion_left_sz = self.g() * 2 * self.t_exp_left;
  125. let expansion_right_sz = (self.stop_round() + 1) * 2 * self.t_exp_right;
  126. let conversion_sz = 2 * (2 * self.t_conv);
  127. sz_polys += expansion_left_sz + expansion_right_sz + conversion_sz;
  128. }
  129. let sz_bytes = sz_polys * self.poly_len * size_of::<u64>();
  130. sz_bytes
  131. }
  132. pub fn query_bytes(&self) -> usize {
  133. let sz_polys;
  134. if self.expand_queries {
  135. sz_polys = 2;
  136. } else {
  137. let first_dimension_sz = self.num_expanded() * 2;
  138. let further_dimension_sz = self.db_dim_2 * 2 * (2 * self.t_gsw);
  139. sz_polys = first_dimension_sz + further_dimension_sz;
  140. }
  141. let sz_bytes = sz_polys * self.poly_len * size_of::<u64>();
  142. sz_bytes
  143. }
  144. pub fn query_v_buf_bytes(&self) -> usize {
  145. self.num_expanded() * 2 * self.poly_len * size_of::<u64>()
  146. }
  147. pub fn bytes_per_chunk(&self) -> usize {
  148. let trials = self.n * self.n;
  149. let chunks = self.instances * trials;
  150. let bytes_per_chunk = f64::ceil(self.db_item_size as f64 / chunks as f64) as usize;
  151. bytes_per_chunk
  152. }
  153. pub fn modp_words_per_chunk(&self) -> usize {
  154. let bytes_per_chunk = self.bytes_per_chunk();
  155. let logp = log2(self.pt_modulus);
  156. let modp_words_per_chunk = f64::ceil((bytes_per_chunk * 8) as f64 / logp as f64) as usize;
  157. modp_words_per_chunk
  158. }
  159. pub fn crt_compose_1(&self, x: u64) -> u64 {
  160. assert_eq!(self.crt_count, 1);
  161. x
  162. }
  163. pub fn crt_compose_2(&self, x: u64, y: u64) -> u64 {
  164. assert_eq!(self.crt_count, 2);
  165. let mut val = (x as u128) * (self.mod1_inv_mod0 as u128);
  166. val += (y as u128) * (self.mod0_inv_mod1 as u128);
  167. barrett_reduction_u128(self, val)
  168. }
  169. pub fn crt_compose(&self, a: &[u64], idx: usize) -> u64 {
  170. if self.crt_count == 1 {
  171. self.crt_compose_1(a[idx])
  172. } else {
  173. self.crt_compose_2(a[idx], a[idx + self.poly_len])
  174. }
  175. }
  176. pub fn init(
  177. poly_len: usize,
  178. moduli: &[u64],
  179. noise_width: f64,
  180. n: usize,
  181. pt_modulus: u64,
  182. q2_bits: u64,
  183. t_conv: usize,
  184. t_exp_left: usize,
  185. t_exp_right: usize,
  186. t_gsw: usize,
  187. expand_queries: bool,
  188. db_dim_1: usize,
  189. db_dim_2: usize,
  190. instances: usize,
  191. db_item_size: usize,
  192. ) -> Self {
  193. assert!(q2_bits >= MIN_Q2_BITS);
  194. let poly_len_log2 = log2(poly_len as u64) as usize;
  195. let crt_count = moduli.len();
  196. assert!(crt_count <= MAX_MODULI);
  197. let mut moduli_array = [0; MAX_MODULI];
  198. for i in 0..crt_count {
  199. moduli_array[i] = moduli[i];
  200. }
  201. let ntt_tables = build_ntt_tables(poly_len, moduli);
  202. let scratch = vec![0u64; crt_count * poly_len];
  203. let mut modulus = 1;
  204. for m in moduli {
  205. modulus *= m;
  206. }
  207. let modulus_log2 = log2_ceil(modulus);
  208. let (barrett_cr_0, barrett_cr_1) = get_barrett(moduli);
  209. let (barrett_cr_0_modulus, barrett_cr_1_modulus) = get_barrett_crs(modulus);
  210. let mut mod0_inv_mod1 = 0;
  211. let mut mod1_inv_mod0 = 0;
  212. if crt_count == 2 {
  213. mod0_inv_mod1 = moduli[0] * invert_uint_mod(moduli[0], moduli[1]).unwrap();
  214. mod1_inv_mod0 = moduli[1] * invert_uint_mod(moduli[1], moduli[0]).unwrap();
  215. }
  216. Self {
  217. poly_len,
  218. poly_len_log2,
  219. ntt_tables,
  220. scratch,
  221. crt_count,
  222. barrett_cr_0,
  223. barrett_cr_1,
  224. barrett_cr_0_modulus,
  225. barrett_cr_1_modulus,
  226. mod0_inv_mod1,
  227. mod1_inv_mod0,
  228. moduli: moduli_array,
  229. modulus,
  230. modulus_log2,
  231. noise_width,
  232. n,
  233. pt_modulus,
  234. q2_bits,
  235. t_conv,
  236. t_exp_left,
  237. t_exp_right,
  238. t_gsw,
  239. expand_queries,
  240. db_dim_1,
  241. db_dim_2,
  242. instances,
  243. db_item_size,
  244. }
  245. }
  246. }