Esempio n. 1
0
        public BigIntMatrix GetMinor(long r, long c)
        {
            BigIntMatrix result = new BigIntMatrix(rowsCount - 1, colsCount - 1);
            long         p      = 0;

            for (long s = 0; s < rowsCount - 1; s++)
            {
                if (p == r)
                {
                    p++;
                }
                long q = 0;
                for (long t = 0; t < colsCount - 1; t++)
                {
                    if (q == c)
                    {
                        q++;
                    }
                    result.Mat[s, t] = Mat[p, q];
                    q++;
                }
                p++;
            }
            return(result);
        }
        private static BigIntMatrix Depermute(BigIntMatrix vect, List <int> pList, int N)
        {
            BigIntMatrix result = new BigIntMatrix(1, 2 * N, 0);

            result = vect.SwapCol(pList);
            return(result);
        }
Esempio n. 3
0
        public BigIntMatrix GenerateRandomInvertibleMatrix(BigInteger p, double l0, bool isVectorMatrix)
        {
            BigIntMatrix result     = new BigIntMatrix(rowsCount, colsCount);
            bool         inversible = false;
            BigIntMatrix matInv     = new BigIntMatrix(this.rowsCount, this.colsCount);

            do
            {
                if (isVectorMatrix)
                {
                    result.GenerateRandomVector(p, l0);
                }
                else
                {
                    result.GenerateRandomMatrix(p, l0);
                }
                try
                {
                    matInv     = result.GaussJordanModInverse(p);
                    inversible = true;
                }
                catch (Exception ex)
                {
                    inversible = false;
                }
            } while (!inversible);

            Mat = result.Mat;
            return(matInv);
        }
 private static BigIntMatrix[] permute(BigIntMatrix[] GivenMatrix, long n, List <int> pList, int N)
 {
     BigIntMatrix[] result = new BigIntMatrix[n];//, N, 2 * N];
     for (long i = 0; i < n; i++)
     {
         result[i] = new BigIntMatrix(N, 2 * N);
         result[i] = GivenMatrix[i].SwapCol(pList);
     }
     return(result);
 }
Esempio n. 5
0
        public BigIntMatrix MatrixDuplicate()
        {
            BigIntMatrix result = new BigIntMatrix(rowsCount, colsCount);

            for (long i = 0; i < rowsCount; i++)
            {
                for (long j = 0; j < colsCount; j++)
                {
                    result.Mat[i, j] = Mat[i, j];
                }
            }
            return(result);
        }
Esempio n. 6
0
        public BigIntMatrix SwapCol(List <int> pList)
        {
            BigIntMatrix result = new BigIntMatrix(rowsCount, colsCount);

            for (int j = 0; j < this.rowsCount; j++)
            {
                for (int k = 0; k < this.colsCount; k++)
                {
                    long k2 = pList[k];
                    result.Mat[j, k2] = this.Mat[j, k];
                }
            }
            return(result);
        }
Esempio n. 7
0
        public BigIntMatrix Append(BigIntMatrix c2, AppendMode appendMode)
        {
            BigIntMatrix result;

            if (appendMode == AppendMode.Beside)
            {
                result = new BigIntMatrix(this.rowsCount, this.colsCount + c2.colsCount);
                if (this.rowsCount == c2.rowsCount)
                {
                    for (int i = 0; i < this.rowsCount; i++)
                    {
                        for (int j = 0; j < this.colsCount; j++)
                        {
                            result.Mat[i, j] = this.Mat[i, j];
                        }
                        for (int j = 0; j < c2.colsCount; j++)
                        {
                            result.Mat[i, j + c2.colsCount] = c2.Mat[i, j];
                        }
                    }
                }
                else
                {
                    throw new System.ArgumentException("Matrices have no equal rows", "original");
                }
            }
            else
            {
                result = new BigIntMatrix(this.rowsCount + c2.rowsCount, this.colsCount);
                if (this.colsCount == c2.colsCount)
                {
                    for (int j = 0; j < this.colsCount; j++)
                    {
                        for (int i = 0; i < this.rowsCount; i++)
                        {
                            result.Mat[i, j] = this.Mat[i, j];
                        }
                        for (int i = 0; i < c2.rowsCount; i++)
                        {
                            result.Mat[i + this.rowsCount, j] = c2.Mat[i, j];
                        }
                    }
                }
                else
                {
                    throw new System.ArgumentException("Matrices have no equal columns", "original");
                }
            }
            return(result);
        }
Esempio n. 8
0
        public static BigIntMatrix operator %(BigIntMatrix c1, BigInteger p)
        {
            BigIntMatrix result = new BigIntMatrix(c1.rowsCount, c1.colsCount);

            for (long i = 0; i < c1.rowsCount; i++)
            {
                for (long j = 0; j < c1.colsCount; j++)
                {
                    result.Mat[i, j] = c1.Mat[i, j] % p;
                    if (result.Mat[i, j] < 0)
                    {
                        result.Mat[i, j] += p;
                    }
                }
            }
            return(result);
        }
Esempio n. 9
0
        public BigIntMatrix Modulus(BigInteger p)
        {
            BigIntMatrix result = new BigIntMatrix(rowsCount, colsCount);

            for (long i = 0; i < rowsCount; i++)
            {
                for (long j = 0; j < colsCount; j++)
                {
                    result.Mat[i, j] = Mat[i, j] % p;
                    if (result.Mat[i, j] < 0)
                    {
                        result.Mat[i, j] += p;
                    }
                }
            }
            return(result);
        }
Esempio n. 10
0
        public BigIntMatrix RemoveRow(long r)
        {
            BigIntMatrix result = new BigIntMatrix(rowsCount - 1, colsCount);
            long         p      = 0;

            for (long s = 0; s < rowsCount - 1; s++)
            {
                if (p == r)
                {
                    p++;
                }
                for (long t = 0; t < colsCount; t++)
                {
                    result.Mat[s, t] = Mat[p, t];
                }
                p++;
            }
            return(result);
        }
Esempio n. 11
0
        public BigIntMatrix RemoveCol(long c)
        {
            BigIntMatrix result = new BigIntMatrix(rowsCount, colsCount - 1);

            for (long s = 0; s < rowsCount; s++)
            {
                long q = 0;
                for (long t = 0; t < colsCount - 1; t++)
                {
                    if (q == c)
                    {
                        q++;
                    }
                    result.Mat[s, t] = Mat[s, q];
                    q++;
                }
            }
            return(result);
        }
Esempio n. 12
0
        public static BigIntMatrix operator *(BigIntMatrix c1, BigInteger n)
        {
            BigIntMatrix result = new BigIntMatrix(c1.rowsCount, c1.colsCount);

            if (c1.colsCount == c1.rowsCount)
            {
                for (int i = 0; i < result.rowsCount; i++)
                {
                    for (int j = 0; j < result.rowsCount; j++)
                    {
                        result.Mat[i, j] = c1.Mat[i, j] * n;
                    }
                }
            }
            else
            {
                throw new System.ArgumentException("First matrix row should be equal to the second matrix col", "original");
            }
            return(result);
        }
Esempio n. 13
0
        public BigIntMatrix dotMul(BigIntMatrix c2)
        {
            BigIntMatrix result = new BigIntMatrix(rowsCount, colsCount);

            if (rowsCount != c2.rowsCount || colsCount != c2.colsCount)
            {
                throw new System.ArgumentException("Matrices dimentions should be the same", "original");
            }
            else
            {
                for (long i = 0; i < rowsCount; i++)
                {
                    for (long j = 0; j < colsCount; j++)
                    {
                        result.Mat[i, j] = Mat[i, j] * c2.Mat[i, j];
                    }
                }
            }
            return(result);
        }
Esempio n. 14
0
        public static BigIntMatrix operator -(BigIntMatrix c1, BigIntMatrix c2)
        {
            BigIntMatrix result = new BigIntMatrix(c1.rowsCount, c1.colsCount);

            if (c1.rowsCount != c2.rowsCount || c1.colsCount != c2.colsCount)
            {
                throw new System.ArgumentException("Matrices dimentions should be the same", "original");
            }
            else
            {
                for (long i = 0; i < c1.rowsCount; i++)
                {
                    for (long j = 0; j < c1.colsCount; j++)
                    {
                        result.Mat[i, j] = c1.Mat[i, j] - c2.Mat[i, j];
                    }
                }
            }
            return(result);
        }
Esempio n. 15
0
        public BigIntMatrix GetSubMatrix(long FromRow, long RowCount, long FromCol, long ColCount)
        {
            BigIntMatrix result;

            if (FromRow < 0 || FromRow > this.rowsCount || FromCol < 0 || FromCol > this.colsCount || FromRow + RowCount > this.rowsCount || FromCol + ColCount > this.colsCount)
            {
                throw new System.ArgumentException("Given indices are out of matrix range", "original");
            }
            else
            {
                result = new BigIntMatrix(RowCount, ColCount);
                for (long i = 0; i < RowCount; i++)
                {
                    for (long j = 0; j < ColCount; j++)
                    {
                        result.Mat[i, j] = this.Mat[i + FromRow, j + FromCol];
                    }
                }
            }
            return(result);
        }
Esempio n. 16
0
        private BigIntMatrix CreateUserQuery(BigIntMatrix[] Ms, BigInteger[,] splitted, int N, long n, BigInteger p)
        {
            int          len = splitted.GetLength(0) * splitted.GetLength(1);
            BigIntMatrix Mat = new BigIntMatrix(len, 2 * N, 0);
            int          l   = 0;

            for (long i = 0; i < n; i++)
            {
                for (long j = 0; j < N; j++)
                {
                    for (int k = 0; k < 2 * N; k++)
                    {
                        //BigInteger num = BigInteger.Remainder(BigInteger.Multiply(splitted[i][j], Ms[i, j, k]), p);
                        BigInteger tmp = BigInteger.Multiply(splitted[i, j], Ms[i].GetElement(j, k));
                        Mat.SumElement(l, k, tmp);
                        //V[0, k] = MyClass.mod((long)V[0, k], p);
                    }
                    l++;
                }
            }
            return(Mat.Modulus(p));
        }
Esempio n. 17
0
        public BigIntMatrix getAndProcessRequest(BigIntMatrix[] Ms, int l0, int N, BigInteger p)
        {
            txtLog.AppendText("---------------------------- Server Started query process --------------------------");
            Stopwatch sw = new Stopwatch();

            sw.Start();
            BigInteger[,] splittedList = spliteDB(l0, ll, N);

            /******* show splited list ******
             * for (long i = 0; i < splittedList.GetLength(0); i++)
             * {
             *  txtLog.AppendText(">>>>>>>>>> Splited Item({0})...", i);
             *  for (long j = 0; j < splittedList.GetLength(1); j++)
             *  {
             *      txtLog.AppendText("> Splited({0}): {1}", j, splittedList[i,j]);
             *  }
             * }
             * /*****************************/

            BigIntMatrix Mat = CreateUserQuery(Ms, splittedList, N, n, p);
            BigIntMatrix V   = new BigIntMatrix(1, 2 * N, 0);
            long         len = splittedList.GetLength(0) * splittedList.GetLength(1);

            for (int i = 0; i < len; i++)
            {
                for (int j = 0; j < 2 * N; j++)
                {
                    V.SumElement(0, j, Mat.GetElement(i, j));
                }
            }

            sw.Stop();
            Protocol2Time = sw.ElapsedMilliseconds;
            //txtLog.AppendText(">>>>>>>>>> Matrix Mat: {0}", Mat.ToString());
            txtLog.AppendText(V.Modulus(p).Print("Matrix V"));
            txtLog.AppendText("---------------------------- Server Finished query process --------------------------");
            return(V.Modulus(p));
        }
Esempio n. 18
0
        public static BigIntMatrix operator *(BigIntMatrix c1, BigIntMatrix c2)
        {
            BigIntMatrix result = new BigIntMatrix(c1.rowsCount, c2.colsCount);

            if (c1.colsCount == c2.rowsCount)
            {
                for (int i = 0; i < result.rowsCount; i++)
                {
                    for (int j = 0; j < result.colsCount; j++)
                    {
                        result.Mat[i, j] = 0;
                        for (int k = 0; k < c1.colsCount; k++) // OR k<c2.rowsCount
                        {
                            result.Mat[i, j] += c1.Mat[i, k] * c2.Mat[k, j];
                        }
                    }
                }
            }
            else
            {
                throw new System.ArgumentException("First matrix row should be equal to the second matrix col", "original");
            }
            return(result);
        }
Esempio n. 19
0
        //---- 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);
        }
        private void btnSetupRequest_Click(object sender, EventArgs e)
        {
            txtReturnTime.Text     = "0";
            txtResult.Text         = "-";
            btnSendRequest.Enabled = false;

            n       = frmParent.n;
            this.i0 = (int)nud_i0.Value;
            int NI = 1;

            while ((Math.Ceiling(Math.Log(n * NI)) + 1) * NI < frmParent.ll)
            {
                NI++;
            }
            N = NI;

            l0             = (int)Math.Ceiling(Math.Log(n * N)) + 1;
            q              = BigInteger.Pow(2, 2 * l0);
            p              = MathClass.GenerateLargePrime(l0);
            nud_i0.Maximum = n;

            txtN.Text   = N.ToString();
            txt_n.Text  = n.ToString();
            txt_p.Text  = p.ToString();
            txt_q.Text  = q.ToString();
            txt_l0.Text = l0.ToString();

            Stopwatch sw = new Stopwatch();

            sw.Start();

            MPrime = new BigIntMatrix[n];
            A      = new BigIntMatrix(N, N);
            B      = new BigIntMatrix(N, N);
            Delta  = new BigIntMatrix(N, N);
            invA   = A.GenerateRandomInvertibleMatrix(p, l0, false);
            B.GenerateRandomMatrix(p, l0);

            M        = A.Append(B, BigIntMatrix.AppendMode.Beside);
            invDelta = Delta.GenerateRandomInvertibleMatrix(p, l0, true);

            txtLog.AppendText(A.Print("A"));
            txtLog.AppendText(B.Print("B"));
            txtLog.AppendText(M.Print("M"));
            txtLog.AppendText(Delta.Print("Delta"));

            BigIntMatrix P, Ai, Bi, D;

            P  = new BigIntMatrix(N, N);
            D  = new BigIntMatrix(N, N);
            Ai = new BigIntMatrix(N, N);
            Bi = new BigIntMatrix(N, N);
            txtLog.AppendText("\r\n>>> N: " + N.ToString());
            txtLog.AppendText("\r\n---------------------------- Matrix Operations Started --------------------------");
            Stopwatch swMat = new Stopwatch();

            swMat.Start();
            for (long i = 0; i < n; i++)
            {
                // --- M Jegon ---
                //---P.GenerateRandomInvertibleMatrix(p, l0, false);
                P.GenerateRandomMatrix(p, l0);
                // --- Soft Noise Matrix D ---
                D.GenerateSoftNoiseMatrix();
                // --- hard noise ---
                if (i == i0)
                {
                    D.GenerateHardNoiseMatrix(q);
                }
                else
                {
                    D.GenerateSoftNoiseMatrix();
                }

                Ai        = P * A;
                Bi        = (P * B) + (D * Delta);
                MPrime[i] = Ai.Append(Bi, BigIntMatrix.AppendMode.Beside) % p;
            }
            swMat.Stop();
            MatrixProcessTime = swMat.ElapsedMilliseconds;
            txtLog.AppendText("\r\n>>> Matrix Process Duration: " + MatrixProcessTime.ToString());
            txtLog.AppendText("\r\n---------------------------- Matrix Operations Finished --------------------------");

            // --- make permutation & depermiutation vector ---
            Random     rnd        = new Random(DateTime.Now.Millisecond);
            List <int> tmpList    = new List <int>();
            List <int> permutList = new List <int>();

            dePermutList.Clear();
            for (int i = 0; i < 2 * N; i++)
            {
                tmpList.Add(i);
                dePermutList.Add(0);
            }

            int j = 0;

            while (tmpList.Count > 0)
            {
                int i = rnd.Next(tmpList.Count);
                permutList.Add(tmpList[i]);
                dePermutList[tmpList[i]] = j++;
                tmpList.RemoveAt(i);
            }

            Ms = permute(MPrime, n, permutList, N);

            sw.Stop();
            Protocol1Time          = sw.ElapsedMilliseconds;
            btnSendRequest.Enabled = true;
            txtLog.AppendText("\r\n---------------------------- Request has been sent to the server --------------------------");
        }