PollardsRhoFactorization is an implementation of Pollard's rho factorization algorithm using the default parameters x = y = 2
(n *big.Int, f func(n *big.Int) func(x *big.Int) *big.Int)
| 29 | // PollardsRhoFactorization is an implementation of Pollard's rho factorization algorithm |
| 30 | // using the default parameters x = y = 2 |
| 31 | func PollardsRhoFactorization(n *big.Int, f func(n *big.Int) func(x *big.Int) *big.Int) (*big.Int, error) { |
| 32 | x, y, d := big.NewInt(2), big.NewInt(2), big.NewInt(1) |
| 33 | bigOne := big.NewInt(1) |
| 34 | g := f(n) |
| 35 | for d.Cmp(bigOne) == 0 { |
| 36 | x = g(x) |
| 37 | y = g(g(y)) |
| 38 | sub := new(big.Int).Sub(x, y) |
| 39 | d.GCD(nil, nil, sub.Abs(sub), n) |
| 40 | } |
| 41 | if d.Cmp(n) == 0 { |
| 42 | return nil, errors.New("factorization failed") |
| 43 | } |
| 44 | return d, nil |
| 45 | } |
no outgoing calls