BigInteger.gcd C# (CSharp) Method

gcd() public method

public gcd ( BigInteger bi ) : BigInteger
bi BigInteger
return BigInteger
        public BigInteger gcd(BigInteger bi)
        {
                BigInteger x;
                BigInteger y;

                if((data[maxLength-1] & 0x80000000) != 0)     // negative
                        x = -this;
                else
                        x = this;

                if((bi.data[maxLength-1] & 0x80000000) != 0)     // negative
                        y = -bi;
                else
                        y = bi;

	        BigInteger g = y;

	        while(x.dataLength > 1 || (x.dataLength == 1 && x.data[0] != 0))
	        {
		        g = x;
		        x = y % x;
		        y = g;
        	}

	        return g;
        }

Usage Example

Ejemplo n.º 1
0
        public static byte[] CreateSignature(byte[] p_data, ElGamalKeyStruct p_key_struct)
        {
            BigInteger x_pminusone = p_key_struct.P - 1;
            BigInteger K;

            do
            {
                K = new BigInteger();
                K.genRandomBits(p_key_struct.P.bitCount() - 1, new Random());
            } while (K.gcd(x_pminusone) != 1);

            BigInteger A = p_key_struct.G.modPow(K, p_key_struct.P);
            BigInteger B = mod(mod(K.modInverse(x_pminusone) * (new BigInteger(p_data) - (p_key_struct.X * (A))), (x_pminusone)), (x_pminusone));

            byte[] x_a_bytes = A.getBytes();
            byte[] x_b_bytes = B.getBytes();

            int x_result_size = (((p_key_struct.P.bitCount() + 7) / 8) * 2);

            byte[] x_result = new byte[x_result_size];

            Array.Copy(x_a_bytes, 0, x_result, x_result_size / 2 - x_a_bytes.Length, x_a_bytes.Length);
            Array.Copy(x_b_bytes, 0, x_result, x_result_size - x_b_bytes.Length, x_b_bytes.Length);

            return(x_result);
        }
All Usage Examples Of BigInteger::gcd