private GaussEliminationResultType CheckGaussEliminationResultType() { for (int row = 0; row < Rows; row++) { // Check single row for all zeros bool allZeros = true; for (int column = 0; column < Columns - 1; column++) { var matrixValue = (dynamic)this[row, column]; if (!NumericUtilities.DoubleCompare((double)matrixValue, 0.0)) { allZeros = false; break; } } if (allZeros) { // Two possible results: Infinity results, No valid result var equationResult = (dynamic)this[row, Columns - 1]; return(NumericUtilities.DoubleCompare((double)equationResult, 0.0) ? GaussEliminationResultType.InfinityResults : GaussEliminationResultType.NoValidResult); } } return(GaussEliminationResultType.ValidResult); }
/// <summary> /// Constructor with given starting prime number. /// </summary> /// <param name="startPrime">Starting prime number set to CurrentPrime property</param> public PrimeNumberGenerator(uint startPrime) { if (!NumericUtilities.IsPrime(startPrime)) { throw new ArgumentException("Given start value is not a prime number!"); } CurrentPrime = startPrime; }
private static double GetWeightsSumThrowIfZero(List <WeightedValue> values) { var sumWeights = values.Aggregate(0.0, (prev, current) => prev + current.Weight); if (NumericUtilities.DoubleCompare(sumWeights, 0.0)) { throw new ArgumentException("Sum of weights cannot be zero"); } return(sumWeights); }
private static uint CountPublicKey(uint euler) { for (uint e = 2; e < euler; e++) { if (NumericUtilities.GreatestCommonDivisor((int)euler, (int)e) == 1) { return(e); } } throw new Exception("Unable to count public key"); }
/// <summary> /// Generate next prime number and save it to CurrentPrime property. /// </summary> /// <returns>Next generated prime number</returns> public uint GetNext() { if (CurrentPrime == 2) { // Optimalization return(CurrentPrime = 3); } do { CurrentPrime += 2; // Skip even numbers } while (!NumericUtilities.IsPrime(CurrentPrime)); return(CurrentPrime); }
/// <summary> /// Get modus of given list. /// </summary> /// <param name="values">List of values</param> /// <returns>Modus of given list</returns> public static double Modus(List <double> values) { TestEmptyOrNullValuesThrow(values); var comparer = NumericUtilities.FloatNumberEqualityComparer <double>(); var occurenceDictionary = new ConcurrentDictionary <double, int>(comparer); foreach (var value in values) { occurenceDictionary.AddOrUpdate(value, 1, (key, val) => val + 1); } return(occurenceDictionary .OrderByDescending(pair => pair.Value) .First().Key); }
/// <summary> /// Create instance of SimpleRSA class. /// P and Q parameters must be prime numbers and P != Q. /// Their product should be large enough to correctly compute encrypted/decrypted messages. /// </summary> /// <param name="p">Prime number P</param> /// <param name="q">Primer number Q</param> public SimpleRSA(ushort p, ushort q) { if (!NumericUtilities.IsPrime(p)) { throw new ArgumentException("P is not prime"); } if (!NumericUtilities.IsPrime(q)) { throw new ArgumentException("Q is not prime"); } var euler = (uint)((p - 1) * (q - 1)); Modulo = (uint)p * q; PublicKey = CountPublicKey(euler); PrivateKey = CountPrivateKey(euler, PublicKey); }
/// <summary> /// Create instance of SimpleElGamal class and count private key. /// Base and modulo should be large enough to correctly encrypt and decrypt given messages. /// </summary> /// <param name="exponentAlice">Exponent of Alice</param> /// <param name="exponentBob">Exponent of Bob</param> /// <param name="base">Shared base value. Must be larger than modulo.</param> /// <param name="modulo">Shared modulo. Must be less than base.</param> public SimpleElGamal(ushort exponentAlice, ushort exponentBob, ushort @base, ushort modulo) { if (@base >= modulo) { throw new ArgumentException("Base shouldn't be larger than modulo"); } Modulo = modulo; var halfKeyAlice = NumericUtilities.SolveModulo(@base, exponentAlice, modulo); var halfKeyBob = NumericUtilities.SolveModulo(@base, exponentBob, modulo); // Discrete logarithm problem. // In real situation Alice wouldn't know about Bob's exponent and vice versa. // They change only "half parts of their own keys". var aliceKey = NumericUtilities.SolveModulo(halfKeyBob, exponentAlice, modulo); var bobKey = NumericUtilities.SolveModulo(halfKeyAlice, exponentBob, modulo); if (aliceKey != bobKey) { throw new Exception("Alice's and Bob's keys are not equal!"); } Key = aliceKey; }
/// <summary> /// Decrypt value. /// </summary> /// <param name="value">Value to decrypt</param> /// <returns>Decrypted value</returns> public uint Decrypt(uint value) => NumericUtilities.SolveModulo(value, PrivateKey, Modulo);
/// <summary> /// Encrypt value. /// </summary> /// <param name="value">Value to encrypt</param> /// <returns>Encrypted value</returns> public uint Encrypt(uint value) => NumericUtilities.SolveModulo(value, PublicKey, Modulo);