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 IMatrix GetEncryptedMatrix(Matrix <double> m, EMatrixFormat format, double scale) { IMatrix res = null; Utils.ProcessInEnv((env) => { EncryptedSealBfvVector[] vecs = null; if (format == EMatrixFormat.ColumnMajor) { vecs = new EncryptedSealBfvVector[m.ColumnCount]; Utils.ParallelProcessInEnv(vecs.Length, env, (penv, taskIndex, k) => { vecs[k] = new EncryptedSealBfvVector(m.Column(k), penv, scale, Format: EVectorFormat.dense, EncryptData: true); }); } else { vecs = new EncryptedSealBfvVector[m.RowCount]; Utils.ParallelProcessInEnv(vecs.Length, env, (penv, taskIndex, k) => { vecs[k] = new EncryptedSealBfvVector(m.Row(k), penv, scale, Format: EVectorFormat.dense, EncryptData: true); }); } res = new EncryptedSealBfvMatrix(vecs, env) { Format = format }; foreach (var v in vecs) { v.Dispose(); } }, this); return(res); }
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)); }
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); }
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); }
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 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 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 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 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 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); }
/// <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); }
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 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); }
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); }
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); }
public IVector Mul(IVector v, IComputationEnvironment env, bool ForceDenseFormat = false) { var ev = v as EncryptedSealBfvVector; var lenv = env as EncryptedSealBfvEnvironment; if (Format == EMatrixFormat.ColumnMajor) { if (ForceDenseFormat) { throw new Exception("Forcing dense format is available only in RowMajor mode"); } return(EncryptedSealBfvVector.DenseMatrixBySparseVectorMultiply(leVectors, ev, lenv)); } if (!ForceDenseFormat) { IVector[] tempVectors = new EncryptedSealBfvVector[leVectors.Length]; Utils.ParallelProcessInEnv(leVectors.Length, env, (penv, task, colIndex) => { tempVectors[colIndex] = leVectors[colIndex].DotProduct(ev, penv); }); var res = EncryptedSealBfvVector.GenerateSpareOfArray(tempVectors, env); foreach (var t in tempVectors) { t.Dispose(); } return(res); } // Column major mode forcing dense output; { var sumVectors = new EncryptedSealBfvVector[Defaults.ThreadCount]; Utils.ParallelProcessInEnv(leVectors.Length, env, (penv, task, colIndex) => { var t = (EncryptedSealBfvVector)leVectors[colIndex].DotProduct(ev, penv, ForceOutputInColumn: colIndex); var s = sumVectors[task]; sumVectors[task] = (sumVectors[task] == null) ? t : (EncryptedSealBfvVector)sumVectors[task].Add(t, env); if (sumVectors[task] != t) { t.Dispose(); } if (s != null) { s.Dispose(); } }); EncryptedSealBfvVector sum = null; for (int i = 0; i < sumVectors.Length; i++) { if (sumVectors[i] == null) { continue; } if (sum == null) { sum = sumVectors[i]; } else { var t = sum; sum = (EncryptedSealBfvVector)sum.Add(sumVectors[i], env); t.Dispose(); sumVectors[i].Dispose(); } } sum.RegisterDim((ulong)leVectors.Length); if (sum.Format != EVectorFormat.dense) { throw new Exception("Internal probloem: expecting the output to be dense"); } return(sum); } }
public IVector LoadVector(StreamReader str) { return(Utils.ProcessInEnv(env => EncryptedSealBfvVector.Read(str, env as EncryptedSealBfvEnvironment), this)); }
public IVector ConvertToColumnVector(IComputationEnvironment env) { var lenv = env as EncryptedSealBfvEnvironment; return(EncryptedSealBfvVector.Stack(leVectors, lenv)); }
static public Task <EncryptedSealBfvVector> DenseMatrixBySparseVectorMultiplyTask(EncryptedSealBfvVector[] denses, EncryptedSealBfvVector sparse, EncryptedSealBfvEnvironment env) { return(Task <EncryptedSealBfvVector> .Factory.StartNew(() => DenseMatrixBySparseVectorMultiply(denses, sparse, env))); }