コード例 #1
0
        public void BalancedFractionalEncodeDecodeNET()
        {
            var polyModulus = new BigPoly("1x^1024 + 1");
            var modulus     = new BigUInt("10000");

            var poly  = new BigPoly();
            var poly1 = new BigPoly();
            var poly2 = new BigPoly();
            var poly3 = new BigPoly();
            var poly4 = new BigPoly();
            var poly5 = new BigPoly();

            for (ulong b = 3; b < 20; b += 2)
            {
                var encoder = new BalancedFractionalEncoder(modulus, polyModulus, 500, 50, b);

                poly = encoder.Encode(0.0);
                Assert.AreEqual(polyModulus.CoeffCount, poly.CoeffCount);
                Assert.IsTrue(poly.IsZero);
                Assert.AreEqual(0.0, encoder.Decode(poly));

                poly1 = encoder.Encode(-1.0);
                Assert.AreEqual(polyModulus.CoeffCount, poly1.CoeffCount);
                Assert.AreEqual(modulus.BitCount, poly1.CoeffBitCount);
                Assert.AreEqual(-1.0, encoder.Decode(poly1));

                poly2 = encoder.Encode(0.1);
                Assert.AreEqual(polyModulus.CoeffCount, poly2.CoeffCount);
                Assert.AreEqual(modulus.BitCount, poly2.CoeffBitCount);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly2) - 0.1) / 0.1 < 0.000001);

                poly3 = encoder.Encode(3.123);
                Assert.AreEqual(polyModulus.CoeffCount, poly3.CoeffCount);
                Assert.AreEqual(modulus.BitCount, poly3.CoeffBitCount);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly3) - 3.123) / 3.123 < 0.000001);

                poly4 = encoder.Encode(-123.456);
                Assert.AreEqual(polyModulus.CoeffCount, poly4.CoeffCount);
                Assert.AreEqual(modulus.BitCount, poly4.CoeffBitCount);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly4) + 123.456) / (-123.456) < 0.000001);

                poly5 = encoder.Encode(12345.98765);
                Assert.AreEqual(polyModulus.CoeffCount, poly5.CoeffCount);
                Assert.AreEqual(modulus.BitCount, poly5.CoeffBitCount);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly5) - 12345.98765) / 12345.98765 < 0.000001);
            }
        }
コード例 #2
0
ファイル: EncoderWrapper.cs プロジェクト: fboemer/PySEAL-1
        public void BalancedFractionalEncodeDecodeNET()
        {
            var polyModulus = new BigPoly("1x^1024 + 1");
            var modulus     = new SmallModulus(0x10000);

            var poly  = new Plaintext();
            var poly1 = new Plaintext();
            var poly2 = new Plaintext();
            var poly3 = new Plaintext();
            var poly4 = new Plaintext();
            var poly5 = new Plaintext();

            for (ulong b = 3; b < 20; b += 2)
            {
                var encoder = new BalancedFractionalEncoder(modulus, polyModulus, 500, 50, b, MemoryPoolHandle.New());

                poly = encoder.Encode(0.0);
                Assert.AreEqual(1, poly.CoeffCount);
                Assert.IsTrue(poly.IsZero);
                Assert.AreEqual(0.0, encoder.Decode(poly));

                poly1 = encoder.Encode(-1.0);
                Assert.AreEqual(-1.0, encoder.Decode(poly1));

                poly2 = encoder.Encode(0.1);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly2) - 0.1) / 0.1 < 0.000001);

                poly3 = encoder.Encode(3.123);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly3) - 3.123) / 3.123 < 0.000001);

                poly4 = encoder.Encode(-123.456);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly4) + 123.456) / (-123.456) < 0.000001);

                poly5 = encoder.Encode(12345.98765);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly5) - 12345.98765) / 12345.98765 < 0.000001);
            }
        }
        public void EncryptAddManyDecryptNET()
        {
            var parms = new EncryptionParameters
            {
                DecompositionBitCount  = 4,
                NoiseStandardDeviation = 3.19,
                NoiseMaxDeviation      = 35.06
            };
            var coeffModulus = parms.CoeffModulus;

            coeffModulus.Resize(48);
            coeffModulus.Set("FFFFFFFFC001");
            var plainModulus = parms.PlainModulus;

            plainModulus.Resize(7);
            plainModulus.Set(1 << 4);
            var polyModulus = parms.PolyModulus;

            polyModulus.Resize(64, 1);
            polyModulus[0].Set(1);
            polyModulus[63].Set(1);

            var keygen = new KeyGenerator(parms);

            keygen.Generate();

            var Encoder = new BinaryEncoder(parms.PlainModulus);

            var encryptor      = new Encryptor(parms, keygen.PublicKey);
            var evaluator      = new Evaluator(parms, keygen.EvaluationKeys);
            var keygenEvals    = keygen.EvaluationKeys;
            var evaluatorEvals = keygen.EvaluationKeys;

            for (int i = 0; i < keygen.EvaluationKeys.Count; ++i)
            {
                Assert.AreEqual(keygenEvals[i], evaluatorEvals[i]);
            }
            var decryptor = new Decryptor(parms, keygen.SecretKey);

            var encrypted1 = encryptor.Encrypt(Encoder.Encode(5));
            var encrypted2 = encryptor.Encrypt(Encoder.Encode(6));
            var encrypted3 = encryptor.Encrypt(Encoder.Encode(7));
            var encrypteds = new List <BigPoly>()
            {
                encrypted1, encrypted2, encrypted3
            };
            var product = evaluator.AddMany(encrypteds);

            Assert.AreEqual(18, Encoder.DecodeInt32(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(Encoder.Encode(-9));
            encrypted2 = encryptor.Encrypt(Encoder.Encode(-17));
            encrypteds = new List <BigPoly>()
            {
                encrypted1, encrypted2
            };
            product = evaluator.AddMany(encrypteds);
            Assert.AreEqual(-26, Encoder.DecodeInt32(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(Encoder.Encode(2));
            encrypted2 = encryptor.Encrypt(Encoder.Encode(-31));
            encrypted3 = encryptor.Encrypt(Encoder.Encode(7));
            encrypteds = new List <BigPoly>()
            {
                encrypted1, encrypted2, encrypted3
            };
            product = evaluator.AddMany(encrypteds);
            Assert.AreEqual(-22, Encoder.DecodeInt32(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(Encoder.Encode(1));
            encrypted2 = encryptor.Encrypt(Encoder.Encode(-1));
            encrypted3 = encryptor.Encrypt(Encoder.Encode(1));
            var encrypted4 = encryptor.Encrypt(Encoder.Encode(-1));

            encrypteds = new List <BigPoly>()
            {
                encrypted1, encrypted2, encrypted3, encrypted4
            };
            product = evaluator.AddMany(encrypteds);
            Assert.AreEqual(0, Encoder.DecodeInt32(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(Encoder.Encode(98765));
            encrypted2 = encryptor.Encrypt(Encoder.Encode(0));
            encrypted3 = encryptor.Encrypt(Encoder.Encode(12345));
            encrypted4 = encryptor.Encrypt(Encoder.Encode(34567));
            encrypteds = new List <BigPoly>()
            {
                encrypted1, encrypted2, encrypted3, encrypted4
            };
            product = evaluator.AddMany(encrypteds);
            Assert.AreEqual(145677, Encoder.DecodeInt32(decryptor.Decrypt(product)));

            BalancedFractionalEncoder fracEncoder = new BalancedFractionalEncoder(plainModulus, polyModulus, 10, 15);

            encrypted1 = encryptor.Encrypt(fracEncoder.Encode(3.1415));
            encrypted2 = encryptor.Encrypt(fracEncoder.Encode(12.345));
            encrypted3 = encryptor.Encrypt(fracEncoder.Encode(98.765));
            encrypted4 = encryptor.Encrypt(fracEncoder.Encode(1.1111));
            encrypteds = new List <BigPoly>()
            {
                encrypted1, encrypted2, encrypted3, encrypted4
            };
            product = evaluator.AddMany(encrypteds);
            Assert.IsTrue(System.Math.Abs(fracEncoder.Decode(decryptor.Decrypt(product)) - 115.3626) < 0.000001);
        }
コード例 #4
0
        public void FVEncryptAddManyDecryptNET()
        {
            var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());

            parms.SetDecompositionBitCount(4);
            parms.SetNoiseStandardDeviation(3.19);
            parms.SetNoiseMaxDeviation(35.06);

            var coeffModulus = new BigUInt(48);

            coeffModulus.Set("FFFFFFFFC001");
            parms.SetCoeffModulus(coeffModulus);

            var plainModulus = new BigUInt(7);

            plainModulus.Set(1 << 6);
            parms.SetPlainModulus(plainModulus);

            var polyModulus = new BigPoly(65, 1);

            polyModulus[0].Set(1);
            polyModulus[64].Set(1);
            parms.SetPolyModulus(polyModulus);

            parms.Validate();

            var keygen = new KeyGenerator(parms, MemoryPoolHandle.AcquireNew());

            keygen.Generate();

            var encoder = new BinaryEncoder(parms.PlainModulus, MemoryPoolHandle.AcquireNew());

            var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew());
            var evaluator = new Evaluator(parms, MemoryPoolHandle.AcquireNew());
            var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew());

            var encrypted1 = encryptor.Encrypt(encoder.Encode(5));
            var encrypted2 = encryptor.Encrypt(encoder.Encode(6));
            var encrypted3 = encryptor.Encrypt(encoder.Encode(7));
            var encrypteds = new List <BigPolyArray>()
            {
                encrypted1, encrypted2, encrypted3
            };
            var product = evaluator.AddMany(encrypteds);

            Assert.AreEqual(18, encoder.DecodeInt32(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(-9));
            encrypted2 = encryptor.Encrypt(encoder.Encode(-17));
            encrypteds = new List <BigPolyArray>()
            {
                encrypted1, encrypted2
            };
            product = evaluator.AddMany(encrypteds);
            Assert.AreEqual(-26, encoder.DecodeInt32(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(2));
            encrypted2 = encryptor.Encrypt(encoder.Encode(-31));
            encrypted3 = encryptor.Encrypt(encoder.Encode(7));
            encrypteds = new List <BigPolyArray>()
            {
                encrypted1, encrypted2, encrypted3
            };
            product = evaluator.AddMany(encrypteds);
            Assert.AreEqual(-22, encoder.DecodeInt32(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(1));
            encrypted2 = encryptor.Encrypt(encoder.Encode(-1));
            encrypted3 = encryptor.Encrypt(encoder.Encode(1));
            var encrypted4 = encryptor.Encrypt(encoder.Encode(-1));

            encrypteds = new List <BigPolyArray>()
            {
                encrypted1, encrypted2, encrypted3, encrypted4
            };
            product = evaluator.AddMany(encrypteds);
            Assert.AreEqual(0, encoder.DecodeInt32(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(98765));
            encrypted2 = encryptor.Encrypt(encoder.Encode(0));
            encrypted3 = encryptor.Encrypt(encoder.Encode(12345));
            encrypted4 = encryptor.Encrypt(encoder.Encode(34567));
            encrypteds = new List <BigPolyArray>()
            {
                encrypted1, encrypted2, encrypted3, encrypted4
            };
            product = evaluator.AddMany(encrypteds);
            Assert.AreEqual(145677, encoder.DecodeInt32(decryptor.Decrypt(product)));

            BalancedFractionalEncoder fracEncoder = new BalancedFractionalEncoder(plainModulus, polyModulus, 10, 15, MemoryPoolHandle.AcquireNew());

            encrypted1 = encryptor.Encrypt(fracEncoder.Encode(3.1415));
            encrypted2 = encryptor.Encrypt(fracEncoder.Encode(12.345));
            encrypted3 = encryptor.Encrypt(fracEncoder.Encode(98.765));
            encrypted4 = encryptor.Encrypt(fracEncoder.Encode(1.1111));
            encrypteds = new List <BigPolyArray>()
            {
                encrypted1, encrypted2, encrypted3, encrypted4
            };
            product = evaluator.AddMany(encrypteds);
            Assert.IsTrue(System.Math.Abs(fracEncoder.Decode(decryptor.Decrypt(product)) - 115.3626) < 0.000001);
        }
コード例 #5
0
        static void ExampleWeightedAverage()
        {
            PrintExampleBanner("Example: Weighted Average");

            // In this example we demonstrate computing a weighted average of 10 rational numbers.

            // The 10 rational numbers we use are:
            var rationalNumbers = new List <double> {
                3.1, 4.159, 2.65, 3.5897, 9.3, 2.3, 8.46, 2.64, 3.383, 2.795
            };

            // The 10 weights are:
            var coefficients = new List <double> {
                0.1, 0.05, 0.05, 0.2, 0.05, 0.3, 0.1, 0.025, 0.075, 0.05
            };

            // Create encryption parameters.
            var parms = new EncryptionParameters();

            parms.PolyModulus.Set("1x^2048 + 1");
            parms.CoeffModulus.Set(ChooserEvaluator.DefaultParameterOptions[2048]);
            parms.PlainModulus.Set(1 << 8);

            // Since we are not doing any encrypted*encrypted multiplication in this example,
            // the decomposition bit count has no practical significance.
            parms.DecompositionBitCount = 32;

            parms.NoiseStandardDeviation = ChooserEvaluator.DefaultNoiseStandardDeviation;
            parms.NoiseMaxDeviation      = 5 * parms.NoiseStandardDeviation;

            Console.WriteLine("Encryption parameters specify {0} coefficients with {1} bits per coefficient",
                              parms.PolyModulus.GetSignificantCoeffCount(), parms.CoeffModulus.GetSignificantBitCount());

            // Generate keys.
            Console.WriteLine("Generating keys...");
            var generator = new KeyGenerator(parms);

            generator.Generate();
            Console.WriteLine("... key generation completed");
            var publicKey      = generator.PublicKey;
            var secretKey      = generator.SecretKey;
            var evaluationKeys = generator.EvaluationKeys;

            /*
             * We will need a fractional encoder for dealing with the rational numbers.
             * Here we reserve 128 coefficients of the polynomial for the integral part (low-degree terms)
             * and 64 coefficients for the fractional part (high-degree terms).
             */
            var encoder = new BalancedFractionalEncoder(parms.PlainModulus, parms.PolyModulus, 128, 64);

            // Create the rest of the tools
            var encryptor = new Encryptor(parms, publicKey);
            var evaluator = new Evaluator(parms, evaluationKeys);
            var decryptor = new Decryptor(parms, secretKey);

            // First we encrypt the rational numbers
            Console.Write("Encrypting ... ");
            var encryptedRationals = new List <BigPoly>();

            for (int i = 0; i < 10; ++i)
            {
                var encodedNumber = encoder.Encode(rationalNumbers[i]);
                encryptedRationals.Add(encryptor.Encrypt(encodedNumber));
                Console.Write(rationalNumbers[i]);
                if (i < 9)
                {
                    Console.Write(", ");
                }
            }
            Console.WriteLine(".");

            // Next we encode the coefficients. There is no reason to encrypt these since they are not private data.
            Console.Write("Encoding ... ");
            var encodedCoefficients = new List <BigPoly>();

            for (int i = 0; i < 10; ++i)
            {
                encodedCoefficients.Add(encoder.Encode(coefficients[i]));
                Console.Write(coefficients[i]);
                if (i < 9)
                {
                    Console.Write(", ");
                }
            }
            Console.WriteLine(".");

            // We also need to encode 0.1. We will multiply the result by this to perform division by 10.
            var divByTen = encoder.Encode(0.1);

            // Now compute all the products of the encrypted rational numbers with the plaintext coefficients
            Console.Write("Computing products ... ");
            var encryptedProducts = new List <BigPoly>();

            for (int i = 0; i < 10; ++i)
            {
                /*
                 * We use Evaluator.MultiplyPlain(...) instead of Evaluator.Multiply(...) (which would
                 * require also the coefficient to be encrypted). This has much better noise growth
                 * behavior than multiplying two encrypted numbers does.
                 */
                var encPlainProduct = evaluator.MultiplyPlain(encryptedRationals[i], encodedCoefficients[i]);
                encryptedProducts.Add(encPlainProduct);
            }
            Console.WriteLine("done.");

            // Now we add together these products. The most convenient way to do that is
            // to use the function Evaluator.AddMany(...).
            Console.Write("Add up all 10 ciphertexts ... ");
            var encryptedDotProduct = evaluator.AddMany(encryptedProducts);

            Console.WriteLine("done");

            // Finally we divide by 10 to obtain the result.
            Console.Write("Divide by 10 ... ");
            var encryptedResult = evaluator.MultiplyPlain(encryptedDotProduct, divByTen);

            Console.WriteLine("done");

            // Decrypt
            Console.Write("Decrypting ... ");
            var plainResult = decryptor.Decrypt(encryptedResult);

            Console.WriteLine("done");

            // Print the answer
            double result = encoder.Decode(plainResult);

            Console.WriteLine("Weighted average: {0}", result);

            // How much noise did we end up with?
            Console.WriteLine("Noise in the result: {0}/{1} bits", Utilities.InherentNoise(encryptedResult, parms, secretKey).GetSignificantBitCount(),
                              Utilities.InherentNoiseMax(parms).GetSignificantBitCount());
        }