poly.rs 20 KB

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