params.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 crt_compose_1(&self, x: u64) -> u64 {
  141. assert_eq!(self.crt_count, 1);
  142. x
  143. }
  144. pub fn crt_compose_2(&self, x: u64, y: u64) -> u64 {
  145. assert_eq!(self.crt_count, 2);
  146. let mut val = (x as u128) * (self.mod1_inv_mod0 as u128);
  147. val += (y as u128) * (self.mod0_inv_mod1 as u128);
  148. barrett_reduction_u128(self, val)
  149. }
  150. pub fn crt_compose(&self, a: &[u64], idx: usize) -> u64 {
  151. if self.crt_count == 1 {
  152. self.crt_compose_1(a[idx])
  153. } else {
  154. self.crt_compose_2(a[idx], a[idx + self.poly_len])
  155. }
  156. }
  157. pub fn init(
  158. poly_len: usize,
  159. moduli: &[u64],
  160. noise_width: f64,
  161. n: usize,
  162. pt_modulus: u64,
  163. q2_bits: u64,
  164. t_conv: usize,
  165. t_exp_left: usize,
  166. t_exp_right: usize,
  167. t_gsw: usize,
  168. expand_queries: bool,
  169. db_dim_1: usize,
  170. db_dim_2: usize,
  171. instances: usize,
  172. db_item_size: usize,
  173. ) -> Self {
  174. let poly_len_log2 = log2(poly_len as u64) as usize;
  175. let crt_count = moduli.len();
  176. assert!(crt_count <= MAX_MODULI);
  177. let mut moduli_array = [0; MAX_MODULI];
  178. for i in 0..crt_count {
  179. moduli_array[i] = moduli[i];
  180. }
  181. let ntt_tables = build_ntt_tables(poly_len, moduli);
  182. let scratch = vec![0u64; crt_count * poly_len];
  183. let mut modulus = 1;
  184. for m in moduli {
  185. modulus *= m;
  186. }
  187. let modulus_log2 = log2_ceil(modulus);
  188. let (barrett_cr_0, barrett_cr_1) = get_barrett(moduli);
  189. let (barrett_cr_0_modulus, barrett_cr_1_modulus) = get_barrett_crs(modulus);
  190. let mut mod0_inv_mod1 = 0;
  191. let mut mod1_inv_mod0 = 0;
  192. if crt_count == 2 {
  193. mod0_inv_mod1 = moduli[0] * invert_uint_mod(moduli[0], moduli[1]).unwrap();
  194. mod1_inv_mod0 = moduli[1] * invert_uint_mod(moduli[1], moduli[0]).unwrap();
  195. }
  196. Self {
  197. poly_len,
  198. poly_len_log2,
  199. ntt_tables,
  200. scratch,
  201. crt_count,
  202. barrett_cr_0,
  203. barrett_cr_1,
  204. barrett_cr_0_modulus,
  205. barrett_cr_1_modulus,
  206. mod0_inv_mod1,
  207. mod1_inv_mod0,
  208. moduli: moduli_array,
  209. modulus,
  210. modulus_log2,
  211. noise_width,
  212. n,
  213. pt_modulus,
  214. q2_bits,
  215. t_conv,
  216. t_exp_left,
  217. t_exp_right,
  218. t_gsw,
  219. expand_queries,
  220. db_dim_1,
  221. db_dim_2,
  222. instances,
  223. db_item_size,
  224. }
  225. }
  226. }