Example #1
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 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 --------------------------");
        }