Beispiel #1
0
        public void MinAndMaxValues()
        {
            var max       = BigInteger.Pow(2, 255) - 1; // should work
            var max_plus  = max + 1;                    // should throw
            var min       = -max;                       // should work
            var min_minus = min - 1;                    // should throw

            for (var keySize = 384; keySize <= 1088; keySize += 8)
            {
                var algorithm = new Paillier
                {
                    KeySize = keySize
                };

                var encryptAlgorithm = new Paillier();
                encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false));

                var decryptAlgorithm = new Paillier();
                decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true));


                // MAX
                var max_enc = encryptAlgorithm.EncryptData(max);
                var max_dec = decryptAlgorithm.DecryptData(max_enc);
                Assert.True(max_dec == max, $"{Environment.NewLine}{Environment.NewLine}" +
                            $"Algorithm parameters (TRUE):{Environment.NewLine}" +
                            $"{algorithm.ToXmlString(true)}{Environment.NewLine}{Environment.NewLine}" +
                            $"max     : {max}{Environment.NewLine}{Environment.NewLine}" +
                            $"max_dec : {max_dec}");

                // MIN
                var min_enc = encryptAlgorithm.EncryptData(min);
                var min_dec = decryptAlgorithm.DecryptData(min_enc);
                Assert.True(min_dec == min, $"{Environment.NewLine}{Environment.NewLine}" +
                            $"Algorithm parameters (TRUE):{Environment.NewLine}" +
                            $"{algorithm.ToXmlString(true)}{Environment.NewLine}{Environment.NewLine}" +
                            $"min     : {min}{Environment.NewLine}{Environment.NewLine}" +
                            $"min_dec : {min_dec}");

                // MAX + 1
                Assert.Throws <ArgumentException>(() => encryptAlgorithm.EncryptData(max_plus));

                // MIN - 1
                Assert.Throws <ArgumentException>(() => encryptAlgorithm.EncryptData(min_minus));

                algorithm.Dispose();
                encryptAlgorithm.Dispose();
                decryptAlgorithm.Dispose();
            }
        }
Beispiel #2
0
        public void TestRandomBigFraction()
        {
            for (var i = 0; i < Globals.iterations; i++)
            {
                for (var keySize = 384; keySize <= 1088; keySize += 8)
                {
                    var algorithm = new Paillier
                    {
                        KeySize = keySize
                    };

                    var encryptAlgorithm = new Paillier();
                    encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false));

                    var decryptAlgorithm = new Paillier();
                    decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true));

                    var n = new BigInteger().GenRandomBits(rnd.Next(1, algorithm.KeyStruct.getMaxPlaintextBits() - 1), rng);
                    var d = ((rnd.Next() % 2) + 1) * 10;
                    var f = new BigFraction(n, d);
                    if (rnd.Next() % 2 == 0) // random sign
                    {
                        f *= -1;
                    }

                    var f_enc = encryptAlgorithm.EncryptData(f);
                    var f_dec = decryptAlgorithm.DecryptData(f_enc);

                    Assert.True(f == f_dec, $"{Environment.NewLine}{Environment.NewLine}" +
                                $"Algorithm parameters (TRUE):{Environment.NewLine}" +
                                $"{algorithm.ToXmlString(true)}{Environment.NewLine}{Environment.NewLine}" +
                                $"f     : {f}{Environment.NewLine}{Environment.NewLine}" +
                                $"f_dec : {f_dec}");

                    algorithm.Dispose();
                    encryptAlgorithm.Dispose();
                    decryptAlgorithm.Dispose();
                }
            }
        }
Beispiel #3
0
        public void TestRandomBigInteger()
        {
            for (var i = 0; i < Globals.iterations; i++)
            {
                for (var keySize = 384; keySize <= 1088; keySize += 8)
                {
                    var algorithm = new Paillier
                    {
                        KeySize = keySize
                    };

                    var encryptAlgorithm = new Paillier();
                    encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false));

                    var decryptAlgorithm = new Paillier();
                    decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true));

                    var z = new BigInteger();

                    z = z.GenRandomBits(rnd.Next(1, algorithm.KeyStruct.getMaxPlaintextBits() - 1), rng);
                    if (rnd.Next() % 2 == 0) // random sign
                    {
                        z = -z;
                    }

                    var z_enc = encryptAlgorithm.EncryptData(z);
                    var z_dec = decryptAlgorithm.DecryptData(z_enc);

                    Assert.True(z == z_dec, $"{Environment.NewLine}{Environment.NewLine}" +
                                $"Algorithm parameters (TRUE):{Environment.NewLine}" +
                                $"{algorithm.ToXmlString(true)}{Environment.NewLine}{Environment.NewLine}" +
                                $"z     : {z}{Environment.NewLine}{Environment.NewLine}" +
                                $"z_dec : {z_dec}");

                    algorithm.Dispose();
                    encryptAlgorithm.Dispose();
                    decryptAlgorithm.Dispose();
                }
            }
        }
Beispiel #4
0
        public void TestZero()
        {
            for (var keySize = 384; keySize <= 1088; keySize += 8)
            {
                var algorithm = new Paillier
                {
                    KeySize = keySize
                };

                var encryptAlgorithm = new Paillier();
                encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false));

                var decryptAlgorithm = new Paillier();
                decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true));

                var z = new BigInteger(0);
                var r = new BigInteger(rnd.Next(1, 65536));

                var z_enc = encryptAlgorithm.EncryptData(z);
                var z_dec = decryptAlgorithm.DecryptData(z_enc);

                Assert.Equal(z, z_dec);

                var r_enc   = encryptAlgorithm.EncryptData(r);
                var zar_enc = decryptAlgorithm.Addition(z_enc, r_enc);
                var raz_enc = decryptAlgorithm.Addition(r_enc, z_enc);
                var zar     = decryptAlgorithm.DecryptData(zar_enc);
                var raz     = decryptAlgorithm.DecryptData(raz_enc);

                Assert.Equal(r, zar);
                Assert.Equal(r, raz);

                algorithm.Dispose();
                encryptAlgorithm.Dispose();
                decryptAlgorithm.Dispose();
            }
        }
Beispiel #5
0
        public void TestMultiplication_BatchFrac()
        {
            var rnd = new Random();
            var rng = new RNGCryptoServiceProvider();

            for (var i = 0; i < Globals.iterations; i++)
            {
                for (var keySize = 384; keySize <= 1088; keySize += 8)
                {
                    var algorithm = new Paillier
                    {
                        KeySize = keySize
                    };

                    var encryptAlgorithm = new Paillier();
                    encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false));

                    var decryptAlgorithm = new Paillier();
                    decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true));


                    BigFraction a, b;
                    do
                    {
                        var n = new BigInteger().GenRandomBits(rnd.Next(1, algorithm.KeyStruct.getMaxPlaintextBits() / 4), rng);
                        var d = ((rnd.Next() % 2) + 1) * 10;
                        a = new BigFraction(n, d);
                    } while (a == 0);
                    do
                    {
                        var n = new BigInteger().GenRandomBits(rnd.Next(1, algorithm.KeyStruct.getMaxPlaintextBits() / 4), rng);
                        var d = ((rnd.Next() % 2) + 1) * 10;
                        b = new BigFraction(n, d);
                    } while (b == 0);

                    if (rnd.Next() % 2 == 0) // randomly change signs
                    {
                        a = -a;
                    }
                    if (rnd.Next() % 2 == 0)
                    {
                        b = -b;
                    }

                    var a_enc = encryptAlgorithm.EncryptData(a);
                    var b_enc = encryptAlgorithm.EncryptData(b);


                    // Addition
                    var aab_enc = decryptAlgorithm.Addition(a_enc, b_enc);
                    var aab_dec = decryptAlgorithm.DecryptData(aab_enc);
                    Assert.True(aab_dec == a + b, $"{Environment.NewLine}{Environment.NewLine}" +
                                $"Algorithm parameters (TRUE):{Environment.NewLine}" +
                                $"{algorithm.ToXmlString(true)}{Environment.NewLine}{Environment.NewLine}" +
                                $"a       : {a}{Environment.NewLine}{Environment.NewLine}" +
                                $"b       : {b}{Environment.NewLine}{Environment.NewLine}" +
                                $"a + b   : {a + b}{Environment.NewLine}{Environment.NewLine}" +
                                $"aab_dec : {aab_dec}");

                    var baa_enc = decryptAlgorithm.Addition(b_enc, a_enc); // verify transitivity
                    var baa_dec = decryptAlgorithm.DecryptData(baa_enc);
                    Assert.True(baa_dec == a + b, $"{Environment.NewLine}{Environment.NewLine}" +
                                $"Algorithm parameters (TRUE):{Environment.NewLine}" +
                                $"{algorithm.ToXmlString(true)}{Environment.NewLine}{Environment.NewLine}" +
                                $"a       : {a}{Environment.NewLine}{Environment.NewLine}" +
                                $"b       : {b}{Environment.NewLine}{Environment.NewLine}" +
                                $"b + a   : {b + a}{Environment.NewLine}{Environment.NewLine}" +
                                $"baa_dec : {baa_dec}");

                    algorithm.Dispose();
                    encryptAlgorithm.Dispose();
                    decryptAlgorithm.Dispose();
                }
            }
        }