예제 #1
0
        public NetBigInteger And(
			NetBigInteger value)
        {
            if (m_sign == 0 || value.m_sign == 0)
            {
                return Zero;
            }

            int[] aMag = m_sign > 0
                ? m_magnitude
                : Add(One).m_magnitude;

            int[] bMag = value.m_sign > 0
                ? value.m_magnitude
                : value.Add(One).m_magnitude;

            bool resultNeg = m_sign < 0 && value.m_sign < 0;
            int resultLength = System.Math.Max(aMag.Length, bMag.Length);
            int[] resultMag = new int[resultLength];

            int aStart = resultMag.Length - aMag.Length;
            int bStart = resultMag.Length - bMag.Length;

            for (int i = 0; i < resultMag.Length; ++i)
            {
                int aWord = i >= aStart ? aMag[i - aStart] : 0;
                int bWord = i >= bStart ? bMag[i - bStart] : 0;

                if (m_sign < 0)
                {
                    aWord = ~aWord;
                }

                if (value.m_sign < 0)
                {
                    bWord = ~bWord;
                }

                resultMag[i] = aWord & bWord;

                if (resultNeg)
                {
                    resultMag[i] = ~resultMag[i];
                }
            }

            NetBigInteger result = new NetBigInteger(1, resultMag, true);

            if (resultNeg)
            {
                result = result.Not();
            }

            return result;
        }
예제 #2
0
		/// <summary>
		/// Computes the client session value
		/// </summary>
		public static byte[] ComputeClientSessionValue(byte[] serverPublicEphemeral, byte[] xdata,  byte[] udata, byte[] clientPrivateEphemeral)
		{
			// (B - kg^x) ^ (a + ux)   (mod N)
			var B = new NetBigInteger(NetUtility.ToHexString(serverPublicEphemeral), 16);
			var x = new NetBigInteger(NetUtility.ToHexString(xdata), 16);
			var u = new NetBigInteger(NetUtility.ToHexString(udata), 16);
			var a = new NetBigInteger(NetUtility.ToHexString(clientPrivateEphemeral), 16);

			var bx = g.ModPow(x, N);
			var btmp = B.Add(N.Multiply(k)).Subtract(bx.Multiply(k)).Mod(N);
			return btmp.ModPow(x.Multiply(u).Add(a), N).ToByteArrayUnsigned();
		}