mathtools/cmd/discreteLog.go

100 lines
3.1 KiB
Go
Raw Permalink Normal View History

2025-09-10 04:36:40 +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"
"math/big"
"github.com/spf13/cobra"
2025-09-30 01:52:52 +00:00
"scm.dairydemon.net/filifa/mathtools/internal/lib"
2025-09-10 04:36:40 +00:00
)
2025-09-10 22:56:03 +00:00
var discreteLogModulus string
2025-09-11 01:02:27 +00:00
var discreteLogBase string
2025-09-10 04:36:40 +00:00
var discreteLogElement string
2025-09-11 00:25:40 +00:00
var discreteLogOrder string
2025-09-10 04:36:40 +00:00
func discreteLog(cmd *cobra.Command, args []string) {
2025-09-11 02:10:01 +00:00
m, ok := new(big.Int).SetString(discreteLogModulus, 10)
2025-09-10 04:36:40 +00:00
if !ok {
2025-09-10 22:56:03 +00:00
cobra.CheckErr("invalid modulus " + discreteLogModulus)
2025-09-10 04:36:40 +00:00
}
2025-09-11 02:10:01 +00:00
b, ok := new(big.Int).SetString(discreteLogBase, 10)
2025-09-10 04:36:40 +00:00
if !ok {
2025-09-11 01:02:27 +00:00
cobra.CheckErr("invalid base " + discreteLogBase)
2025-09-10 04:36:40 +00:00
}
x, ok := new(big.Int).SetString(discreteLogElement, 10)
if !ok {
cobra.CheckErr("invalid element " + discreteLogElement)
}
2025-09-11 00:25:40 +00:00
var order *big.Int
if discreteLogOrder != "" {
order, ok = new(big.Int).SetString(discreteLogOrder, 10)
if !ok {
cobra.CheckErr("invalid order " + discreteLogOrder)
}
}
2025-09-30 01:52:52 +00:00
k, err := lib.BabyStepGiantStep(m, b, x, order)
2025-09-11 00:50:11 +00:00
if err != nil {
cobra.CheckErr(err)
}
2025-09-10 04:36:40 +00:00
fmt.Println(k)
}
// discreteLogCmd represents the discreteLog command
var discreteLogCmd = &cobra.Command{
2025-09-18 01:02:05 +00:00
Use: "discrete-log -b N -m N -e N",
2025-09-10 04:36:40 +00:00
Short: "Compute the discrete logarithm",
2025-09-18 01:02:05 +00:00
Long: `Compute the discrete logarithm.
Given a base b, modulus m, and element e, compute a value k such that b^k = e (mod m).
Note that no efficient method of finding the discrete logarithm is currently known. For slightly improved performance, the order of the group (i.e. the totient of m) can be provided.`,
Run: discreteLog,
2025-09-10 04:36:40 +00:00
}
func init() {
rootCmd.AddCommand(discreteLogCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// discreteLogCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// discreteLogCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
2025-09-10 22:56:03 +00:00
discreteLogCmd.Flags().StringVarP(&discreteLogModulus, "modulus", "m", "", "modulus of the cyclic group")
discreteLogCmd.MarkFlagRequired("modulus")
2025-09-10 04:36:40 +00:00
2025-09-11 01:02:27 +00:00
discreteLogCmd.Flags().StringVarP(&discreteLogBase, "base", "b", "", "logarithmic base")
discreteLogCmd.MarkFlagRequired("base")
2025-09-10 04:36:40 +00:00
discreteLogCmd.Flags().StringVarP(&discreteLogElement, "element", "e", "", "element of the cyclic group to compute logarithm of")
discreteLogCmd.MarkFlagRequired("element")
2025-09-11 00:25:40 +00:00
discreteLogCmd.Flags().StringVar(&discreteLogOrder, "order", "", "order of the cyclic group")
2025-09-10 04:36:40 +00:00
}