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
|
||||
}
|
||||
|
||||
isPrimitive := true
|
||||
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 {
|
||||
if isPrimitiveRoot(g, modulus, phi, tpf) {
|
||||
return g, nil
|
||||
}
|
||||
}
|
||||
|
||||
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