public static BigInteger SolveLinearEquatation(DLPInput input, BigInteger logatithmicExpression, BigInteger a, BigInteger b) { var mod = input.order; var gcd = BigInteger.GreatestCommonDivisor(a, mod); if (gcd == -1) { return((b * a.ModInverse(mod)).ModPositive(mod)); } else { //return 1; Console.WriteLine(gcd); BigInteger u, v; BigIntegerExtension.ExtendedGcd(a, mod, out u, out v); var reducedMOD = mod / gcd; var x0 = ((b / gcd) * u).ModPositive(reducedMOD); for (BigInteger j = 0; j < gcd; j++) { var x = x0 + j * reducedMOD; // Console.WriteLine(x); if (BigInteger.ModPow(input.g, x, input.p) == logatithmicExpression) { return(x); } } } return(-1); }
public static BigInteger[] SolveTriangularSystemOfLinearEquatations(DLPInput input, BigInteger[] factorBase, ModRationalNumber[][] matrix) { var solution = new List <BigInteger>(); int n = matrix.Length; int m = matrix[0].Length; for (int i = n - 1; i >= 0; i--) { var logarithmicExpression = factorBase[i]; var constantTerm = matrix[i][m - 1]; var coefficient = matrix[i][i]; var a = coefficient.Numerator * constantTerm.Denominator; var b = coefficient.Denominator * constantTerm.Numerator; var mod = coefficient.Mod; //Console.WriteLine("try to solve "+i); var x = SolveLinearEquatation(input, logarithmicExpression, a.ModPositive(mod), b.ModPositive(mod)); solution.Add(x); for (int k = i - 1; k >= 0; k--) { matrix[k][m - 1] = matrix[k][m - 1] - solution[n - 1 - i] * matrix[k][i]; } // Console.WriteLine("solved + " + i); } solution.Reverse(); return(solution.ToArray()); }
private void OnSolveDLPBtnClick(object sender, RoutedEventArgs e) { var input = new Utility.DLPInput(); bool isValid = GetValidatedInput(ref input); if (!isValid) { ShowMessage(); return; } else { BigInteger x = -1; string elapsedTime = ""; switch (currentAlgorithm) { case (int)Algorithms.BabyStep: x = Test.TestBabyStepGiantStepDLP.SolveDLP(input.g, input.h, input.p); elapsedTime = Test.TestBabyStepGiantStepDLP.ElapsedTime; break; case (int)Algorithms.RhoPollard: x = Test.TestRhoPollardDLP.SolveDLP(input.g, input.h, input.p); elapsedTime = Test.TestRhoPollardDLP.ElapsedTime; break; case (int)Algorithms.IndexCalculus: int fbSize = 1, countOfLinEq = 1; bool isValidForIndexCalculus = int.TryParse(factorBaseTxtBox.Text, out fbSize) && int.TryParse(linearEquatationsCountTxtBox.Text, out countOfLinEq); if (isValidForIndexCalculus) { DLPAlgorithm.IndexCalculus.FactorBaseSize = fbSize; DLPAlgorithm.IndexCalculus.LinearEquatationsCount = countOfLinEq; } x = Test.TestIndexCalculusDLP.SolveDLP(input.g, input.h, input.p); elapsedTime = Test.TestIndexCalculusDLP.ElapsedTime; break; default: x = -1; break; } logTxtBox.Text = x.ToString(); timeTxtBox.Text = elapsedTime; } }
private bool GetValidatedInput(ref Utility.DLPInput input) { BigInteger p = 1, g = 1, h = 1; var isValid = BigInteger.TryParse(pTxtBox.Text, out p) && BigInteger.TryParse(gTxtBox.Text, out g) && BigInteger.TryParse(hTxtBox.Text, out h); if (!isValid) { return(false); } else { input.p = p; input.g = g; input.h = h; input.order = p - 1; return(true); } }
public static BigInteger[] SolveSystemOfLinearEquatations(DLPInput input, ref BigInteger[] factorBase, BigInteger[][] coefficients, BigInteger[] constantTerms) { //Print(coefficients); Console.WriteLine("Size of fb = " + factorBase.Length); var reducedCoefficients = RemoveNullColumns(coefficients, ref factorBase); Console.WriteLine("Size of reduced fb = " + factorBase.Length); var augmentedMatrix = ToAugmentedMatrix(reducedCoefficients, constantTerms); var convertedAugmentedMatrix = Converter.ToTwoDimensionalModRationalNumberArray(augmentedMatrix, input.order); //Print(convertedAugmentedMatrix); Console.WriteLine("Count of rows before to triangular = " + convertedAugmentedMatrix.Length); convertedAugmentedMatrix = ToTriangularForm(convertedAugmentedMatrix); Console.WriteLine("Count of rows after triangular = " + convertedAugmentedMatrix.Length); //Print(convertedAugmentedMatrix); convertedAugmentedMatrix = RemoveNullLines(convertedAugmentedMatrix); Console.WriteLine("Count of rows after null lines removal = " + convertedAugmentedMatrix.Length); // Print(convertedAugmentedMatrix); // Console.WriteLine(convertedAugmentedMatrix.Length +" - " + factorBase.Length ); if (convertedAugmentedMatrix.Length < factorBase.Length) { // ReduceFactorBase(ref factorBase, ref convertedAugmentedMatrix); //Console.WriteLine(convertedAugmentedMatrix.Length + " - " + factorBase.Length); return(null); } // Print(convertedAugmentedMatrix); var solution = SolveTriangularSystemOfLinearEquatations(input, factorBase, convertedAugmentedMatrix); return(solution); //return null; }