diff --git a/internal/lib/lib.go b/internal/lib/lib.go index fa0eab7..d1db9d8 100644 --- a/internal/lib/lib.go +++ b/internal/lib/lib.go @@ -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 +}