Beispiel #1
0
    public static void Rerun_SamekeyNumber(Paillier encryptAlgorithm, Paillier decryptAlgorithm,
                                           BigInteger A, BigInteger B)
    {
        byte[] A_bytes = A.getBytes();
        byte[] B_bytes = B.getBytes();

        //encrypt A and B
        byte[] A_enc_bytes = encryptAlgorithm.EncryptData(A.getBytes());
        byte[] B_enc_bytes = encryptAlgorithm.EncryptData(B.getBytes());

        // decrypt A and B
        byte[] A_dec_bytes = decryptAlgorithm.DecryptData(A_enc_bytes);
        byte[] B_dec_bytes = decryptAlgorithm.DecryptData(B_enc_bytes);

        // getting homomorphic addition result
        byte[] C_enc_bytes = encryptAlgorithm.Addition(A_enc_bytes, B_enc_bytes);
        byte[] C_dec_bytes = decryptAlgorithm.DecryptData(C_enc_bytes);

        // convert to BigInteger
        BigInteger A_dec = new BigInteger(A_dec_bytes);
        BigInteger B_dec = new BigInteger(B_dec_bytes);
        BigInteger C_dec = new BigInteger(C_dec_bytes);

        // printing out
        Console.WriteLine("Plaintext: {0} + {1} = {2}", A.ToString(), B.ToString(), (A + B).ToString());
        Console.WriteLine("Encrypted: {0} + {1} = {2}", A_dec.ToString(), B_dec.ToString(), C_dec.ToString());
    }
Beispiel #2
0
    public static void Rerun_SameKey(Paillier encryptAlgorithm, Paillier decryptAlgorithm)
    {
        var random = new Random();
        var A = new BigInteger(random.Next(32768));
        var B = new BigInteger(random.Next(32768));

        var A_bytes = A.getBytes();
        var B_bytes = B.getBytes();

        //encrypt A and B
        var A_enc_bytes = encryptAlgorithm.EncryptData(A.getBytes());
        var B_enc_bytes = encryptAlgorithm.EncryptData(B.getBytes());

        // decrypt A and B
        var A_dec_bytes = decryptAlgorithm.DecryptData(A_enc_bytes);
        var B_dec_bytes = decryptAlgorithm.DecryptData(B_enc_bytes);

        // getting homomorphic addition result
        var C_enc_bytes = encryptAlgorithm.Addition(A_enc_bytes, B_enc_bytes);
        var C_dec_bytes = decryptAlgorithm.DecryptData(C_enc_bytes);

        // convert to BigInteger
        var A_dec = new BigInteger(A_dec_bytes);
        var B_dec = new BigInteger(B_dec_bytes);
        var C_dec = new BigInteger(C_dec_bytes);

        // printing out
        Console.WriteLine("Plaintext: {0} + {1} = {2}", A.ToString(), B.ToString(), (A + B).ToString());
        Console.WriteLine("Encrypted: {0} + {1} = {2}", A_dec.ToString(), B_dec.ToString(), C_dec.ToString());
    }
Beispiel #3
0
        public void TestNegativeCases()
        {
            {
                var algorithm = new Paillier
                {
                    KeySize = 384
                };

                //Test negative number
                var z           = new BigInteger(8);
                var z_enc_bytes = algorithm.EncryptData(z);
                var z_dec       = algorithm.DecryptData(z_enc_bytes);
                Assert.Equal(z, z_dec);

                //Test positive number
                var z_2           = new BigInteger(10);
                var z_enc_bytes_2 = algorithm.EncryptData(z_2);
                var z_dec_2       = algorithm.DecryptData(z_enc_bytes_2);
                Assert.Equal(z_2, z_dec_2);

                //Test addition of positive and negative numbers
                var z_enc_addition = algorithm.Addition(z_enc_bytes, z_enc_bytes_2);
                var z_addition     = algorithm.DecryptData(z_enc_addition);
                Assert.Equal(z + z_2, z_addition);

                //Test subtraction of positive and negative numbers
                var z_enc_subtraction = algorithm.Subtraction(z_enc_bytes, z_enc_bytes_2);
                var z_subtraction     = algorithm.DecryptData(z_enc_subtraction);
                Assert.Equal(z - z_2, z_subtraction);

                algorithm.Dispose();
            }
        }
Beispiel #4
0
        public void TestNegativeFloatingPoint()
        {
            {
                var algorithm = new Paillier
                {
                    KeySize = 384
                };

                //Test 0 > plaintext > -1
                var z           = new BigFraction(BigInteger.Parse("-1001"), BigInteger.Parse("100"));
                var z_enc_bytes = algorithm.EncryptData(z);
                var z_dec       = algorithm.DecryptData(z_enc_bytes);
                Assert.Equal(z, z_dec);

                //Test plaintext < -1
                var z_2           = new BigFraction(BigInteger.Parse("-1000000001"), BigInteger.Parse("100"));
                var z_2_enc_bytes = algorithm.EncryptData(z_2);
                var z_2_dec       = algorithm.DecryptData(z_2_enc_bytes);
                Assert.Equal(z_2, z_2_dec);

                //Test addition
                var z_enc_addition = algorithm.Addition(z_enc_bytes, z_2_enc_bytes);
                var z_addition     = algorithm.DecryptData(z_enc_addition);
                Assert.Equal(z + z_2, z_addition);

                algorithm.Dispose();
            }
        }
Beispiel #5
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 #6
0
        public void TestSpecificCases()
        {
            {
                var algorithm = new Paillier
                {
                    KeySize = 384
                };

                var z = new BigFraction(BigInteger.Parse("1000"), BigInteger.Parse("1"));

                var z_enc_bytes = algorithm.EncryptData(z);
                var z_dec       = algorithm.DecryptData(z_enc_bytes);

                Assert.Equal(z, z_dec);

                algorithm.Dispose();
            }

            {
                // based on https://github.com/bazzilic/PaillierExt/issues/15
                for (var keySize = 384; keySize <= 1088; keySize += 8)
                {
                    var algorithm = new Paillier
                    {
                        KeySize = keySize
                    };

                    var sum = algorithm.EncryptData(new BigInteger(0));
                    var one = algorithm.EncryptData(new BigInteger(1));

                    for (var i = 0; i < 1000; i++)
                    {
                        sum = algorithm.Addition(sum, one);
                    }

                    var sums = algorithm.DecryptData(sum);

                    Assert.Equal(sums, new BigInteger(1000));

                    algorithm.Dispose();
                }
            }
        }
Beispiel #7
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();
                }
            }
        }