move crt solver
This commit is contained in:
@@ -36,3 +36,43 @@ func SqrtRepetend(x *big.Int) ([]*big.Int, error) {
|
||||
|
||||
return repetend, nil
|
||||
}
|
||||
|
||||
func CRTSolution(a1, n1, a2, n2 *big.Int) (*big.Int, *big.Int) {
|
||||
// use Bezout's identity to find m1, m2 such that m1*n1 + m2*n2 = 1
|
||||
m1 := new(big.Int)
|
||||
m2 := new(big.Int)
|
||||
tmp := new(big.Int)
|
||||
tmp.GCD(m1, m2, n1, n2)
|
||||
|
||||
// x = a1*m2*n2 + a2*m1*n1
|
||||
x := new(big.Int).Set(a1)
|
||||
x.Mul(x, m2)
|
||||
x.Mul(x, n2)
|
||||
|
||||
tmp.Set(a2)
|
||||
tmp.Mul(tmp, m1)
|
||||
tmp.Mul(tmp, n1)
|
||||
|
||||
x.Add(x, tmp)
|
||||
|
||||
N := new(big.Int).Set(n1)
|
||||
N.Mul(N, n2)
|
||||
|
||||
x.Mod(x, N)
|
||||
|
||||
return x, N
|
||||
}
|
||||
|
||||
func ArePairwiseCoprime(moduli []*big.Int) bool {
|
||||
z := new(big.Int)
|
||||
for i, a := range moduli {
|
||||
for _, b := range moduli[i+1:] {
|
||||
z.GCD(nil, nil, a, b)
|
||||
if z.Cmp(big.NewInt(1)) != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user