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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
//! # Polynomial Factorization over Finite Fields
//!
//! This module provides implementations of algorithms for factoring polynomials
//! over finite fields. It includes Berlekamp's algorithm for small fields,
//! Cantor-Zassenhaus algorithm for larger fields, and square-free factorization.
//! It also contains a simplified approach to Berlekamp-Zassenhaus for integer polynomials.

use std::sync::Arc;

use itertools::Itertools;
use num_bigint::BigInt;
use num_traits::One;
use num_traits::ToPrimitive;
use num_traits::Zero;
use rand_v10 as rand;

use crate::numerical::matrix::Matrix;
use crate::symbolic::finite_field::FiniteFieldPolynomial;
use crate::symbolic::finite_field::PrimeField;
use crate::symbolic::finite_field::PrimeFieldElement;

/// Factors a polynomial over a finite field.
///
/// This function acts as a dispatcher, choosing between Berlekamp's algorithm
/// (for small fields) and Cantor-Zassenhaus algorithm (for larger fields)
/// based on the size of the field's modulus.
///
/// # Arguments
/// * `poly` - The polynomial to factor, represented as a `FiniteFieldPolynomial`.
///
/// # Returns
/// A `Vec<FiniteFieldPolynomial>` containing the irreducible factors of the polynomial.
///
/// # Errors
///
/// This function will return an error if the chosen factorization algorithm
/// (Berlekamp or Cantor-Zassenhaus) encounters an error.
pub fn factor_gf(poly: &FiniteFieldPolynomial) -> Result<Vec<FiniteFieldPolynomial>, String> {
    if poly.field.modulus.to_u64().unwrap_or(u64::MAX) < 50 {
        berlekamp_factorization(poly)
    } else {
        cantor_zassenhaus(poly)
    }
}

/// Computes the derivative of a polynomial over a prime field.
///
/// This function applies the standard power rule for differentiation to each term
/// of the polynomial, with arithmetic performed modulo the field's characteristic.
///
/// # Arguments
/// * `p` - The polynomial to differentiate.
///
/// # Returns
/// A new `FiniteFieldPolynomial` representing the derivative.
#[must_use]
pub fn poly_derivative_gf(p: &FiniteFieldPolynomial) -> FiniteFieldPolynomial {
    if p.coeffs.is_empty() {
        return FiniteFieldPolynomial::new(&[], p.field.clone());
    }

    let mut deriv_coeffs = Vec::with_capacity(p.coeffs.len().saturating_sub(1));

    let degree = p.degree().try_into().unwrap_or(0);

    for i in 0..degree {
        let original_coeff = &p.coeffs[i];

        let power = degree - i;

        let new_coeff_val = &original_coeff.value * BigInt::from(power);

        deriv_coeffs.push(PrimeFieldElement::new(new_coeff_val, p.field.clone()));
    }

    FiniteFieldPolynomial::new(&deriv_coeffs, p.field.clone())
}

/// Performs square-free factorization of a polynomial over a prime field.
///
/// This algorithm decomposes a polynomial `f(x)` into a product of powers of distinct
/// irreducible polynomials: `f(x) = f_1(x) * f_2(x)^2 * ... * f_k(x)^k`.
/// It is a preprocessing step for many factorization algorithms.
///
/// # Arguments
/// * `f` - The polynomial to factorize.
///
/// # Returns
/// A `Vec<(FiniteFieldPolynomial, usize)>` where each tuple contains a square-free
/// polynomial and its multiplicity.
///
/// # Errors
///
/// This function will return an error if `poly_gcd_gf` fails during the computation
/// of the greatest common divisor.
pub fn square_free_factorization_gf(
    f: FiniteFieldPolynomial
) -> Result<Vec<(FiniteFieldPolynomial, usize)>, String> {
    let mut factors = Vec::new();

    let mut i = 1;

    let mut f_i = f;

    while f_i.degree() > 0 {
        let f_prime = poly_derivative_gf(&f_i);

        let g = poly_gcd_gf(f_i.clone(), f_prime)?;

        let (h, _) = f_i.clone().long_division(&g.clone())?;

        if h.degree() > 0 {
            factors.push((h, i));
        }

        f_i = g;

        i += 1;
    }

    Ok(factors)
}

/// Factors a square-free polynomial over a small prime field using Berlekamp's algorithm.
///
/// Berlekamp's algorithm is a classical method for factoring polynomials over finite fields.
/// It constructs a matrix (Berlekamp matrix) whose null space provides information about
/// the factors. The algorithm then uses GCD computations to split the polynomial.
///
/// # Arguments
/// * `f` - The square-free polynomial to factor.
///
/// # Returns
/// A `Vec<FiniteFieldPolynomial>` containing the irreducible factors.
///
/// # Errors
///
/// This function will return an error if:
/// - The field modulus is too large to fit in `u64`.
/// - `poly_pow_mod` fails to compute `x^(p^i) mod f(x)`.
/// - `Matrix::null_space` fails to compute the null space of the Berlekamp matrix.
/// - `poly_gcd_gf` fails during factor splitting.
pub fn berlekamp_factorization(
    f: &FiniteFieldPolynomial
) -> Result<Vec<FiniteFieldPolynomial>, String> {
    let p_val = match f.field.modulus.to_u64() {
        | Some(val) => val,
        | None => {
            return Err("Modulus is too large \
                 for Berlekamp \
                 factorization."
                .to_string());
        },
    };

    let n = f.degree().try_into().unwrap_or(0);

    if n <= 1 {
        return Ok(vec![f.clone()]);
    }

    let mut q_data = Vec::new();

    let x_poly = FiniteFieldPolynomial::new(
        &[
            PrimeFieldElement::new(One::one(), f.field.clone()),
            PrimeFieldElement::new(Zero::zero(), f.field.clone()),
        ],
        f.field.clone(),
    );

    for i in 0..n {
        let exp = BigInt::from(p_val).pow(i as u32);

        let x_pow_mod_f = poly_pow_mod(x_poly.clone(), &exp, f)?;

        let mut row = vec![PrimeFieldElement::new(Zero::zero(), f.field.clone()); n];

        let offset = n.saturating_sub(x_pow_mod_f.coeffs.len());

        for (j, coeff) in x_pow_mod_f.coeffs.iter().enumerate() {
            row[offset + j] = coeff.clone();
        }

        q_data.extend(row);
    }

    let mut q_matrix = Matrix::new(n, n, q_data);

    for i in 0..n {
        let val = q_matrix.get(i, i).clone();

        *q_matrix.get_mut(i, i) = val - PrimeFieldElement::new(One::one(), f.field.clone());
    }

    let null_space_matrix = q_matrix.null_space();

    let basis_vectors = null_space_matrix?.get_cols();

    let r = basis_vectors.len();

    if r == 1 {
        return Ok(vec![f.clone()]);
    }

    let mut factors = vec![f.clone()];

    for v_coeffs in basis_vectors.iter().skip(1) {
        let v = FiniteFieldPolynomial::new(&v_coeffs.clone(), f.field.clone());

        let mut new_factors = Vec::new();

        for s in 0..p_val {
            let s_elem = PrimeFieldElement::new(BigInt::from(s), f.field.clone());

            let v_minus_s = v.clone() - FiniteFieldPolynomial::new(&[s_elem], f.field.clone());

            for factor in &factors {
                let h = poly_gcd_gf(factor.clone(), v_minus_s.clone())?;

                if h.degree() > 0 && h.degree() < factor.degree() {
                    new_factors.push(h.clone());

                    let (quotient, _) = factor.clone().long_division(&h)?;

                    new_factors.push(quotient);
                } else {
                    new_factors.push(factor.clone());
                }
            }

            factors = new_factors;

            new_factors = Vec::new();

            if factors.len() == r {
                break;
            }
        }

        if factors.len() == r {
            break;
        }
    }

    Ok(factors)
}

/// Factors a polynomial with integer coefficients using the Berlekamp-Zassenhaus algorithm.
///
/// This algorithm combines modular factorization (using Berlekamp's algorithm over `GF(p)`),
/// Hensel lifting (to lift factors from `GF(p)` to `GF(p^k)`), and recombination techniques
/// to find factors over the integers.
///
/// **Note**: This is a simplified implementation. A full implementation would handle
/// content, leading coefficients, square-free factorization, and more robust Hensel lifting.
///
/// # Arguments
/// * `poly` - The polynomial to factor, assumed to have integer coefficients.
///
/// # Returns
/// A `Vec<FiniteFieldPolynomial>` containing the factors over the integers.
///
/// # Errors
///
/// This function will return an error if:
/// - `berlekamp_factorization` fails during the modular factorization step.
/// - `poly_long_division` fails during the division of the remaining polynomial.
pub fn berlekamp_zassenhaus(
    poly: &FiniteFieldPolynomial
) -> Result<Vec<FiniteFieldPolynomial>, String> {
    let p = BigInt::from(5);

    let field = PrimeField::new(p.clone());

    let f_mod_p = poly_with_field(poly, field);

    let factors_mod_p = berlekamp_factorization(&f_mod_p)?;

    if factors_mod_p.len() <= 1 {
        return Ok(vec![poly.clone()]);
    }

    let g_mod_p = factors_mod_p[0].clone();

    let h_mod_p = factors_mod_p.iter().skip(1).fold(
        FiniteFieldPolynomial::new(
            &[PrimeFieldElement::new(One::one(), f_mod_p.field.clone())],
            f_mod_p.field,
        ),
        |acc, factor| acc * factor.clone(),
    );

    let k = 4;

    let (g_lifted, _h_lifted) = match hensel_lift(poly, &g_mod_p, &h_mod_p, &p, k) {
        | Some((g, h)) => (g, h),
        | None => {
            return Ok(vec![poly.clone()]);
        },
    };

    let mut true_factors = Vec::new();

    let mut remaining_poly = poly.clone();

    let lifted_factors = [g_lifted];

    for i in 1..=lifted_factors.len() {
        for subset in lifted_factors.iter().combinations(i) {
            let mut potential_factor = FiniteFieldPolynomial::new(
                &[PrimeFieldElement::new(One::one(), poly.field.clone())],
                poly.field.clone(),
            );

            for factor in subset {
                potential_factor = potential_factor * factor.clone();
            }

            let p_k = p.pow(k);

            let p_k_half = &p_k / 2;

            let centered_coeffs = potential_factor
                .coeffs
                .into_iter()
                .map(|c| {
                    let mut val = c.value;

                    if val > p_k_half {
                        val -= &p_k;
                    }

                    PrimeFieldElement::new(val, poly.field.clone())
                })
                .collect::<Vec<PrimeFieldElement>>();

            let centered_factor = FiniteFieldPolynomial::new(&centered_coeffs, poly.field.clone());

            let (quotient, remainder) = remaining_poly
                .clone()
                .long_division(&centered_factor.clone())?;

            if remainder.coeffs.is_empty() || remainder.coeffs.iter().all(|c| c.value.is_zero()) {
                true_factors.push(centered_factor);

                remaining_poly = quotient;
            }
        }
    }

    if !remaining_poly.coeffs.is_empty() {
        true_factors.push(remaining_poly);
    }

    Ok(true_factors)
}

/// Lifts a factorization f ≡ g*h (mod p) to a factorization f ≡ `g_k`*`h_k` (mod p^k).
pub(crate) fn hensel_lift(
    f: &FiniteFieldPolynomial,
    g: &FiniteFieldPolynomial,
    h: &FiniteFieldPolynomial,
    p: &BigInt,
    k: u32,
) -> Option<(FiniteFieldPolynomial, FiniteFieldPolynomial)> {
    let mut g_i = g.clone();

    let mut h_i = h.clone();

    let mut current_p = p.clone();

    for _ in 0..=k.ilog2() {
        let field = PrimeField::new(current_p.clone());

        let f_mod_pi = poly_with_field(f, field.clone());

        let g_i_mod_pi = poly_with_field(&g_i, field.clone());

        let h_i_mod_pi = poly_with_field(&h_i, field.clone());

        let e = f_mod_pi - (g_i_mod_pi.clone() * h_i_mod_pi.clone());

        if e.coeffs.is_empty() {
            current_p = &current_p * &current_p;

            continue;
        }

        let e_prime_coeffs = e
            .coeffs
            .into_iter()
            .map(|c| PrimeFieldElement::new(c.value / &current_p, field.clone()))
            .collect::<Vec<PrimeFieldElement>>();

        let e_prime = FiniteFieldPolynomial::new(&e_prime_coeffs, field.clone());

        let (gcd, s, t) = poly_extended_gcd(g_i_mod_pi.clone(), h_i_mod_pi.clone()).ok()?;

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

        let (_, d_h) = (s * e_prime.clone())
            .long_division(&h_i_mod_pi.clone())
            .ok()?;

        let (_, d_g) = (t * e_prime).long_division(&g_i_mod_pi).ok()?;

        g_i = g_i + poly_mul_scalar(&d_g, &current_p);

        h_i = h_i + poly_mul_scalar(&d_h, &current_p);

        current_p = &current_p * &current_p;
    }

    Some((g_i, h_i))
}

/// Factors a square-free polynomial over a large prime field using Cantor-Zassenhaus algorithm.
///
/// The Cantor-Zassenhaus algorithm is a probabilistic algorithm for factoring polynomials
/// over finite fields. It relies on distinct-degree factorization and equal-degree splitting.
///
/// # Arguments
/// * `f` - The square-free polynomial to factor.
///
/// # Returns
/// A `Vec<FiniteFieldPolynomial>` containing the irreducible factors.
///
/// # Errors
///
/// This function will return an error if `distinct_degree_factorization` or
/// `equal_degree_splitting` fail during their respective factorization steps.
pub fn cantor_zassenhaus(f: &FiniteFieldPolynomial) -> Result<Vec<FiniteFieldPolynomial>, String> {
    let ddf_factors = distinct_degree_factorization(f)?;

    let mut final_factors = Vec::new();

    for (poly_product, degree) in ddf_factors {
        if poly_product.degree().try_into().unwrap_or(0) == degree {
            final_factors.push(poly_product);
        } else {
            let mut split_factors = equal_degree_splitting(&poly_product, degree)?;

            final_factors.append(&mut split_factors);
        }
    }

    Ok(final_factors)
}

/// Performs Distinct-Degree Factorization (DDF) of a polynomial over a finite field.
///
/// DDF groups irreducible factors by their degree. It uses the property that
/// `x^(p^d) - x` is the product of all monic irreducible polynomials over `GF(p)`
/// whose degree divides `d`.
///
/// # Arguments
/// * `f` - The polynomial to factor.
///
/// # Returns
/// A `Vec<(FiniteFieldPolynomial, usize)>` where each tuple contains a polynomial
/// (which is a product of irreducible factors of a certain degree) and that degree.
///
/// # Errors
///
/// This function will return an error if `poly_pow_mod` or `poly_gcd_gf` fail
/// during the factorization process.
pub fn distinct_degree_factorization(
    f: &FiniteFieldPolynomial
) -> Result<Vec<(FiniteFieldPolynomial, usize)>, String> {
    let mut factors = Vec::new();

    let p = &f.field.modulus;

    let x = FiniteFieldPolynomial::new(
        &[
            PrimeFieldElement::new(One::one(), f.field.clone()),
            PrimeFieldElement::new(Zero::zero(), f.field.clone()),
        ],
        f.field.clone(),
    );

    let mut h = x.clone();

    let mut f_star = f.clone();

    let mut d = 1;

    while f_star.degree() >= 2 * (d as isize) {
        h = poly_pow_mod(h.clone(), p, &f_star)?;

        let g_d = poly_gcd_gf(f_star.clone(), h.clone() - x.clone())?;

        if g_d.degree() > 0 {
            factors.push((g_d.clone(), d));

            let (quotient, _) = f_star.long_division(&g_d)?;

            f_star = quotient;
        }

        d += 1;
    }

    if f_star.degree() > 0 {
        factors.push((f_star.clone(), f_star.degree().try_into().unwrap_or(0)));
    }

    Ok(factors)
}

/// Performs Equal-Degree Splitting.
///
/// # Errors
///
/// This function will return an error if `poly_pow_mod` or `poly_gcd_gf` fail
/// during the splitting process.
pub(crate) fn equal_degree_splitting(
    f: &FiniteFieldPolynomial,
    d: usize,
) -> Result<Vec<FiniteFieldPolynomial>, String> {
    if f.degree().try_into().unwrap_or(0) == d {
        return Ok(vec![f.clone()]);
    }

    let mut factors = vec![f.clone()];

    let mut result = Vec::new();

    while let Some(current_f) = factors.pop() {
        if (current_f.degree().try_into().unwrap_or(0)) == d {
            result.push(current_f);

            continue;
        }

        let p = &current_f.field.modulus;

        let exp = (p.pow(d as u32) - BigInt::one()) / 2;

        loop {
            let a = random_poly(
                (current_f.degree().try_into().unwrap_or(0_usize)).saturating_sub(1),
                current_f.field.clone(),
            );

            let b = poly_pow_mod(a, &exp, &current_f)?
                - FiniteFieldPolynomial::new(
                    &[PrimeFieldElement::new(One::one(), current_f.field.clone())],
                    current_f.field.clone(),
                );

            let g = poly_gcd_gf(current_f.clone(), b)?;

            if g.degree() > 0 && g.degree() < current_f.degree() {
                factors.push(g.clone());

                let (quotient, _) = current_f.long_division(&g)?;

                factors.push(quotient);

                break;
            }
        }
    }

    Ok(result)
}

/// Generates a random monic polynomial of a given degree.
pub(crate) fn random_poly(
    degree: usize,
    field: Arc<PrimeField>,
) -> FiniteFieldPolynomial {
    let mut coeffs = Vec::with_capacity(degree + 1);

    coeffs.push(PrimeFieldElement::new(One::one(), field.clone()));

    for _ in 0..degree {
        let random_val = BigInt::from(rand::random::<u64>()) % &field.modulus;

        coeffs.push(PrimeFieldElement::new(random_val, field.clone()));
    }

    FiniteFieldPolynomial::new(&coeffs, field)
}

/// Computes the greatest common divisor (GCD) of two polynomials over a prime field.
///
/// # Errors
///
/// This function will return an error if `long_division` fails during the Euclidean algorithm.
pub fn poly_gcd_gf(
    a: FiniteFieldPolynomial,
    b: FiniteFieldPolynomial,
) -> Result<FiniteFieldPolynomial, String> {
    if b.coeffs.is_empty() || b.coeffs.iter().all(|c| c.value.is_zero()) {
        Ok(a)
    } else {
        let (_, remainder) = a.long_division(&b)?;

        poly_gcd_gf(b, remainder)
    }
}

/// Computes base^exp mod modulus for polynomials over a prime field.
///
/// # Errors
///
/// This function will return an error if `long_division` fails during the modular exponentiation.
pub fn poly_pow_mod(
    base: FiniteFieldPolynomial,
    exp: &BigInt,
    modulus: &FiniteFieldPolynomial,
) -> Result<FiniteFieldPolynomial, String> {
    let mut res = FiniteFieldPolynomial::new(
        &[PrimeFieldElement::new(One::one(), base.field.clone())],
        base.field.clone(),
    );

    let mut b = base;

    let mut e = exp.clone();

    while e > Zero::zero() {
        if &e % 2 == One::one() {
            let (_, remainder) = (res * b.clone()).long_division(&modulus.clone())?;

            res = remainder;
        }

        let (_, remainder) = (b.clone() * b.clone()).long_division(&modulus.clone())?;

        b = remainder;

        e >>= 1;
    }

    Ok(res)
}

/// Helper to multiply a polynomial by a scalar `BigInt`.
#[must_use]
pub fn poly_mul_scalar(
    poly: &FiniteFieldPolynomial,
    scalar: &BigInt,
) -> FiniteFieldPolynomial {
    let new_coeffs = poly
        .coeffs
        .iter()
        .map(|c| PrimeFieldElement::new(&c.value * scalar, c.field.clone()))
        .collect::<Vec<PrimeFieldElement>>();

    FiniteFieldPolynomial::new(&new_coeffs, poly.field.clone())
}

/// Helper to change the field of a polynomial's coefficients.
pub(crate) fn poly_with_field(
    poly: &FiniteFieldPolynomial,
    field: Arc<PrimeField>,
) -> FiniteFieldPolynomial {
    let new_coeffs = poly
        .coeffs
        .iter()
        .map(|c| PrimeFieldElement::new(c.value.clone(), field.clone()))
        .collect::<Vec<PrimeFieldElement>>();

    FiniteFieldPolynomial::new(&new_coeffs, field)
}

/// Polynomial Extended Euclidean Algorithm for `a(x)s(x) + b(x)t(x) = gcd(a(x), b(x))`.
///
/// # Errors
///
/// This function will return an error if the underlying polynomial long division fails.
pub 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))
}