refactor fast primitive root test
This commit is contained in:
parent
3c1ade74aa
commit
e1dedd9c33
|
|
@ -159,22 +159,24 @@ func PrimitiveRootFast(modulus *big.Int, tpf map[string]*big.Int) (*big.Int, err
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
isPrimitive := true
|
if isPrimitiveRoot(g, modulus, phi, tpf) {
|
||||||
for p := range tpf {
|
|
||||||
// we already know factors are valid from computing phi
|
|
||||||
k, _ := new(big.Int).SetString(p, 10)
|
|
||||||
k.Div(phi, k)
|
|
||||||
k.Exp(g, k, modulus)
|
|
||||||
if k.Cmp(big.NewInt(1)) == 0 {
|
|
||||||
isPrimitive = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if isPrimitive {
|
|
||||||
return g, nil
|
return g, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, errors.New("no primitive root")
|
return nil, errors.New("no primitive root")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isPrimitiveRoot(g *big.Int, modulus *big.Int, phi *big.Int, tpf map[string]*big.Int) bool {
|
||||||
|
for p := range tpf {
|
||||||
|
// we already know factors are valid from computing phi
|
||||||
|
k, _ := new(big.Int).SetString(p, 10)
|
||||||
|
k.Div(phi, k)
|
||||||
|
k.Exp(g, k, modulus)
|
||||||
|
if k.Cmp(big.NewInt(1)) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue