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) }