rssn 0.2.9

A comprehensive scientific computing library for Rust, aiming for feature parity with NumPy and SymPy.
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! # Symbolic Finite Field Arithmetic
//!
//! This module provides symbolic structures for arithmetic in finite fields (Galois fields).
//! It defines prime fields GF(p) and extension fields GF(p^n), along with the necessary
//! arithmetic operations for their elements and for polynomials over these fields.
#![allow(clippy::should_implement_trait)]

use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Div;
use std::ops::DivAssign;
use std::ops::Mul;
use std::ops::MulAssign;
use std::ops::Neg;
use std::ops::Sub;
use std::ops::SubAssign;
use std::sync::Arc;

use num_bigint::BigInt;
use num_traits::One;
use num_traits::Zero;

use crate::symbolic::number_theory::extended_gcd_inner;

// Helper module for serializing Arc<T>
mod arc_serde {

    use std::sync::Arc;

    use serde::Deserialize;
    use serde::Deserializer;
    use serde::Serialize;
    use serde::Serializer;

    pub fn serialize<S, T>(
        arc: &Arc<T>,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
        T: Serialize,
    {
        arc.as_ref().serialize(serializer)
    }

    pub fn deserialize<'de, D, T>(deserializer: D) -> Result<Arc<T>, D::Error>
    where
        D: Deserializer<'de>,
        T: Deserialize<'de>,
    {
        T::deserialize(deserializer).map(Arc::new)
    }
}

/// Represents a prime finite field `GF(p)`.
#[derive(Debug, PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)]
pub struct PrimeField {
    /// The prime modulus `p` defining the field characteristic.
    pub modulus: BigInt,
}

impl PrimeField {
    /// Creates a new prime field `GF(p)` with the given modulus.
    ///
    /// # Arguments
    /// * `modulus` - A `BigInt` representing the prime modulus `p` of the field.
    ///
    /// # Returns
    /// An `Arc<PrimeField>` pointing to the newly created field structure.
    #[must_use]
    pub fn new(modulus: BigInt) -> Arc<Self> {
        Arc::new(Self { modulus })
    }
}

/// Represents an element of a prime finite field `GF(p)`.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PrimeFieldElement {
    /// The numeric value of the field element, always in range `[0, p-1]`.
    pub value: BigInt,
    /// The prime field this element belongs to.
    #[serde(with = "arc_serde")]
    pub field: Arc<PrimeField>,
}

impl PrimeFieldElement {
    /// Creates a new element in a prime field.
    ///
    /// The value is reduced modulo the field's characteristic (the prime modulus).
    ///
    /// # Arguments
    /// * `value` - The initial `BigInt` value of the element.
    /// * `field` - An `Arc` pointing to the `PrimeField` this element belongs to.
    ///
    /// # Returns
    /// A new `PrimeFieldElement`.
    #[must_use]
    pub fn new(
        value: BigInt,
        field: Arc<PrimeField>,
    ) -> Self {
        let modulus = &field.modulus;

        let mut val = value % modulus;

        if val < Zero::zero() {
            val += modulus;
        }

        Self { value: val, field }
    }

    /// Computes the multiplicative inverse of the element in the prime field.
    ///
    /// The inverse `x` of an element `a` is such that `a * x = 1 (mod p)`.
    /// This implementation uses the Extended Euclidean Algorithm.
    ///
    /// # Returns
    /// * `Some(PrimeFieldElement)` containing the inverse if it exists.
    /// * `None` if the element is not invertible (i.e., its value is not coprime to the modulus).
    #[must_use]
    pub fn inverse(&self) -> Option<Self> {
        let (g, x, _) = extended_gcd_inner(self.value.clone(), self.field.modulus.clone());

        if g.is_one() {
            let modulus = &self.field.modulus;

            let mut inv = x % modulus;

            if inv < Zero::zero() {
                inv += modulus;
            }

            Some(Self::new(inv, self.field.clone()))
        } else {
            None
        }
    }
}

impl Add for PrimeFieldElement {
    type Output = Self;

    fn add(
        self,
        rhs: Self,
    ) -> Self {
        if self.field != rhs.field {
            return Self::new(Zero::zero(), self.field);
        }

        let val = (self.value + rhs.value) % &self.field.modulus;

        Self::new(val, self.field)
    }
}

impl Sub for PrimeFieldElement {
    type Output = Self;

    fn sub(
        self,
        rhs: Self,
    ) -> Self {
        if self.field != rhs.field {
            return Self::new(Zero::zero(), self.field);
        }

        let val = (self.value - rhs.value + &self.field.modulus) % &self.field.modulus;

        Self::new(val, self.field)
    }
}

impl Mul for PrimeFieldElement {
    type Output = Self;

    fn mul(
        self,
        rhs: Self,
    ) -> Self {
        if self.field != rhs.field {
            return Self::new(Zero::zero(), self.field);
        }

        let val = (self.value * rhs.value) % &self.field.modulus;

        Self::new(val, self.field)
    }
}

impl Div for PrimeFieldElement {
    type Output = Self;

    fn div(
        self,
        rhs: Self,
    ) -> Self {
        if self.field != rhs.field {
            return Self::new(Zero::zero(), self.field);
        }

        let inv_rhs = match rhs.inverse() {
            | Some(inv) => inv,
            | None => {
                return Self::new(Zero::zero(), self.field);
            },
        };

        self * inv_rhs
    }
}

impl Neg for PrimeFieldElement {
    type Output = Self;

    fn neg(self) -> Self {
        let val = (-self.value + &self.field.modulus) % &self.field.modulus;

        Self::new(val, self.field)
    }
}

impl PartialEq for PrimeFieldElement {
    fn eq(
        &self,
        other: &Self,
    ) -> bool {
        self.field == other.field && self.value == other.value
    }
}

impl Eq for PrimeFieldElement {}

impl Zero for PrimeFieldElement {
    fn zero() -> Self {
        let dummy_field = PrimeField::new(BigInt::from(2));

        Self::new(Zero::zero(), dummy_field)
    }

    fn is_zero(&self) -> bool {
        self.value.is_zero()
    }
}

impl One for PrimeFieldElement {
    fn one() -> Self {
        let dummy_field = PrimeField::new(BigInt::from(2));

        Self::new(One::one(), dummy_field)
    }
}

impl AddAssign for PrimeFieldElement {
    fn add_assign(
        &mut self,
        rhs: Self,
    ) {
        *self = self.clone() + rhs;
    }
}

impl SubAssign for PrimeFieldElement {
    fn sub_assign(
        &mut self,
        rhs: Self,
    ) {
        *self = self.clone() - rhs;
    }
}

impl MulAssign for PrimeFieldElement {
    fn mul_assign(
        &mut self,
        rhs: Self,
    ) {
        *self = self.clone() * rhs;
    }
}

impl DivAssign for PrimeFieldElement {
    fn div_assign(
        &mut self,
        rhs: Self,
    ) {
        *self = self.clone() / rhs;
    }
}

/// Represents a univariate polynomial with coefficients from a prime finite field.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct FiniteFieldPolynomial {
    /// The coefficients of the polynomial in descending order of degree.
    pub coeffs: Vec<PrimeFieldElement>,
    /// The underlying prime field for the coefficients.
    #[serde(with = "arc_serde")]
    pub field: Arc<PrimeField>,
}

impl FiniteFieldPolynomial {
    /// Creates a new polynomial over a prime field.
    ///
    /// This function also removes leading zero coefficients to keep the representation canonical.
    ///
    /// # Arguments
    /// * `coeffs` - A vector of `PrimeFieldElement`s representing the coefficients in descending order of degree.
    /// * `field` - An `Arc` pointing to the `PrimeField` the coefficients belong to.
    ///
    /// # Returns
    /// A new `FiniteFieldPolynomial`.
    #[must_use]
    pub fn new(
        coeffs: &[PrimeFieldElement],
        field: Arc<PrimeField>,
    ) -> Self {
        let first_non_zero = coeffs
            .iter()
            .position(|c| !c.value.is_zero())
            .unwrap_or(coeffs.len());

        Self {
            coeffs: coeffs[first_non_zero..].to_vec(),
            field,
        }
    }

    /// Returns the degree of the polynomial.
    ///
    /// The degree is the highest power of the variable with a non-zero coefficient.
    /// The degree of the zero polynomial is defined as -1.
    ///
    /// # Returns
    /// An `isize` representing the degree.
    #[must_use]
    pub const fn degree(&self) -> isize {
        if self.coeffs.is_empty() {
            -1
        } else {
            (self.coeffs.len() - 1) as isize
        }
    }

    /// Performs polynomial long division over the prime field.
    ///
    /// # Arguments
    /// * `divisor` - The polynomial to divide by.
    ///
    /// # Returns
    /// A tuple `(quotient, remainder)`.
    ///
    /// # Errors
    ///
    /// This function will return an error if the divisor is the zero polynomial
    /// or if the leading coefficient of the divisor is not invertible in the field.
    ///
    /// # Panics
    /// Panics if the divisor is the zero polynomial.
    pub fn long_division(
        self,
        divisor: &Self,
    ) -> Result<(Self, Self), String> {
        if divisor.coeffs.is_empty() || divisor.coeffs.iter().all(|c| c.value.is_zero()) {
            return Err("Division by zero \
                 polynomial"
                .to_string());
        }

        let mut quotient =
            vec![PrimeFieldElement::new(Zero::zero(), self.field.clone()); self.coeffs.len()];

        let mut remainder = self.coeffs.clone();

        let divisor_deg = divisor.coeffs.len() - 1;

        let lead_divisor_inv = divisor.coeffs[0].inverse().ok_or_else(|| {
            "Leading coefficient \
                 of divisor is not \
                 invertible."
                .to_string()
        })?;

        while remainder.len() > divisor_deg && !remainder.is_empty() {
            let lead_rem = remainder[0].clone();

            let coeff = lead_rem * lead_divisor_inv.clone();

            let degree_diff = remainder.len() - divisor.coeffs.len();

            if degree_diff < quotient.len() {
                quotient[degree_diff] = coeff.clone();
            }

            for (i, vars) in remainder.iter_mut().enumerate().take(divisor_deg + 1) {
                let term = coeff.clone() * divisor.coeffs[i].clone();

                *vars = (*vars).clone() - term;
            }

            remainder.remove(0);
        }

        Ok((
            Self::new(&quotient, self.field.clone()),
            Self::new(&remainder, self.field),
        ))
    }
}

impl Add for FiniteFieldPolynomial {
    type Output = Self;

    fn add(
        self,
        rhs: Self,
    ) -> Self {
        let max_len = self.coeffs.len().max(rhs.coeffs.len());

        let mut result_coeffs =
            vec![PrimeFieldElement::new(Zero::zero(), self.field.clone()); max_len];

        let self_start = max_len - self.coeffs.len();

        result_coeffs[self_start..(self.coeffs.len() + self_start)]
            .clone_from_slice(&self.coeffs[..]);

        let rhs_start = max_len - rhs.coeffs.len();

        for i in 0..rhs.coeffs.len() {
            result_coeffs[rhs_start + i] =
                result_coeffs[rhs_start + i].clone() + rhs.coeffs[i].clone();
        }

        Self::new(&result_coeffs, self.field)
    }
}

#[allow(clippy::suspicious_arithmetic_impl)]
impl Sub for FiniteFieldPolynomial {
    type Output = Self;

    fn sub(
        self,
        rhs: Self,
    ) -> Self {
        let neg_rhs_coeffs = rhs
            .coeffs
            .into_iter()
            .map(|c| -c)
            .collect::<Vec<PrimeFieldElement>>();

        let neg_rhs = Self::new(&neg_rhs_coeffs, rhs.field);

        self + neg_rhs
    }
}

impl Mul for FiniteFieldPolynomial {
    type Output = Self;

    fn mul(
        self,
        rhs: Self,
    ) -> Self {
        if self.coeffs.is_empty() || rhs.coeffs.is_empty() {
            return Self::new(&[], self.field);
        }

        let deg1 = self.coeffs.len() - 1;

        let deg2 = rhs.coeffs.len() - 1;

        let mut result_coeffs =
            vec![PrimeFieldElement::new(Zero::zero(), self.field.clone()); deg1 + deg2 + 1];

        for i in 0..=deg1 {
            for j in 0..=deg2 {
                let term_mul = self.coeffs[i].clone() * rhs.coeffs[j].clone();

                let existing = result_coeffs[i + j].clone();

                result_coeffs[i + j] = existing + term_mul;
            }
        }

        Self::new(&result_coeffs, self.field)
    }
}

/// Represents an extension field `GF(p^n)` defined by an irreducible polynomial over `GF(p)`.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ExtensionField {
    /// The base prime field `GF(p)`.
    #[serde(with = "arc_serde")]
    pub prime_field: Arc<PrimeField>,
    /// The irreducible polynomial of degree `n` used to define the extension.
    pub irreducible_poly: FiniteFieldPolynomial,
}

/// Represents an element of an extension field `GF(p^n)`.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ExtensionFieldElement {
    /// The polynomial representation of the extension field element.
    pub poly: FiniteFieldPolynomial,
    /// The extension field this element belongs to.
    #[serde(with = "arc_serde")]
    pub field: Arc<ExtensionField>,
}

impl ExtensionFieldElement {
    /// Creates a new element in an extension field.
    ///
    /// The element is represented by a polynomial, which is reduced modulo the
    /// field's irreducible polynomial to keep it in canonical form.
    ///
    /// # Arguments
    /// * `poly` - The `FiniteFieldPolynomial` representing the initial value of the element.
    /// * `field` - An `Arc` pointing to the `ExtensionField` this element belongs to.
    ///
    /// # Returns
    /// A new `ExtensionFieldElement`.
    #[must_use]
    pub fn new(
        poly: FiniteFieldPolynomial,
        field: Arc<ExtensionField>,
    ) -> Self {
        match poly.long_division(&field.irreducible_poly.clone()) {
            | Ok((_, remainder)) => {
                Self {
                    poly: remainder,
                    field,
                }
            },
            | Err(_) => {
                Self {
                    poly: FiniteFieldPolynomial::new(&[], field.prime_field.clone()),
                    field,
                }
            },
        }
    }

    /// Computes the multiplicative inverse of the element in the extension field.
    ///
    /// This is done using the Extended Euclidean Algorithm for polynomials.
    ///
    /// # Returns
    /// * `Some(ExtensionFieldElement)` containing the inverse if it exists.
    /// * `None` if the element is not invertible.
    #[must_use]
    pub fn inverse(&self) -> Option<Self> {
        let (gcd, _, inv) =
            poly_extended_gcd(self.poly.clone(), self.field.irreducible_poly.clone()).ok()?;

        if gcd.degree() > 0 || gcd.coeffs.is_empty() {
            return None;
        }

        let inv_factor = gcd.coeffs[0].inverse()?;

        Some(Self::new(
            inv * FiniteFieldPolynomial::new(&[inv_factor], self.poly.field.clone()),
            self.field.clone(),
        ))
    }
}

pub(crate) fn poly_extended_gcd(
    a: FiniteFieldPolynomial,
    b: FiniteFieldPolynomial,
) -> Result<
    (
        FiniteFieldPolynomial,
        FiniteFieldPolynomial,
        FiniteFieldPolynomial,
    ),
    String,
> {
    let zero_poly = FiniteFieldPolynomial::new(&[], a.field.clone());

    if b.coeffs.is_empty() || b.coeffs.iter().all(|c| c.value.is_zero()) {
        let one_poly = FiniteFieldPolynomial::new(
            &[PrimeFieldElement::new(One::one(), a.field.clone())],
            a.field.clone(),
        );

        return Ok((a, one_poly, zero_poly));
    }

    let (q, r) = a.long_division(&b)?;

    let (g, x, y) = poly_extended_gcd(b, r)?;

    let t = x - (q * y.clone());

    Ok((g, y, t))
}

impl ExtensionFieldElement {
    /// Adds two extension field elements.
    ///
    /// # Errors
    ///
    /// This function will return an error if the elements belong to different
    /// extension fields (though this check is usually handled implicitly by
    /// type constraints and underlying polynomial arithmetic).
    pub fn add(
        self,
        rhs: Self,
    ) -> Result<Self, String> {
        Ok(Self::new(self.poly + rhs.poly, self.field))
    }

    /// Subtracts one extension field element from another.
    ///
    /// # Errors
    ///
    /// This function will return an error if the elements belong to different
    /// extension fields (though this check is usually handled implicitly by
    /// type constraints and underlying polynomial arithmetic).
    pub fn sub(
        self,
        rhs: Self,
    ) -> Result<Self, String> {
        Ok(Self::new(self.poly - rhs.poly, self.field))
    }

    /// Multiplies two extension field elements.
    ///
    /// # Errors
    ///
    /// This function will return an error if the elements belong to different
    /// extension fields (though this check is usually handled implicitly by
    /// type constraints and underlying polynomial arithmetic).
    pub fn mul(
        self,
        rhs: Self,
    ) -> Result<Self, String> {
        Ok(Self::new(self.poly * rhs.poly, self.field))
    }

    /// Divides one extension field element by another.
    ///
    /// # Errors
    ///
    /// This function will return an error if `rhs` is the zero element or is not
    /// invertible within the extension field.
    pub fn div(
        self,
        rhs: &Self,
    ) -> Result<Self, String> {
        let inv_rhs = rhs.inverse().ok_or_else(|| {
            "Division by zero or \
                 non-invertible \
                 element."
                .to_string()
        })?;

        self.mul(inv_rhs)
    }
}

impl Neg for ExtensionFieldElement {
    type Output = Self;

    fn neg(self) -> Self {
        let zero_poly = FiniteFieldPolynomial::new(&[], self.poly.field.clone());

        Self::new(zero_poly - self.poly, self.field)
    }
}