From b9fd3d903c6052dd8e4ce6d9185fe1fb766bad61 Mon Sep 17 00:00:00 2001 From: filifa Date: Wed, 10 Sep 2025 22:10:01 -0400 Subject: [PATCH] change variable names for consistency --- cmd/discreteLog.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/cmd/discreteLog.go b/cmd/discreteLog.go index f47c8e6..0bd405d 100644 --- a/cmd/discreteLog.go +++ b/cmd/discreteLog.go @@ -40,10 +40,12 @@ func ceilSqrt(x *big.Int) *big.Int { return z } -func babyStepGiantStep(n, g, x, order *big.Int) (*big.Int, error) { - z := new(big.Int).GCD(nil, nil, g, n) +// 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) if z.Cmp(big.NewInt(1)) != 0 { - return nil, fmt.Errorf("base %v is not invertible modulo %v", g, n) + return nil, fmt.Errorf("base %v is not invertible modulo %v", b, n) } var m *big.Int @@ -58,13 +60,13 @@ func babyStepGiantStep(n, g, x, order *big.Int) (*big.Int, error) { table := make(map[string]*big.Int) for j := big.NewInt(1); j.Cmp(m) <= 0; j.Add(j, big.NewInt(1)) { - a := new(big.Int).Exp(g, j, n) + a := new(big.Int).Exp(b, j, n) table[a.String()] = new(big.Int).Set(j) } - // p = g^-m modulo n + // p = b^-m modulo n p := new(big.Int).Neg(m) - p.Exp(g, p, n) + p.Exp(b, p, n) gamma := new(big.Int).Set(x) @@ -84,12 +86,12 @@ func babyStepGiantStep(n, g, x, order *big.Int) (*big.Int, error) { } func discreteLog(cmd *cobra.Command, args []string) { - n, ok := new(big.Int).SetString(discreteLogModulus, 10) + m, ok := new(big.Int).SetString(discreteLogModulus, 10) if !ok { cobra.CheckErr("invalid modulus " + discreteLogModulus) } - g, ok := new(big.Int).SetString(discreteLogBase, 10) + b, ok := new(big.Int).SetString(discreteLogBase, 10) if !ok { cobra.CheckErr("invalid base " + discreteLogBase) } @@ -107,7 +109,7 @@ func discreteLog(cmd *cobra.Command, args []string) { } } - k, err := babyStepGiantStep(n, g, x, order) + k, err := babyStepGiantStep(m, b, x, order) if err != nil { cobra.CheckErr(err) }