params.rs 6.9 KB

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