• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Network.NetBigInteger类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中Lidgren.Network.NetBigInteger的典型用法代码示例。如果您正苦于以下问题:C# NetBigInteger类的具体用法?C# NetBigInteger怎么用?C# NetBigInteger使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



NetBigInteger类属于Lidgren.Network命名空间,在下文中一共展示了NetBigInteger类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: ComputeServerVerifier

		/// <summary>
		/// Creates a verifier that the server can later use to authenticate users later on (v)
		/// </summary>
		public static byte[] ComputeServerVerifier(byte[] privateKey)
		{
			NetBigInteger x = new NetBigInteger(NetUtility.ToHexString(privateKey), 16);

			// Verifier (v) = g^x (mod N)
			var serverVerifier = g.ModPow(x, N);

			return serverVerifier.ToByteArrayUnsigned();
		}
开发者ID:g1111111g,项目名称:lidgren-network-gen3,代码行数:12,代码来源:NetSRP.cs


示例2: And

		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;
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:55,代码来源:NetBigInteger.cs


示例3: Add

		public NetBigInteger Add(
			NetBigInteger value)
		{
			if (m_sign == 0)
				return value;

			if (m_sign != value.m_sign)
			{
				if (value.m_sign == 0)
					return this;

				if (value.m_sign < 0)
					return Subtract(value.Negate());

				return value.Subtract(Negate());
			}

			return AddToMagnitude(value.m_magnitude);
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:19,代码来源:NetBigInteger.cs


示例4: createUValueOf

		private static NetBigInteger createUValueOf(
			ulong value)
		{
			int msw = (int)(value >> 32);
			int lsw = (int)value;

			if (msw != 0)
				return new NetBigInteger(1, new int[] { msw, lsw }, false);

			if (lsw != 0)
			{
				NetBigInteger n = new NetBigInteger(1, new int[] { lsw }, false);
				// Check for a power of two
				if ((lsw & -lsw) == lsw)
				{
					n.m_numBits = 1;
				}
				return n;
			}

			return Zero;
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:22,代码来源:NetBigInteger.cs


示例5: Mod

		public NetBigInteger Mod(
			NetBigInteger m)
		{
			if (m.m_sign < 1)
				throw new ArithmeticException("Modulus must be positive");

			NetBigInteger biggie = Remainder(m);

			return (biggie.m_sign >= 0 ? biggie : biggie.Add(m));
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:10,代码来源:NetBigInteger.cs


示例6: Max

		public NetBigInteger Max(
			NetBigInteger value)
		{
			return CompareTo(value) > 0 ? this : value;
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:5,代码来源:NetBigInteger.cs


示例7: DivideAndRemainder

		public NetBigInteger[] DivideAndRemainder(
			NetBigInteger val)
		{
			if (val.m_sign == 0)
				throw new ArithmeticException("Division by zero error");

			NetBigInteger[] biggies = new NetBigInteger[2];

			if (m_sign == 0)
			{
				biggies[0] = Zero;
				biggies[1] = Zero;
			}
			else if (val.QuickPow2Check()) // val is power of two
			{
				int e = val.Abs().BitLength - 1;
				NetBigInteger quotient = Abs().ShiftRight(e);
				int[] remainder = LastNBits(e);

				biggies[0] = val.m_sign == m_sign ? quotient : quotient.Negate();
				biggies[1] = new NetBigInteger(m_sign, remainder, true);
			}
			else
			{
				int[] remainder = (int[])m_magnitude.Clone();
				int[] quotient = Divide(remainder, val.m_magnitude);

				biggies[0] = new NetBigInteger(m_sign * val.m_sign, quotient, true);
				biggies[1] = new NetBigInteger(m_sign, remainder, true);
			}

			return biggies;
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:33,代码来源:NetBigInteger.cs


示例8: Modulus

		public NetBigInteger Modulus(
			NetBigInteger val)
		{
			return Mod(val);
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:5,代码来源:NetBigInteger.cs


示例9: ModPow

		public NetBigInteger ModPow(
			NetBigInteger exponent,
			NetBigInteger m)
		{
			if (m.m_sign < 1)
				throw new ArithmeticException("Modulus must be positive");

			if (m.Equals(One))
				return Zero;

			if (exponent.m_sign == 0)
				return One;

			if (m_sign == 0)
				return Zero;

			int[] zVal = null;
			int[] yAccum = null;
			int[] yVal;

			// Montgomery exponentiation is only possible if the modulus is odd,
			// but AFAIK, this is always the case for crypto algo's
			bool useMonty = ((m.m_magnitude[m.m_magnitude.Length - 1] & 1) == 1);
			long mQ = 0;
			if (useMonty)
			{
				mQ = m.GetMQuote();

				// tmp = this * R mod m
				NetBigInteger tmp = ShiftLeft(32 * m.m_magnitude.Length).Mod(m);
				zVal = tmp.m_magnitude;

				useMonty = (zVal.Length <= m.m_magnitude.Length);

				if (useMonty)
				{
					yAccum = new int[m.m_magnitude.Length + 1];
					if (zVal.Length < m.m_magnitude.Length)
					{
						int[] longZ = new int[m.m_magnitude.Length];
						zVal.CopyTo(longZ, longZ.Length - zVal.Length);
						zVal = longZ;
					}
				}
			}

			if (!useMonty)
			{
				if (m_magnitude.Length <= m.m_magnitude.Length)
				{
					//zAccum = new int[m.magnitude.Length * 2];
					zVal = new int[m.m_magnitude.Length];
					m_magnitude.CopyTo(zVal, zVal.Length - m_magnitude.Length);
				}
				else
				{
					//
					// in normal practice we'll never see ..
					//
					NetBigInteger tmp = Remainder(m);

					//zAccum = new int[m.magnitude.Length * 2];
					zVal = new int[m.m_magnitude.Length];
					tmp.m_magnitude.CopyTo(zVal, zVal.Length - tmp.m_magnitude.Length);
				}

				yAccum = new int[m.m_magnitude.Length * 2];
			}

			yVal = new int[m.m_magnitude.Length];

			//
			// from LSW to MSW
			//
			for (int i = 0; i < exponent.m_magnitude.Length; i++)
			{
				int v = exponent.m_magnitude[i];
				int bits = 0;

				if (i == 0)
				{
					while (v > 0)
					{
						v <<= 1;
						bits++;
					}

					//
					// first time in initialise y
					//
					zVal.CopyTo(yVal, 0);

					v <<= 1;
					bits++;
				}

				while (v != 0)
				{
					if (useMonty)
					{
//.........这里部分代码省略.........
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:101,代码来源:NetBigInteger.cs


示例10: ExtEuclid

		private static NetBigInteger ExtEuclid(
			NetBigInteger a,
			NetBigInteger b,
			NetBigInteger u1Out,
			NetBigInteger u2Out)
		{
			NetBigInteger u1 = One;
			NetBigInteger u3 = a;
			NetBigInteger v1 = Zero;
			NetBigInteger v3 = b;

			while (v3.m_sign > 0)
			{
				NetBigInteger[] q = u3.DivideAndRemainder(v3);

				NetBigInteger tmp = v1.Multiply(q[0]);
				NetBigInteger tn = u1.Subtract(tmp);
				u1 = v1;
				v1 = tn;

				u3 = v3;
				v3 = q[1];
			}

			if (u1Out != null)
			{
				u1Out.m_sign = u1.m_sign;
				u1Out.m_magnitude = u1.m_magnitude;
			}

			if (u2Out != null)
			{
				NetBigInteger tmp = u1.Multiply(a);
				tmp = u3.Subtract(tmp);
				NetBigInteger res = tmp.Divide(b);
				u2Out.m_sign = res.m_sign;
				u2Out.m_magnitude = res.m_magnitude;
			}

			return u3;
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:41,代码来源:NetBigInteger.cs


示例11: ComputeClientSessionValue

		/// <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();
		}
开发者ID:Werkheisera2,项目名称:RaginRovers,代码行数:15,代码来源:NetSRP.cs


示例12: ComputeServerSessionValue

		/// <summary>
		/// Computes the server session value
		/// </summary>
		public static byte[] ComputeServerSessionValue(byte[] clientPublicEphemeral, byte[] verifier, byte[] udata, byte[] serverPrivateEphemeral)
		{
			// S = (Av^u) ^ b (mod N)
			var A = new NetBigInteger(NetUtility.ToHexString(clientPublicEphemeral), 16);
			var v = new NetBigInteger(NetUtility.ToHexString(verifier), 16);
			var u = new NetBigInteger(NetUtility.ToHexString(udata), 16);
			var b = new NetBigInteger(NetUtility.ToHexString(serverPrivateEphemeral), 16);

			NetBigInteger retval = v.ModPow(u, N).Multiply(A).Mod(N).ModPow(b, N).Mod(N);

			return retval.ToByteArrayUnsigned();
		}
开发者ID:Werkheisera2,项目名称:RaginRovers,代码行数:15,代码来源:NetSRP.cs


示例13: ComputeServerEphemeral

		/// <summary>
		/// Compute server ephemeral value (B)
		/// </summary>
		public static byte[] ComputeServerEphemeral(byte[] serverPrivateEphemeral, byte[] verifier) // b
		{
			var b = new NetBigInteger(NetUtility.ToHexString(serverPrivateEphemeral), 16);
			var v = new NetBigInteger(NetUtility.ToHexString(verifier), 16);

			// B = kv + g^b (mod N) 
			var bb = g.ModPow(b, N);
			var kv = v.Multiply(k);
			var B = (kv.Add(bb)).Mod(N);

			return B.ToByteArrayUnsigned();
		}
开发者ID:Werkheisera2,项目名称:RaginRovers,代码行数:15,代码来源:NetSRP.cs


示例14: ComputeClientEphemeral

		/// <summary>
		/// Compute client public ephemeral value (A)
		/// </summary>
		public static byte[] ComputeClientEphemeral(byte[] clientPrivateEphemeral) // a
		{
			// A= g^a (mod N) 
			NetBigInteger a = new NetBigInteger(NetUtility.ToHexString(clientPrivateEphemeral), 16);
			NetBigInteger retval = g.ModPow(a, N);

			return retval.ToByteArrayUnsigned();
		}
开发者ID:Werkheisera2,项目名称:RaginRovers,代码行数:11,代码来源:NetSRP.cs


示例15: CompareTo

		public int CompareTo(
			NetBigInteger value)
		{
			return m_sign < value.m_sign ? -1
				: m_sign > value.m_sign ? 1
				: m_sign == 0 ? 0
				: m_sign * CompareNoLeadingZeroes(0, m_magnitude, 0, value.m_magnitude);
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:8,代码来源:NetBigInteger.cs


示例16: Divide

		public NetBigInteger Divide(
			NetBigInteger val)
		{
			if (val.m_sign == 0)
				throw new ArithmeticException("Division by zero error");

			if (m_sign == 0)
				return Zero;

			if (val.QuickPow2Check()) // val is power of two
			{
				NetBigInteger result = Abs().ShiftRight(val.Abs().BitLength - 1);
				return val.m_sign == m_sign ? result : result.Negate();
			}

			int[] mag = (int[])m_magnitude.Clone();

			return new NetBigInteger(m_sign * val.m_sign, Divide(mag, val.m_magnitude), true);
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:19,代码来源:NetBigInteger.cs


示例17: Multiply

		public NetBigInteger Multiply(
			NetBigInteger val)
		{
			if (m_sign == 0 || val.m_sign == 0)
				return Zero;

			if (val.QuickPow2Check()) // val is power of two
			{
				NetBigInteger result = ShiftLeft(val.Abs().BitLength - 1);
				return val.m_sign > 0 ? result : result.Negate();
			}

			if (QuickPow2Check()) // this is power of two
			{
				NetBigInteger result = val.ShiftLeft(Abs().BitLength - 1);
				return m_sign > 0 ? result : result.Negate();
			}

			int maxBitLength = BitLength + val.BitLength;
			int resLength = (maxBitLength + BitsPerInt - 1) / BitsPerInt;

			int[] res = new int[resLength];

			if (val == this)
			{
				Square(res, m_magnitude);
			}
			else
			{
				Multiply(res, m_magnitude, val.m_magnitude);
			}

			return new NetBigInteger(m_sign * val.m_sign, res, true);
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:34,代码来源:NetBigInteger.cs


示例18: Gcd

		public NetBigInteger Gcd(
			NetBigInteger value)
		{
			if (value.m_sign == 0)
				return Abs();

			if (m_sign == 0)
				return value.Abs();

			NetBigInteger r;
			NetBigInteger u = this;
			NetBigInteger v = value;

			while (v.m_sign != 0)
			{
				r = u.Mod(v);
				u = v;
				v = r;
			}

			return u;
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:22,代码来源:NetBigInteger.cs


示例19: Remainder

		public NetBigInteger Remainder(
			NetBigInteger n)
		{
			if (n.m_sign == 0)
				throw new ArithmeticException("Division by zero error");

			if (m_sign == 0)
				return Zero;

			// For small values, use fast remainder method
			if (n.m_magnitude.Length == 1)
			{
				int val = n.m_magnitude[0];

				if (val > 0)
				{
					if (val == 1)
						return Zero;

					int rem = Remainder(val);

					return rem == 0
						? Zero
						: new NetBigInteger(m_sign, new int[] { rem }, false);
				}
			}

			if (CompareNoLeadingZeroes(0, m_magnitude, 0, n.m_magnitude) < 0)
				return this;

			int[] result;
			if (n.QuickPow2Check())  // n is power of two
			{
				result = LastNBits(n.Abs().BitLength - 1);
			}
			else
			{
				result = (int[])m_magnitude.Clone();
				result = Remainder(result, n.m_magnitude);
			}

			return new NetBigInteger(m_sign, result, true);
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:43,代码来源:NetBigInteger.cs


示例20: Min

		public NetBigInteger Min(
			NetBigInteger value)
		{
			return CompareTo(value) < 0 ? this : value;
		}
开发者ID:BenMatase,项目名称:RaginRovers,代码行数:5,代码来源:NetBigInteger.cs



注:本文中的Lidgren.Network.NetBigInteger类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Network.NetBuffer类代码示例发布时间:2022-05-26
下一篇:
C# LibuvSharp.Loop类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap