poly.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. #[cfg(target_feature = "avx2")]
  2. use std::arch::x86_64::*;
  3. use std::ops::{Add, Mul, Neg};
  4. use std::cell::RefCell;
  5. use rand::Rng;
  6. use rand::distributions::Standard;
  7. use crate::{arith::*, params::*, ntt::*, util::*, discrete_gaussian::*};
  8. const SCRATCH_SPACE: usize = 8192;
  9. thread_local!(static SCRATCH: RefCell<Vec<u64>> = RefCell::new(vec![0u64; SCRATCH_SPACE]));
  10. pub trait PolyMatrix<'a> {
  11. fn is_ntt(&self) -> bool;
  12. fn get_rows(&self) -> usize;
  13. fn get_cols(&self) -> usize;
  14. fn get_params(&self) -> &Params;
  15. fn num_words(&self) -> usize;
  16. fn zero(params: &'a Params, rows: usize, cols: usize) -> Self;
  17. fn random(params: &'a Params, rows: usize, cols: usize) -> Self;
  18. fn as_slice(&self) -> &[u64];
  19. fn as_mut_slice(&mut self) -> &mut [u64];
  20. fn zero_out(&mut self) {
  21. for item in self.as_mut_slice() {
  22. *item = 0;
  23. }
  24. }
  25. fn get_poly(&self, row: usize, col: usize) -> &[u64] {
  26. let num_words = self.num_words();
  27. let start = (row * self.get_cols() + col) * num_words;
  28. &self.as_slice()[start..start + num_words]
  29. }
  30. fn get_poly_mut(&mut self, row: usize, col: usize) -> &mut [u64] {
  31. let num_words = self.num_words();
  32. let start = (row * self.get_cols() + col) * num_words;
  33. &mut self.as_mut_slice()[start..start + num_words]
  34. }
  35. fn copy_into(&mut self, p: &Self, target_row: usize, target_col: usize) {
  36. assert!(target_row < self.get_rows());
  37. assert!(target_col < self.get_cols());
  38. assert!(target_row + p.get_rows() <= self.get_rows());
  39. assert!(target_col + p.get_cols() <= self.get_cols());
  40. for r in 0..p.get_rows() {
  41. for c in 0..p.get_cols() {
  42. let pol_src = p.get_poly(r, c);
  43. let pol_dst = self.get_poly_mut(target_row + r, target_col + c);
  44. pol_dst.copy_from_slice(pol_src);
  45. }
  46. }
  47. }
  48. fn pad_top(&self, pad_rows: usize) -> Self;
  49. }
  50. pub struct PolyMatrixRaw<'a> {
  51. pub params: &'a Params,
  52. pub rows: usize,
  53. pub cols: usize,
  54. pub data: Vec<u64>,
  55. }
  56. pub struct PolyMatrixNTT<'a> {
  57. pub params: &'a Params,
  58. pub rows: usize,
  59. pub cols: usize,
  60. pub data: Vec<u64>,
  61. }
  62. impl<'a> PolyMatrix<'a> for PolyMatrixRaw<'a> {
  63. fn is_ntt(&self) -> bool {
  64. false
  65. }
  66. fn get_rows(&self) -> usize {
  67. self.rows
  68. }
  69. fn get_cols(&self) -> usize {
  70. self.cols
  71. }
  72. fn get_params(&self) -> &Params {
  73. &self.params
  74. }
  75. fn as_slice(&self) -> &[u64] {
  76. self.data.as_slice()
  77. }
  78. fn as_mut_slice(&mut self) -> &mut [u64] {
  79. self.data.as_mut_slice()
  80. }
  81. fn num_words(&self) -> usize {
  82. self.params.poly_len
  83. }
  84. fn zero(params: &'a Params, rows: usize, cols: usize) -> PolyMatrixRaw<'a> {
  85. let num_coeffs = rows * cols * params.poly_len;
  86. let data: Vec<u64> = vec![0; num_coeffs];
  87. PolyMatrixRaw {
  88. params,
  89. rows,
  90. cols,
  91. data,
  92. }
  93. }
  94. fn random(params: &'a Params, rows: usize, cols: usize) -> Self {
  95. let rng = rand::thread_rng();
  96. let mut iter = rng.sample_iter(&Standard);
  97. let mut out = PolyMatrixRaw::zero(params, rows, cols);
  98. for r in 0..rows {
  99. for c in 0..cols {
  100. for i in 0..params.poly_len {
  101. let val: u64 = iter.next().unwrap();
  102. out.get_poly_mut(r, c)[i] = val % params.modulus;
  103. }
  104. }
  105. }
  106. out
  107. }
  108. fn pad_top(&self, pad_rows: usize) -> Self {
  109. let mut padded = Self::zero(self.params, self.rows + pad_rows, self.cols);
  110. padded.copy_into(&self, pad_rows, 0);
  111. padded
  112. }
  113. }
  114. impl<'a> PolyMatrixRaw<'a> {
  115. pub fn identity(params: &'a Params, rows: usize, cols: usize) -> PolyMatrixRaw<'a> {
  116. let num_coeffs = rows * cols * params.poly_len;
  117. let mut data: Vec<u64> = vec![0; num_coeffs];
  118. for r in 0..rows {
  119. let c = r;
  120. let idx = r * cols * params.poly_len + c * params.poly_len;
  121. data[idx] = 1;
  122. }
  123. PolyMatrixRaw {
  124. params,
  125. rows,
  126. cols,
  127. data,
  128. }
  129. }
  130. pub fn noise(params: &'a Params, rows: usize, cols: usize, dg: &mut DiscreteGaussian) -> Self {
  131. let mut out = PolyMatrixRaw::zero(params, rows, cols);
  132. dg.sample_matrix(&mut out);
  133. out
  134. }
  135. pub fn ntt(&self) -> PolyMatrixNTT<'a> {
  136. to_ntt_alloc(&self)
  137. }
  138. pub fn to_vec(&self, modulus_bits: usize) -> Vec<u8> {
  139. let params = self.params;
  140. let sz_bits = self.rows * self.cols * params.poly_len * modulus_bits;
  141. let sz_bytes = f64::ceil((sz_bits as f64) / 8f64) as usize;
  142. let sz_bytes_roundup_16 = ((sz_bytes + 15) / 16) * 16;
  143. let mut data = vec![0u8; sz_bytes_roundup_16];
  144. let mut bit_offs = 0;
  145. for i in 0..self.rows * self.cols * params.poly_len {
  146. write_arbitrary_bits(data.as_mut_slice(), self.data[i], bit_offs, modulus_bits);
  147. bit_offs += modulus_bits;
  148. }
  149. data
  150. }
  151. }
  152. impl<'a> PolyMatrix<'a> for PolyMatrixNTT<'a> {
  153. fn is_ntt(&self) -> bool {
  154. true
  155. }
  156. fn get_rows(&self) -> usize {
  157. self.rows
  158. }
  159. fn get_cols(&self) -> usize {
  160. self.cols
  161. }
  162. fn get_params(&self) -> &Params {
  163. &self.params
  164. }
  165. fn as_slice(&self) -> &[u64] {
  166. self.data.as_slice()
  167. }
  168. fn as_mut_slice(&mut self) -> &mut [u64] {
  169. self.data.as_mut_slice()
  170. }
  171. fn num_words(&self) -> usize {
  172. self.params.poly_len * self.params.crt_count
  173. }
  174. fn zero(params: &'a Params, rows: usize, cols: usize) -> PolyMatrixNTT<'a> {
  175. let num_coeffs = rows * cols * params.poly_len * params.crt_count;
  176. let data: Vec<u64> = vec![0; num_coeffs];
  177. PolyMatrixNTT {
  178. params,
  179. rows,
  180. cols,
  181. data,
  182. }
  183. }
  184. fn random(params: &'a Params, rows: usize, cols: usize) -> Self {
  185. let rng = rand::thread_rng();
  186. let mut iter = rng.sample_iter(&Standard);
  187. let mut out = PolyMatrixNTT::zero(params, rows, cols);
  188. for r in 0..rows {
  189. for c in 0..cols {
  190. for i in 0..params.crt_count {
  191. for j in 0..params.poly_len {
  192. let idx = calc_index(&[i, j], &[params.crt_count, params.poly_len]);
  193. let val: u64 = iter.next().unwrap();
  194. out.get_poly_mut(r, c)[idx] = val % params.moduli[i];
  195. }
  196. }
  197. }
  198. }
  199. out
  200. }
  201. fn pad_top(&self, pad_rows: usize) -> Self {
  202. let mut padded = Self::zero(self.params, self.rows + pad_rows, self.cols);
  203. padded.copy_into(&self, pad_rows, 0);
  204. padded
  205. }
  206. }
  207. impl<'a> PolyMatrixNTT<'a> {
  208. pub fn raw(&self) -> PolyMatrixRaw<'a> {
  209. from_ntt_alloc(&self)
  210. }
  211. }
  212. pub fn multiply_poly(params: &Params, res: &mut [u64], a: &[u64], b: &[u64]) {
  213. for c in 0..params.crt_count {
  214. for i in 0..params.poly_len {
  215. let idx = c * params.poly_len + i;
  216. res[idx] = multiply_modular(params, a[idx], b[idx], c);
  217. }
  218. }
  219. }
  220. pub fn multiply_add_poly(params: &Params, res: &mut [u64], a: &[u64], b: &[u64]) {
  221. for c in 0..params.crt_count {
  222. for i in 0..params.poly_len {
  223. let idx = c * params.poly_len + i;
  224. res[idx] = multiply_add_modular(params, a[idx], b[idx], res[idx], c);
  225. }
  226. }
  227. }
  228. pub fn add_poly(params: &Params, res: &mut [u64], a: &[u64], b: &[u64]) {
  229. for c in 0..params.crt_count {
  230. for i in 0..params.poly_len {
  231. let idx = c * params.poly_len + i;
  232. res[idx] = add_modular(params, a[idx], b[idx], c);
  233. }
  234. }
  235. }
  236. pub fn invert_poly(params: &Params, res: &mut [u64], a: &[u64]) {
  237. for i in 0..params.poly_len {
  238. res[i] = params.modulus - a[i];
  239. }
  240. }
  241. pub fn automorph_poly(params: &Params, res: &mut [u64], a: &[u64], t: usize) {
  242. let poly_len = params.poly_len;
  243. for i in 0..poly_len {
  244. let num = (i * t) / poly_len;
  245. let rem = (i * t) % poly_len;
  246. if num % 2 == 0 {
  247. res[rem] = a[i];
  248. } else {
  249. res[rem] = params.modulus - a[i];
  250. }
  251. }
  252. }
  253. #[cfg(target_feature = "avx2")]
  254. pub fn multiply_add_poly_avx(params: &Params, res: &mut [u64], a: &[u64], b: &[u64]) {
  255. for c in 0..params.crt_count {
  256. for i in (0..params.poly_len).step_by(4) {
  257. unsafe {
  258. let p_x = &a[c*params.poly_len + i] as *const u64;
  259. let p_y = &b[c*params.poly_len + i] as *const u64;
  260. let p_z = &mut res[c*params.poly_len + i] as *mut u64;
  261. let x = _mm256_loadu_si256(p_x as *const __m256i);
  262. let y = _mm256_loadu_si256(p_y as *const __m256i);
  263. let z = _mm256_loadu_si256(p_z as *const __m256i);
  264. let product = _mm256_mul_epu32(x, y);
  265. let out = _mm256_add_epi64(z, product);
  266. _mm256_storeu_si256(p_z as *mut __m256i, out);
  267. }
  268. }
  269. }
  270. }
  271. pub fn modular_reduce(params: &Params, res: &mut [u64]) {
  272. for c in 0..params.crt_count {
  273. for i in 0..params.poly_len {
  274. res[c*params.poly_len + i] %= params.moduli[c];
  275. }
  276. }
  277. }
  278. #[cfg(not(target_feature = "avx2"))]
  279. pub fn multiply(res: &mut PolyMatrixNTT, a: &PolyMatrixNTT, b: &PolyMatrixNTT) {
  280. assert!(res.rows == a.rows);
  281. assert!(res.cols == b.cols);
  282. assert!(a.cols == b.rows);
  283. let params = res.params;
  284. for i in 0..a.rows {
  285. for j in 0..b.cols {
  286. for z in 0..params.poly_len*params.crt_count {
  287. res.get_poly_mut(i, j)[z] = 0;
  288. }
  289. for k in 0..a.cols {
  290. let params = res.params;
  291. let res_poly = res.get_poly_mut(i, j);
  292. let pol1 = a.get_poly(i, k);
  293. let pol2 = b.get_poly(k, j);
  294. multiply_add_poly(params, res_poly, pol1, pol2);
  295. }
  296. }
  297. }
  298. }
  299. #[cfg(target_feature = "avx2")]
  300. pub fn multiply(res: &mut PolyMatrixNTT, a: &PolyMatrixNTT, b: &PolyMatrixNTT) {
  301. assert!(res.rows == a.rows);
  302. assert!(res.cols == b.cols);
  303. assert!(a.cols == b.rows);
  304. let params = res.params;
  305. for i in 0..a.rows {
  306. for j in 0..b.cols {
  307. for z in 0..params.poly_len*params.crt_count {
  308. res.get_poly_mut(i, j)[z] = 0;
  309. }
  310. let res_poly = res.get_poly_mut(i, j);
  311. for k in 0..a.cols {
  312. let pol1 = a.get_poly(i, k);
  313. let pol2 = b.get_poly(k, j);
  314. multiply_add_poly_avx(params, res_poly, pol1, pol2);
  315. }
  316. modular_reduce(params, res_poly);
  317. }
  318. }
  319. }
  320. pub fn add(res: &mut PolyMatrixNTT, a: &PolyMatrixNTT, b: &PolyMatrixNTT) {
  321. assert!(res.rows == a.rows);
  322. assert!(res.cols == a.cols);
  323. assert!(a.rows == b.rows);
  324. assert!(a.cols == b.cols);
  325. let params = res.params;
  326. for i in 0..a.rows {
  327. for j in 0..a.cols {
  328. let res_poly = res.get_poly_mut(i, j);
  329. let pol1 = a.get_poly(i, j);
  330. let pol2 = b.get_poly(i, j);
  331. add_poly(params, res_poly, pol1, pol2);
  332. }
  333. }
  334. }
  335. pub fn invert(res: &mut PolyMatrixRaw, a: &PolyMatrixRaw) {
  336. assert!(res.rows == a.rows);
  337. assert!(res.cols == a.cols);
  338. let params = res.params;
  339. for i in 0..a.rows {
  340. for j in 0..a.cols {
  341. let res_poly = res.get_poly_mut(i, j);
  342. let pol1 = a.get_poly(i, j);
  343. invert_poly(params, res_poly, pol1);
  344. }
  345. }
  346. }
  347. pub fn automorph<'a>(res: &mut PolyMatrixRaw<'a>, a: &PolyMatrixRaw<'a>, t: usize) {
  348. assert!(res.rows == a.rows);
  349. assert!(res.cols == a.cols);
  350. let params = res.params;
  351. for i in 0..a.rows {
  352. for j in 0..a.cols {
  353. let res_poly = res.get_poly_mut(i, j);
  354. let pol1 = a.get_poly(i, j);
  355. automorph_poly(params, res_poly, pol1, t);
  356. }
  357. }
  358. }
  359. pub fn automorph_alloc<'a>(a: &PolyMatrixRaw<'a>, t: usize) -> PolyMatrixRaw<'a> {
  360. let mut res = PolyMatrixRaw::zero(a.params, a.rows, a.cols);
  361. automorph(&mut res, a, t);
  362. res
  363. }
  364. pub fn stack<'a>(a: &PolyMatrixRaw<'a>, b: &PolyMatrixRaw<'a>) -> PolyMatrixRaw<'a> {
  365. assert_eq!(a.cols, b.cols);
  366. let mut c = PolyMatrixRaw::zero(a.params, a.rows + b.rows, a.cols);
  367. c.copy_into(a, 0, 0);
  368. c.copy_into(b, a.rows, 0);
  369. c
  370. }
  371. pub fn scalar_multiply(res: &mut PolyMatrixNTT, a: &PolyMatrixNTT, b: &PolyMatrixNTT) {
  372. assert_eq!(a.rows, 1);
  373. assert_eq!(a.cols, 1);
  374. let params = res.params;
  375. let pol2 = a.get_poly(0, 0);
  376. for i in 0..b.rows {
  377. for j in 0..b.cols {
  378. let res_poly = res.get_poly_mut(i, j);
  379. let pol1 = b.get_poly(i, j);
  380. multiply_poly(params, res_poly, pol1, pol2);
  381. }
  382. }
  383. }
  384. pub fn scalar_multiply_alloc<'a>(a: &PolyMatrixNTT<'a>, b: &PolyMatrixNTT<'a>) -> PolyMatrixNTT<'a> {
  385. let mut res = PolyMatrixNTT::zero(b.params, b.rows, b.cols);
  386. scalar_multiply(&mut res, a, b);
  387. res
  388. }
  389. pub fn single_poly<'a>(params: &'a Params, val: u64) -> PolyMatrixRaw<'a> {
  390. let mut res = PolyMatrixRaw::zero(params, 1, 1);
  391. res.get_poly_mut(0, 0)[0] = val;
  392. res
  393. }
  394. pub fn to_ntt(a: &mut PolyMatrixNTT, b: &PolyMatrixRaw) {
  395. let params = a.params;
  396. for r in 0..a.rows {
  397. for c in 0..a.cols {
  398. let pol_src = b.get_poly(r, c);
  399. let pol_dst = a.get_poly_mut(r, c);
  400. for n in 0..params.crt_count {
  401. for z in 0..params.poly_len {
  402. pol_dst[n * params.poly_len + z] = pol_src[z] % params.moduli[n];
  403. }
  404. }
  405. ntt_forward(params, pol_dst);
  406. }
  407. }
  408. }
  409. pub fn to_ntt_alloc<'a>(b: &PolyMatrixRaw<'a>) -> PolyMatrixNTT<'a> {
  410. let mut a = PolyMatrixNTT::zero(b.params, b.rows, b.cols);
  411. to_ntt(&mut a, b);
  412. a
  413. }
  414. pub fn from_ntt(a: &mut PolyMatrixRaw, b: &PolyMatrixNTT) {
  415. let params = a.params;
  416. SCRATCH.with(|scratch_cell| {
  417. let scratch_vec = &mut *scratch_cell.borrow_mut();
  418. let scratch = scratch_vec.as_mut_slice();
  419. for r in 0..a.rows {
  420. for c in 0..a.cols {
  421. let pol_src = b.get_poly(r, c);
  422. let pol_dst = a.get_poly_mut(r, c);
  423. scratch[0..pol_src.len()].copy_from_slice(pol_src);
  424. ntt_inverse(params, scratch);
  425. for z in 0..params.poly_len {
  426. pol_dst[z] = params.crt_compose(scratch, z);
  427. }
  428. }
  429. }
  430. });
  431. }
  432. pub fn from_ntt_alloc<'a>(b: &PolyMatrixNTT<'a>) -> PolyMatrixRaw<'a> {
  433. let mut a = PolyMatrixRaw::zero(b.params, b.rows, b.cols);
  434. from_ntt(&mut a, b);
  435. a
  436. }
  437. impl<'a, 'b> Neg for &'b PolyMatrixRaw<'a> {
  438. type Output = PolyMatrixRaw<'a>;
  439. fn neg(self) -> Self::Output {
  440. let mut out = PolyMatrixRaw::zero(self.params, self.rows, self.cols);
  441. invert(&mut out, self);
  442. out
  443. }
  444. }
  445. impl<'a, 'b> Mul for &'b PolyMatrixNTT<'a> {
  446. type Output = PolyMatrixNTT<'a>;
  447. fn mul(self, rhs: Self) -> Self::Output {
  448. let mut out = PolyMatrixNTT::zero(self.params, self.rows, rhs.cols);
  449. multiply(&mut out, self, rhs);
  450. out
  451. }
  452. }
  453. impl<'a, 'b> Add for &'b PolyMatrixNTT<'a> {
  454. type Output = PolyMatrixNTT<'a>;
  455. fn add(self, rhs: Self) -> Self::Output {
  456. let mut out = PolyMatrixNTT::zero(self.params, self.rows, self.cols);
  457. add(&mut out, self, rhs);
  458. out
  459. }
  460. }
  461. #[cfg(test)]
  462. mod test {
  463. use super::*;
  464. fn get_params() -> Params {
  465. get_test_params()
  466. }
  467. fn assert_all_zero(a: &[u64]) {
  468. for i in a {
  469. assert_eq!(*i, 0);
  470. }
  471. }
  472. #[test]
  473. fn sets_all_zeros() {
  474. let params = get_params();
  475. let m1 = PolyMatrixNTT::zero(&params, 2, 1);
  476. assert_all_zero(m1.as_slice());
  477. }
  478. #[test]
  479. fn multiply_correctness() {
  480. let params = get_params();
  481. let m1 = PolyMatrixNTT::zero(&params, 2, 1);
  482. let m2 = PolyMatrixNTT::zero(&params, 3, 2);
  483. let m3 = &m2 * &m1;
  484. assert_all_zero(m3.as_slice());
  485. }
  486. #[test]
  487. fn full_multiply_correctness() {
  488. let params = get_params();
  489. let mut m1 = PolyMatrixRaw::zero(&params, 1, 1);
  490. let mut m2 = PolyMatrixRaw::zero(&params, 1, 1);
  491. m1.get_poly_mut(0, 0)[1] = 100;
  492. m2.get_poly_mut(0, 0)[1] = 7;
  493. let m1_ntt = to_ntt_alloc(&m1);
  494. let m2_ntt = to_ntt_alloc(&m2);
  495. let m3_ntt = &m1_ntt * &m2_ntt;
  496. let m3 = from_ntt_alloc(&m3_ntt);
  497. assert_eq!(m3.get_poly(0, 0)[2], 700);
  498. }
  499. }