params.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. use crate::{arith::*, ntt::*, number_theory::*};
  2. pub static Q2_VALUES: [u64; 37] = [
  3. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12289, 12289, 61441, 65537, 65537, 520193, 786433, 786433, 3604481, 7340033, 16515073, 33292289, 67043329, 132120577, 268369921, 469762049, 1073479681, 2013265921, 4293918721, 8588886017, 17175674881, 34359214081, 68718428161
  4. ];
  5. #[derive(Debug)]
  6. #[derive(PartialEq)]
  7. pub struct Params {
  8. pub poly_len: usize,
  9. pub poly_len_log2: usize,
  10. pub ntt_tables: Vec<Vec<Vec<u64>>>,
  11. pub scratch: Vec<u64>,
  12. pub crt_count: usize,
  13. pub moduli: Vec<u64>,
  14. pub modulus: u64,
  15. pub modulus_log2: u64,
  16. pub noise_width: f64,
  17. pub n: usize,
  18. pub pt_modulus: u64,
  19. pub q2_bits: u64,
  20. pub t_conv: usize,
  21. pub t_exp_left: usize,
  22. pub t_exp_right: usize,
  23. pub t_gsw: usize,
  24. pub expand_queries: bool,
  25. pub db_dim_1: usize,
  26. pub db_dim_2: usize,
  27. }
  28. impl Params {
  29. pub fn get_ntt_forward_table(&self, i: usize) -> &[u64] {
  30. self.ntt_tables[i][0].as_slice()
  31. }
  32. pub fn get_ntt_forward_prime_table(&self, i: usize) -> &[u64] {
  33. self.ntt_tables[i][1].as_slice()
  34. }
  35. pub fn get_ntt_inverse_table(&self, i: usize) -> &[u64] {
  36. self.ntt_tables[i][2].as_slice()
  37. }
  38. pub fn get_ntt_inverse_prime_table(&self, i: usize) -> &[u64] {
  39. self.ntt_tables[i][3].as_slice()
  40. }
  41. pub fn get_sk_gsw(&self) -> (usize, usize) {
  42. (self.n, 1)
  43. }
  44. pub fn get_sk_reg(&self) -> (usize, usize) {
  45. (1, 1)
  46. }
  47. pub fn m_conv(&self) -> usize {
  48. self.t_conv
  49. }
  50. pub fn crt_compose_1(&self, x: u64) -> u64 {
  51. assert_eq!(self.crt_count, 1);
  52. x
  53. }
  54. pub fn crt_compose_2(&self, x: u64, y: u64) -> u64 {
  55. assert_eq!(self.crt_count, 2);
  56. let a = self.moduli[0];
  57. let b = self.moduli[1];
  58. let a_inv_mod_b = invert_uint_mod(a, b).unwrap();
  59. let b_inv_mod_a = invert_uint_mod(b, a).unwrap();
  60. let mut val = (x as u128) * (b_inv_mod_a as u128) * (b as u128);
  61. val += (y as u128) * (a_inv_mod_b as u128) * (a as u128);
  62. (val % (self.modulus as u128)) as u64 // FIXME: use barrett
  63. }
  64. pub fn crt_compose(&self, a: &[u64], idx: usize) -> u64 {
  65. if self.crt_count == 1 {
  66. self.crt_compose_1(a[idx])
  67. } else {
  68. self.crt_compose_2(a[idx], a[idx + self.poly_len])
  69. }
  70. }
  71. pub fn init(
  72. poly_len: usize,
  73. moduli: &Vec<u64>,
  74. noise_width: f64,
  75. n: usize,
  76. pt_modulus: u64,
  77. q2_bits: u64,
  78. t_conv: usize,
  79. t_exp_left: usize,
  80. t_exp_right: usize,
  81. t_gsw: usize,
  82. expand_queries: bool,
  83. db_dim_1: usize,
  84. db_dim_2: usize,
  85. ) -> Self {
  86. let poly_len_log2 = log2(poly_len as u64) as usize;
  87. let crt_count = moduli.len();
  88. let ntt_tables = build_ntt_tables(poly_len, moduli.as_slice());
  89. let scratch = vec![0u64; crt_count * poly_len];
  90. let mut modulus = 1;
  91. for m in moduli {
  92. modulus *= m;
  93. }
  94. let modulus_log2 = log2_ceil(modulus as usize) as u64;
  95. Self {
  96. poly_len,
  97. poly_len_log2,
  98. ntt_tables,
  99. scratch,
  100. crt_count,
  101. moduli: moduli.clone(),
  102. modulus,
  103. modulus_log2,
  104. noise_width,
  105. n,
  106. pt_modulus,
  107. q2_bits,
  108. t_conv,
  109. t_exp_left,
  110. t_exp_right,
  111. t_gsw,
  112. expand_queries,
  113. db_dim_1,
  114. db_dim_2,
  115. }
  116. }
  117. }