/* 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 . */ package cmd import ( "fmt" "log" "math/big" "github.com/spf13/cobra" "scm.dairydemon.net/filifa/mathtools/internal/lib" ) 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 { root, err = lib.PrimitiveRoot(m) if err != nil { log.Fatal(err) } } else { root, err = lib.PrimitiveRootFast(m, tpf) if err != nil { log.Fatal(err) } } fmt.Println(root) } // primitiveRootCmd represents the primitiveRoot command var primitiveRootCmd = &cobra.Command{ Use: "primitive-root -m M", Short: "Compute a primitive root modulo n", 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. 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.`, Run: primitiveRoot, } 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") // TODO: add a check flag for verifying -t input is the totient, test for performance }