public IVector PointwiseMultiply(IVector v, IComputationEnvironment env) { var vr = v as RawVector; Vector <double> mul = null; if (this.v.Count == vr.v.Count) { mul = this.v.PointwiseMultiply(vr.v); } else if (this.v.Count == 1 && this.Format == EVectorFormat.sparse) // multiplying by constant { mul = vr.v.Multiply(this.v[0]); } else if (vr.v.Count == 1 && vr.Format == EVectorFormat.sparse) // mutliplying by constant { mul = this.v.Multiply(vr.v[0]); } else { throw new Exception("Vectors dimensions do not match"); } var res = new RawVector(mul, 1, BlockSize); res.RegisterScale(Scale * vr.Scale); return(res); }
public Vector <double> Decrypt(IComputationEnvironment env) { var leenv = env as EncryptedSealBfvEnvironment; var splits = eVectors.Zip(leenv.Environments, (v, e) => v.Decrypt(e)).ToArray(); return(JoinSplitNumbers(splits, leenv)); }
public IEnumerable <BigInteger> DecryptFullPrecision(IComputationEnvironment env) { var leenv = env as EncryptedSealBfvEnvironment; var splits = eVectors.Zip(leenv.Environments, (v, e) => v.DecryptFullPrecision(e)).ToArray(); return(JoinSplitNumbers(splits, leenv)); }
public IVector Permute(IVector[] selections, int[] shifts, ulong outputDim, IComputationEnvironment env) { if (selections.Length != shifts.Length) { throw new Exception("number of selection vectors and number of shifts does not match"); } var res = Vector <double> .Build.Dense((int)Dim); for (int i = 0; i < selections.Length; i++) { if (selections[i] == null) { continue; } if (selections[i].Dim != Dim) { throw new Exception("dimension of selection vector does not match dimension of data vector"); } if (selections[i].Scale != selections[0].Scale) { throw new Exception("scales of all selection vectors should be the same"); } var s = selections[i] as RawVector; var t = v.PointwiseMultiply(s.v); res = res.Add(Rotate(t, shifts[i])); } var resVec = new RawVector(res.SubVector(0, (int)outputDim), 1, BlockSize); resVec.RegisterScale(Scale * selections[0].Scale); return(resVec); }
public IVector Rotate(int amount, IComputationEnvironment env) { var res = new RawVector(Rotate(v, amount), 1, BlockSize); res.RegisterScale(Scale); return(res); }
public IMatrix ElementWiseMultiply(IMatrix m, IComputationEnvironment env) { if (m.Format != Format) { throw new Exception("Format mismatch"); } if (m.RowCount != RowCount) { throw new Exception("Row-count mismatch"); } if (m.ColumnCount != ColumnCount) { throw new Exception("Column count mismatch"); } var me = m as EncryptedSealBfvMatrix; var lenv = env as EncryptedSealBfvEnvironment; var tempVectors = new EncryptedSealBfvVector[leVectors.Length]; Utils.ParallelProcessInEnv(leVectors.Length, lenv, (penv, task, colIndex) => { tempVectors[colIndex] = (EncryptedSealBfvVector)leVectors[colIndex].PointwiseMultiply(me.leVectors[colIndex], penv); }); return(new EncryptedSealBfvMatrix() { leVectors = tempVectors, Format = Format }); }
public IVector Multiply(double x, IComputationEnvironment env) { var res = this.v.Multiply(x); var t = new RawVector(res, 1, BlockSize); t.RegisterScale(Scale); return(t); }
public IVector Mul(IVector v, IComputationEnvironment env, bool ForceDenseFormat = false) { var vr = v as RawVector; var res = new RawVector(m.Multiply((Vector <double>)vr.Data), 1, v.BlockSize); res.RegisterScale(Scale * vr.Scale); return(res); }
public EncryptedSealBfvVector(IVector v, IComputationEnvironment env) // copy constructor { var lev = v as EncryptedSealBfvVector; var lenv = env as EncryptedSealBfvEnvironment; Scale = lev.Scale; eVectors = lev.eVectors.Zip(lenv.Environments, (x, aenv) => new AtomicSealBfvEncryptedVector(x, aenv)).ToArray(); }
public IVector Interleave(int shift, IComputationEnvironment env) { if (this.Format != EMatrixFormat.ColumnMajor) { throw new Exception("Expecting ColumnMajor matrix"); } return(EncryptedSealBfvVector.Interleave(leVectors, shift, (EncryptedSealBfvEnvironment)env)); }
public IVector SumAllSlots(IComputationEnvironment env) { var sum = v.Sum(); var vsum = Vector <double> .Build.DenseOfEnumerable(new double[] { sum }); var res = new RawVector(vsum, 1, BlockSize); res.RegisterScale(Scale); return(res); }
IVector ConvolveOnce(IMatrix m, int cornerIndex, int mapIndex, IComputationEnvironment env) { var corner = Corners[cornerIndex]; // patch matrix should not be disposed since this will result in deleting the inputs which are copied only by reference var patch = Factory.GetMatrix(Offsets.Select(offset => ElementAt(m, corner, offset, InputShape)).ToArray(), EMatrixFormat.ColumnMajor, CopyVectors: false); patch.DataDisposedExternaly = true; return(patch.Mul(weightWindows[mapIndex], env)); }
public IVector SumAllSlots(ulong length, IComputationEnvironment env) { var leenv = env as EncryptedSealBfvEnvironment; var res = new EncryptedSealBfvVector() { Scale = Scale, eVectors = ForEveryEncryptedVector(i => eVectors[i].SumAllSlotsTask(length, leenv.Environments[i])) }; return(res); }
public Matrix <double> Decrypt(IComputationEnvironment env) { var leenv = env as EncryptedSealBfvEnvironment; var vectors = new Vector <double> [leVectors.Length]; Utils.ParallelProcessInEnv(vectors.Length, env, (penv, taskIndex, k) => vectors[k] = leVectors[k].Decrypt(penv)); Matrix <double> res = (Format == EMatrixFormat.RowMajor) ? Matrix <double> .Build.DenseOfRowVectors(vectors) : Matrix <double> .Build.DenseOfColumnVectors(vectors); return(res); }
public EncryptedSealBfvMatrix(EncryptedSealBfvVector[] columns, IComputationEnvironment env, bool CopyVectors = true) { if (columns != null && columns.Any(c => c.Dim != columns[0].Dim)) { throw new Exception("all columns of a matrix should have the same size"); } if (columns != null) { leVectors = (CopyVectors) ? columns.Select(x => new EncryptedSealBfvVector(x, env)).ToArray() : columns; } }
public IVector Rotate(int amount, IComputationEnvironment env) { var eenv = env as EncryptedSealBfvEnvironment; var res = new EncryptedSealBfvVector() { Scale = Scale, eVectors = ForEveryEncryptedVector(i => eVectors[i].RotateTask(amount, eenv.Environments[i])) }; return(res); }
public IVector DotProduct(IVector v, IComputationEnvironment env) { var vr = v as RawVector; var dot = this.v.DotProduct(vr.v); var vdot = Vector <double> .Build.DenseOfEnumerable(new double[] { dot }); var res = new RawVector(vdot, 1, BlockSize); res.RegisterScale(Scale * vr.Scale); return(res); }
public EncryptedSealBfvVector(IEnumerable <BigInteger> v, IComputationEnvironment env, bool EncryptData = true, EVectorFormat Format = EVectorFormat.dense) { this.Scale = Scale; var leenv = env as EncryptedSealBfvEnvironment; var splits = SplitBigNumbers(v, leenv); eVectors = new AtomicSealBfvEncryptedVector[leenv.Environments.Length]; Parallel.For(0, eVectors.Length, i => { eVectors[i] = new AtomicSealBfvEncryptedVector(splits[i], leenv.Environments[i], Scale: 1, SignedNumbers: false, EncryptData: EncryptData, Format: Format); }); }
public IVector ConvertToColumnVector(IComputationEnvironment env) { if ((ulong)m.ColumnCount * (ulong)m.RowCount > this.BlockSize) { throw new Exception("block too long for interleaving"); } var v = new RawVector(Vector <double> .Build.DenseOfEnumerable(m.Enumerate()), 1, BlockSize); v.RegisterScale(Scale); return(v); }
public IVector PointwiseMultiply(IVector v, IComputationEnvironment env) { var leenv = env as EncryptedSealBfvEnvironment; var lev = v as EncryptedSealBfvVector; var res = new EncryptedSealBfvVector() { Scale = Scale * v.Scale, eVectors = ForEveryEncryptedVector(i => eVectors[i].PointwiseMultiplyTask(lev.eVectors[i], leenv.Environments[i])) }; return(res); }
public IVector SumAllSlots(IComputationEnvironment env) { var leenv = env as EncryptedSealBfvEnvironment; var res = new EncryptedSealBfvVector() { Scale = Scale, eVectors = new AtomicSealBfvEncryptedVector[eVectors.Length] }; Parallel.For(0, eVectors.Length, i => res.eVectors[i] = (AtomicSealBfvEncryptedVector)eVectors[i].SumAllSlots(leenv.Environments[i])); return(res); }
public IVector DotProduct(IVector v, IComputationEnvironment env, int?ForceOutputInColumn = null) { var leenv = env as EncryptedSealBfvEnvironment; var ev = v as EncryptedSealBfvVector; var res = new EncryptedSealBfvVector() { Scale = Scale * v.Scale, eVectors = ForEveryEncryptedVector(i => eVectors[i].DotProductTask(ev.eVectors[i], leenv.Environments[i], ForceOutputInColumn)) }; return(res); }
public IComputationEnvironment AllocateComputationEnv() { if (Parent == null) { Parent = new RawComputationalEnvironment() { ParentFactory = this, Primes = Primes } } ; return(Parent); }
public static void ParallelProcessInEnv(int count, IComputationEnvironment masterEnv, IFactory factory, Action <IComputationEnvironment, int, int> lambda) { if (count < 2) { int taskID = 0; if (masterEnv != null) { for (int k = 0; k < count; k++) { lambda(masterEnv, taskID, k); } } else { ProcessInEnv((env) => { for (int k = 0; k < count; k++) { lambda(env, taskID, k); } }, factory); } } else { int nextItem = -1; int threadCount = (Defaults.ThreadCount > count) ? count : Defaults.ThreadCount; Task[] tasks = new Task[threadCount]; for (int tsk = 0; tsk < tasks.Length; tsk++) { int taskIndex = tsk; tasks[tsk] = Task.Run(() => { int currentTask = (int)Task.CurrentId; var env = factory.AllocateComputationEnv(); int k = 0; while (k < count) { k = Interlocked.Increment(ref nextItem); if (k >= count) { break; } lambda(env, taskIndex, k); } factory.FreeComputationEnv(env); }); } Task.WaitAll(tasks); } }
public IVector Permute(IVector[] selections, int[] shifts, ulong outputDim, IComputationEnvironment env) { var eenv = env as EncryptedSealBfvEnvironment; var I = selections.Select((s, i) => (s != null) ? i : -1).Where(i => i >= 0).ToArray(); var selectI = I.Select(i => selections[i]).ToArray(); var shiftI = I.Select(i => shifts[i]).ToArray(); var res = new EncryptedSealBfvVector() { Scale = Scale, eVectors = ForEveryEncryptedVector(i => eVectors[i].PermuteTask(selectI.Select(x => ((EncryptedSealBfvVector)x).eVectors[i]).ToArray(), shiftI, outputDim, eenv.Environments[i])) }; return(res); }
public static IVector GenerateSpareOfArray(IVector[] SparseVectors, IComputationEnvironment env) { var lenv = env as EncryptedSealBfvEnvironment; var res = new EncryptedSealBfvVector() { Scale = SparseVectors[0].Scale, eVectors = new AtomicSealBfvEncryptedVector[((EncryptedSealBfvVector)(SparseVectors[0])).eVectors.Length] }; for (int i = 0; i < res.eVectors.Length; i++) { res.eVectors[i] = AtomicSealBfvEncryptedVector.GenerateSparseOfArray(SparseVectors.Select(v => ((EncryptedSealBfvVector)v).eVectors[i]).ToArray(), lenv.Environments[i]); } return(res); }
public IVector Duplicate(ulong count, IComputationEnvironment env) { int shift = 1; while (shift < (int)Dim) { shift *= 2; } var w = Vector <double> .Build.Dense(shift *(int)count); for (int i = 0; i < (int)count; i++) { w.SetSubVector(i * shift, (int)Dim, v); } return(new RawVector(w / Scale, Scale, BlockSize)); }
public IVector Subtract(IVector v, IComputationEnvironment env) { if (v.Scale == 0) { return(this); } var vr = v as RawVector; if (Scale != 0 && Scale != vr.Scale) { throw new Exception("Scales do not match."); } var res = this.v.Subtract(vr.v); var t = new RawVector(res, 1, BlockSize); t.RegisterScale(Scale); return(t); }
public IVector Interleave(int shift, IComputationEnvironment env) { int items = (shift > 0) ? shift : -shift; bool allignedToEnd = (shift < 0); if (items == 0) { throw new Exception("number of items cannot be zero"); } var w = m.Column(0); for (int i = 1; i < m.ColumnCount; i++) { w = w.Add(Shift(m.Column(i), shift * i)); } var t = new RawVector(w, 1, BlockSize); t.RegisterScale(Scale); return(t); }
public IVector Subtract(IVector v, IComputationEnvironment env) { if (v.Scale == 0) { return(this); } if (Scale != v.Scale) { throw new Exception("Scales do not match."); } var leenv = env as EncryptedSealBfvEnvironment; var lev = v as EncryptedSealBfvVector; var res = new EncryptedSealBfvVector() { Scale = Scale, eVectors = ForEveryEncryptedVector(i => eVectors[i].SubtractTask(lev.eVectors[i], leenv.Environments[i])) }; return(res); }