From e1dedd9c33d06e7ad9d84a7d0d8e22e67ed77240 Mon Sep 17 00:00:00 2001 From: filifa Date: Thu, 21 Aug 2025 20:01:32 -0400 Subject: [PATCH] refactor fast primitive root test --- internal/lib/lib.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) 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 +}