BigInteger.isProbablePrime C# (CSharp) Method

isProbablePrime() public method

public isProbablePrime ( int confidence ) : bool
confidence int
return bool
        public bool isProbablePrime(int confidence)
        {
                BigInteger thisVal;
                if((this.data[maxLength-1] & 0x80000000) != 0)        // negative
                        thisVal = -this;
                else
                        thisVal = this;


                // test for divisibility by primes < 2000
                for(int p = 0; p < primesBelow2000.Length; p++)
                {
                        BigInteger divisor = primesBelow2000[p];

                        if(divisor >= thisVal)
                                break;

                        BigInteger resultNum = thisVal % divisor;
                        if(resultNum.IntValue() == 0)
                        {
                                /*
				Console.WriteLine("Not prime!  Divisible by {0}\n",
                                                  primesBelow2000[p]);
                                */
                                return false;
                        }
                }

                if(thisVal.RabinMillerTest(confidence))
                        return true;
                else
                {
                        //Console.WriteLine("Not prime!  Failed primality test\n");
                        return false;
                }
        }

Same methods

BigInteger::isProbablePrime ( ) : bool

Usage Example

Ejemplo n.º 1
0
        public RSA(BigInteger p, BigInteger q)
        {
            if (!p.isProbablePrime())
            {
                throw new Exception("Podana przez Ciebie liczba p prawdopodobnie nie jest liczbą pierwszą!");
            }

            if (!q.isProbablePrime())
            {
                throw new Exception("Podana przez Ciebie liczba q prawdopodobnie nie jest liczbą pierwszą!");
            }

            this.p = p;
            this.q = q;
            this.n = this.p * this.q;
            this.eulerOfN = (this.p - 1) * (this.q - 1);
            Random randomGenerator = new Random(DateTime.Now.Millisecond);

            do
            {
                this.e = this.eulerOfN.genCoPrime(randomGenerator.Next(1, this.eulerOfN.bitCount()), randomGenerator);
            }
            while ((this.e > this.eulerOfN) && this.e.gcd(this.eulerOfN) > 1);

            this.d = e.modInverse(eulerOfN);
        }
All Usage Examples Of BigInteger::isProbablePrime