From 117b046be824b59a4b1ba82485cfa5a866579038 Mon Sep 17 00:00:00 2001 From: filifa Date: Tue, 19 Aug 2025 22:40:33 -0400 Subject: [PATCH] refactor into multiplicativeOrder function --- cmd/primitiveRoot.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cmd/primitiveRoot.go b/cmd/primitiveRoot.go index 04f969b..4f65cba 100644 --- a/cmd/primitiveRoot.go +++ b/cmd/primitiveRoot.go @@ -40,6 +40,17 @@ func totient(n *big.Int) *big.Int { return total } +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 computeNaive(modulus *big.Int) (*big.Int, error) { if modulus.Cmp(big.NewInt(1)) == 0 { return big.NewInt(0), nil @@ -53,14 +64,8 @@ func computeNaive(modulus *big.Int) (*big.Int, error) { continue } - 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) - } - - if k.Cmp(phi) == 0 { + order := multiplicativeOrder(g, modulus) + if order.Cmp(phi) == 0 { return g, nil } }