public void GenerateRandomVector(BigInteger p, double l0) { long len = (long)Math.Ceiling(3.0 * l0 / 8.0) + 1; byte[] rndBytes = new byte[len]; for (long i = 0; i < rowsCount; i++) { rnd.NextBytes(rndBytes); BigInteger num = new BigInteger(rndBytes); Mat[i, i] = MathClass.mod(num, p); } }
//---- Gauss Jordan Matrix Mod-Inverse --- public BigIntMatrix GaussJordanModInverse(BigInteger p) { BigIntMatrix workMat = new BigIntMatrix(2 * rowsCount, 2 * colsCount); //--- build work matrix --- long n = this.rowsCount; for (long i = 0; i < n; i++) { for (long j = 0; j < n; j++) { workMat.Mat[i, j] = this.Mat[i, j]; } workMat.Mat[i, i + n] = 1; } // --- partial pivoting --- for (long i = n - 1; i > 0; i--) { if (workMat.Mat[i - 1, 0] < workMat.Mat[i, 0]) { for (long j = 0; j < 2 * n; j++) { BigInteger d = workMat.Mat[i, j]; workMat.Mat[i, j] = workMat.Mat[i - 1, j]; workMat.Mat[i - 1, j] = d; } } } // --- reducing to diagnoal matrix --- for (int i = 0; i < n; i++) { BigInteger mi = MathClass.EEA(workMat.Mat[i, i], p); for (int j = 0; j < 2 * n; j++) { if (i != j) { BigInteger d = workMat.Mat[j, i] * mi; for (int k = 0; k < 2 * n; k++) { workMat.Mat[j, k] = MathClass.mod(workMat.Mat[j, k] - workMat.Mat[i, k] * d, p); } } } } BigIntMatrix result = new BigIntMatrix(rowsCount, colsCount); //--- reducing to unit matrix --- for (int i = 0; i < n; i++) { BigInteger d = MathClass.EEA(workMat.Mat[i, i], p); for (int j = 0; j < 2 * n; j++) { workMat.Mat[i, j] = workMat.Mat[i, j] * d; } for (int j = 0; j < n; j++) { //--- check whether matrix is inversible or not --- if (i != j && workMat.Mat[i, j] != 0) { throw new System.ArgumentException("ERROR: Matrix is not inversible...", "Matrix inversion error"); } result.Mat[i, j] = MathClass.mod(workMat.Mat[i, j + n], p); } } return(result); }
private void btnSendRequest_Click(object sender, EventArgs e) { BigIntMatrix V = frmParent.getAndProcessRequest(Ms, l0, N, p); //MessageBox.Show("Your request has been sent to the server...", "request is on going", MessageBoxButtons.OK, MessageBoxIcon.Information); txtLog.AppendText("\r\n---------------------------- Server Respond to the query ------------------------------------"); txtLog.AppendText(V.Print("Result Vector from server")); //--- DePermute V --- Stopwatch sw = new Stopwatch(); sw.Start(); BigIntMatrix VPrime = Depermute(V, dePermutList, N); BigIntMatrix undisturbedHalf = VPrime.GetSubMatrix(0, 1, 0, N); //BigIntMatrix invA = A.ModInverse(p); //***---- BigIntMatrix invA = A.GaussJordanModInverse(p); BigIntMatrix resultHalf = undisturbedHalf * invA * B % p; BigIntMatrix undisturbedVector = undisturbedHalf.Append(resultHalf, BigIntMatrix.AppendMode.Beside); BigIntMatrix ScrambledNoise = (VPrime - undisturbedVector) % p; //BigIntMatrix invDelta = Delta.ModInverse(p); //***---- invDelta = Delta.GaussJordanModInverse(p); BigIntMatrix halfScrambledNoise = ScrambledNoise.GetSubMatrix(0, 1, N, N); BigIntMatrix unScrambledNoise = halfScrambledNoise * invDelta % p; //BigInteger[] eea = MyClass.Extended_GCD(q, p); //---BigInteger qInv = MathClass.NumberModInverse(q, p); BigInteger qInv = MathClass.EEA(q, p); //long qInv = (long)eea[1]; BigInteger[,] eJegon = new BigInteger[1, N]; for (int i = 0; i < N; i++) { BigInteger epsilon = MathClass.mod(unScrambledNoise.GetElement(0, i), q); if (epsilon >= q / 2) { epsilon -= q; } eJegon[0, i] = MathClass.mod(unScrambledNoise.GetElement(0, i) - epsilon, p); eJegon[0, i] *= qInv;// % p;//MyClass.mod((long)eJegon[0, i] * qInv, p); } //Matrix<double> ResultVector = Matrix<double>.Build.DenseOfArray(eJegon).Multiply(qInv).Modulus(p); BigIntMatrix ResultVector = new BigIntMatrix(eJegon).Modulus(p); BigInteger[] resVect = new BigInteger[N]; for (int i = 0; i < N; i++) { resVect[i] = ResultVector.GetElement(0, i); } BigInteger result = 0; for (int i = 0; i < N; i++) { result <<= l0; result += resVect[i]; } sw.Stop(); Protocol3Time = sw.ElapsedMilliseconds; txtLog.AppendText(ResultVector.Print("Obtained Result Vector")); txtLog.AppendText(invA.Print("Inv A")); txtLog.AppendText(invDelta.Print("Inv Delta")); txtLog.AppendText("\r\n>>>>>>>>>> Inv q = " + qInv.ToString()); txtLog.AppendText("\r\n>>>>>>>>>> Obtained Result for index(" + i0.ToString() + "): " + result.ToString()); txtLog.AppendText("\r\n--------------------------------------------- Timing ----------------------------------------------------"); long totalDuration = Protocol1Time + Protocol3Time + frmParent.Protocol2Time; txtLog.AppendText("\r\n>>> Protocol 1 Duration: " + Protocol1Time.ToString()); txtLog.AppendText("\r\n>>> Protocol 2 Duration: " + frmParent.Protocol2Time.ToString()); txtLog.AppendText("\r\n>>> Protocol 3 Duration: " + Protocol3Time.ToString()); txtLog.AppendText("\r\n>>> Total Duration: " + totalDuration.ToString()); txtLog.AppendText("\r\n>>> Matrix Process Duration: " + MatrixProcessTime.ToString()); txtLog.AppendText("\r\n>>> Deference check: " + (frmParent.getValueByIndex(i0) - result).ToString()); txtLog.AppendText("\r\n--------------------------------------------- Timing ----------------------------------------------------"); txtResult.Text = result.ToString(); txtReturnTime.Text = totalDuration.ToString(); MessageBox.Show("Query process has been finished successfully...", "Query Operation", MessageBoxButtons.OK, MessageBoxIcon.Information); }