Beispiel #1
0
        public void DecodeTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PlainModulus = new SmallModulus(1024)
            };
            SEALContext    context = SEALContext.Create(parms);
            IntegerEncoder encoder = new IntegerEncoder(context);

            Plaintext plain = new Plaintext("0x^5 + 1x^4 + 1x^3 + 1x^1 + 0");

            Assert.AreEqual(6ul, plain.CoeffCount);

            ulong resultU64 = encoder.DecodeUInt64(plain);

            Assert.AreEqual(26UL, resultU64);

            long resultI64 = encoder.DecodeInt64(plain);

            Assert.AreEqual(26L, resultI64);

            uint resultU32 = encoder.DecodeUInt32(plain);

            Assert.AreEqual(26U, resultU32);

            int resultI32 = encoder.DecodeInt32(plain);

            Assert.AreEqual(26, resultI32);

            BigUInt bui = encoder.DecodeBigUInt(plain);

            Assert.IsNotNull(bui);
            Assert.AreEqual(0, bui.CompareTo(26ul));
        }
        static public List <List <long> > decryptValues(List <List <Ciphertext> > weihtedSums, SecretKey sk, SEALContext context)
        {
            List <List <long> > results = new List <List <long> >();

            Decryptor      decryptor = new Decryptor(context, sk);
            IntegerEncoder encoder   = new IntegerEncoder(context);

            int sample_ind = 0;

            foreach (List <Ciphertext> sample in weihtedSums)
            {
                List <long> resultsRow = new List <long>();
                foreach (Ciphertext encryptedClassScore in sample)
                {
                    Plaintext plainClassScore = new Plaintext();

                    if (decryptor.InvariantNoiseBudget(encryptedClassScore) == 0)
                    {
                        throw new Exception("Noise budget depleated in sample " + sample_ind + ". Aborting...");
                    }
                    decryptor.Decrypt(encryptedClassScore, plainClassScore);
                    long ClassScore = encoder.DecodeInt64(plainClassScore) / (1000 * 1000);

                    resultsRow.Add(ClassScore);
                }
                results.Add(resultsRow);
                sample_ind++;
            }

            return(results);
        }
Beispiel #3
0
        public void DecodeTest()
        {
            IntegerEncoder encoder = new IntegerEncoder(GlobalContext.BFVContext);

            Plaintext plain = new Plaintext("0x^5 + 1x^4 + 1x^3 + 1x^1 + 0");

            Assert.AreEqual(6ul, plain.CoeffCount);

            ulong resultU64 = encoder.DecodeUInt64(plain);

            Assert.AreEqual(26UL, resultU64);

            long resultI64 = encoder.DecodeInt64(plain);

            Assert.AreEqual(26L, resultI64);

            uint resultU32 = encoder.DecodeUInt32(plain);

            Assert.AreEqual(26U, resultU32);

            int resultI32 = encoder.DecodeInt32(plain);

            Assert.AreEqual(26, resultI32);

            BigUInt bui = encoder.DecodeBigUInt(plain);

            Assert.IsNotNull(bui);
            Assert.AreEqual(0, bui.CompareTo(26ul));
        }
Beispiel #4
0
        public static void ExampleParameterSelection()
        {
            PrintExampleBanner("Example: Automatic Parameter Selection");

            /*
             * Here we demonstrate the automatic parameter selection tool. Suppose we want to find parameters
             * that are optimized in a way that allows us to evaluate the polynomial 42x^3-27x+1. We need to know
             * the size of the input data, so let's assume that x is an integer with base-3 representation of length
             * at most 10.
             */
            Console.Write("Finding optimized parameters for computing 42x^3-27x+1 ... ");

            var chooserEncoder   = new ChooserEncoder();
            var chooserEvaluator = new ChooserEvaluator();

            /*
             * First create a ChooserPoly representing the input data. You can think of this modeling a freshly
             * encrypted cipheretext of a plaintext polynomial with length at most 10 coefficients, where the
             * coefficients have absolute value at most 1.
             */
            var cinput = new ChooserPoly(10, 1);

            // Compute the first term
            var ccubedInput = chooserEvaluator.Exponentiate(cinput, 3);
            var cterm1      = chooserEvaluator.MultiplyPlain(ccubedInput, chooserEncoder.Encode(42));

            // Compute the second term
            var cterm2 = chooserEvaluator.MultiplyPlain(cinput, chooserEncoder.Encode(27));

            // Subtract the first two terms
            var csum12 = chooserEvaluator.Sub(cterm1, cterm2);

            // Add the constant term 1
            var cresult = chooserEvaluator.AddPlain(csum12, chooserEncoder.Encode(1));

            // To find an optimized set of parameters, we use ChooserEvaluator.SelectParameters(...).
            var optimalParms = new EncryptionParameters();

            chooserEvaluator.SelectParameters(new List <ChooserPoly> {
                cresult
            }, 0, optimalParms);

            // We still need to validate the returned parameters
            optimalParms.Validate();

            Console.WriteLine("done.");

            // Let's print these to see what was recommended
            Console.WriteLine("Selected parameters:");
            Console.WriteLine("{{ poly_modulus: {0}", optimalParms.PolyModulus);
            Console.WriteLine("{{ coeff_modulus: {0}", optimalParms.CoeffModulus);
            Console.WriteLine("{{ plain_modulus: {0}", optimalParms.PlainModulus.ToDecimalString());
            Console.WriteLine("{{ decomposition_bit_count: {0}", optimalParms.DecompositionBitCount);
            Console.WriteLine("{{ noise_standard_deviation: {0}", optimalParms.NoiseStandardDeviation);
            Console.WriteLine("{{ noise_max_deviation: {0}", optimalParms.NoiseMaxDeviation);

            // Let's try to actually perform the homomorphic computation using the recommended parameters.
            // Generate keys.
            Console.WriteLine("Generating keys ...");
            var generator = new KeyGenerator(optimalParms);

            /*
             * Need to generate one evaluation key because below we will use Evaluator.Exponentiate(...),
             * which relinearizes after every multiplication it performs (see ExampleRelinearization()
             * for more details.
             */
            generator.Generate(1);
            Console.WriteLine("... key generation completed");
            var publicKey      = generator.PublicKey;
            var secretKey      = generator.SecretKey;
            var evaluationKeys = generator.EvaluationKeys;

            // Create the encoding/encryption tools
            var encoder   = new IntegerEncoder(optimalParms.PlainModulus, 3);
            var encryptor = new Encryptor(optimalParms, publicKey);
            var evaluator = new Evaluator(optimalParms, evaluationKeys);
            var decryptor = new Decryptor(optimalParms, secretKey);

            // Now perform the computations on real encrypted data.
            const int inputValue = 12345;
            var       plainInput = encoder.Encode(inputValue);

            Console.WriteLine("Encoded {0} as polynomial {1}", inputValue, plainInput);

            Console.Write("Encrypting ... ");
            var input = encryptor.Encrypt(plainInput);

            Console.WriteLine("done.");

            // Compute the first term
            Console.Write("Computing first term ... ");
            var cubedInput = evaluator.Exponentiate(input, 3);
            var term1      = evaluator.MultiplyPlain(cubedInput, encoder.Encode(42));

            Console.WriteLine("done.");

            // Compute the second term
            Console.Write("Computing second term ... ");
            var term2 = evaluator.MultiplyPlain(input, encoder.Encode(27));

            Console.WriteLine("done.");

            // Subtract the first two terms
            Console.Write("Subtracting first two terms ... ");
            var sum12 = evaluator.Sub(term1, term2);

            Console.WriteLine("done.");

            // Add the constant term 1
            Console.Write("Adding one ... ");
            var result = evaluator.AddPlain(sum12, encoder.Encode(1));

            Console.WriteLine("done.");

            // Decrypt and decode
            Console.Write("Decrypting ... ");
            var plainResult = decryptor.Decrypt(result);

            Console.WriteLine("done.");

            // Finally print the result
            Console.WriteLine("Polynomial 42x^3-27x+1 evaluated at x=12345: {0}", encoder.DecodeInt64(plainResult));

            // How much noise budget are we left with?
            Console.WriteLine("Noise budget in result: {0} bits", decryptor.InvariantNoiseBudget(result));
        }