qfall-math 0.1.1

Mathematical foundations for rapid prototyping of lattice-based cryptography
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Copyright © 2023 Phil Milewski
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.

//! Implementation of the [`Mul`] trait for [`Z`] values.

use super::super::Z;
use crate::{
    integer_mod_q::Zq,
    macros::arithmetics::{
        arithmetic_assign_between_types, arithmetic_assign_trait_borrowed_to_owned,
        arithmetic_between_types, arithmetic_trait_borrowed_to_owned,
        arithmetic_trait_mixed_borrowed_owned,
    },
    rational::Q,
};
use flint_sys::{
    fmpq::fmpq_mul_fmpz,
    fmpz::{fmpz, fmpz_mul, fmpz_mul_si, fmpz_mul_ui},
    fmpz_mod::fmpz_mod_mul_fmpz,
};
use std::ops::{Mul, MulAssign};

impl MulAssign<&Z> for Z {
    /// Computes the multiplication of `self` and `other` reusing
    /// the memory of `self`.
    /// [`MulAssign`] can be used on [`Z`] in combination with
    /// [`Z`], [`i64`], [`i32`], [`i16`], [`i8`], [`u64`], [`u32`], [`u16`] and [`u8`].
    ///
    /// Parameters:
    /// - `other`: specifies the value to multiply to `self`
    ///
    /// Returns the product of both numbers as a [`Z`].
    ///
    /// # Examples
    /// ```
    /// use qfall_math::integer::Z;
    ///
    /// let mut a: Z = Z::from(42);
    /// let b: Z = Z::from(24);
    ///
    /// a *= &b;
    /// a *= b;
    /// a *= 5;
    /// ```
    fn mul_assign(&mut self, other: &Self) {
        unsafe { fmpz_mul(&mut self.value, &self.value, &other.value) };
    }
}
impl MulAssign<i64> for Z {
    /// Documentation at [`Z::mul_assign`].
    fn mul_assign(&mut self, other: i64) {
        unsafe { fmpz_mul_si(&mut self.value, &self.value, other) };
    }
}
impl MulAssign<u64> for Z {
    /// Documentation at [`Z::mul_assign`].
    fn mul_assign(&mut self, other: u64) {
        unsafe { fmpz_mul_ui(&mut self.value, &self.value, other) };
    }
}

arithmetic_assign_trait_borrowed_to_owned!(MulAssign, mul_assign, Z, Z);
arithmetic_assign_between_types!(MulAssign, mul_assign, Z, i64, i32 i16 i8);
arithmetic_assign_between_types!(MulAssign, mul_assign, Z, u64, u32 u16 u8);

impl Mul for &Z {
    type Output = Z;
    /// Implements the [`Mul`] trait for two [`Z`] values.
    /// [`Mul`] is implemented for any combination of [`Z`] and borrowed [`Z`].
    ///
    /// Parameters:
    /// - `other`: specifies the value to multiply with `self`
    ///
    /// Returns the product of both numbers as a [`Z`].
    ///
    /// # Examples
    /// ```
    /// use qfall_math::integer::Z;
    ///
    /// let a: Z = Z::from(42);
    /// let b: Z = Z::from(24);
    ///
    /// let c: Z = &a * &b;
    /// let d: Z = a * b;
    /// let e: Z = &c * d;
    /// let f: Z = c * &e;
    /// ```
    fn mul(self, other: Self) -> Self::Output {
        let mut out = Z::default();
        unsafe {
            fmpz_mul(&mut out.value, &self.value, &other.value);
        }
        out
    }
}

arithmetic_trait_borrowed_to_owned!(Mul, mul, Z, Z, Z);
arithmetic_trait_mixed_borrowed_owned!(Mul, mul, Z, Z, Z);
arithmetic_between_types!(Mul, mul, Z, Z, i64 i32 i16 i8 u64 u32 u16 u8);

impl Mul<&Q> for &Z {
    type Output = Q;

    /// Implements the [`Mul`] trait for [`Z`] and [`Q`] values.
    /// [`Mul`] is implemented for any combination of owned and borrowed values.
    ///
    /// Parameters:
    ///  - `other`: specifies the value to multiply with `self`
    ///
    /// Returns the product of both numbers as a [`Q`].
    ///
    /// # Examples
    /// ```
    /// use qfall_math::rational::Q;
    /// use qfall_math::integer::Z;
    /// use std::str::FromStr;
    ///
    /// let a: Z = Z::from(-42);
    /// let b: Q = Q::from((42, 19));
    ///
    /// let c: Q = &a * &b;
    /// let d: Q = a * b;
    /// let e: Q = &Z::from(42) * d;
    /// let f: Q = Z::from(42) * &e;
    /// ```
    fn mul(self, other: &Q) -> Self::Output {
        let mut out = Q::default();
        unsafe {
            fmpq_mul_fmpz(&mut out.value, &other.value, &self.value);
        }
        out
    }
}

arithmetic_trait_borrowed_to_owned!(Mul, mul, Z, Q, Q);
arithmetic_trait_mixed_borrowed_owned!(Mul, mul, Z, Q, Q);
arithmetic_between_types!(Mul, mul, Z, Q, f32 f64);

impl Mul<&Zq> for &Z {
    type Output = Zq;
    /// Implements the [`Mul`] trait for [`Z`] and [`Zq`] values.
    /// [`Mul`] is implemented for any combination of owned and borrowed values.
    ///
    /// Parameters:
    ///  - `other`: specifies the value to multiply with `self`
    ///
    /// Returns the product of both numbers as a [`Zq`].
    ///
    /// # Examples
    /// ```
    /// use qfall_math::integer_mod_q::Zq;
    /// use qfall_math::integer::Z;
    /// use std::str::FromStr;
    ///
    /// let a: Z = Z::from(42);
    /// let b: Zq = Zq::from((42, 9));
    ///
    /// let c: Zq = &a * &b;
    /// let d: Zq = a * b;
    /// let e: Zq = &Z::from(42) * d;
    /// let f: Zq = Z::from(42) * &e;
    /// ```
    fn mul(self, other: &Zq) -> Self::Output {
        let mut out = fmpz(0);
        unsafe {
            fmpz_mod_mul_fmpz(
                &mut out,
                &other.value.value,
                &self.value,
                other.modulus.get_fmpz_mod_ctx_struct(),
            );
        }
        Zq {
            modulus: other.modulus.clone(),
            value: Z { value: out },
        }
    }
}

arithmetic_trait_borrowed_to_owned!(Mul, mul, Z, Zq, Zq);
arithmetic_trait_mixed_borrowed_owned!(Mul, mul, Z, Zq, Zq);

#[cfg(test)]
mod test_mul_assign {
    use crate::integer::Z;

    /// Ensure that `mul_assign` works for small numbers.
    #[test]
    fn correct_small() {
        let mut a: Z = Z::MINUS_ONE;
        let b = Z::MINUS_ONE;
        let c = Z::ZERO;

        a *= &b;
        assert_eq!(1, a);
        a *= &b;
        assert_eq!(-1, a);
        a *= &c;
        assert_eq!(0, a);
        a *= &c;
    }

    /// Ensure that `mul_assign` works for large numbers.
    #[test]
    fn correct_large() {
        let mut a: Z = Z::from(i64::MIN);
        let mut b = Z::from(u64::MAX);

        a *= Z::MINUS_ONE;
        assert_eq!(-1 * Z::from(i64::MIN), a);
        b *= Z::MINUS_ONE;
        assert_eq!(-1 * Z::from(u64::MAX), b);
    }

    /// Ensure that `mul_assign` is available for all types.
    #[test]
    fn availability() {
        let mut a: Z = Z::from(42);
        let b: Z = Z::from(1);

        a *= &b;
        a *= b;
        a *= 1_u8;
        a *= 1_u16;
        a *= 1_u32;
        a *= 1_u64;
        a *= 1_i8;
        a *= 1_i16;
        a *= 1_i32;
        a *= 1_i64;
    }
}

#[cfg(test)]
mod test_mul_between_types {
    use crate::integer::Z;

    /// Testing multiplication between different types
    #[test]
    #[allow(clippy::op_ref)]
    fn mul() {
        let a: Z = Z::from(42);
        let b: u64 = 5;
        let c: u32 = 5;
        let d: u16 = 5;
        let e: u8 = 5;
        let f: i64 = 5;
        let g: i32 = 5;
        let h: i16 = 5;
        let i: i8 = 5;

        let _: Z = &a * &b;
        let _: Z = &a * &c;
        let _: Z = &a * &d;
        let _: Z = &a * &e;
        let _: Z = &a * &f;
        let _: Z = &a * &g;
        let _: Z = &a * &h;
        let _: Z = &a * &i;

        let _: Z = &b * &a;
        let _: Z = &c * &a;
        let _: Z = &d * &a;
        let _: Z = &e * &a;
        let _: Z = &f * &a;
        let _: Z = &g * &a;
        let _: Z = &h * &a;
        let _: Z = &i * &a;

        let _: Z = &a * b;
        let _: Z = &a * c;
        let _: Z = &a * d;
        let _: Z = &a * e;
        let _: Z = &a * f;
        let _: Z = &a * g;
        let _: Z = &a * h;
        let _: Z = &a * i;

        let _: Z = &b * Z::from(42);
        let _: Z = &c * Z::from(42);
        let _: Z = &d * Z::from(42);
        let _: Z = &e * Z::from(42);
        let _: Z = &f * Z::from(42);
        let _: Z = &g * Z::from(42);
        let _: Z = &h * Z::from(42);
        let _: Z = &i * Z::from(42);

        let _: Z = Z::from(42) * &b;
        let _: Z = Z::from(42) * &c;
        let _: Z = Z::from(42) * &d;
        let _: Z = Z::from(42) * &e;
        let _: Z = Z::from(42) * &f;
        let _: Z = Z::from(42) * &g;
        let _: Z = Z::from(42) * &h;
        let _: Z = Z::from(42) * &i;

        let _: Z = b * &a;
        let _: Z = c * &a;
        let _: Z = d * &a;
        let _: Z = e * &a;
        let _: Z = f * &a;
        let _: Z = g * &a;
        let _: Z = h * &a;
        let _: Z = i * &a;

        let _: Z = Z::from(42) * b;
        let _: Z = Z::from(42) * c;
        let _: Z = Z::from(42) * d;
        let _: Z = Z::from(42) * e;
        let _: Z = Z::from(42) * f;
        let _: Z = Z::from(42) * g;
        let _: Z = Z::from(42) * h;
        let _: Z = Z::from(42) * i;

        let _: Z = b * Z::from(42);
        let _: Z = c * Z::from(42);
        let _: Z = d * Z::from(42);
        let _: Z = e * Z::from(42);
        let _: Z = f * Z::from(42);
        let _: Z = g * Z::from(42);
        let _: Z = h * Z::from(42);
        let _: Z = i * Z::from(42);
    }
}

#[cfg(test)]
mod test_mul {
    use super::Z;

    /// Testing multiplication for two [`Z`]
    #[test]
    fn mul() {
        let a: Z = Z::from(42);
        let b: Z = Z::from(4);
        let c: Z = a * b;
        assert_eq!(c, Z::from(168));
    }

    /// Testing multiplication for two borrowed [`Z`]
    #[test]
    fn mul_borrow() {
        let a: Z = Z::from(42);
        let b: Z = Z::from(4);
        let c: Z = &a * &b;
        assert_eq!(c, Z::from(168));
    }

    /// Testing multiplication for borrowed [`Z`] and [`Z`]
    #[test]
    fn mul_first_borrowed() {
        let a: Z = Z::from(42);
        let b: Z = Z::from(4);
        let c: Z = &a * b;
        assert_eq!(c, Z::from(168));
    }

    /// Testing multiplication for [`Z`] and borrowed [`Z`]
    #[test]
    fn mul_second_borrowed() {
        let a: Z = Z::from(42);
        let b: Z = Z::from(4);
        let c: Z = a * &b;
        assert_eq!(c, Z::from(168));
    }

    /// Testing multiplication for large [`Z`]
    #[test]
    fn mul_large_numbers() {
        let a: Z = Z::from(i64::MAX);
        let b: Z = Z::from(2);
        let c: Z = Z::from(i32::MIN);
        let d: Z = Z::from(i32::MAX);

        let e: Z = a * b;
        let f: Z = c * d;

        assert_eq!(e, Z::from(u64::MAX - 1));
        assert_eq!(f, Z::from(i64::from(i32::MAX) * i64::from(i32::MIN)));
    }
}

#[cfg(test)]
mod test_mul_between_z_and_zq {
    use super::Z;
    use crate::integer_mod_q::Zq;

    /// Testing multiplication for [`Z`] and [`Zq`]
    #[test]
    fn mul() {
        let a: Z = Z::from(9);
        let b: Zq = Zq::from((4, 11));
        let c: Zq = a * b;
        assert_eq!(c, Zq::from((3, 11)));
    }

    /// Testing multiplication for both borrowed [`Z`] and [`Zq`]
    #[test]
    fn mul_borrow() {
        let a: Z = Z::from(9);
        let b: Zq = Zq::from((4, 11));
        let c: Zq = &a * &b;
        assert_eq!(c, Zq::from((3, 11)));
    }

    /// Testing multiplication for borrowed [`Z`] and [`Zq`]
    #[test]
    fn mul_first_borrowed() {
        let a: Z = Z::from(9);
        let b: Zq = Zq::from((4, 11));
        let c: Zq = &a * b;
        assert_eq!(c, Zq::from((3, 11)));
    }

    /// Testing multiplication for [`Z`] and borrowed [`Zq`]
    #[test]
    fn mul_second_borrowed() {
        let a: Z = Z::from(9);
        let b: Zq = Zq::from((4, 11));
        let c: Zq = a * &b;
        assert_eq!(c, Zq::from((3, 11)));
    }

    /// Testing multiplication for large numbers
    #[test]
    fn mul_large_numbers() {
        let a: Z = Z::from(u64::MAX);
        let b: Zq = Zq::from((i64::MAX, u64::MAX - 58));
        let c: Zq = Zq::from((i64::MAX - 1, i64::MAX));

        let d: Zq = &a * b;
        let e: Zq = a * c;

        assert_eq!(
            d,
            Zq::from(((u64::MAX - 1) / 2, u64::MAX - 58)) * Zq::from((u64::MAX, u64::MAX - 58))
        );
        assert_eq!(e, Zq::from((u64::MAX, i64::MAX)) * Zq::from((-1, i64::MAX)));
    }
}

#[cfg(test)]
mod test_mul_between_z_and_q {
    use super::Z;
    use crate::rational::Q;

    /// Ensuring multiplication between different types is available
    #[test]
    fn availability() {
        let a: Z = Z::from(42);
        let b: Q = Q::from((5, 7));

        let _: Q = &a * &b;
        let _: Q = &a * b.clone();
        let _: Q = a.clone() * &b;
        let _: Q = a.clone() * b;
        let _: Q = &a * 0.5_f32;
        let _: Q = &a * 0.5_f64;
        let _: Q = a.clone() * 0.5_f32;
        let _: Q = a.clone() * 0.5_f64;
        let _: Q = 0.5_f32 * &a;
        let _: Q = 0.5_f64 * &a;
        let _: Q = 0.5_f32 * a.clone();
        let _: Q = 0.5_f64 * a.clone();
    }

    /// Testing multiplication for [`Z`] and [`Q`]
    #[test]
    fn mul() {
        let a: Z = Z::from(4);
        let b: Q = Q::from((5, 7));
        let c: Q = a * b;
        assert_eq!(c, Q::from((20, 7)));
    }

    /// Testing multiplication for both borrowed [`Z`] and [`Q`]
    #[test]
    fn mul_borrow() {
        let a: Z = Z::from(4);
        let b: Q = Q::from((5, 7));
        let c: Q = &a * &b;
        assert_eq!(c, Q::from((20, 7)));
    }

    /// Testing multiplication for borrowed [`Z`] and [`Q`]
    #[test]
    fn mul_first_borrowed() {
        let a: Z = Z::from(4);
        let b: Q = Q::from((5, 7));
        let c: Q = &a * b;
        assert_eq!(c, Q::from((20, 7)));
    }

    /// Testing multiplication for [`Z`] and borrowed [`Q`]
    #[test]
    fn mul_second_borrowed() {
        let a: Z = Z::from(4);
        let b: Q = Q::from((5, 7));
        let c: Q = a * &b;
        assert_eq!(c, Q::from((20, 7)));
    }

    /// Testing multiplication for large numbers
    #[test]
    fn mul_large_numbers() {
        let a: Z = Z::from(u64::MAX);
        let b: Q = Q::from((1, u64::MAX));
        let c: Q = Q::from((u64::MAX, 2));

        let d: Q = &a * b;
        let e: Q = a * c;

        assert_eq!(d, Q::from((1, u64::MAX)) * Q::from(u64::MAX));
        assert_eq!(e, Q::from(u64::MAX) * Q::from((u64::MAX, 2)));
    }
}