From 48cc045e80c30d18300410f6407fa255a3ea25ac Mon Sep 17 00:00:00 2001 From: filifa Date: Wed, 10 Sep 2025 20:50:11 -0400 Subject: [PATCH] return error if no solution found --- cmd/discreteLog.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/discreteLog.go b/cmd/discreteLog.go index 8a1f98d..f025a86 100644 --- a/cmd/discreteLog.go +++ b/cmd/discreteLog.go @@ -17,6 +17,7 @@ along with this program. If not, see . package cmd import ( + "errors" "fmt" "math/big" @@ -39,7 +40,7 @@ func ceilSqrt(x *big.Int) *big.Int { return z } -func babyStepGiantStep(n, g, x, order *big.Int) *big.Int { +func babyStepGiantStep(n, g, x, order *big.Int) (*big.Int, error) { var m *big.Int if order == nil { // m = ceil(sqrt(n - 1)) @@ -67,15 +68,14 @@ func babyStepGiantStep(n, g, x, order *big.Int) *big.Int { if ok { i.Mul(i, m) i.Add(i, j) - return i + return i, nil } gamma.Mul(gamma, p) gamma.Mod(gamma, n) } - // TODO: return an error instead - return nil + return nil, errors.New("no solution") } func discreteLog(cmd *cobra.Command, args []string) { @@ -102,7 +102,11 @@ func discreteLog(cmd *cobra.Command, args []string) { } } - k := babyStepGiantStep(n, g, x, order) + k, err := babyStepGiantStep(n, g, x, order) + if err != nil { + cobra.CheckErr(err) + } + fmt.Println(k) }