Skip to content

Commit

Permalink
sagemathgh-36457: check coprimality of moduli in CRT_basis()
Browse files Browse the repository at this point in the history
    
The `xgcd()` calls used in this function to compute modular inverses
already compute the greatest common divisor as a byproduct, so we might
as well add the cheap `.is_one()` check. Benchmarking reveals no
measurable timing difference between the two versions.
    
URL: sagemath#36457
Reported by: Lorenz Panny
Reviewer(s): Kwankyu Lee
  • Loading branch information
Release Manager committed Dec 3, 2023
2 parents 92d129e + 9a4a464 commit cfcb850
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/sage/arith/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3584,10 +3584,6 @@ def CRT_basis(moduli):
`a_i` is congruent to 1 modulo `m_i` and to 0 modulo `m_j` for
`j\not=i`.
.. note::
The pairwise coprimality of the input is not checked.
EXAMPLES::
sage: a1 = ZZ(mod(42,5))
Expand All @@ -3610,7 +3606,14 @@ def CRT_basis(moduli):
if n == 0:
return []
M = prod(moduli)
return [((xgcd(m,M//m)[2])*(M//m)) % M for m in moduli]
cs = []
for m in moduli:
Mm = M // m
d, _, v = xgcd(m, Mm)
if not d.is_one():
raise ValueError('moduli must be coprime')
cs.append((v * Mm) % M)
return cs


def CRT_vectors(X, moduli):
Expand Down

0 comments on commit cfcb850

Please sign in to comment.