return error if no solution found
This commit is contained in:
parent
11fa36071f
commit
48cc045e80
|
|
@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
|
@ -39,7 +40,7 @@ func ceilSqrt(x *big.Int) *big.Int {
|
||||||
return z
|
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
|
var m *big.Int
|
||||||
if order == nil {
|
if order == nil {
|
||||||
// m = ceil(sqrt(n - 1))
|
// m = ceil(sqrt(n - 1))
|
||||||
|
|
@ -67,15 +68,14 @@ func babyStepGiantStep(n, g, x, order *big.Int) *big.Int {
|
||||||
if ok {
|
if ok {
|
||||||
i.Mul(i, m)
|
i.Mul(i, m)
|
||||||
i.Add(i, j)
|
i.Add(i, j)
|
||||||
return i
|
return i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
gamma.Mul(gamma, p)
|
gamma.Mul(gamma, p)
|
||||||
gamma.Mod(gamma, n)
|
gamma.Mod(gamma, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: return an error instead
|
return nil, errors.New("no solution")
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func discreteLog(cmd *cobra.Command, args []string) {
|
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)
|
fmt.Println(k)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue