server.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. use crate::arith;
  2. use crate::gadget::*;
  3. use crate::params::*;
  4. use crate::poly::*;
  5. pub fn coefficient_expansion(
  6. v: &mut Vec<PolyMatrixNTT>,
  7. g: usize,
  8. stopround: usize,
  9. params: &Params,
  10. v_w_left: &Vec<PolyMatrixNTT>,
  11. v_w_right: &Vec<PolyMatrixNTT>,
  12. v_neg1: &Vec<PolyMatrixNTT>,
  13. max_bits_to_gen_right: usize,
  14. ) {
  15. let poly_len = params.poly_len;
  16. let mut ct = PolyMatrixRaw::zero(params, 2, 1);
  17. let mut ct_auto = PolyMatrixRaw::zero(params, 2, 1);
  18. let mut ct_auto_1 = PolyMatrixRaw::zero(params, 1, 1);
  19. let mut ct_auto_1_ntt = PolyMatrixNTT::zero(params, 1, 1);
  20. let mut ginv_ct_left = PolyMatrixRaw::zero(params, params.t_exp_left, 1);
  21. let mut ginv_ct_left_ntt = PolyMatrixNTT::zero(params, params.t_exp_left, 1);
  22. let mut ginv_ct_right = PolyMatrixRaw::zero(params, params.t_exp_right, 1);
  23. let mut ginv_ct_right_ntt = PolyMatrixNTT::zero(params, params.t_exp_right, 1);
  24. let mut w_times_ginv_ct = PolyMatrixNTT::zero(params, 2, 1);
  25. for r in 0..g {
  26. let num_in = 1 << r;
  27. let num_out = 2 * num_in;
  28. let t = (poly_len / (1 << r)) + 1;
  29. let neg1 = &v_neg1[r];
  30. for i in 0..num_out {
  31. if stopround > 0 && i % 2 == 1 && r > stopround
  32. || (r == stopround && i / 2 >= max_bits_to_gen_right)
  33. {
  34. continue;
  35. }
  36. let (w, _gadget_dim, gi_ct, gi_ct_ntt) = match i % 2 {
  37. 0 => (&v_w_left[r], params.t_exp_left, &mut ginv_ct_left, &mut ginv_ct_left_ntt),
  38. 1 | _ => (&v_w_right[r], params.t_exp_right, &mut ginv_ct_right, &mut ginv_ct_right_ntt),
  39. };
  40. // let (w, gadget_dim) = match i % 2 {
  41. // 0 => (&v_w_left[r], params.t_exp_left),
  42. // 1 | _ => (&v_w_right[r], params.t_exp_right),
  43. // };
  44. if i < num_in {
  45. let (src, dest) = v.split_at_mut(num_in);
  46. scalar_multiply(&mut dest[i], neg1, &src[i]);
  47. }
  48. // let ct = from_ntt_alloc(&v[i]);
  49. // let ct_auto = automorph_alloc(&ct, t);
  50. // let ct_auto_0 = ct_auto.submatrix(0, 0, 1, 1);
  51. // let ct_auto_1_ntt = ct_auto.submatrix(1, 0, 1, 1).ntt();
  52. // let ginv_ct = gadget_invert_alloc(gadget_dim, &ct_auto_0);
  53. // let ginv_ct_ntt = ginv_ct.ntt();
  54. // let w_times_ginv_ct = w * &ginv_ct_ntt;
  55. from_ntt(&mut ct, &v[i]);
  56. automorph(&mut ct_auto, &ct, t);
  57. gadget_invert_rdim(gi_ct, &ct_auto, 1);
  58. to_ntt_no_reduce(gi_ct_ntt, &gi_ct);
  59. ct_auto_1.data.as_mut_slice().copy_from_slice(ct_auto.get_poly(1, 0));
  60. to_ntt(&mut ct_auto_1_ntt, &ct_auto_1);
  61. multiply(&mut w_times_ginv_ct, w, &gi_ct_ntt);
  62. let mut idx = 0;
  63. for j in 0..2 {
  64. for n in 0..params.crt_count {
  65. for z in 0..poly_len {
  66. let sum = v[i].data[idx]
  67. + w_times_ginv_ct.data[idx]
  68. + j * ct_auto_1_ntt.data[n * poly_len + z];
  69. v[i].data[idx] = arith::modular_reduce(params, sum, n);
  70. idx += 1;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. #[cfg(test)]
  78. mod test {
  79. use crate::{client::*, util::*};
  80. use super::*;
  81. fn get_params() -> Params {
  82. get_expansion_testing_params()
  83. }
  84. #[test]
  85. fn coefficient_expansion_is_correct() {
  86. let params = get_params();
  87. let v_neg1 = params.get_v_neg1();
  88. let mut seeded_rng = get_seeded_rng();
  89. let mut client = Client::init(&params, &mut seeded_rng);
  90. let public_params = client.generate_keys();
  91. let mut v = Vec::new();
  92. for _ in 0..params.poly_len {
  93. v.push(PolyMatrixNTT::zero(&params, 2, 1));
  94. }
  95. let scale_k = params.modulus / params.pt_modulus;
  96. let mut sigma = PolyMatrixRaw::zero(&params, 1, 1);
  97. sigma.data[7] = scale_k;
  98. v[0] = client.encrypt_matrix_reg(&sigma.ntt());
  99. let v_w_left = public_params.v_expansion_left.unwrap();
  100. let v_w_right = public_params.v_expansion_right.unwrap();
  101. coefficient_expansion(
  102. &mut v,
  103. client.g,
  104. client.stop_round,
  105. &params,
  106. &v_w_left,
  107. &v_w_right,
  108. &v_neg1,
  109. params.t_gsw * params.db_dim_2,
  110. );
  111. }
  112. }