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 (
|
2025-09-11 00:50:11 +00:00
|
|
|
"errors"
|
2025-09-10 04:36:40 +00:00
|
|
|
"fmt"
|
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// whyyyy doesn't math/big have a ceil functionnnn
|
|
|
|
|
func ceilSqrt(x *big.Int) *big.Int {
|
|
|
|
|
z := new(big.Int).Sqrt(x)
|
|
|
|
|
s := new(big.Int).Exp(z, big.NewInt(2), nil)
|
|
|
|
|
if s.Cmp(x) != 0 {
|
|
|
|
|
z.Add(z, big.NewInt(1))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return z
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-11 02:10:01 +00:00
|
|
|
// TODO: this can be extended to work with n, b not coprime
|
|
|
|
|
// https://cp-algorithms.com/algebra/discrete-log.html
|
|
|
|
|
func babyStepGiantStep(n, b, x, order *big.Int) (*big.Int, error) {
|
|
|
|
|
z := new(big.Int).GCD(nil, nil, b, n)
|
2025-09-11 01:23:26 +00:00
|
|
|
if z.Cmp(big.NewInt(1)) != 0 {
|
2025-09-11 02:10:01 +00:00
|
|
|
return nil, fmt.Errorf("base %v is not invertible modulo %v", b, n)
|
2025-09-11 01:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-11 00:25:40 +00:00
|
|
|
var m *big.Int
|
|
|
|
|
if order == nil {
|
|
|
|
|
// m = ceil(sqrt(n - 1))
|
|
|
|
|
z := big.NewInt(1)
|
|
|
|
|
z.Sub(n, z)
|
|
|
|
|
m = ceilSqrt(z)
|
|
|
|
|
} else {
|
|
|
|
|
m = ceilSqrt(order)
|
|
|
|
|
}
|
2025-09-10 04:36:40 +00:00
|
|
|
|
|
|
|
|
table := make(map[string]*big.Int)
|
2025-09-10 22:54:01 +00:00
|
|
|
for j := big.NewInt(1); j.Cmp(m) <= 0; j.Add(j, big.NewInt(1)) {
|
2025-09-11 02:10:01 +00:00
|
|
|
a := new(big.Int).Exp(b, j, n)
|
2025-09-10 04:36:40 +00:00
|
|
|
table[a.String()] = new(big.Int).Set(j)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-11 02:10:01 +00:00
|
|
|
// p = b^-m modulo n
|
2025-09-10 04:36:40 +00:00
|
|
|
p := new(big.Int).Neg(m)
|
2025-09-11 02:10:01 +00:00
|
|
|
p.Exp(b, p, n)
|
2025-09-10 04:36:40 +00:00
|
|
|
|
|
|
|
|
gamma := new(big.Int).Set(x)
|
|
|
|
|
|
|
|
|
|
for i := big.NewInt(0); i.Cmp(m) == -1; i.Add(i, big.NewInt(1)) {
|
|
|
|
|
j, ok := table[gamma.String()]
|
|
|
|
|
if ok {
|
|
|
|
|
i.Mul(i, m)
|
|
|
|
|
i.Add(i, j)
|
2025-09-11 00:50:11 +00:00
|
|
|
return i, nil
|
2025-09-10 04:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gamma.Mul(gamma, p)
|
|
|
|
|
gamma.Mod(gamma, n)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-11 00:50:11 +00:00
|
|
|
return nil, errors.New("no solution")
|
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-11 02:10:01 +00:00
|
|
|
k, err := 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{
|
|
|
|
|
Use: "discrete-log",
|
|
|
|
|
Short: "Compute the discrete logarithm",
|
2025-09-11 00:25:40 +00:00
|
|
|
Long: `Compute the discrete logarithm.`,
|
2025-09-10 04:36:40 +00:00
|
|
|
Run: discreteLog,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|