Esempio n. 1
0
        static void Main(string[] args)
        {
            var mode = "benchmark";

            //"sign";
            //"compare";

            if (mode == "benchmark")
            {
                var summary = BenchmarkRunner.Run <TestCases>();
            }
            else if (mode == "sign")
            {
                int falseCounter = 0, trueCounter = 0;
                for (int i = 0; i < 150; i++)
                {
                    var(publicKey1, secretKey1) = PlainScheme.GenerateKeys();
                    var sign1 = PlainScheme.Sign(secretKey1, "Hello");

                    //Console.WriteLine(PlainScheme.Verify(publicKey1, sign1, "Hello"));
                    var result = PlainScheme.Verify(publicKey1, sign1, "Hello");

                    if (result)
                    {
                        trueCounter++;
                    }
                    else
                    {
                        falseCounter++;
                    }
                }

                Console.WriteLine("True/False: {0}/{1}", trueCounter, falseCounter);
            }
            else if (mode == "compare")
            {
                var num1    = BigInteger.Parse("141562178236123719023182636512735127318231273816231730913");
                var num2    = BigInteger.Parse("9573092321784619475983727739052210384891203183287482102384");
                var modulus = BigInteger.Parse("69457109491271640540324213238132238195765914719571218");
                Plain.Static.P = modulus;
                var matr = Utils.GenerateMatrix();

                for (int i = 0; i < 100000; i++)
                {
                }
            }
        }
Esempio n. 2
0
        public static (PublicKey, SecretKey) GenerateKeys()
        {
            (Static.P, Static.Q) = Static.PrimeGenerator.GeneratePair();

            var g    = Utils.GenerateNonInvertibleMatrix();
            var gPow = g.Pow(Static.Q + 1);

            while (gPow != g)
            {
                var m = (Static.P - 1) / Static.Q;

                g    = g.Pow(m);
                gPow = g.Pow(Static.Q + 1);
            }


            var d = Utils.GenerateMatrix();

            while (!d.IsInvertible() || !d.IsNonMultipliableBothSide(g))
            {
                d = Utils.GenerateMatrix();
            }

            var u = Utils.GenerateMatrix();

            while (!u.IsInvertible() || !u.IsNonMultipliableBothSide(g) || !u.IsNonMultipliableBothSide(d))
            {
                u = Utils.GenerateMatrix();
            }

            var numberW = Utils.GenerateNumber();
            var numberT = Utils.GenerateNumber();
            var numberX = Utils.GenerateNumber().Mod(Static.Q - 2) + 2; // Crete 1 < x < q

            var e = Utils.GenerateRightUnit(g);

            var dPowW         = d.Pow(numberW);
            var dPowWInversed = dPowW.Inverse();
            var uPowT         = u.Pow(numberT);
            var uPowTInversed = uPowT.Inverse();



            var l = dPowWInversed * e * uPowT;
            var y = dPowWInversed * g.Pow(numberX) * dPowW;
            var z = uPowTInversed * g * uPowT;

            return(new PublicKey
            {
                Modulus = Static.P,
                PrimeNumber = Static.Q,
                MatrixL = l,
                MatrixY = y,
                MatrixZ = z
            }, new SecretKey
            {
                MatrixD = d,
                MatrixG = g,
                MatrixU = u,
                NumberW = numberW,
                NumberX = numberX,
                NumberT = numberT
            });
        }