예제 #1
0
        public void ExceptionsTest()
        {
            SEALContext  context = GlobalContext.BFVContext;
            KeyGenerator keygen  = new KeyGenerator(context);
            SecretKey    secret  = new SecretKey();
            List <uint>  elts    = new List <uint> {
                16385
            };
            List <uint> elts_null = null;
            List <int>  steps     = new List <int> {
                4096
            };
            List <int> steps_null = null;

            Utilities.AssertThrows <ArgumentNullException>(() => keygen = new KeyGenerator(null));

            Utilities.AssertThrows <ArgumentNullException>(() => keygen = new KeyGenerator(context, null));
            Utilities.AssertThrows <ArgumentNullException>(() => keygen = new KeyGenerator(null, keygen.SecretKey));
            Utilities.AssertThrows <ArgumentException>(() => keygen     = new KeyGenerator(context, secret));

            Utilities.AssertThrows <ArgumentNullException>(() => keygen.CreateGaloisKeys(elts_null));
            Utilities.AssertThrows <ArgumentException>(() => keygen.CreateGaloisKeys(elts));
            Utilities.AssertThrows <ArgumentNullException>(() => keygen.CreateGaloisKeys(steps_null));
            Utilities.AssertThrows <ArgumentException>(() => keygen.CreateGaloisKeys(steps));

            EncryptionParameters smallParms = new EncryptionParameters(SchemeType.CKKS);

            smallParms.PolyModulusDegree = 128;
            smallParms.CoeffModulus      = CoeffModulus.Create(smallParms.PolyModulusDegree, new int[] { 60 });
            context = new SEALContext(smallParms, true, SecLevelType.None);
            keygen  = new KeyGenerator(context);
            Utilities.AssertThrows <InvalidOperationException>(() => keygen.CreateRelinKeys());
            Utilities.AssertThrows <InvalidOperationException>(() => keygen.CreateGaloisKeys());
        }
예제 #2
0
        public void SeededKeyTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 8,
                PlainModulus      = new Modulus(257),
                CoeffModulus      = CoeffModulus.Create(8, new int[] { 40, 40 })
            };
            SEALContext context = new SEALContext(parms,
                                                  expandModChain: false,
                                                  secLevel: SecLevelType.None);
            KeyGenerator keygen = new KeyGenerator(context);

            keygen.CreatePublicKey(out PublicKey publicKey);

            Encryptor    encryptor = new Encryptor(context, publicKey);
            Decryptor    decryptor = new Decryptor(context, keygen.SecretKey);
            Evaluator    evaluator = new Evaluator(context);
            BatchEncoder encoder   = new BatchEncoder(context);

            GaloisKeys galoisKeys = new GaloisKeys();

            using (MemoryStream stream = new MemoryStream())
            {
                keygen.CreateGaloisKeys().Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                galoisKeys.Load(context, stream);
            }

            Plaintext    plain = new Plaintext();
            List <ulong> vec   = new List <ulong>
            {
                1, 2, 3, 4,
                5, 6, 7, 8
            };

            encoder.Encode(vec, plain);

            Ciphertext encrypted = new Ciphertext();
            Ciphertext encdest   = new Ciphertext();
            Plaintext  plaindest = new Plaintext();

            encryptor.Encrypt(plain, encrypted);
            evaluator.RotateColumns(encrypted, galoisKeys, encdest);
            decryptor.Decrypt(encdest, plaindest);
            encoder.Decode(plaindest, vec);

            Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong>
            {
                5, 6, 7, 8,
                1, 2, 3, 4
            }));

            evaluator.RotateRows(encdest, -1, galoisKeys, encrypted);
            decryptor.Decrypt(encrypted, plaindest);
            encoder.Decode(plaindest, vec);

            Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong>
            {
                8, 5, 6, 7,
                4, 1, 2, 3
            }));

            evaluator.RotateRowsInplace(encrypted, 2, galoisKeys);
            decryptor.Decrypt(encrypted, plaindest);
            encoder.Decode(plaindest, vec);

            Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong>
            {
                6, 7, 8, 5,
                2, 3, 4, 1
            }));

            evaluator.RotateColumnsInplace(encrypted, galoisKeys);
            decryptor.Decrypt(encrypted, plaindest);
            encoder.Decode(plaindest, vec);

            Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong>
            {
                2, 3, 4, 1,
                6, 7, 8, 5
            }));
        }