poly.rs 18 KB

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