예제 #1
0
        public void BatchUnbatchPlaintextNET()
        {
            var parms = new EncryptionParameters();

            parms.PolyModulus  = "1x^64 + 1";
            parms.CoeffModulus = new List <SmallModulus> {
                DefaultParams.SmallMods60Bit(0)
            };
            parms.PlainModulus = 257;

            var context = new SEALContext(parms);

            Assert.IsTrue(context.Qualifiers.EnableBatching);

            var crtbuilder = new PolyCRTBuilder(context);

            Assert.AreEqual(64, crtbuilder.SlotCount);
            var plain = new Plaintext(crtbuilder.SlotCount);

            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                plain[i] = (UInt64)i;
            }

            crtbuilder.Compose(plain);
            crtbuilder.Decompose(plain);
            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                Assert.IsTrue(plain[i] == (UInt64)i);
            }

            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                plain[i] = (UInt64)5;
            }
            crtbuilder.Compose(plain);
            Assert.IsTrue(plain.ToString().Equals("5"));
            crtbuilder.Decompose(plain);
            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                Assert.IsTrue(plain[i] == (UInt64)5);
            }

            var short_plain = new Plaintext(20);

            for (int i = 0; i < 20; i++)
            {
                short_plain[i] = (UInt64)i;
            }
            crtbuilder.Compose(short_plain);
            crtbuilder.Decompose(short_plain);
            for (int i = 0; i < 20; i++)
            {
                Assert.IsTrue(short_plain[i] == (UInt64)i);
            }
            for (int i = 20; i < crtbuilder.SlotCount; i++)
            {
                Assert.IsTrue(short_plain[i] == 0UL);
            }
        }
예제 #2
0
        public static void ExampleBatching()
        {
            PrintExampleBanner("Example: Batching using CRT");

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

            /*
             * For PolyCRTBuilder it is necessary to have PlainModulus be a prime number congruent to 1 modulo
             * 2*degree(PolyModulus). We can use for example the following parameters:
             */
            parms.SetPolyModulus("1x^4096 + 1");
            parms.SetCoeffModulus(ChooserEvaluator.DefaultParameterOptions[4096]);
            parms.SetPlainModulus(40961);
            parms.Validate();

            // Create the PolyCRTBuilder
            var crtbuilder = new PolyCRTBuilder(parms);
            int slotCount  = crtbuilder.SlotCount;

            Console.WriteLine($"Encryption parameters allow {slotCount} slots.");

            // Create a list of values that are to be stored in the slots. We initialize all values to 0 at this point.
            var values = new List <BigUInt>(slotCount);

            for (int i = 0; i < slotCount; ++i)
            {
                values.Add(new BigUInt(parms.PlainModulus.BitCount, 0));
            }

            // Set the first few entries of the values list to be non-zero
            values[0].Set(2);
            values[1].Set(3);
            values[2].Set(5);
            values[3].Set(7);
            values[4].Set(11);
            values[5].Set(13);

            // Now compose these into one polynomial using PolyCRTBuilder
            Console.Write("Plaintext slot contents (slot, value): ");
            for (int i = 0; i < 6; ++i)
            {
                string toWrite = "(" + i.ToString() + ", " + values[i].ToDecimalString() + ")";
                toWrite += (i != 5) ? ", " : "\n";
                Console.Write(toWrite);
            }
            var plainComposedPoly = crtbuilder.Compose(values);

            // Let's do some homomorphic operations now. First we need all the encryption tools.
            // 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;

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

            // Encrypt plainComposed_poly
            Console.Write("Encrypting ... ");
            var encryptedComposedPoly = encryptor.Encrypt(plainComposedPoly);

            Console.WriteLine("done.");

            // Let's square and then decrypt the encryptedComposedPoly
            Console.Write("Squaring the encrypted polynomial ... ");
            var encryptedSquare = evaluator.Square(encryptedComposedPoly);

            Console.WriteLine("done.");

            Console.Write("Decrypting the squared polynomial ... ");
            var plainSquare = decryptor.Decrypt(encryptedSquare);

            Console.WriteLine("done.");

            // Print the squared slots
            crtbuilder.Decompose(plainSquare, values);
            Console.Write("Squared slot contents (slot, value): ");
            for (int i = 0; i < 6; ++i)
            {
                string toWrite = "(" + i.ToString() + ", " + values[i].ToDecimalString() + ")";
                toWrite += (i != 5) ? ", " : "\n";
                Console.Write(toWrite);
            }

            // Now let's try to multiply the squares with the plaintext coefficients (3, 1, 4, 1, 5, 9, 0, 0, ..., 0).
            // First create the coefficient list
            var plainCoeffList = new List <BigUInt>(slotCount);

            for (int i = 0; i < slotCount; ++i)
            {
                plainCoeffList.Add(new BigUInt(parms.PlainModulus.BitCount, 0));
            }

            plainCoeffList[0].Set(3);
            plainCoeffList[1].Set(1);
            plainCoeffList[2].Set(4);
            plainCoeffList[3].Set(1);
            plainCoeffList[4].Set(5);
            plainCoeffList[5].Set(9);

            // Use PolyCRTBuilder to compose plainCoeffList into a polynomial
            var plainCoeffPoly = crtbuilder.Compose(plainCoeffList);

            // Print the coefficient list
            Console.Write("Coefficient slot contents (slot, value): ");
            for (int i = 0; i < 6; ++i)
            {
                string toWrite = "(" + i.ToString() + ", " + plainCoeffList[i].ToDecimalString() + ")";
                toWrite += (i != 5) ? ", " : "\n";
                Console.Write(toWrite);
            }

            // Now use MultiplyPlain to multiply each encrypted slot with the corresponding coefficient
            Console.Write("Multiplying squared slots with the coefficients ... ");
            var encryptedScaledSquare = evaluator.MultiplyPlain(encryptedSquare, plainCoeffPoly);

            Console.WriteLine(" done.");

            // Decrypt it
            Console.Write("Decrypting the scaled squared polynomial ... ");
            var plainScaledSquare = decryptor.Decrypt(encryptedScaledSquare);

            Console.WriteLine("done.");

            // Print the scaled squared slots
            crtbuilder.Decompose(plainScaledSquare, values);
            Console.Write("Scaled squared slot contents (slot, value): ");
            for (int i = 0; i < 6; ++i)
            {
                string toWrite = "(" + i.ToString() + ", " + values[i].ToDecimalString() + ")";
                toWrite += (i != 5) ? ", " : "\n";
                Console.Write(toWrite);
            }

            // How much noise budget are we left with?
            Console.WriteLine("Noise budget in result: {0} bits", decryptor.InvariantNoiseBudget(encryptedScaledSquare));
        }
예제 #3
0
        public void BatchUnbatchIntVectorNET()
        {
            var parms = new EncryptionParameters();

            parms.PolyModulus  = "1x^64 + 1";
            parms.CoeffModulus = new List <SmallModulus> {
                DefaultParams.SmallMods60Bit(0)
            };
            parms.PlainModulus = 257;

            var context = new SEALContext(parms);

            Assert.IsTrue(context.Qualifiers.EnableBatching);

            var crtbuilder = new PolyCRTBuilder(context);

            Assert.AreEqual(64, crtbuilder.SlotCount);
            var plain_vec = new List <Int64>();

            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                plain_vec.Add((Int64)i * (1 - 2 * (i % 2)));
            }

            var plain = new Plaintext();

            crtbuilder.Compose(plain_vec, plain);
            var plain_vec2 = new List <Int64>();

            crtbuilder.Decompose(plain, plain_vec2);
            Assert.IsTrue(plain_vec.SequenceEqual(plain_vec2));

            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                plain_vec[i] = -5;
            }
            crtbuilder.Compose(plain_vec, plain);
            Assert.IsTrue(plain.ToString().Equals("FC"));
            crtbuilder.Decompose(plain, plain_vec2);
            Assert.IsTrue(plain_vec.SequenceEqual(plain_vec2));

            var short_plain_vec = new List <Int64>();

            for (int i = 0; i < 20; i++)
            {
                short_plain_vec.Add((Int64)i * (1 - 2 * (i % 2)));
            }
            crtbuilder.Compose(short_plain_vec, plain);
            var short_plain_vec2 = new List <Int64>();

            crtbuilder.Decompose(plain, short_plain_vec2);
            Assert.AreEqual(20, short_plain_vec.Count);
            Assert.AreEqual(64, short_plain_vec2.Count);
            for (int i = 0; i < 20; i++)
            {
                Assert.AreEqual(short_plain_vec[i], short_plain_vec2[i]);
            }
            for (int i = 20; i < crtbuilder.SlotCount; i++)
            {
                Assert.AreEqual(0L, short_plain_vec2[i]);
            }
        }