poly.rs 18 KB

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