Beispiel #1
0
        private static void TestMultiExp()
        {
            Console.Write("Testing multi-exponentiation");
            int k    = 111;
            var seed = new byte[1000];
            var r    = new Random();

            r.NextBytes(seed);
            var rnd = new HMACDRBG(seed);
            var mod = new Modular(new BigInteger(5555555566666777777));

            var g  = Enumerable.Range(0, k).Select(i => rnd.GenerateInteger(BigInteger.One << r.Next(200) + 200)).ToArray();
            var me = new MultiExponentiation(mod.Modulus, g);

            for (int i = 0; i < 1000; ++i)
            {
                var e = Enumerable.Range(0, k).Select(j => rnd.GenerateInteger(BigInteger.One << r.Next(100) + 100)).ToArray();
                //var z = mod.Pow(g, e, ref cache);
                var z  = me.Pow(e);
                var z2 = BigInteger.One;
                for (int j = 0; j < k; ++j)
                {
                    z2 = (z2 * BigInteger.ModPow(g[j], e[j], mod.Modulus)).Mod(mod.Modulus);
                }

                if (!z.Equals(z2))
                {
                    Console.WriteLine("ouch");
                }

                if (i % 100 == 0)
                {
                    Console.Write('.');
                }

                r.NextBytes(seed);
                rnd.Reseed(seed);
            }

            Console.WriteLine();
        }
Beispiel #2
0
        static void BenchmarkLiu <T>(LiuScheme <T> lsag, int participants) where T : ILinkableSignature
        {
            Console.WriteLine("Benchmark for {0} participants", participants);
            lsag.GroupParameters = KnownGroupParameters.RFC5114_2_1_160;

            var message      = "hi";
            var messageBytes = Encoding.UTF8.GetBytes(message);

            var keys       = Enumerable.Range(0, participants).Select(i => lsag.GenerateKeyPair()).ToArray();
            var publicKeys = keys.Select(k => k[1]).ToArray();

            Console.WriteLine("Computing signatures");
            Stopwatch timer = new Stopwatch();

            timer.Start();
            var signatures = Enumerable.Range(0, participants).
                             Select(i => lsag.GenerateSignature(messageBytes, publicKeys, keys[i][0], i)).ToArray();

            timer.Stop();
            Console.WriteLine("Generation took {0}, {1}s / participant, {2}s / participant^2", timer.Elapsed,
                              timer.Elapsed.TotalSeconds / participants, timer.Elapsed.TotalSeconds / participants / participants);

            Console.WriteLine("Verifying signatures");
            timer.Restart();
            var cache = new MultiExponentiation(lsag.GroupParameters.Prime, publicKeys);

            if (signatures.All(s => lsag.VerifySignature(messageBytes, s, cache)))
            {
                Console.WriteLine("OK");
            }
            else
            {
                Console.WriteLine("FAIL");
            }

            timer.Stop();
            Console.WriteLine("Verification took {0}, {1}s / participant, {2}s / participant^2", timer.Elapsed,
                              timer.Elapsed.TotalSeconds / participants, timer.Elapsed.TotalSeconds / participants / participants);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            TcpListener server    = null;
            Int32       port      = 13000;
            IPAddress   localAddr = IPAddress.Parse("127.0.0.1");

            // TcpListener server = new TcpListener(port);
            server = new TcpListener(localAddr, port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data
            Byte[] bytes = new Byte[10000];
            String data  = null;

            TcpClient client = server.AcceptTcpClient();

            Console.WriteLine("Connected!");
            int length = 20;

            // Get a stream object for reading and writing
            NetworkStream stream = client.GetStream();

            stream.Read(bytes, 0, bytes.Length);
            data = System.Text.Encoding.ASCII.GetString(bytes, 0, bytes.Length);
            Console.WriteLine("Received: {0}", data);
            Console.WriteLine();

            string[]     temp_split = data.Split('|', '/');
            BigInteger[] V          = new BigInteger[length];
            for (int i = 0; i < length; ++i)
            {
                V[i] = BigInteger.Parse(temp_split[i]);
            }
            BigInteger X = BigInteger.Parse(temp_split[length]);
            BigInteger N = BigInteger.Parse(temp_split[length + 1]);

            Console.WriteLine();

            temp_split = data.Split('/');
            string message      = temp_split[0];
            var    messageBytes = Encoding.UTF8.GetBytes(message);

            var pub_keys  = JsonConvert.DeserializeObject <BigInteger[]>(temp_split[1]);
            var liu2005   = JsonConvert.DeserializeObject <Liu2005>(temp_split[2]);
            var signature = JsonConvert.DeserializeObject <LSAG.Liu2005.Signature>(temp_split[3]);

            var cache = new MultiExponentiation(liu2005.GroupParameters.Prime, pub_keys);

            bool res = liu2005.VerifySignature(messageBytes, signature, cache);

            if (res)
            {
                Console.WriteLine("SUCCESS");
            }
            else
            {
                Console.WriteLine("FAILURE");
            }


            int[] B      = new int[length];
            var   random = new Random();

            Console.Write("B: ");
            for (int i = 0; i < length; ++i)
            {
                B[i] = random.Next(0, 2);
                Console.Write(B[i] + " ");
            }
            Console.WriteLine();
            Console.WriteLine();

            string msg = "";

            for (int i = 0; i < length - 1; ++i)
            {
                msg += B[i] + "|";
            }
            msg += B[19];

            bytes = System.Text.Encoding.ASCII.GetBytes(msg);

            stream.Write(bytes, 0, bytes.Length);

            bytes = new Byte[256];
            stream.Read(bytes, 0, bytes.Length);
            data = System.Text.Encoding.ASCII.GetString(bytes, 0, bytes.Length);
            Console.WriteLine("Received: {0}", data);
            Console.WriteLine();

            BigInteger Y1 = BigInteger.Parse(data);

            BigInteger X1 = 1;

            for (int i = 0; i < length; ++i)
            {
                X1 = BigInteger.Remainder(X1, N);
                X1 = BigInteger.Multiply(BigInteger.Remainder(BigInteger.Pow(V[i], B[i]), N), X1);
                //X1 *= Math.Pow(V[i],B[i])%N;
                //X2 *= Math.Pow(V[i], (1-B[i]));
            }
            //X1 = Y1*Y1*X1%N;
            X1 = BigInteger.Remainder(BigInteger.Multiply(X, X1), N);
            //X1 = (X*X1)%N;
            Y1 = BigInteger.Remainder(BigInteger.Multiply(Y1, Y1), N);
            //Y1 = (Y1*Y1)%N;
            if (X1.Equals(Y1))
            {
                bytes = System.Text.Encoding.ASCII.GetBytes("ACCEPT");
                stream.Write(bytes, 0, bytes.Length);
            }
            else
            {
                bytes = System.Text.Encoding.ASCII.GetBytes("REJECT");
                stream.Write(bytes, 0, bytes.Length);
            }



            //Send to Client

            /*SHA1 sha = new SHA1CryptoServiceProvider();
             * string id = "*****@*****.**";
             *
             *
             * byte[] seed_bytes = System.Text.Encoding.UTF8.GetBytes(P+Q+id);
             * byte [] seed = sha.ComputeHash(seed_bytes);
             *
             * StringBuilder sb = new StringBuilder();
             * foreach (byte b in seed)
             *  sb.Append(b.ToString("X2"));
             *
             * string seed_string = sb.ToString();
             *
             * byte[] PI_bytes = System.Text.Encoding.UTF8.GetBytes(seed_string+N);
             * byte [] PI = sha.ComputeHash(PI_bytes);
             *
             * sb = new StringBuilder();
             * foreach (byte b in PI)
             *  sb.Append(b.ToString("X2"));
             *
             * string PI_string = sb.ToString();
             *
             * string[] PIC = {PI_string, N.ToString(), seed_string};
             *
             * Console.WriteLine("PI:\t" + PIC[0]);
             * Console.WriteLine("N:\t" + PIC[1]);
             * Console.WriteLine("Seed:\t" + PIC[2]);
             * Console.WriteLine();*/


            // Enter the listening loop.

            /*while(true)
             * {*/
            /*Console.Write("Waiting for a connection... ");
             *
             * // Perform a blocking call to accept requests.
             * // You could also use server.AcceptSocket() here.
             *
             *
             * int i;
             *
             * byte[] msg = System.Text.Encoding.ASCII.GetBytes("010");
             *
             * // Send back a response.
             * stream.Write(msg, 0, msg.Length);
             * Console.WriteLine("Sent: {0}", data);
             *
             *
             *
             *
             * // Loop to receive all the data sent by the client.*/
            /*while((i = stream.Read(bytes, 0, bytes.Length))!=0)
             * {
             *  // Translate data bytes to a ASCII string.
             *  data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
             *  Console.WriteLine("Received: {0}", data);
             *
             *  // Process the data sent by the client.
             *  data = data.ToUpper();
             *
             *  byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
             *
             *  // Send back a response.
             *  stream.Write(msg, 0, msg.Length);
             *  Console.WriteLine("Sent: {0}", data);
             * }*/

            // Shutdown and end connection
            //client.Close();
            //}

            /*}
             * catch(SocketException e)
             * {
             *  Console.WriteLine("SocketException: {0}", e);
             * }
             * finally
             * {
             * // Stop listening for new clients.
             *  server.Stop();
             * }*/

            /*Console.WriteLine("\nHit enter to continue...");
             * Console.Read();*/
        }