return error if no solution found

This commit is contained in:
filifa 2025-09-10 20:50:11 -04:00
parent 11fa36071f
commit 48cc045e80
1 changed files with 9 additions and 5 deletions

View File

@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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)
}