mathtools/internal/lib/lib.go

183 lines
3.5 KiB
Go
Raw Normal View History

2025-08-21 22:58:43 +00:00
package lib
import (
"errors"
"math/big"
)
func SqrtRepetend(x *big.Int) ([]*big.Int, error) {
m := big.NewInt(0)
d := big.NewInt(1)
a0 := new(big.Int).Sqrt(x)
s := new(big.Int).Exp(a0, big.NewInt(2), nil)
if x.Cmp(s) == 0 {
return nil, errors.New("input is a perfect square")
}
repetend := make([]*big.Int, 0)
a := new(big.Int).Set(a0)
twoa0 := new(big.Int).Mul(big.NewInt(2), a0)
for a.Cmp(twoa0) != 0 {
// m = d * a - m
tmp := new(big.Int)
m.Sub(tmp.Mul(d, a), m)
// d = (x - m^2) // d
tmp.Exp(m, big.NewInt(2), nil)
d.Div(tmp.Sub(x, tmp), d)
// a = (a0 + m) // d
a.Div(tmp.Add(a0, m), d)
repetend = append(repetend, new(big.Int).Set(a))
}
return repetend, nil
}
2025-08-21 23:01:22 +00:00
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
}
2025-08-21 23:20:36 +00:00
func Totient(n *big.Int) *big.Int {
N := new(big.Int).Set(n)
phi := new(big.Int).Set(N)
sqrtn := new(big.Int).Sqrt(N)
for i := big.NewInt(2); i.Cmp(sqrtn) != 1; i.Add(i, big.NewInt(1)) {
mod := new(big.Int).Mod(N, i)
if mod.Cmp(big.NewInt(0)) != 0 {
continue
}
// phi -= phi // i
tmp := new(big.Int).Div(phi, i)
phi.Sub(phi, tmp)
for mod.Cmp(big.NewInt(0)) == 0 {
N.Div(N, i)
mod.Mod(N, i)
}
}
if N.Cmp(big.NewInt(1)) == 1 {
// phi -= phi // N
tmp := new(big.Int).Div(phi, N)
phi.Sub(phi, tmp)
}
return phi
}
func MultiplicativeOrder(g *big.Int, modulus *big.Int) *big.Int {
e := new(big.Int).Set(g)
var k *big.Int
for k = big.NewInt(1); e.Cmp(big.NewInt(1)) != 0; k.Add(k, big.NewInt(1)) {
e.Mul(e, g)
e.Mod(e, modulus)
}
return k
}
func PrimitiveRoot(modulus *big.Int) (*big.Int, error) {
if modulus.Cmp(big.NewInt(1)) == 0 {
return big.NewInt(0), nil
}
phi := Totient(modulus)
for g := big.NewInt(1); g.Cmp(modulus) == -1; g.Add(g, big.NewInt(1)) {
gcd := new(big.Int).GCD(nil, nil, g, modulus)
if gcd.Cmp(big.NewInt(1)) != 0 {
continue
}
order := MultiplicativeOrder(g, modulus)
if order.Cmp(phi) == 0 {
return g, nil
}
}
return nil, errors.New("no primitive root")
}
func PrimitiveRootFast(modulus *big.Int, tpf map[string]*big.Int) (*big.Int, error) {
2025-08-21 23:20:36 +00:00
phi := big.NewInt(1)
for p, exp := range tpf {
pow, ok := new(big.Int).SetString(p, 10)
2025-08-21 23:20:36 +00:00
if !ok {
return nil, errors.New("invalid factor " + p)
2025-08-21 23:20:36 +00:00
}
pow.Exp(pow, exp, nil)
phi.Mul(phi, pow)
2025-08-21 23:20:36 +00:00
}
for g := big.NewInt(1); g.Cmp(modulus) == -1; g.Add(g, big.NewInt(1)) {
gcd := new(big.Int).GCD(nil, nil, g, modulus)
if gcd.Cmp(big.NewInt(1)) != 0 {
continue
}
2025-08-22 00:01:32 +00:00
if isPrimitiveRoot(g, modulus, phi, tpf) {
2025-08-21 23:20:36 +00:00
return g, nil
}
}
return nil, errors.New("no primitive root")
}
2025-08-22 00:01:32 +00:00
func isPrimitiveRoot(g *big.Int, modulus *big.Int, phi *big.Int, tpf map[string]*big.Int) bool {
for p := range tpf {
// we already know factors are valid from computing phi
k, _ := new(big.Int).SetString(p, 10)
k.Div(phi, k)
k.Exp(g, k, modulus)
if k.Cmp(big.NewInt(1)) == 0 {
return false
}
}
return true
}