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()); }
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()); }
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(); } }
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(); } }
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(); } }
public void TestMergeEncryption() { for (var i = 0; i < 100; i++) { var algorithm = new Paillier { KeySize = 384 }; BigInteger expectedSum = 0; var Nsq = new SqlBytes(algorithm.KeyStruct.NSquare.ToByteArray()); var udf1 = new PaillierAggregateSum(); udf1.Init(); for (var j = 0; j < 100; j++) { var value = new BigInteger(rnd.Next(1000000)) / new BigInteger(Math.Pow(10, rnd.Next() % 8)); if (rnd.Next(2) == 0) // randomly change signs { value = -value; } expectedSum += value; var value_enc = new SqlBytes(algorithm.EncryptData(value)); udf1.Accumulate(value_enc, Nsq); } Assert.Equal(expectedSum, algorithm.DecryptData(udf1.Terminate().Buffer)); } }
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(); } }
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(); } } }
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(); } } }
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(); } } }
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(); } } }