Esempio n. 1
0
        public void TransformPlainToFromNTTNET()
        {
            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 evaluator = new Evaluator(parms, MemoryPoolHandle.AcquireNew());
            var plain     = new BigPoly(65, 1);

            plain.Set("0");
            evaluator.TransformToNTT(plain);
            Assert.IsTrue(plain.ToString() == "0");
            evaluator.TransformFromNTT(plain);
            Assert.IsTrue(plain.ToString() == "0");

            plain.Set("1");
            evaluator.TransformToNTT(plain);
            for (int i = 0; i < 64; i++)
            {
                Assert.IsTrue(plain[i].ToString() == "1");
            }
            Assert.IsTrue(plain[64].ToString() == "0");
            evaluator.TransformFromNTT(plain);
            Assert.IsTrue(plain.ToString() == "1");

            plain.Set("2");
            evaluator.TransformToNTT(plain);
            for (int i = 0; i < 64; i++)
            {
                Assert.IsTrue(plain[i].ToString() == "2");
            }
            Assert.IsTrue(plain[64].ToString() == "0");
            evaluator.TransformFromNTT(plain);
            Assert.IsTrue(plain.ToString() == "2");

            plain.Set("Fx^10 + Ex^9 + Dx^8 + Cx^7 + Bx^6 + Ax^5 + 1x^4 + 2x^3 + 3x^2 + 4x^1 + 5");
            evaluator.TransformToNTT(plain);
            evaluator.TransformFromNTT(plain);
            Assert.IsTrue(plain.ToString() == "Fx^10 + Ex^9 + Dx^8 + Cx^7 + Bx^6 + Ax^5 + 1x^4 + 2x^3 + 3x^2 + 4x^1 + 5");
        }
Esempio n. 2
0
        public void FVKeyGenerationNoEVKNET()
        {
            var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());

            parms.SetDecompositionBitCount(0);
            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(0);

            Assert.IsFalse(keygen.PublicKey[0].IsZero);
            Assert.IsFalse(keygen.PublicKey[1].IsZero);
            Assert.IsFalse(keygen.SecretKey.IsZero);
        }
Esempio n. 3
0
        public void TransformEncryptedToFromNTTNET()
        {
            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 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 plain  = new BigPoly(65, 1);
            var cipher = new BigPolyArray(2, 65, 1);

            plain.Set("0");
            encryptor.Encrypt(plain, cipher);
            evaluator.TransformToNTT(cipher);
            evaluator.TransformFromNTT(cipher);
            decryptor.Decrypt(cipher, plain);
            Assert.IsTrue(plain.ToString() == "0");

            plain.Set("1");
            encryptor.Encrypt(plain, cipher);
            evaluator.TransformToNTT(cipher);
            evaluator.TransformFromNTT(cipher);
            decryptor.Decrypt(cipher, plain);
            Assert.IsTrue(plain.ToString() == "1");

            plain.Set("Fx^10 + Ex^9 + Dx^8 + Cx^7 + Bx^6 + Ax^5 + 1x^4 + 2x^3 + 3x^2 + 4x^1 + 5");
            encryptor.Encrypt(plain, cipher);
            evaluator.TransformToNTT(cipher);
            evaluator.TransformFromNTT(cipher);
            decryptor.Decrypt(cipher, plain);
            Assert.IsTrue(plain.ToString() == "Fx^10 + Ex^9 + Dx^8 + Cx^7 + Bx^6 + Ax^5 + 1x^4 + 2x^3 + 3x^2 + 4x^1 + 5");
        }
Esempio n. 4
0
        public void FVEncryptSquareDecryptNET()
        {
            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 BalancedEncoder(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(1));
            var product    = evaluator.Square(encrypted1);

            Assert.AreEqual(1UL, encoder.DecodeUInt64(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(0));
            product    = evaluator.Square(encrypted1);
            Assert.AreEqual(0UL, encoder.DecodeUInt64(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(-5));
            product    = evaluator.Square(encrypted1);
            Assert.AreEqual(25UL, encoder.DecodeUInt64(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(-1));
            product    = evaluator.Square(encrypted1);
            Assert.AreEqual(1UL, encoder.DecodeUInt64(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(123));
            product    = evaluator.Square(encrypted1);
            Assert.AreEqual(15129UL, encoder.DecodeUInt64(decryptor.Decrypt(product)));
        }
Esempio n. 5
0
        public void FVEncryptAddsNoiseNET()
        {
            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 Encoder = new BinaryEncoder(parms.PlainModulus, MemoryPoolHandle.AcquireNew());

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

            keygen.Generate();

            var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew());

            // however, this line is fine
            Assert.AreEqual(encryptor.PublicKey[0], keygen.PublicKey[0]);
            Assert.AreEqual(encryptor.PublicKey[1], keygen.PublicKey[1]);

            var encrypted1 = encryptor.Encrypt(Encoder.Encode(0x12345678));
            var encrypted2 = encryptor.Encrypt(Encoder.Encode(0x12345678));

            // this is what we want to check
            Assert.AreNotEqual(encrypted1[0], encrypted2[0]);
            Assert.AreNotEqual(encrypted1[1], encrypted2[1]);


            var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew());

            Assert.AreEqual(0x12345678U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted1)));
            Assert.AreEqual(0x12345678U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted2)));
        }
Esempio n. 6
0
        public void FVEncryptExponentiateDecryptNET()
        {
            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(1);

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

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

            var encrypted = encryptor.Encrypt(encoder.Encode(5));
            var power     = evaluator.Exponentiate(encrypted, 1);

            Assert.AreEqual(5, encoder.DecodeInt32(decryptor.Decrypt(power)));

            encrypted = encryptor.Encrypt(encoder.Encode(7));
            power     = evaluator.Exponentiate(encrypted, 2);
            Assert.AreEqual(49, encoder.DecodeInt32(decryptor.Decrypt(power)));

            encrypted = encryptor.Encrypt(encoder.Encode(-7));
            power     = evaluator.Exponentiate(encrypted, 3);
            Assert.AreEqual(-343, encoder.DecodeInt32(decryptor.Decrypt(power)));
        }
        public void SaveLoadEncryptionParamsNET()
        {
            var stream = new MemoryStream();

            // Create encryption parameters.
            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);

            var parms2 = new EncryptionParameters(MemoryPoolHandle.AcquireNew());

            stream.Seek(0, SeekOrigin.Begin);
            parms.Save(stream);
            stream.Seek(0, SeekOrigin.Begin);
            parms2.Load(stream);

            Assert.AreEqual(parms.DecompositionBitCount, parms2.DecompositionBitCount);
            Assert.AreEqual(parms.NoiseStandardDeviation, parms2.NoiseStandardDeviation);
            Assert.AreEqual(parms.NoiseMaxDeviation, parms2.NoiseMaxDeviation);
            Assert.AreEqual(parms.CoeffModulus, parms2.CoeffModulus);

            // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY
            //Assert.AreEqual(parms.AuxCoeffModulus, parms2.AuxCoeffModulus);

            Assert.AreEqual(parms.PlainModulus, parms2.PlainModulus);
            Assert.AreEqual(parms.PolyModulus, parms2.PolyModulus);
        }
        public void EncryptionParamsWriteReadNET()
        {
            // Create encryption parameters.
            var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());

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

            var coeffModulus = new BigUInt(48);

            coeffModulus.Set("7FFFFC801");
            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);

            Assert.AreEqual(4, parms.DecompositionBitCount);
            Assert.AreEqual(3.19, parms.NoiseStandardDeviation);
            Assert.AreEqual(35.06, parms.NoiseMaxDeviation);
            Assert.AreEqual("7FFFFC801", parms.CoeffModulus.ToString());

            // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY
            //Assert.AreEqual("FFFFFFFFC001", parms.AuxCoeffModulus.ToString());

            Assert.AreEqual("40", parms.PlainModulus.ToString());
            Assert.AreEqual("1x^64 + 1", parms.PolyModulus.ToString());
        }
Esempio n. 9
0
        public void FVEncryptDecryptNET()
        {
            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 Encoder = new BinaryEncoder(parms.PlainModulus, MemoryPoolHandle.AcquireNew());

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

            keygen.Generate();

            var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew());

            Assert.AreEqual(encryptor.PublicKey[0], keygen.PublicKey[0]);
            Assert.AreEqual(encryptor.PublicKey[1], keygen.PublicKey[1]);

            var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew());

            Assert.AreEqual(decryptor.SecretKey, keygen.SecretKey);

            var encrypted = encryptor.Encrypt(Encoder.Encode(0x12345678));

            Assert.AreEqual(0x12345678U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(0));
            Assert.AreEqual(0U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(1));
            Assert.AreEqual(1U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(2));
            Assert.AreEqual(2U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(0x7FFFFFFFFFFFFFFDUL));
            Assert.AreEqual(0x7FFFFFFFFFFFFFFDUL, Encoder.DecodeUInt64(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(0x7FFFFFFFFFFFFFFEUL));
            Assert.AreEqual(0x7FFFFFFFFFFFFFFEUL, Encoder.DecodeUInt64(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(0x7FFFFFFFFFFFFFFFUL));
            Assert.AreEqual(0x7FFFFFFFFFFFFFFFUL, Encoder.DecodeUInt64(decryptor.Decrypt(encrypted)));
        }
        public void GetQualifiersNET()
        {
            {
                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 qualifiers = parms.Qualifiers;
                Assert.IsTrue(qualifiers.ParametersSet);
                Assert.IsTrue(qualifiers.EnableRelinearization);
                Assert.IsTrue(qualifiers.EnableNussbaumer);
                Assert.IsTrue(qualifiers.EnableNTT);

                // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY
                //Assert.IsTrue(qualifiers.EnableNTTInMultiply);

                Assert.IsFalse(qualifiers.EnableBatching);
            }

            {
                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(64, 1);
                polyModulus[0].Set(1);
                polyModulus[63].Set(1);
                parms.SetPolyModulus(polyModulus);

                parms.Validate();

                var qualifiers = parms.Qualifiers;
                Assert.IsFalse(qualifiers.ParametersSet);
                Assert.IsFalse(qualifiers.EnableRelinearization);
                Assert.IsFalse(qualifiers.EnableNussbaumer);
                Assert.IsFalse(qualifiers.EnableNTT);

                // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY
                //Assert.IsFalse(qualifiers.EnableNTTInMultiply);

                Assert.IsFalse(qualifiers.EnableBatching);
            }

            {
                var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());
                parms.SetDecompositionBitCount(4);
                parms.SetNoiseStandardDeviation(3.19);
                parms.SetNoiseMaxDeviation(35.06);

                var coeffModulus = new BigUInt(48);
                coeffModulus.Set("0");
                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 qualifiers = parms.Qualifiers;
                Assert.IsFalse(qualifiers.ParametersSet);
                Assert.IsFalse(qualifiers.EnableRelinearization);
                Assert.IsFalse(qualifiers.EnableNussbaumer);
                Assert.IsFalse(qualifiers.EnableNTT);

                // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY
                //Assert.IsFalse(qualifiers.EnableNTTInMultiply);

                Assert.IsFalse(qualifiers.EnableBatching);
            }

            {
                var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());
                parms.SetDecompositionBitCount(0);
                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 qualifiers = parms.Qualifiers;
                Assert.IsTrue(qualifiers.ParametersSet);
                Assert.IsFalse(qualifiers.EnableRelinearization);
                Assert.IsTrue(qualifiers.EnableNussbaumer);
                Assert.IsTrue(qualifiers.EnableNTT);

                // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY
                //Assert.IsTrue(qualifiers.EnableNTTInMultiply);

                Assert.IsFalse(qualifiers.EnableBatching);
            }

            {
                var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());
                parms.SetDecompositionBitCount(4);
                parms.SetNoiseStandardDeviation(-3.19);
                parms.SetNoiseMaxDeviation(35.06);

                var coeffModulus = new BigUInt(48);
                coeffModulus.Set("7FFFFC801");
                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 qualifiers = parms.Qualifiers;
                Assert.IsFalse(qualifiers.ParametersSet);
                Assert.IsFalse(qualifiers.EnableRelinearization);
                Assert.IsFalse(qualifiers.EnableNussbaumer);
                Assert.IsFalse(qualifiers.EnableNTT);

                // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY
                //Assert.IsFalse(qualifiers.EnableNTTInMultiply);

                Assert.IsFalse(qualifiers.EnableBatching);
            }

            {
                var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());
                parms.SetDecompositionBitCount(0);
                parms.SetNoiseStandardDeviation(3.19);
                parms.SetNoiseMaxDeviation(35.06);

                var coeffModulus = new BigUInt(48);
                coeffModulus.Set("7FFFFC801");
                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 qualifiers = parms.Qualifiers;
                Assert.IsTrue(qualifiers.ParametersSet);
                Assert.IsFalse(qualifiers.EnableRelinearization);
                Assert.IsTrue(qualifiers.EnableNussbaumer);
                Assert.IsTrue(qualifiers.EnableNTT);

                // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY
                //Assert.IsFalse(qualifiers.EnableNTTInMultiply);

                Assert.IsFalse(qualifiers.EnableBatching);
            }

            {
                var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());
                parms.SetDecompositionBitCount(0);
                parms.SetNoiseStandardDeviation(3.19);
                parms.SetNoiseMaxDeviation(35.06);

                var coeffModulus = new BigUInt(48);
                coeffModulus.Set("7FFFFFFFF");
                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 qualifiers = parms.Qualifiers;
                Assert.IsTrue(qualifiers.ParametersSet);
                Assert.IsFalse(qualifiers.EnableRelinearization);
                Assert.IsTrue(qualifiers.EnableNussbaumer);
                Assert.IsFalse(qualifiers.EnableNTT);

                // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY
                //Assert.IsFalse(qualifiers.EnableNTTInMultiply);

                Assert.IsFalse(qualifiers.EnableBatching);
            }

            {
                var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());
                parms.SetDecompositionBitCount(0);
                parms.SetNoiseStandardDeviation(3.19);
                parms.SetNoiseMaxDeviation(35.06);

                var coeffModulus = new BigUInt(48);
                coeffModulus.Set("7FFFFFFFF");
                parms.SetCoeffModulus(coeffModulus);

                var plainModulus = new BigUInt(7);
                plainModulus.Set(12289);
                parms.SetPlainModulus(plainModulus);

                var polyModulus = new BigPoly(65, 1);
                polyModulus[0].Set(1);
                polyModulus[64].Set(1);
                parms.SetPolyModulus(polyModulus);

                parms.Validate();

                var qualifiers = parms.Qualifiers;
                Assert.IsTrue(qualifiers.ParametersSet);
                Assert.IsFalse(qualifiers.EnableRelinearization);
                Assert.IsTrue(qualifiers.EnableNussbaumer);
                Assert.IsFalse(qualifiers.EnableNTT);

                // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY
                //Assert.IsFalse(qualifiers.EnableNTTInMultiply);

                Assert.IsTrue(qualifiers.EnableBatching);
            }
        }
Esempio n. 11
0
        public void FVEncryptAddDecryptNET()
        {
            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(0x12345678));
            var encrypted2 = encryptor.Encrypt(encoder.Encode(0x54321));
            var sum        = evaluator.Add(encrypted1, encrypted2);

            Assert.AreEqual(0x12399999UL, encoder.DecodeUInt64(decryptor.Decrypt(sum)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(0));
            encrypted2 = encryptor.Encrypt(encoder.Encode(0));
            sum        = evaluator.Add(encrypted1, encrypted2);
            Assert.AreEqual(0UL, encoder.DecodeUInt64(decryptor.Decrypt(sum)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(0));
            encrypted2 = encryptor.Encrypt(encoder.Encode(5));
            sum        = evaluator.Add(encrypted1, encrypted2);
            Assert.AreEqual(5UL, encoder.DecodeUInt64(decryptor.Decrypt(sum)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(5));
            encrypted2 = encryptor.Encrypt(encoder.Encode(-3));
            sum        = evaluator.Add(encrypted1, encrypted2);
            Assert.AreEqual(2, encoder.DecodeInt32(decryptor.Decrypt(sum)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(-7));
            encrypted2 = encryptor.Encrypt(encoder.Encode(2));
            sum        = evaluator.Add(encrypted1, encrypted2);
            Assert.AreEqual(-5, encoder.DecodeInt32(decryptor.Decrypt(sum)));
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
        public void FVEncryptMultiplyManyDecryptNET()
        {
            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(1);

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

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

            var encrypted1 = encryptor.Encrypt(encoder.Encode(2));
            var encrypted2 = encryptor.Encrypt(encoder.Encode(-3));
            var encrypted3 = encryptor.Encrypt(encoder.Encode(4));
            var encrypteds = new List <BigPolyArray>()
            {
                encrypted1, encrypted2, encrypted3
            };
            var product = evaluator.MultiplyMany(encrypteds);

            Assert.AreEqual(-24, 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.MultiplyMany(encrypteds);
            Assert.AreEqual(153, 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.MultiplyMany(encrypteds);
            Assert.AreEqual(-434, 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.MultiplyMany(encrypteds);
            Assert.AreEqual(1, 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.MultiplyMany(encrypteds);
            Assert.AreEqual(0, encoder.DecodeInt32(decryptor.Decrypt(product)));
        }
Esempio n. 14
0
        public void FVKeyGenerationNET()
        {
            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(1);

            Assert.IsFalse(keygen.PublicKey[0].IsZero);
            Assert.IsFalse(keygen.PublicKey[1].IsZero);
            Assert.IsFalse(keygen.SecretKey.IsZero);
            Assert.AreEqual(1, keygen.EvaluationKeys.Size);
            Assert.AreEqual(12, keygen.EvaluationKeys.Keys[0].Item1.Size);
            Assert.AreEqual(12, keygen.EvaluationKeys.Keys[0].Item2.Size);
            for (int i = 0; i < 12; ++i)
            {
                Assert.IsFalse(keygen.EvaluationKeys[0].Item1[i].IsZero);
                Assert.IsFalse(keygen.EvaluationKeys[0].Item2[i].IsZero);
            }

            var publicKey      = keygen.PublicKey;
            var secretKey      = keygen.SecretKey;
            var evaluationKeys = keygen.EvaluationKeys;

            keygen.Generate(1);
            Assert.IsFalse(publicKey[0].Equals(keygen.PublicKey[0]));
            Assert.IsFalse(publicKey[1].Equals(keygen.PublicKey[1]));
            Assert.IsFalse(secretKey.Equals(keygen.SecretKey));
            for (int i = 0; i < 12; ++i)
            {
                Assert.IsFalse(keygen.EvaluationKeys[0].Item1[i].Equals(evaluationKeys[0].Item1[i]));
                Assert.IsFalse(keygen.EvaluationKeys[0].Item2[i].Equals(evaluationKeys[0].Item2[i]));
            }

            keygen.GenerateEvaluationKeys(3);
            Assert.AreEqual(3, keygen.EvaluationKeys.Size);
            for (int i = 0; i < 12; ++i)
            {
                Assert.AreEqual(12, keygen.EvaluationKeys.Keys[0].Item1.Size);
                Assert.AreEqual(12, keygen.EvaluationKeys.Keys[0].Item2.Size);
                Assert.AreEqual(12, keygen.EvaluationKeys.Keys[1].Item1.Size);
                Assert.AreEqual(12, keygen.EvaluationKeys.Keys[1].Item2.Size);
                Assert.AreEqual(12, keygen.EvaluationKeys.Keys[2].Item1.Size);
                Assert.AreEqual(12, keygen.EvaluationKeys.Keys[2].Item2.Size);
                Assert.IsFalse(keygen.EvaluationKeys[0].Item1[i].IsZero);
                Assert.IsFalse(keygen.EvaluationKeys[0].Item2[i].IsZero);
                Assert.IsFalse(keygen.EvaluationKeys[1].Item1[i].IsZero);
                Assert.IsFalse(keygen.EvaluationKeys[1].Item1[i].IsZero);
                Assert.IsFalse(keygen.EvaluationKeys[2].Item2[i].IsZero);
                Assert.IsFalse(keygen.EvaluationKeys[2].Item2[i].IsZero);
            }
        }