change variable names for consistency

This commit is contained in:
filifa 2025-09-10 22:10:01 -04:00
parent fffdf712e7
commit b9fd3d903c
1 changed files with 11 additions and 9 deletions

View File

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