mathtools/cmd/primitiveRoot.go

87 lines
2.7 KiB
Go
Raw Normal View History

2025-08-20 01:37:38 +00:00
/*
Copyright © 2025 filifa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"fmt"
"log"
"math/big"
"github.com/spf13/cobra"
2025-08-21 23:20:36 +00:00
"scm.dairydemon.net/filifa/mathtools/internal/lib"
2025-08-20 01:37:38 +00:00
)
var modulus string
var tpf []string
func primitiveRoot(cmd *cobra.Command, args []string) {
m, ok := new(big.Int).SetString(modulus, 10)
if !ok {
log.Fatal("invalid input " + modulus)
}
root := new(big.Int)
var err error
if len(tpf) == 0 {
2025-08-21 23:20:36 +00:00
root, err = lib.PrimitiveRoot(m)
2025-08-20 01:37:38 +00:00
if err != nil {
log.Fatal(err)
}
} else {
2025-08-21 23:20:36 +00:00
root, err = lib.PrimitiveRootFast(m, tpf)
2025-08-20 04:51:51 +00:00
if err != nil {
log.Fatal(err)
}
2025-08-20 01:37:38 +00:00
}
fmt.Println(root)
}
// primitiveRootCmd represents the primitiveRoot command
var primitiveRootCmd = &cobra.Command{
Use: "primitive-root -m M",
Short: "Compute a primitive root modulo n",
2025-08-20 04:56:56 +00:00
Long: `Compute a primitive root modulo n.
This command computes a value g such that, for all integers a coprime to n, g^k = a (mod n) for some k. In other words, this command computes a generator for the multiplicative group of integers modulo n.
2025-08-21 03:05:08 +00:00
For improved performance, provide the prime factorization of the totient of n with the -t flag.
Note that primitive roots only exist for the moduli 1, 2, 4, p^k, and 2p^k, where p is an odd prime. The totients of these numbers are 1, 1, 2, (p-1)*p^(k-1), and (p-1)*p^(k-1), respectively.`,
2025-08-20 04:56:56 +00:00
Run: primitiveRoot,
2025-08-20 01:37:38 +00:00
}
func init() {
rootCmd.AddCommand(primitiveRootCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// primitiveRootCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// primitiveRootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
primitiveRootCmd.Flags().StringVarP(&modulus, "modulus", "m", "", "modulus")
primitiveRootCmd.MarkFlagRequired("modulus")
primitiveRootCmd.Flags().StringSliceVarP(&tpf, "totient-factorization", "t", make([]string, 0), "prime factorization of the totient of the modulus")
2025-08-21 03:05:08 +00:00
// TODO: add a check flag for verifying -t input is the totient, test for performance
2025-08-20 01:37:38 +00:00
}