Skip to content

Instantly share code, notes, and snippets.

@vshapenko
Created March 15, 2019 09:54
Show Gist options
  • Save vshapenko/d2d78595f7c396c1076bf076bef3a96f to your computer and use it in GitHub Desktop.
Save vshapenko/d2d78595f7c396c1076bf076bef3a96f to your computer and use it in GitHub Desktop.
let getPrimes (str:string)=
let one=BigInteger.One
let two=new BigInteger(2)
let zero=BigInteger.Zero
let decomposePq(n: BigInteger): (BigInteger* BigInteger) =
let f(x: BigInteger, c: BigInteger):BigInteger = ((x * x) % n + c) % n
let rec rho(x: BigInteger, y: BigInteger, g: BigInteger, c: BigInteger): BigInteger =
if (g.Equals(one)) then
let xx = f(x, c)
let yy = f(f(y, c), c)
let gg = BigInteger.GreatestCommonDivisor(n,BigInteger.Abs(xx - yy))
rho(xx, yy, gg, c)
else g
let p =
if (n % two = zero) then two
else
let count=n.GetByteCount()
let c = BigInteger.Abs(rndBigInt count)
let x = BigInteger.Abs(rndBigInt count)
rho(x, x, one, c)
let q = n / p
if (q > p) then (p, q) else (q, p)
decomposePq (BigInteger.Abs(BigInteger(Encoding.UTF8.GetBytes(str))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment