示例#1
0
 public EncryptedSealBfvFactory(string fileName)
 {
     referenceEnvironment = new EncryptedSealBfvEnvironment(fileName)
     {
         ParentFactory = this
     };
 }
示例#2
0
 public EncryptedSealBfvFactory(Stream stream)
 {
     referenceEnvironment = new EncryptedSealBfvEnvironment(stream)
     {
         ParentFactory = this
     };
 }
示例#3
0
 public IComputationEnvironment AllocateComputationEnv()
 {
     environmentQueue.TryDequeue(out EncryptedSealBfvEnvironment env);
     if (env == null)
     {
         env = new EncryptedSealBfvEnvironment(referenceEnvironment);
     }
     return(env);
 }
示例#4
0
        public EncryptedSealBfvFactory(ulong[] primes, ulong n, int DecompositionBitCount = DefaultDecompositionBitCount, int GaloisDecompositionBitCount = DefaultGaloisDecompositionBitCount, int SmallModulusCount = -1)
        {
            EncryptedSealBfvEnvironment eenv = new EncryptedSealBfvEnvironment()
            {
                ParentFactory = this
            };

            eenv.GenerateEncryptionKeys(primes, n, DecompositionBitCount, GaloisDecompositionBitCount, SmallModulusCount);
            referenceEnvironment = eenv;
        }
示例#5
0
        static public EncryptedSealBfvVector Stack(EncryptedSealBfvVector[] vecs, EncryptedSealBfvEnvironment env)
        {
            var res = new EncryptedSealBfvVector()
            {
                eVectors = vecs[0].ForEveryEncryptedVector(i => AtomicSealBfvEncryptedVector.StackTask(vecs.Select(v => v.eVectors[i]).ToArray(), env.Environments[i])),
                Scale    = vecs[0].Scale
            };

            return(res);
        }
示例#6
0
        public EncryptedSealBfvFactory()
        {
            ulong[] primes = new ulong[] { 40961, 65537, 114689, 147457, 188417 };
            EncryptedSealBfvEnvironment eenv = new EncryptedSealBfvEnvironment()
            {
                ParentFactory = this
            };

            eenv.GenerateEncryptionKeys(primes, 4096, DefaultDecompositionBitCount, DefaultGaloisDecompositionBitCount);
            referenceEnvironment = eenv;
        }
示例#7
0
 public EncryptedSealBfvEnvironment(EncryptedSealBfvEnvironment reference)
 {
     bigFactor               = reference.bigFactor;
     ParentFactory           = reference.ParentFactory;
     preComputedCoefficients = reference.preComputedCoefficients;
     Environments            = reference.Environments.Select(x => new AtomicSealBfvEncryptedEnvironment(x)).ToArray();
     if (reference.environmentQueue == null)
     {
         reference.CreateQueue();
     }
     environmentQueue = reference.environmentQueue;
 }
示例#8
0
        UInt64[][] SplitBigNumbers(IEnumerable <BigInteger> v, EncryptedSealBfvEnvironment leenv)
        {
            var res = new UInt64[leenv.Environments.Length][];


            var z = v.Select(x => (x < 0) ? x + leenv.bigFactor : x);

            for (int i = 0; i < res.Length; i++)
            {
                res[i] = z.Select(x => (UInt64)(x % leenv.Environments[i].plainmodulusValue)).ToArray();
            }
            return(res);
        }
示例#9
0
        /// <summary>
        /// test the noise budget in a vector and updates the minimal budget seen if needed
        /// </summary>
        /// <param name="res"> input vector</param>
        /// <param name="decryptor"> a decryptor object that is used to measure the noise level</param>
        public static int TestVectorBudget(EncryptedSealBfvVector v, EncryptedSealBfvEnvironment lenv)
        {
            var vectors = v.Data as AtomicSealBfvEncryptedVector[];

            for (int i = 0; i < vectors.Length; i++)
            {
                var ciphers = vectors[i].Data as Ciphertext[];
                foreach (var c in ciphers)
                {
                    TestBudget(c, lenv.Environments[i].decryptor);
                }
            }
            return(MinBudgetSoFar);
        }
示例#10
0
        Vector <double>[] SplitBigNumbers(Vector <double> v, EncryptedSealBfvEnvironment leenv)
        {
            var res = new Vector <double> [leenv.Environments.Length];


            var w = v.Multiply(Scale).PointwiseRound().Select(x => (BigInteger)x).ToArray();
            var z = w.Select(x => (x < 0) ? x + leenv.bigFactor : x);

            for (int i = 0; i < res.Length; i++)
            {
                res[i] = Vector <double> .Build.DenseOfEnumerable(z.Select(x => (double)(x % leenv.Environments[i].plainmodulusValue)));
            }
            return(res);
        }
示例#11
0
        IEnumerable <BigInteger> JoinSplitNumbers(IEnumerable <BigInteger>[] split, EncryptedSealBfvEnvironment leenv)
        {
            var bigNumbers = new BigInteger[split[0].Count()];

            for (int i = 0; i < split.Length; i++)
            {
                for (int j = 0; j < bigNumbers.Length; j++)
                {
                    bigNumbers[j] += ((BigInteger)split[i].ElementAt(j)) * leenv.preComputedCoefficients[i];
                }
            }
            for (int j = 0; j < bigNumbers.Length; j++)
            {
                bigNumbers[j] = bigNumbers[j] % leenv.bigFactor;
                if (IsSigned && (bigNumbers[j] * 2 > leenv.bigFactor))
                {
                    bigNumbers[j] = bigNumbers[j] - leenv.bigFactor;
                }
            }
            return(bigNumbers);
        }
示例#12
0
        Vector <double> JoinSplitNumbers(Vector <double>[] split, EncryptedSealBfvEnvironment leenv)
        {
            var bigNumbers = new BigInteger[split[0].Count];

            for (int i = 0; i < split.Length; i++)
            {
                for (int j = 0; j < bigNumbers.Length; j++)
                {
                    bigNumbers[j] += ((BigInteger)split[i][j]) * leenv.preComputedCoefficients[i];
                }
            }
            for (int j = 0; j < bigNumbers.Length; j++)
            {
                bigNumbers[j] = bigNumbers[j] % leenv.bigFactor;
                if (bigNumbers[j] * 2 > leenv.bigFactor)
                {
                    bigNumbers[j] = bigNumbers[j] - leenv.bigFactor;
                }
            }
            return(Vector <double> .Build.DenseOfEnumerable(bigNumbers.Select(x => (double)x / Scale)));
        }
示例#13
0
        public static EncryptedSealBfvMatrix Read(StreamReader str, EncryptedSealBfvEnvironment env)
        {
            var mtx  = new EncryptedSealBfvMatrix();
            var line = str.ReadLine();

            if (line != "<Start LargeEncryptedMatrix>")
            {
                throw new Exception("Bad stream format.");
            }
            mtx.Format    = (EMatrixFormat)Enum.Parse(mtx.Format.GetType(), str.ReadLine());
            mtx.leVectors = new EncryptedSealBfvVector[int.Parse(str.ReadLine())];
            for (int i = 0; i < mtx.leVectors.Length; i++)
            {
                mtx.leVectors[i] = EncryptedSealBfvVector.Read(str, env);
            }

            line = str.ReadLine();
            if (line != "<End LargeEncryptedMatrix>")
            {
                throw new Exception("Bad stream format.");
            }
            return(mtx);
        }
示例#14
0
        public static EncryptedSealBfvVector Read(StreamReader str, EncryptedSealBfvEnvironment env)
        {
            var vct  = new EncryptedSealBfvVector();
            var line = str.ReadLine();

            if (line != "<Start LargeEncryptedVector>")
            {
                throw new Exception("Bad stream format.");
            }
            vct.Scale    = double.Parse(str.ReadLine());
            vct.eVectors = new AtomicSealBfvEncryptedVector[int.Parse(str.ReadLine())];
            for (int i = 0; i < vct.eVectors.Length; i++)
            {
                vct.eVectors[i] = AtomicSealBfvEncryptedVector.Read(str, env.Environments[i]);
            }

            line = str.ReadLine();
            if (line != "<End LargeEncryptedVector>")
            {
                throw new Exception("Bad stream format.");
            }
            return(vct);
        }
示例#15
0
 static public Task <EncryptedSealBfvVector> InterleaveTask(EncryptedSealBfvVector[] vecs, int shift, EncryptedSealBfvEnvironment env)
 {
     return(Task <EncryptedSealBfvVector> .Factory.StartNew(() => Interleave(vecs, shift, env)));
 }
示例#16
0
 static public Task <EncryptedSealBfvVector> StackTask(EncryptedSealBfvVector[] vecs, EncryptedSealBfvEnvironment env)
 {
     return(Task <EncryptedSealBfvVector> .Factory.StartNew(() => Stack(vecs, env)));
 }
示例#17
0
 static public Task <EncryptedSealBfvVector> DenseMatrixBySparseVectorMultiplyTask(EncryptedSealBfvVector[] denses, EncryptedSealBfvVector sparse, EncryptedSealBfvEnvironment env)
 {
     return(Task <EncryptedSealBfvVector> .Factory.StartNew(() => DenseMatrixBySparseVectorMultiply(denses, sparse, env)));
 }
示例#18
0
        static public EncryptedSealBfvVector DenseMatrixBySparseVectorMultiply(EncryptedSealBfvVector[] denses, EncryptedSealBfvVector sparse, EncryptedSealBfvEnvironment env)
        {
            var res = new EncryptedSealBfvVector()
            {
                Scale    = denses[0].Scale * sparse.Scale,
                eVectors = denses[0].ForEveryEncryptedVector(i => AtomicSealBfvEncryptedVector.DenseMatrixBySparseVectorMultiplyTask(denses.Select(d => d.eVectors[i]).ToArray(), sparse.eVectors[i], env.Environments[i]))
            };

            return(res);
        }