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 modulus;
  5pub mod dft;
  6pub mod ring;
  7pub mod poly;
  8pub mod scalar;
  9
 10pub const CHUNK: usize= 8;
 11
 12pub mod macros{
 13    
 14    #[macro_export]
 15    macro_rules! apply_v {
 16
 17        ($self:expr, $f:expr, $a:expr, $CHUNK:expr) => {
 18
 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
 50        ($self:expr, $f:expr, $a:expr, $b:expr, $CHUNK:expr) => {
 51
 52            let n: usize = $a.len();
 53            debug_assert!($b.len() == n, "invalid argument b: b.len() = {} != a.len() = {}", $b.len(), n);
 54            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
 55
 56            match CHUNK{
 57                8 => {
 58                    
 59                    izip!($a.chunks_exact(8), $b.chunks_exact_mut(8)).for_each(|(a, b)| {
 60                        $f(&$self, &a[0], &mut b[0]);
 61                        $f(&$self, &a[1], &mut b[1]);
 62                        $f(&$self, &a[2], &mut b[2]);
 63                        $f(&$self, &a[3], &mut b[3]);
 64                        $f(&$self, &a[4], &mut b[4]);
 65                        $f(&$self, &a[5], &mut b[5]);
 66                        $f(&$self, &a[6], &mut b[6]);
 67                        $f(&$self, &a[7], &mut b[7]);
 68                    });
 69
 70                    let m = n - (n&(CHUNK-1));
 71                    izip!($a[m..].iter(), $b[m..].iter_mut()).for_each(|(a, b)| {
 72                        $f(&$self, a, b);
 73                    });
 74                },
 75                _=>{
 76                    izip!($a.iter(), $b.iter_mut()).for_each(|(a, b)| {
 77                        $f(&$self, a, b);
 78                    });
 79                }
 80            }
 81        };
 82    }
 83
 84    #[macro_export]
 85    macro_rules! apply_vvv {
 86
 87        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $CHUNK:expr) => {
 88
 89            let n: usize = $a.len();
 90            debug_assert!($b.len() == n, "invalid argument b: b.len() = {} != a.len() = {}", $b.len(), n);
 91            debug_assert!($c.len() == n, "invalid argument c: b.len() = {} != a.len() = {}", $c.len(), n);
 92            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
 93
 94            match CHUNK{
 95                8 => {
 96                    
 97                    izip!($a.chunks_exact(8), $b.chunks_exact(8), $c.chunks_exact_mut(8)).for_each(|(a, b, c)| {
 98                        $f(&$self, &a[0], &b[0], &mut c[0]);
 99                        $f(&$self, &a[1], &b[1], &mut c[1]);
100                        $f(&$self, &a[2], &b[2], &mut c[2]);
101                        $f(&$self, &a[3], &b[3], &mut c[3]);
102                        $f(&$self, &a[4], &b[4], &mut c[4]);
103                        $f(&$self, &a[5], &b[5], &mut c[5]);
104                        $f(&$self, &a[6], &b[6], &mut c[6]);
105                        $f(&$self, &a[7], &b[7], &mut c[7]);
106                    });
107
108                    let m = n - (n&7);
109                    izip!($a[m..].iter(), $b[m..].iter(), $c[m..].iter_mut()).for_each(|(a, b, c)| {
110                        $f(&$self, a, b, c);
111                    });
112                },
113                _=>{
114                    izip!($a.iter(), $b.iter(), $c.iter_mut()).for_each(|(a, b, c)| {
115                        $f(&$self, a, b, c);
116                    });
117                }
118            }
119        };
120    }
121
122    #[macro_export]
123    macro_rules! apply_sv {
124
125        ($self:expr, $f:expr, $a:expr, $b:expr, $CHUNK:expr) => {
126
127            let n: usize = $b.len();
128
129            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
130
131            match CHUNK{
132                8 => {
133                    
134                    izip!($b.chunks_exact_mut(8)).for_each(|b| {
135                        $f(&$self, $a, &mut b[0]);
136                        $f(&$self, $a, &mut b[1]);
137                        $f(&$self, $a, &mut b[2]);
138                        $f(&$self, $a, &mut b[3]);
139                        $f(&$self, $a, &mut b[4]);
140                        $f(&$self, $a, &mut b[5]);
141                        $f(&$self, $a, &mut b[6]);
142                        $f(&$self, $a, &mut b[7]);
143                    });
144
145                    let m = n - (n&7);
146                    izip!($b[m..].iter_mut()).for_each(|b| {
147                        $f(&$self, $a, b);
148                    });
149                },
150                _=>{
151                    izip!($b.iter_mut()).for_each(|b| {
152                        $f(&$self, $a, b);
153                    });
154                }
155            }
156        };
157    }
158
159    #[macro_export]
160    macro_rules! apply_svv {
161
162        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $CHUNK:expr) => {
163
164            let n: usize = $b.len();
165            debug_assert!($c.len() == n, "invalid argument c: c.len() = {} != b.len() = {}", $c.len(), n);
166            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
167
168            match CHUNK{
169                8 => {
170                    
171                    izip!($b.chunks_exact(8), $c.chunks_exact_mut(8)).for_each(|(b, c)| {
172                        $f(&$self, $a, &b[0], &mut c[0]);
173                        $f(&$self, $a, &b[1], &mut c[1]);
174                        $f(&$self, $a, &b[2], &mut c[2]);
175                        $f(&$self, $a, &b[3], &mut c[3]);
176                        $f(&$self, $a, &b[4], &mut c[4]);
177                        $f(&$self, $a, &b[5], &mut c[5]);
178                        $f(&$self, $a, &b[6], &mut c[6]);
179                        $f(&$self, $a, &b[7], &mut c[7]);
180                    });
181
182                    let m = n - (n&7);
183                    izip!($b[m..].iter(), $c[m..].iter_mut()).for_each(|(b, c)| {
184                        $f(&$self, $a, b, c);
185                    });
186                },
187                _=>{
188                    izip!($b.iter(), $c.iter_mut()).for_each(|(b, c)| {
189                        $f(&$self, $a, b, c);
190                    });
191                }
192            }
193        };
194    }
195
196    #[macro_export]
197    macro_rules! apply_vvsv {
198
199        ($self:expr, $f:expr, $a:expr, $b:expr, $c:expr, $d:expr, $CHUNK:expr) => {
200
201            let n: usize = $a.len();
202            debug_assert!($b.len() == n, "invalid argument b: b.len() = {} != a.len() = {}", $b.len(), n);
203            debug_assert!($d.len() == n, "invalid argument d: d.len() = {} != a.len() = {}", $d.len(), n);
204            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
205
206            match CHUNK{
207                8 => {
208                    
209                    izip!($a.chunks_exact(8), $b.chunks_exact(8), $d.chunks_exact_mut(8)).for_each(|(a, b, d)| {
210                        $f(&$self, &a[0], &b[0], $c, &mut d[0]);
211                        $f(&$self, &a[1], &b[1], $c, &mut d[1]);
212                        $f(&$self, &a[2], &b[2], $c, &mut d[2]);
213                        $f(&$self, &a[3], &b[3], $c, &mut d[3]);
214                        $f(&$self, &a[4], &b[4], $c, &mut d[4]);
215                        $f(&$self, &a[5], &b[5], $c, &mut d[5]);
216                        $f(&$self, &a[6], &b[6], $c, &mut d[6]);
217                        $f(&$self, &a[7], &b[7], $c, &mut d[7]);
218                    });
219
220                    let m = n - (n&7);
221                    izip!($a[m..].iter(), $b[m..].iter(), $d[m..].iter_mut()).for_each(|(a, b, d)| {
222                        $f(&$self, a, b, $c, d);
223                    });
224                },
225                _=>{
226                    izip!($a.iter(), $b.iter(), $d.iter_mut()).for_each(|(a, b, d)| {
227                        $f(&$self, a, b, $c, d);
228                    });
229                }
230            }
231        };
232    }
233
234    #[macro_export]
235    macro_rules! apply_vsv {
236
237        ($self:expr, $f:expr, $a:expr, $c:expr, $b:expr, $CHUNK:expr) => {
238
239            let n: usize = $a.len();
240            debug_assert!($b.len() == n, "invalid argument b: b.len() = {} != a.len() = {}", $b.len(), n);
241            debug_assert!(CHUNK&(CHUNK-1) == 0, "invalid CHUNK const: not a power of two");
242
243            match CHUNK{
244                8 => {
245                    
246                    izip!($a.chunks_exact(8), $b.chunks_exact_mut(8)).for_each(|(a, b)| {
247                        $f(&$self, &a[0], $c, &mut b[0]);
248                        $f(&$self, &a[1], $c, &mut b[1]);
249                        $f(&$self, &a[2], $c, &mut b[2]);
250                        $f(&$self, &a[3], $c, &mut b[3]);
251                        $f(&$self, &a[4], $c, &mut b[4]);
252                        $f(&$self, &a[5], $c, &mut b[5]);
253                        $f(&$self, &a[6], $c, &mut b[6]);
254                        $f(&$self, &a[7], $c, &mut b[7]);
255                    });
256
257                    let m = n - (n&7);
258                    izip!($a[m..].iter(), $b[m..].iter_mut()).for_each(|(a, b)| {
259                        $f(&$self, a, $c, b);
260                    });
261                },
262                _=>{
263                    izip!($a.iter(), $b.iter_mut()).for_each(|(a, b)| {
264                        $f(&$self, a, $c, b);
265                    });
266                }
267            }
268        };
269    }
270}