arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/math/src/lib.rs
math/src/lib.rsRaw
  1#![feature(bigint_helper_methods)]
  2#![feature(test)]
  3
  4pub mod dft;
  5pub mod modulus;
  6pub mod num_bigint;
  7pub mod poly;
  8pub mod ring;
  9pub mod scalar;
 10
 11pub const CHUNK: usize = 8;
 12pub const GALOISGENERATOR: usize = 5;
 13
 14pub mod macros {
 15
 16    #[macro_export]
 17    macro_rules! apply_v {
 18        ($self:expr, $f:expr, $a:expr, $CHUNK:expr) => {
 19            match CHUNK {
 20                8 => {
 21                    $a.chunks_exact_mut(8).for_each(|a| {
 22                        $f(&$self, &mut a[0]);
 23                        $f(&$self, &mut a[1]);
 24                        $f(&$self, &mut a[2]);
 25                        $f(&$self, &mut a[3]);
 26                        $f(&$self, &mut a[4]);
 27                        $f(&$self, &mut a[5]);
 28                        $f(&$self, &mut a[6]);
 29                        $f(&$self, &mut a[7]);
 30                    });
 31
 32                    let n: usize = $a.len();
 33                    let m = n - (n & (CHUNK - 1));
 34                    $a[m..].iter_mut().for_each(|a| {
 35                        $f(&$self, a);
 36                    });
 37                }
 38                _ => {
 39                    $a.iter_mut().for_each(|a| {
 40                        $f(&$self, a);
 41                    });
 42                }
 43            }
 44        };
 45    }
 46
 47    #[macro_export]
 48    macro_rules! apply_vv {
 49        ($self:expr, $f:expr, $a:expr, $b:expr, $CHUNK:expr) => {
 50            let n: usize = $a.len();
 51            debug_assert!(
 52                $b.len() == n,
 53                "invalid argument b: b.len() = {} != a.len() = {}",
 54                $b.len(),
 55                n
 56            );
 57            debug_assert!(
 58                CHUNK & (CHUNK - 1) == 0,
 59                "invalid CHUNK const: not a power of two"
 60            );
 61
 62            match CHUNK {
 63                8 => {
 64                    izip!($a.chunks_exact(8), $b.chunks_exact_mut(8)).for_each(|(a, b)| {
 65                        $f(&$self, &a[0], &mut b[0]);
 66                        $f(&$self, &a[1], &mut b[1]);
 67                        $f(&$self, &a[2], &mut b[2]);
 68                        $f(&$self, &a[3], &mut b[3]);
 69                        $f(&$self, &a[4], &mut b[4]);
 70                        $f(&$self, &a[5], &mut b[5]);
 71                        $f(&$self, &a[6], &mut b[6]);
 72                        $f(&$self, &a[7], &mut b[7]);
 73                    });
 74
 75                    let m = n - (n & (CHUNK - 1));
 76                    izip!($a[m..].iter(), $b[m..].iter_mut()).for_each(|(a, b)| {
 77                        $f(&$self, a, b);
 78                    });
 79                }
 80                _ => {
 81                    izip!($a.iter(), $b.iter_mut()).for_each(|(a, b)| {
 82                        $f(&$self, a, b);
 83                    });
 84                }
 85            }
 86        };
 87    }
 88
 89    #[macro_export]
 90    macro_rules! apply_vvv {
 91        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $CHUNK:expr) => {
 92            let n: usize = $a.len();
 93            debug_assert!(
 94                $b.len() == n,
 95                "invalid argument b: b.len() = {} != a.len() = {}",
 96                $b.len(),
 97                n
 98            );
 99            debug_assert!(
100                $c.len() == n,
101                "invalid argument c: b.len() = {} != a.len() = {}",
102                $c.len(),
103                n
104            );
105            debug_assert!(
106                CHUNK & (CHUNK - 1) == 0,
107                "invalid CHUNK const: not a power of two"
108            );
109
110            match CHUNK {
111                8 => {
112                    izip!(
113                        $a.chunks_exact(8),
114                        $b.chunks_exact(8),
115                        $c.chunks_exact_mut(8)
116                    )
117                    .for_each(|(a, b, c)| {
118                        $f(&$self, &a[0], &b[0], &mut c[0]);
119                        $f(&$self, &a[1], &b[1], &mut c[1]);
120                        $f(&$self, &a[2], &b[2], &mut c[2]);
121                        $f(&$self, &a[3], &b[3], &mut c[3]);
122                        $f(&$self, &a[4], &b[4], &mut c[4]);
123                        $f(&$self, &a[5], &b[5], &mut c[5]);
124                        $f(&$self, &a[6], &b[6], &mut c[6]);
125                        $f(&$self, &a[7], &b[7], &mut c[7]);
126                    });
127
128                    let m = n - (n & 7);
129                    izip!($a[m..].iter(), $b[m..].iter(), $c[m..].iter_mut()).for_each(
130                        |(a, b, c)| {
131                            $f(&$self, a, b, c);
132                        },
133                    );
134                }
135                _ => {
136                    izip!($a.iter(), $b.iter(), $c.iter_mut()).for_each(|(a, b, c)| {
137                        $f(&$self, a, b, c);
138                    });
139                }
140            }
141        };
142    }
143
144    #[macro_export]
145    macro_rules! apply_sv {
146        ($self:expr, $f:expr, $a:expr, $b:expr, $CHUNK:expr) => {
147            let n: usize = $b.len();
148
149            debug_assert!(
150                CHUNK & (CHUNK - 1) == 0,
151                "invalid CHUNK const: not a power of two"
152            );
153
154            match CHUNK {
155                8 => {
156                    izip!($b.chunks_exact_mut(8)).for_each(|b| {
157                        $f(&$self, $a, &mut b[0]);
158                        $f(&$self, $a, &mut b[1]);
159                        $f(&$self, $a, &mut b[2]);
160                        $f(&$self, $a, &mut b[3]);
161                        $f(&$self, $a, &mut b[4]);
162                        $f(&$self, $a, &mut b[5]);
163                        $f(&$self, $a, &mut b[6]);
164                        $f(&$self, $a, &mut b[7]);
165                    });
166
167                    let m = n - (n & 7);
168                    izip!($b[m..].iter_mut()).for_each(|b| {
169                        $f(&$self, $a, b);
170                    });
171                }
172                _ => {
173                    izip!($b.iter_mut()).for_each(|b| {
174                        $f(&$self, $a, b);
175                    });
176                }
177            }
178        };
179    }
180
181    #[macro_export]
182    macro_rules! apply_svv {
183        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $CHUNK:expr) => {
184            let n: usize = $b.len();
185            debug_assert!(
186                $c.len() == n,
187                "invalid argument c: c.len() = {} != b.len() = {}",
188                $c.len(),
189                n
190            );
191            debug_assert!(
192                CHUNK & (CHUNK - 1) == 0,
193                "invalid CHUNK const: not a power of two"
194            );
195
196            match CHUNK {
197                8 => {
198                    izip!($b.chunks_exact(8), $c.chunks_exact_mut(8)).for_each(|(b, c)| {
199                        $f(&$self, $a, &b[0], &mut c[0]);
200                        $f(&$self, $a, &b[1], &mut c[1]);
201                        $f(&$self, $a, &b[2], &mut c[2]);
202                        $f(&$self, $a, &b[3], &mut c[3]);
203                        $f(&$self, $a, &b[4], &mut c[4]);
204                        $f(&$self, $a, &b[5], &mut c[5]);
205                        $f(&$self, $a, &b[6], &mut c[6]);
206                        $f(&$self, $a, &b[7], &mut c[7]);
207                    });
208
209                    let m = n - (n & 7);
210                    izip!($b[m..].iter(), $c[m..].iter_mut()).for_each(|(b, c)| {
211                        $f(&$self, $a, b, c);
212                    });
213                }
214                _ => {
215                    izip!($b.iter(), $c.iter_mut()).for_each(|(b, c)| {
216                        $f(&$self, $a, b, c);
217                    });
218                }
219            }
220        };
221    }
222
223    #[macro_export]
224    macro_rules! apply_vvsv {
225        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $d:expr, $CHUNK:expr) => {
226            let n: usize = $a.len();
227            debug_assert!(
228                $b.len() == n,
229                "invalid argument b: b.len() = {} != a.len() = {}",
230                $b.len(),
231                n
232            );
233            debug_assert!(
234                $d.len() == n,
235                "invalid argument d: d.len() = {} != a.len() = {}",
236                $d.len(),
237                n
238            );
239            debug_assert!(
240                CHUNK & (CHUNK - 1) == 0,
241                "invalid CHUNK const: not a power of two"
242            );
243
244            match CHUNK {
245                8 => {
246                    izip!(
247                        $a.chunks_exact(8),
248                        $b.chunks_exact(8),
249                        $d.chunks_exact_mut(8)
250                    )
251                    .for_each(|(a, b, d)| {
252                        $f(&$self, &a[0], &b[0], $c, &mut d[0]);
253                        $f(&$self, &a[1], &b[1], $c, &mut d[1]);
254                        $f(&$self, &a[2], &b[2], $c, &mut d[2]);
255                        $f(&$self, &a[3], &b[3], $c, &mut d[3]);
256                        $f(&$self, &a[4], &b[4], $c, &mut d[4]);
257                        $f(&$self, &a[5], &b[5], $c, &mut d[5]);
258                        $f(&$self, &a[6], &b[6], $c, &mut d[6]);
259                        $f(&$self, &a[7], &b[7], $c, &mut d[7]);
260                    });
261
262                    let m = n - (n & 7);
263                    izip!($a[m..].iter(), $b[m..].iter(), $d[m..].iter_mut()).for_each(
264                        |(a, b, d)| {
265                            $f(&$self, a, b, $c, d);
266                        },
267                    );
268                }
269                _ => {
270                    izip!($a.iter(), $b.iter(), $d.iter_mut()).for_each(|(a, b, d)| {
271                        $f(&$self, a, b, $c, d);
272                    });
273                }
274            }
275        };
276    }
277
278    #[macro_export]
279    macro_rules! apply_vsv {
280        ($self:expr, $f:expr, $a:expr, $c:expr, $b:expr, $CHUNK:expr) => {
281            let n: usize = $a.len();
282            debug_assert!(
283                $b.len() == n,
284                "invalid argument b: b.len() = {} != a.len() = {}",
285                $b.len(),
286                n
287            );
288            debug_assert!(
289                CHUNK & (CHUNK - 1) == 0,
290                "invalid CHUNK const: not a power of two"
291            );
292
293            match CHUNK {
294                8 => {
295                    izip!($a.chunks_exact(8), $b.chunks_exact_mut(8)).for_each(|(a, b)| {
296                        $f(&$self, &a[0], $c, &mut b[0]);
297                        $f(&$self, &a[1], $c, &mut b[1]);
298                        $f(&$self, &a[2], $c, &mut b[2]);
299                        $f(&$self, &a[3], $c, &mut b[3]);
300                        $f(&$self, &a[4], $c, &mut b[4]);
301                        $f(&$self, &a[5], $c, &mut b[5]);
302                        $f(&$self, &a[6], $c, &mut b[6]);
303                        $f(&$self, &a[7], $c, &mut b[7]);
304                    });
305
306                    let m = n - (n & 7);
307                    izip!($a[m..].iter(), $b[m..].iter_mut()).for_each(|(a, b)| {
308                        $f(&$self, a, $c, b);
309                    });
310                }
311                _ => {
312                    izip!($a.iter(), $b.iter_mut()).for_each(|(a, b)| {
313                        $f(&$self, a, $c, b);
314                    });
315                }
316            }
317        };
318    }
319
320    #[macro_export]
321    macro_rules! apply_vssv {
322        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $d:expr, $CHUNK:expr) => {
323            let n: usize = $a.len();
324            debug_assert!(
325                $d.len() == n,
326                "invalid argument d: d.len() = {} != a.len() = {}",
327                $d.len(),
328                n
329            );
330            debug_assert!(
331                CHUNK & (CHUNK - 1) == 0,
332                "invalid CHUNK const: not a power of two"
333            );
334
335            match CHUNK {
336                8 => {
337                    izip!($a.chunks_exact(8), $d.chunks_exact_mut(8)).for_each(|(a, d)| {
338                        $f(&$self, &a[0], $b, $c, &mut d[0]);
339                        $f(&$self, &a[1], $b, $c, &mut d[1]);
340                        $f(&$self, &a[2], $b, $c, &mut d[2]);
341                        $f(&$self, &a[3], $b, $c, &mut d[3]);
342                        $f(&$self, &a[4], $b, $c, &mut d[4]);
343                        $f(&$self, &a[5], $b, $c, &mut d[5]);
344                        $f(&$self, &a[6], $b, $c, &mut d[6]);
345                        $f(&$self, &a[7], $b, $c, &mut d[7]);
346                    });
347
348                    let m = n - (n & 7);
349                    izip!($a[m..].iter(), $d[m..].iter_mut()).for_each(|(a, d)| {
350                        $f(&$self, a, $b, $c, d);
351                    });
352                }
353                _ => {
354                    izip!($a.iter(), $d.iter_mut()).for_each(|(a, d)| {
355                        $f(&$self, a, $b, $c, d);
356                    });
357                }
358            }
359        };
360    }
361
362    #[macro_export]
363    macro_rules! apply_ssv {
364        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $CHUNK:expr) => {
365            let n: usize = $c.len();
366            debug_assert!(
367                CHUNK & (CHUNK - 1) == 0,
368                "invalid CHUNK const: not a power of two"
369            );
370
371            match CHUNK {
372                8 => {
373                    izip!($c.chunks_exact_mut(8)).for_each(|c| {
374                        $f(&$self, $a, $b, &mut c[0]);
375                        $f(&$self, $a, $b, &mut c[1]);
376                        $f(&$self, $a, $b, &mut c[2]);
377                        $f(&$self, $a, $b, &mut c[3]);
378                        $f(&$self, $a, $b, &mut c[4]);
379                        $f(&$self, $a, $b, &mut c[5]);
380                        $f(&$self, $a, $b, &mut c[6]);
381                        $f(&$self, $a, $b, &mut c[7]);
382                    });
383
384                    let m = n - (n & 7);
385                    izip!($c[m..].iter_mut()).for_each(|c| {
386                        $f(&$self, $a, $b, c);
387                    });
388                }
389                _ => {
390                    izip!($c.iter_mut()).for_each(|c| {
391                        $f(&$self, $a, $b, c);
392                    });
393                }
394            }
395        };
396    }
397
398    #[macro_export]
399    macro_rules! apply_vvssv {
400        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $CHUNK:expr) => {
401            let n: usize = $a.len();
402            debug_assert!(
403                $b.len() == n,
404                "invalid argument b: b.len() = {} != a.len() = {}",
405                $b.len(),
406                n
407            );
408            debug_assert!(
409                $e.len() == n,
410                "invalid argument e: e.len() = {} != a.len() = {}",
411                $e.len(),
412                n
413            );
414            debug_assert!(
415                CHUNK & (CHUNK - 1) == 0,
416                "invalid CHUNK const: not a power of two"
417            );
418
419            match CHUNK {
420                8 => {
421                    izip!(
422                        $a.chunks_exact(8),
423                        $b.chunks_exact(8),
424                        $e.chunks_exact_mut(8)
425                    )
426                    .for_each(|(a, b, e)| {
427                        $f(&$self, &a[0], &b[0], $c, $d, &mut e[0]);
428                        $f(&$self, &a[1], &b[1], $c, $d,  &mut e[1]);
429                        $f(&$self, &a[2], &b[2], $c, $d,  &mut e[2]);
430                        $f(&$self, &a[3], &b[3], $c, $d,  &mut e[3]);
431                        $f(&$self, &a[4], &b[4], $c, $d,  &mut e[4]);
432                        $f(&$self, &a[5], &b[5], $c, $d,  &mut e[5]);
433                        $f(&$self, &a[6], &b[6], $c, $d,  &mut e[6]);
434                        $f(&$self, &a[7], &b[7], $c, $d,  &mut e[7]);
435                    });
436
437                    let m = n - (n & 7);
438                    izip!($a[m..].iter(), $b[m..].iter(), $e[m..].iter_mut()).for_each(
439                        |(a, b, e)| {
440                            $f(&$self, a, b, $c, $d, e);
441                        },
442                    );
443                }
444                _ => {
445                    izip!($a.iter(), $b.iter(), $e.iter_mut()).for_each(|(a, b, e)| {
446                        $f(&$self, a, b, $c, $d, e);
447                    });
448                }
449            }
450        };
451    }
452}