Ejemplo n.º 1
0
        public void MulTest()
        {
            Random random = new Random(1234);

            for (int i = 0; i <= 50000; i++)
            {
                int bits1 = random.Next(BigUInt <Pow2.N32> .Bits + 1);
                int bits2 = random.Next(BigUInt <Pow2.N32> .Bits - bits1);

                UInt32[] value1 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits1);
                UInt32[] value2 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits2);

                if (random.Next(2) < 1)
                {
                    value1[0] = 0;
                }

                if (random.Next(2) < 1)
                {
                    value2[0] = 0;
                }

                BigUInt <Pow2.N32> v1 = new BigUInt <Pow2.N32>(value1, enable_clone: false);
                BigUInt <Pow2.N32> v2 = new BigUInt <Pow2.N32>(value2, enable_clone: false);
                BigInteger         bi1 = v1, bi2 = v2;

                BigUInt <Pow2.N32> v = BigUInt <Pow2.N32> .Mul(v1, v2);

                BigInteger bi = bi1 * bi2;

                Console.WriteLine(bi);
                Console.WriteLine(v);
                Assert.AreEqual(bi, v);
            }
        }
Ejemplo n.º 2
0
        public void EmptyBigUIntNET()
        {
            var biguint = new BigUInt();

            Assert.AreEqual(0, biguint.BitCount);
            Assert.AreEqual(0ul, biguint.ByteCount);
            Assert.AreEqual(0ul, biguint.UInt64Count);
            Assert.IsTrue(biguint.UInt64Count == 0ul);
            Assert.AreEqual(0, biguint.GetSignificantBitCount());
            Assert.AreEqual("0", biguint.ToString());
            Assert.IsTrue(biguint.IsZero);
            Assert.IsFalse(biguint.IsAlias);
            biguint.SetZero();

            var biguint2 = new BigUInt();

            Assert.IsTrue(biguint.Equals(biguint2));

            biguint.Resize(1);
            Assert.AreEqual(1, biguint.BitCount);
            Assert.IsTrue(biguint.UInt64Count != 0ul);
            Assert.IsFalse(biguint.IsAlias);

            biguint.Resize(0);
            Assert.AreEqual(0, biguint.BitCount);
            Assert.IsTrue(biguint.UInt64Count == 0ul);
            Assert.IsFalse(biguint.IsAlias);
        }
Ejemplo n.º 3
0
        public void RoundDivTest()
        {
            Random random = new Random(1234);

            for (int i = 0; i <= 50000; i++)
            {
                int bits1 = random.Next(BigUInt <Pow2.N32> .Bits / 2 + 1, BigUInt <Pow2.N32> .Bits + 1);
                int bits2 = random.Next(BigUInt <Pow2.N32> .Bits - bits1);

                UInt32[] value1 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits1);
                UInt32[] value2 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits2);

                BigUInt <Pow2.N32> v1 = new BigUInt <Pow2.N32>(value1, enable_clone: false);
                BigUInt <Pow2.N32> v2 = new BigUInt <Pow2.N32>(value2, enable_clone: false);
                BigInteger         bi1 = v1, bi2 = v2;

                if (v2.IsZero)
                {
                    continue;
                }

                BigUInt <Pow2.N32> vdiv = BigUInt <Pow2.N32> .RoundDiv(v1, v2);

                BigInteger bidiv = bi1 / bi2, birem = bi1 % bi2;

                if (birem * 2 >= bi2)
                {
                    bidiv++;
                }

                Assert.AreEqual(bidiv, vdiv);
            }
        }
Ejemplo n.º 4
0
        public void DivULongTest()
        {
            Random random = new Random(1234);

            for (int i = 0; i <= 50000; i++)
            {
                int bits1 = random.Next(BigUInt <Pow2.N32> .Bits / 2 + 1, BigUInt <Pow2.N32> .Bits + 1);
                int bits2 = random.Next(64);

                UInt32[] value1 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits1);
                UInt32[] value2 = UIntUtil.Random(random, 2, bits2);

                BigUInt <Pow2.N32> v1 = new BigUInt <Pow2.N32>(value1, enable_clone: false);
                UInt64             v2 = UIntUtil.Pack(value2[1], value2[0]);
                BigInteger         bi1 = v1, bi2 = v2;

                if (v2 == 0)
                {
                    continue;
                }

                (BigUInt <Pow2.N32> vdiv, UInt64 vrem)
                    = BigUInt <Pow2.N32> .Div(v1, v2);

                BigInteger bidiv = bi1 / bi2, birem = bi1 - bi2 * bidiv;

                Assert.IsTrue(vrem < v2);

                Assert.AreEqual(birem, vrem, "rem");
                Assert.AreEqual(bidiv, vdiv, "div");

                Assert.AreEqual(v1, BigUInt <Pow2.N32> .Add(BigUInt <Pow2.N32> .Mul(vdiv, v2), vrem));
            }
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
0
        public void ExceptionsTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PlainModulus = new SmallModulus(4ul)
            };
            SEALContext    context      = SEALContext.Create(parms);
            SEALContext    context_null = null;
            IntegerEncoder enc          = new IntegerEncoder(context);
            IntegerEncoder copy_null    = null;
            BigUInt        bui_null     = null;
            BigUInt        bui          = new BigUInt(5ul);
            Plaintext      plain        = new Plaintext();

            Assert.ThrowsException <ArgumentNullException>(() => enc = new IntegerEncoder(context_null));
            Assert.ThrowsException <ArgumentNullException>(() => enc = new IntegerEncoder(copy_null));

            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1ul, null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1L, null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1, null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1u, null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(bui_null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(bui, null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(bui_null, plain));

            Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeUInt32(null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeUInt64(null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeInt32(null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeInt64(null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeBigUInt(null));
        }
Ejemplo n.º 7
0
        public void ExceptionsTest()
        {
            SEALContext    context      = GlobalContext.BFVContext;
            SEALContext    context_null = null;
            IntegerEncoder enc          = new IntegerEncoder(context);
            BigUInt        bui_null     = null;
            BigUInt        bui          = new BigUInt(5ul);
            Plaintext      plain        = new Plaintext();

            Assert.ThrowsException <ArgumentNullException>(() => enc = new IntegerEncoder(context_null));
            Assert.ThrowsException <ArgumentException>(() => enc     = new IntegerEncoder(GlobalContext.CKKSContext));

            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1ul, null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1L, null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1, null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1u, null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(bui_null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(bui, null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(bui_null, plain));

            Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeUInt32(null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeUInt64(null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeInt32(null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeInt64(null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeBigUInt(null));
        }
Ejemplo n.º 8
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));
        }
Ejemplo n.º 9
0
        public static BigPolyArray[] GetEncryptedDataInBatches(EncCal encCal, Pgm picture, int size = 3)
        {
            var encryptedData = new BigPolyArray[picture.Length * size];

            Parallel.For(0, picture.Length, l =>
            {
                int slotCount = encCal.CrtBuilder.SlotCount;
                var values1   = new BigUInt[encCal.CrtBuilder.SlotCount];
                var values2   = new BigUInt[encCal.CrtBuilder.SlotCount];
                var values3   = new BigUInt[encCal.CrtBuilder.SlotCount];
                Parallel.For(0, slotCount, i =>
                {
                    values1[i] = new BigUInt(encCal.GetBitCount(), 0);
                    values2[i] = new BigUInt(encCal.GetBitCount(), 0);
                    values3[i] = new BigUInt(encCal.GetBitCount(), 0);
                });
                Parallel.For(0, picture.Width, i =>
                {
                    values1[i].Set(picture.Data[(l * picture.Length) + i]);
                    values2[i + 1].Set(picture.Data[(l * picture.Length) + i]);
                    values3[i + 2].Set(picture.Data[(l * picture.Length) + i]);
                });
                encryptedData[size * l]     = encCal.GetEnc(encCal.CrtBuilder.Compose(values1.ToList()));
                encryptedData[size * l + 1] = encCal.GetEnc(encCal.CrtBuilder.Compose(values2.ToList()));
                encryptedData[size * l + 2] = encCal.GetEnc(encCal.CrtBuilder.Compose(values3.ToList()));
            });
            return(encryptedData);
        }
Ejemplo n.º 10
0
        public void EmptyConstructorTest()
        {
            BigUInt bui = new BigUInt();

            Assert.IsTrue(bui.IsZero);
            Assert.AreEqual(0, bui.BitCount);
        }
Ejemplo n.º 11
0
        public void ParseTest()
        {
            Random random = new Random(1234);

            for (int i = 0; i <= 2500; i++)
            {
                UInt32[] mantissa = UIntUtil.Random(random, BigUInt <Pow2.N16> .Length, random.Next(BigUInt <Pow2.N16> .Bits + 1));

                BigUInt <Pow2.N16> v  = new BigUInt <Pow2.N16>(mantissa, enable_clone: false);
                BigUInt <Pow2.N16> v2 = new BigUInt <Pow2.N16>(v.ToString());

                Assert.AreEqual(v, v2);
            }

            for (int i = 0; i <= 2500; i++)
            {
                UInt32[] mantissa = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, random.Next(BigUInt <Pow2.N32> .Bits + 1));

                BigUInt <Pow2.N32> v  = new BigUInt <Pow2.N32>(mantissa, enable_clone: false);
                BigUInt <Pow2.N32> v2 = new BigUInt <Pow2.N32>(v.ToString());

                Assert.AreEqual(v, v2);
            }

            for (int i = 0; i <= 2500; i++)
            {
                UInt32[] mantissa = UIntUtil.Random(random, BigUInt <Pow2.N64> .Length, random.Next(BigUInt <Pow2.N64> .Bits + 1));

                BigUInt <Pow2.N64> v  = new BigUInt <Pow2.N64>(mantissa, enable_clone: false);
                BigUInt <Pow2.N64> v2 = new BigUInt <Pow2.N64>(v.ToString());

                Assert.AreEqual(v, v2);
            }
        }
Ejemplo n.º 12
0
        public void ExponentiateBigUIntModNET()
        {
            var myUInt   = new BigUInt("ABABABAB");
            var modulus  = new BigUInt("CAACAACAA");
            var exponent = new BigUInt("5");
            var result   = Utilities.ExponentiateUIntMod(myUInt, exponent, modulus);

            Assert.AreEqual("33773505765", result.ToDecimalString());

            myUInt.Set("1");
            exponent.Set("F00F00F00F00F00");
            result = Utilities.ExponentiateUIntMod(myUInt, exponent, modulus);
            Assert.AreEqual("1", result.ToDecimalString());

            modulus.Set("AAAAAAAAAAAAAAAAAAAAA");
            myUInt.Set("F00F00F00F00F00");
            exponent.Set("1");
            result = Utilities.ExponentiateUIntMod(myUInt, exponent, modulus);
            Assert.AreEqual("F00F00F00F00F00", result.ToString());

            myUInt.Set("F00F00F00F00F00");
            exponent.Set("0");
            result = Utilities.ExponentiateUIntMod(myUInt, exponent, modulus);
            Assert.AreEqual("1", result.ToString());
        }
        public void PolyInftyNormCoeffmodNET()
        {
            var     poly    = new BigPoly("1x^10 + 2x^9 + 5x^8 + Ax^7 + Bx^6 + 4x^5 + 1x^2 + 1");
            BigUInt value   = new BigUInt();
            BigUInt modulus = new BigUInt();

            modulus.Set(5);
            var inftynorm = Utilities.PolyInftyNormCoeffmod(poly, modulus);

            value.Set("2");
            Assert.AreEqual(value, inftynorm);

            poly.Set("AAx^10 + ABx^9 + CAx^8 + CFx^7 + FEx^6 + F7x^5 + 1x^2 + 2");
            modulus.Set("10");
            inftynorm = Utilities.PolyInftyNormCoeffmod(poly, modulus);
            value.Set("7");
            Assert.AreEqual(value, inftynorm);

            poly.Set("Ax^10 + ABx^9 + ABCx^8 + ABCDx^7 + ABCDEx^6 + ABCDEFx^5 + 1x^2 + 2");
            modulus.Set("4");
            inftynorm = Utilities.PolyInftyNormCoeffmod(poly, modulus);
            value.Set("2");
            Assert.AreEqual(value, inftynorm);

            poly.Set("1x^5 + 2x^4 + 3x^3 + 4x^2 + 5x^1 + 6");
            modulus.Set("4");
            inftynorm = Utilities.PolyInftyNormCoeffmod(poly, modulus);
            value.Set("2");
            Assert.AreEqual(value, inftynorm);
        }
        public void BadCreateTest()
        {
            UInt32[] value1     = new UInt32[] { 0x1234u, 0x5678u, 0x9ABCu };
            UInt32[] value2     = new UInt32[] { 0x3478u, 0xFEDCu, 0x2341u, 0x6785u, 0xABC9u, 0xEF0Du };
            UInt32[] value_full = new UInt32[] { ~0u, ~0u, ~0u, ~0u };

            Assert.ThrowsException <OverflowException>(() => {
                BigUInt <Pow2.N4> n = new BigUInt <Pow2.N4>(Array.AsReadOnly(value_full), 0, carry: true);
            });

            Assert.ThrowsException <ArgumentException>(() => {
                BigUInt <Pow2.N4> n = new BigUInt <Pow2.N4>(value1, enable_clone: true);
            });

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => {
                BigUInt <Pow2.N4> n = new BigUInt <Pow2.N4>(value2, -1);
            });

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => {
                BigUInt <Pow2.N4> n = new BigUInt <Pow2.N4>(value2, 0);
            });

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => {
                BigUInt <Pow2.N4> n = new BigUInt <Pow2.N4>(value2, 1);
            });

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => {
                BigUInt <Pow2.N4> n = new BigUInt <Pow2.N4>(value1, 2);
            });

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => {
                BigUInt <Pow2.N4> n = new BigUInt <Pow2.N4>(Array.AsReadOnly(value_full), 1, carry: true);
            });
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
0
        public void OperatorInt32Test()
        {
            BigUInt bui   = new BigUInt("ABCDEF1234567890ABCDEF");
            int     value = (int)bui;

            Assert.AreEqual(-1867788817, value);
        }
Ejemplo n.º 17
0
        public void HexStringConstructorTest()
        {
            BigUInt bui = new BigUInt("1234567890ABCDEF1234567890ABCDEF");

            Assert.IsFalse(bui.IsZero);
            Assert.AreEqual(2ul, bui.UInt64Count);
            Assert.AreEqual(125, bui.BitCount);
            Assert.AreEqual(125, bui.GetSignificantBitCount());

            Assert.AreEqual(0x1234567890ABCDEFul, bui.Data(0));
            Assert.AreEqual(0x1234567890ABCDEFul, bui.Data(1));

            bui = new BigUInt("FEDCBAFEDCBA0987654321");

            Assert.IsFalse(bui.IsZero);
            Assert.AreEqual(2ul, bui.UInt64Count);
            Assert.AreEqual(88, bui.BitCount);
            Assert.AreEqual(88, bui.GetSignificantBitCount());

            Assert.AreEqual(0xFEDCBAul, bui.Data(1));
            Assert.AreEqual(0xFEDCBA0987654321ul, bui.Data(0));

            bui = new BigUInt(bitCount: 80, hexString: "DEADBEEF");

            Assert.IsFalse(bui.IsZero);
            Assert.AreEqual(2ul, bui.UInt64Count);
            Assert.AreEqual(80, bui.BitCount);
            Assert.AreEqual(32, bui.GetSignificantBitCount());
            Assert.AreEqual(0ul, bui.Data(1));
            Assert.AreEqual(0xDEADBEEFul, bui.Data(0));
        }
Ejemplo n.º 18
0
        public void OperatorInt64Test()
        {
            BigUInt bui   = new BigUInt("ABCDEF124567890ABCDEF");
            long    value = (long)bui;

            Assert.AreEqual(-1070635735584092689, value);
        }
Ejemplo n.º 19
0
        public void OperatorUInt32Test()
        {
            BigUInt bui   = new BigUInt("ABCDEF1234567890ABCDEF");
            uint    value = (uint)bui;

            Assert.AreEqual(0x90ABCDEFu, value);
        }
Ejemplo n.º 20
0
        public void CmpTest()
        {
            const int length = 4;
            Random    random = new Random(1234);

            for (int i = 0; i <= 2500; i++)
            {
                UInt32[] mantissa1 = UIntUtil.Random(random, length, random.Next(length * UIntUtil.UInt32Bits + 1));
                UInt32[] mantissa2 = UIntUtil.Random(random, length, random.Next(length * UIntUtil.UInt32Bits + 1));

                BigUInt <Pow2.N4> n1 = new BigUInt <Pow2.N4>(mantissa1, enable_clone: false);
                BigUInt <Pow2.N4> n2 = random.Next(8) < 7 ? new BigUInt <Pow2.N4>(mantissa2, enable_clone: false) : n1.Copy();
                BigInteger        bi1 = n1, bi2 = n2;

                Console.WriteLine(n1);
                Console.WriteLine(n2);

                Assert.AreEqual(n1.Equals(n2), bi1.Equals(bi2));
                Assert.AreEqual(n1 == n2, bi1 == bi2);
                Assert.AreEqual(n1 != n2, bi1 != bi2);
                Assert.AreEqual(n1 < n2, bi1 < bi2);
                Assert.AreEqual(n1 > n2, bi1 > bi2);
                Assert.AreEqual(n1 <= n2, bi1 <= bi2);
                Assert.AreEqual(n1 >= n2, bi1 >= bi2);

                Console.Write("\n");
            }
        }
Ejemplo n.º 21
0
        public void OperatorUInt64Test()
        {
            BigUInt bui   = new BigUInt("ABCDEF1234567890ABCDEF");
            ulong   value = (ulong)bui;

            Assert.AreEqual(0x1234567890ABCDEFul, value);
        }
Ejemplo n.º 22
0
        public void SaveLoadUIntNET()
        {
            using (MemoryStream stream = new MemoryStream())
            {
                var value  = new BigUInt();
                var value2 = new BigUInt("100");
                stream.Seek(0, SeekOrigin.Begin);
                value.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                value2.Load(stream);
                Assert.AreEqual(value, value2);

                value.Set("123");
                stream.Seek(0, SeekOrigin.Begin);
                value.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                value2.Load(stream);
                Assert.AreEqual(value, value2);

                value.Set("FFFFFFFFFFFFFFFFFFFFFFFFFF");
                stream.Seek(0, SeekOrigin.Begin);
                value.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                value2.Load(stream);
                Assert.AreEqual(value, value2);

                value.Set("0");
                stream.Seek(0, SeekOrigin.Begin);
                value.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                value2.Load(stream);
                Assert.AreEqual(value, value2);
            }
        }
Ejemplo n.º 23
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");
        }
Ejemplo n.º 24
0
        public void LeftBlockShiftTest()
        {
            Random random = new Random(1234);

            for (int sft = 0; sft < BigUInt <Pow2.N32> .Length; sft++)
            {
                UInt32[] mantissa = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, BigUInt <Pow2.N32> .Bits - sft * UIntUtil.UInt32Bits);

                BigUInt <Pow2.N32> v  = new BigUInt <Pow2.N32>(mantissa, enable_clone: false);
                BigInteger         bi = v;

                BigUInt <Pow2.N32> v_sft = BigUInt <Pow2.N32> .LeftBlockShift(v, sft);

                BigInteger bi_sft = bi << (sft * UIntUtil.UInt32Bits);

                Console.WriteLine(sft);
                Console.WriteLine(v.ToHexcode());
                Console.WriteLine(v_sft.ToHexcode());
                Assert.AreEqual(bi_sft, v_sft);

                Console.Write("\n");
            }

            Assert.ThrowsException <OverflowException>(() => {
                BigUInt <Pow2.N32> v = new BigUInt <Pow2.N32>(0x12345678u);

                BigUInt <Pow2.N32> v_sft = BigUInt <Pow2.N32> .LeftBlockShift(v, BigUInt <Pow2.N32> .Length);
            });
        }
Ejemplo n.º 25
0
        public void OperatorTildeTest()
        {
            BigUInt bui    = new BigUInt("DEADBEEF");
            BigUInt newone = ~bui;

            Assert.AreEqual(1ul, newone.UInt64Count);
            Assert.AreEqual(0x21524110ul, newone.Data(0));
        }
Ejemplo n.º 26
0
        public void ToDecimalStringTest()
        {
            BigUInt bui    = new BigUInt("DEADBEEF");
            string  decStr = bui.ToDecimalString();

            Assert.IsNotNull(decStr);
            Assert.IsTrue("3735928559".Equals(decStr));
        }
Ejemplo n.º 27
0
        public void OperatorPlusTest()
        {
            BigUInt bui    = new BigUInt("DEADBEEF");
            BigUInt newone = +bui;

            Assert.AreEqual(1ul, newone.UInt64Count);
            Assert.AreEqual(0xDEADBEEFul, newone.Data(0));
        }
Ejemplo n.º 28
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");
        }
Ejemplo n.º 29
0
        public void BitCountConstructorTest()
        {
            BigUInt bui = new BigUInt(bitCount: 70);

            Assert.IsTrue(bui.IsZero);
            Assert.AreEqual(70, bui.BitCount);
            Assert.AreEqual(2ul, bui.UInt64Count);
            Assert.AreEqual(0, bui.GetSignificantBitCount());
        }
Ejemplo n.º 30
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)));
        }
Ejemplo n.º 31
0
        static void Main(string[] args)
        {
            BigUInt fnminus2 = new BigUInt(1);
            BigUInt fnminus1 = new BigUInt(1);

            for (int i = 3; i<=1000; i++) {
                BigUInt fn = new BigUInt(fnminus2);
                fn.Add(fnminus1);
            }
        }
Ejemplo n.º 32
0
        static void Main(string[] args)
        {
            StreamReader sin = File.OpenText("numbers.txt");
            BigUInt answer = new BigUInt(0);

            string number = sin.ReadLine();
            while (!string.IsNullOrEmpty(number)) {
                answer = answer.Add(new BigUInt(number));
                number = sin.ReadLine();
            }

            sin.Close();

            Console.Out.WriteLine(answer.ToString());

            Console.Out.WriteLine("Klaar");
            Console.In.ReadLine();
        }