static BigInteger facto(BigInteger num) { if (num < 0) return 0; else if (num.Equals(0)) return 1; else return num * facto(num - 1); }
public Point Multiply(Point p, BigInteger x) { if (x.Equals(BigInteger.Zero)) return p.Curve.Infinity; if ((x % 2).Equals(BigInteger.One)) { return p + Multiply(p, x - 1); } return Multiply(p + p, x / 2); }
public void FindBySerialNumber(BigInteger hexValue, BigInteger decimalValue) { FindCore( cert => { byte[] serialBytes = cert.GetSerialNumber(); BigInteger serialNumber = FindPal.PositiveBigIntegerFromByteArray(serialBytes); bool match = hexValue.Equals(serialNumber) || decimalValue.Equals(serialNumber); return match; }); }
private sysnum.BigInteger hash(string source) { if (source == null || source.Length == 0) { return(sysnum.BigInteger.Zero); } else { char[] sourceArray = source.ToCharArray(); long longsourceArray = (long)sourceArray[0]; //if (longsourceArray < 8) //{ // longsourceArray <<= 12; //} //if (longsourceArray < 16) //{ // longsourceArray <<= 11; //} //if (longsourceArray < 32) //{ // longsourceArray <<= 10; //} //else if (longsourceArray < 64) //{ // longsourceArray <<= 9; //} //else if (longsourceArray < 128) //{ // longsourceArray <<= 8; //} if (longsourceArray < 1024) { longsourceArray *= 10; longsourceArray += 19200; } sysnum.BigInteger x = new sysnum.BigInteger(longsourceArray << 7); //sysnum.BigInteger x = new sysnum.BigInteger(((long)sourceArray[0]) << 7); sysnum.BigInteger m = sysnum.BigInteger.Parse("1000003"); sysnum.BigInteger mask = sysnum.BigInteger.Pow(new sysnum.BigInteger(2), this.hashbits) - sysnum.BigInteger.One; foreach (char item in sourceArray) { sysnum.BigInteger temp = new sysnum.BigInteger((long)item); x = ((x * m) ^ temp) & mask; } x = x ^ (new sysnum.BigInteger(source.Length)); if (x.Equals(sysnum.BigInteger.MinusOne)) { x = new sysnum.BigInteger(-2); } return(x); } }
private static Tuple<BigInteger, BigInteger> ScalarMul(Tuple<BigInteger, BigInteger> p, BigInteger e) { if (e.Equals(BigInteger.Zero)) { return new Tuple<BigInteger, BigInteger>(BigInteger.Zero, BigInteger.One); } var q = ScalarMul(p, e / Two); q = EdwardsSquare(q.Item1, q.Item2); if (!e.IsEven) q = Edwards(q.Item1, q.Item2, p.Item1, p.Item2); return q; }
private static BigInteger ExpMod(BigInteger number, BigInteger exponent, BigInteger modulo) { if (exponent.Equals(BigInteger.Zero)) { return BigInteger.One; } BigInteger t = BigInteger.Pow(ExpMod(number, exponent / Two, modulo), 2).Mod(modulo); if (!exponent.IsEven) { t *= number; t = t.Mod(modulo); } return t; }
public void DefaultCtorWorks () { var a = new BigInteger (); Assert.AreEqual (BigInteger.One, ++a, "#1"); a = new BigInteger (); Assert.AreEqual (BigInteger.MinusOne, --a, "#2"); a = new BigInteger (); Assert.AreEqual (BigInteger.MinusOne, ~a, "#3"); a = new BigInteger (); Assert.AreEqual ("0", a.ToString (), "#4"); a = new BigInteger (); Assert.AreEqual (true, a == a, "#5"); a = new BigInteger (); Assert.AreEqual (false, a < a, "#6"); a = new BigInteger (); Assert.AreEqual (true, a < 10l, "#7"); a = new BigInteger (); Assert.AreEqual (true, a.IsEven, "#8"); a = new BigInteger (); Assert.AreEqual (0, (int)a, "#9"); a = new BigInteger (); Assert.AreEqual (0, (uint)a, "#10"); a = new BigInteger (); Assert.AreEqual (0, (ulong)a, "#11"); a = new BigInteger (); Assert.AreEqual (true, a.Equals (a), "#12"); a = new BigInteger (); Assert.AreEqual (a, BigInteger.Min (a, a), "#13"); a = new BigInteger (); Assert.AreEqual (a, BigInteger.GreatestCommonDivisor (a, a), "#14"); a = new BigInteger (); Assert.AreEqual (BigInteger.Zero.GetHashCode (), a.GetHashCode (), "#15"); }
private void ReKeyBuddyKeys(UInt32 buddy_key_id, BigInteger buddy_public_key) { /*is it the same public key we know? */ if (buddy_public_key.Equals(_my_buddy_recent_dh_public_key)) return; _my_buddy_old_dh_public_key = _my_buddy_recent_dh_public_key; _my_buddy_recent_dh_public_key = buddy_public_key; _my_buddy_old_dh_public_key_id = _my_buddy_recent_dh_public_key_id; _my_buddy_recent_dh_public_key_id++; }
/// <summary> /// Verifies that the server's proof value matches the value /// calculated by the client. /// </summary> /// <param name="serverProof">The 20-byte server proof.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown if /// the server proof value is not exactly 20 bytes.</exception> /// <exception cref="InvalidOperationException">Thrown if the object has not /// yet been initialized.</exception> /// <remarks> /// This method should be called after the <see cref="LoginProof(byte[], int, int, byte[], byte[])">LoginProof</see> method. /// </remarks> /// <returns><b>True</b> if the server proof is valid; /// otherwise <b>false</b>.</returns> public bool VerifyServerProof(byte[] serverProof) { if (serverProof.Length != 20) throw new ArgumentOutOfRangeException("Server proof must be exactly 20 bytes."); using (MemoryStream ms_m2 = new MemoryStream(92)) { BinaryWriter bw = new BinaryWriter(ms_m2); bw.Write(EnsureArrayLength(A.ToByteArray(), 32)); bw.Write(m1.ToByteArray()); bw.Write(k); byte[] client_m2_data = ms_m2.GetBuffer(); ms_m2.Close(); byte[] client_hash_m2 = s_sha.ComputeHash(client_m2_data); BigInteger client_m2 = new BigInteger(client_hash_m2); BigInteger server_m2 = new BigInteger(serverProof); Debug.WriteLine(client_m2.ToHexString(), "Client"); Debug.WriteLine(server_m2.ToHexString(), "Server"); return client_m2.Equals(server_m2); } }
/// <summary> /// Validates a Warcraft III server signature. /// </summary> /// <param name="serverSignature">The server signature from /// Battle.net's SID_AUTH_INFO message.</param> /// <param name="ipAddress">The IPv4 address of the server /// currently connected-to.</param> /// <returns><b>True</b> if the signature matches; /// otherwise <b>false</b>.</returns> /// <exception cref="ArgumentOutOfRangeException">Thrown if /// the server signature is not exactly 128 bytes.</exception> public static bool ValidateServerSignature(byte[] serverSignature, byte[] ipAddress) { // code based on iago's code. if (serverSignature.Length != 128) throw new ArgumentOutOfRangeException("The server signature must be exactly 128 bytes long."); BigInteger key = new BigInteger(new byte[] { 0, 1, 0, 1 } /* ReverseArray(new BigInteger((ulong)SignatureKey).GetBytes()) */); BigInteger mod = BigInteger.Parse(ServerModulus, NumberStyles.HexNumber); BigInteger sig = new BigInteger(ReverseArray(serverSignature)); byte[] result = BigInteger.ModPow(sig, key, mod).ToByteArray(); BigInteger res = new BigInteger(ReverseArray(result)); using (MemoryStream ms_res = new MemoryStream(result.Length)) { ms_res.Write(ipAddress, 0, 4); for (int i = 4; i < result.Length; i++) ms_res.WriteByte(0xbb); ms_res.Seek(-1, SeekOrigin.Current); ms_res.WriteByte(0x0b); BigInteger cor_res = new BigInteger(ms_res.GetBuffer()); return cor_res.Equals(res); } }
public unsafe void FindBySerialNumber(BigInteger hexValue, BigInteger decimalValue) { FindCore( delegate(SafeCertContextHandle pCertContext) { byte[] actual = pCertContext.CertContext->pCertInfo->SerialNumber.ToByteArray(); GC.KeepAlive(pCertContext); // Convert to BigInteger as the comparison must not fail due to spurious leading zeros BigInteger actualAsBigInteger = PositiveBigIntegerFromByteArray(actual); return hexValue.Equals(actualAsBigInteger) || decimalValue.Equals(actualAsBigInteger); }); }