refactor fast primitive root test

This commit is contained in:
filifa 2025-08-21 20:01:32 -04:00
parent 3c1ade74aa
commit e1dedd9c33
1 changed files with 15 additions and 13 deletions

View File

@ -159,22 +159,24 @@ func PrimitiveRootFast(modulus *big.Int, tpf map[string]*big.Int) (*big.Int, err
continue
}
isPrimitive := true
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 {
isPrimitive = false
break
}
}
if isPrimitive {
if isPrimitiveRoot(g, modulus, phi, tpf) {
return g, nil
}
}
return nil, errors.New("no primitive root")
}
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
}