示例#1
0
        public void FVEncryptDecryptNET()
        {
            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 << 6);
            var polyModulus = parms.PolyModulus;

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

            var Encoder = new BinaryEncoder(parms.PlainModulus);

            var keygen = new KeyGenerator(parms);

            keygen.Generate();

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

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

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

            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)));
        }
示例#2
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)));
        }
示例#3
0
        public void FVEncryptAddsNoiseNET()
        {
            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 << 6);
            var polyModulus = parms.PolyModulus;

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

            var Encoder = new BinaryEncoder(parms.PlainModulus);

            var keygen = new KeyGenerator(parms);

            keygen.Generate();

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

            // 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);

            Assert.AreEqual(0x12345678U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted1)));
            Assert.AreEqual(0x12345678U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted2)));
        }
        public void BinaryEncodeDecodeUInt32NET()
        {
            var modulus = new BigUInt("FFFFFFFFFFFFFFFF");
            var encoder = new BinaryEncoder(modulus);

            var poly = encoder.Encode(0U);

            Assert.AreEqual(0, poly.GetSignificantCoeffCount());
            Assert.IsTrue(poly.IsZero);
            Assert.AreEqual(0U, encoder.DecodeUInt32(poly));

            var poly1 = encoder.Encode(1U);

            Assert.AreEqual(1, poly1.GetSignificantCoeffCount());
            Assert.AreEqual(1, poly1.CoeffBitCount);
            Assert.AreEqual("1", poly1.ToString());
            Assert.AreEqual(1U, encoder.DecodeUInt32(poly1));

            var poly2 = encoder.Encode(2U);

            Assert.AreEqual(2, poly2.GetSignificantCoeffCount());
            Assert.AreEqual(1, poly2.CoeffBitCount);
            Assert.AreEqual("1x^1", poly2.ToString());
            Assert.AreEqual(2U, encoder.DecodeUInt32(poly2));

            var poly3 = encoder.Encode(3U);

            Assert.AreEqual(2, poly3.GetSignificantCoeffCount());
            Assert.AreEqual(1, poly3.CoeffBitCount);
            Assert.AreEqual("1x^1 + 1", poly3.ToString());
            Assert.AreEqual(3U, encoder.DecodeUInt32(poly3));

            var poly4 = encoder.Encode(0xFFFFFFFFU);

            Assert.AreEqual(32, poly4.GetSignificantCoeffCount());
            Assert.AreEqual(1, poly4.CoeffBitCount);
            for (int i = 0; i < 32; ++i)
            {
                Assert.IsTrue("1" == poly4[i].ToString());
            }
            Assert.AreEqual(0xFFFFFFFFU, encoder.DecodeUInt32(poly4));

            var poly5 = encoder.Encode(0x80F02U);

            Assert.AreEqual(20, poly5.GetSignificantCoeffCount());
            Assert.AreEqual(1, poly5.CoeffBitCount);
            for (int i = 0; i < 20; ++i)
            {
                if (i == 19 || (i >= 8 && i <= 11) || i == 1)
                {
                    Assert.AreEqual("1", poly5[i].ToString());
                }
                else
                {
                    Assert.IsTrue(poly5[i].IsZero);
                }
            }
            Assert.AreEqual(0x80F02U, encoder.DecodeUInt32(poly5));

            var poly6 = new BigPoly(3, 10);

            poly6[0].Set(1);
            poly6[1].Set(500);
            poly6[2].Set(1023);
            Assert.AreEqual(1U + 500 * 2 + 1023 * 4, encoder.DecodeUInt32(poly6));

            modulus.Set(1024);
            var encoder2 = new BinaryEncoder(modulus);
            var poly7    = new BigPoly(4, 10);

            poly7[0].Set(1023); // -1   (*1)
            poly7[1].Set(512);  // -512 (*2)
            poly7[2].Set(511);  // 511  (*4)
            poly7[3].Set(1);    // 1    (*8)
            Assert.AreEqual((uint)(-1 + -512 * 2 + 511 * 4 + 1 * 8), encoder2.DecodeUInt32(poly7));
        }
示例#5
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)));
        }
示例#6
0
        public void BinaryEncodeDecodeUInt32NET()
        {
            var modulus = new SmallModulus(0xFFFFFFFFFFFFFFF);
            var encoder = new BinaryEncoder(modulus, MemoryPoolHandle.New());

            var poly = encoder.Encode(0U);

            Assert.AreEqual(0, poly.SignificantCoeffCount());
            Assert.IsTrue(poly.IsZero);
            Assert.AreEqual(0U, encoder.DecodeUInt32(poly));

            var poly1 = encoder.Encode(1U);

            Assert.AreEqual(1, poly1.SignificantCoeffCount());
            Assert.AreEqual("1", poly1.ToString());
            Assert.AreEqual(1U, encoder.DecodeUInt32(poly1));

            var poly2 = encoder.Encode(2U);

            Assert.AreEqual(2, poly2.SignificantCoeffCount());
            Assert.AreEqual("1x^1", poly2.ToString());
            Assert.AreEqual(2U, encoder.DecodeUInt32(poly2));

            var poly3 = encoder.Encode(3U);

            Assert.AreEqual(2, poly3.SignificantCoeffCount());
            Assert.AreEqual("1x^1 + 1", poly3.ToString());
            Assert.AreEqual(3U, encoder.DecodeUInt32(poly3));

            var poly4 = encoder.Encode(0xFFFFFFFFU);

            Assert.AreEqual(32, poly4.SignificantCoeffCount());
            for (int i = 0; i < 32; ++i)
            {
                Assert.IsTrue("1" == poly4[i].ToString());
            }
            Assert.AreEqual(0xFFFFFFFFU, encoder.DecodeUInt32(poly4));

            var poly5 = encoder.Encode(0x80F02U);

            Assert.AreEqual(20, poly5.SignificantCoeffCount());
            for (int i = 0; i < 20; ++i)
            {
                if (i == 19 || (i >= 8 && i <= 11) || i == 1)
                {
                    Assert.AreEqual("1", poly5[i].ToString());
                }
                else
                {
                    Assert.IsTrue(poly5[i] == 0);
                }
            }
            Assert.AreEqual(0x80F02U, encoder.DecodeUInt32(poly5));

            var poly6 = new Plaintext(3);

            poly6[0] = 1;
            poly6[1] = 500;
            poly6[2] = 1023;
            Assert.AreEqual(1U + 500 * 2 + 1023 * 4, encoder.DecodeUInt32(poly6));

            modulus.Set(1024);
            var encoder2 = new BinaryEncoder(modulus, MemoryPoolHandle.New());
            var poly7    = new Plaintext(4);

            poly7[0] = 1023; // -1   (*1)
            poly7[1] = 512;  // -512 (*2)
            poly7[2] = 511;  // 511  (*4)
            poly7[3] = 1;    // 1    (*8)
            Assert.AreEqual((uint)(-1 + -512 * 2 + 511 * 4 + 1 * 8), encoder2.DecodeUInt32(poly7));
        }