本文整理汇总了C#中Lidgren.Network.NetBigInteger类的典型用法代码示例。如果您正苦于以下问题:C# NetBigInteger类的具体用法?C# NetBigInteger怎么用?C# NetBigInteger使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NetBigInteger类属于Lidgren.Network命名空间,在下文中一共展示了NetBigInteger类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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();
}
示例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;
}
示例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);
}
示例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;
}
示例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));
}
示例6: Max
public NetBigInteger Max(
NetBigInteger value)
{
return CompareTo(value) > 0 ? this : value;
}
示例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;
}
示例8: Modulus
public NetBigInteger Modulus(
NetBigInteger val)
{
return Mod(val);
}
示例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)
{
//.........这里部分代码省略.........
示例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;
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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);
}