Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' Large Prime Generator '''
- Public Function PrimeGen(ByVal length As Integer, ByRef Rand As Random, _
- Optional ByVal iteration As Integer = 0) As BigInteger
- If length < 2 Then length = 2
- If iteration = 0 Then iteration = Math.Sqrt(length)
- Dim temp, r, y, a(iteration - 1) As BigInteger
- PrimeGen = RandBigInt(length, Rand)
- While True
- If PrimeGen.IsEven Then PrimeGen -= 1
- If PrimeGen Mod 5 = 0 Then PrimeGen -= 2
- If PrimeGen <= 3 Then Return Rand.Next(2, 4)
- For i = 0 To a.Length - 1
- While a(i) <= 2 Or a(i) >= PrimeGen - 2 : a(i) = RandBigInt(length, Rand) : End While
- Next
- r = New BigInteger((PrimeGen - 1).ToByteArray())
- While r Mod 2 = 0 : r = r / 2 : End While
- For i = 0 To iteration - 1
- temp = r
- y = BigInteger.ModPow(a(i), r, PrimeGen)
- While temp <> PrimeGen - 1 AndAlso y <> 1 AndAlso y <> PrimeGen - 1
- y = BigInteger.ModPow(y, 2, PrimeGen)
- temp = BigInteger.Multiply(temp, 2)
- End While
- If y <> PrimeGen - 1 AndAlso BigInteger.ModPow(temp, 1, 2) = 0 Then : Exit For
- ElseIf i = iteration - 1 Then : Return PrimeGen : End If
- Next
- PrimeGen -= 2
- End While
- End Function
- Private Function RandBigInt(ByVal BitLength As BigInteger, ByRef Rnd As Random) As BigInteger
- Dim Rmdr, numParts(BigInteger.DivRem(BitLength, 8, Rmdr)) As Byte
- For i = 0 To numParts.Length - 2 : numParts(i) = Rnd.Next(0, 256) : Next
- numParts(numParts.Length - 1) = If(Rmdr = 0, 0, Rnd.Next(0, 2 ^ Rmdr - 1))
- RandBigInt = New BigInteger(numParts)
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement