public void DoesNotSolveUnsolvableSystem() { var coefficients = new List <long[]> { new[] { true, false, false }.ToLongs() }; var solution = GaussianEliminationHelpers.Solve(coefficients, 3).ToList(); if (solution.Count > 0) { Assert.True(solution[solution.Count].Operation != Operation.Complete); } }
public void DoesNotSolveUnsolvableSystem() { var coefficients = new List <Packed> { Packed.Create(new[] { true, false, false }) }; var solution = GaussianEliminationHelpers.Solve(coefficients, 3).ToList(); if (solution.Count > 0) { Assert.IsTrue(solution[solution.Count].Operation != Operation.Complete); } }
public void SolvesAlreadySolvedSystem() { var coefficients = new List <long[]> { new[] { true, false, false }.ToLongs(), new[] { false, true, false }.ToLongs(), new[] { false, false, true }.ToLongs() }; var steps = GaussianEliminationHelpers.Solve(coefficients, 3).ToList(); Assert.True(steps.Count > 0); Assert.Equal(Operation.Complete, steps[steps.Count - 1].Operation); AssertIsSolved(coefficients, 3); }
public void SolvesComplicatedSystem() { var coefficients = new List <Packed> { Packed.Create(new[] { true, false, true }), Packed.Create(new[] { true, true, true }), Packed.Create(new[] { false, false, true }) }; var steps = GaussianEliminationHelpers.Solve(coefficients, 3).ToList(); Assert.IsTrue(steps.Count > 0); Assert.AreEqual(Operation.Complete, steps[steps.Count - 1].Operation); AssertIsSolved(coefficients, 3); }
public void ProducesStepsWhichResultInIdenticalChangesAndASolution() { var coefficients = new List <long[]> { new [] { true, true, true }.ToLongs(), new [] { false, true, true }.ToLongs(), new [] { false, true, false }.ToLongs() }; var copy = coefficients.Select(x => x.Clone() as long[]).ToList(); var steps = GaussianEliminationHelpers.Solve(coefficients, 3).ToList(); // Assert that it has been solved Assert.True(steps.Count > 0); Assert.Equal(Operation.Complete, steps[steps.Count - 1].Operation); AssertIsSolved(coefficients, 3); // Perform the steps on the copy var mappedOperations = new Dictionary <Operation, Action <int, int, IList <long[]> > > { { Operation.Swap, (from, to, list) => { var temp = list[to]; list[to] = list[from]; list[from] = temp; } }, { Operation.Xor, (from, to, list) => list[to].Xor(list[from]) } }; foreach (var step in steps) { if (!mappedOperations.TryGetValue(step.Operation, out var operation)) { continue; } operation.Invoke(step.From, step.To, copy); } // Assert that the copy has been solved AssertIsSolved(copy, 3); }
/// <summary> /// Tries to decode the original data from all <see cref="Slice"/>s previously given to /// <see cref="Remember"/>. /// </summary> /// <remarks> /// Decoding the original data is akin to solving a set of linear equations. Each <see cref="Slice"/> /// represents one more equation. So enough <see cref="Slice"/>s have to be given to <see cref="Remember"/> /// in order to be able to solve them. /// </remarks> public bool TrySolve(out byte[] solution) { var solved = false; foreach (var step in GaussianEliminationHelpers.Solve( _coefficientsList, _numCoefficients)) { switch (step.Operation) { case Operation.Complete: solved = true; break; case Operation.Swap: Swap( step.From, step.To, _solutionsList); break; case Operation.Xor: Xor( step.From, step.To, _solutionsList); break; default: throw new NotImplementedException(); } } solution = solved ? _solutionsList .SelectMany(x => x.GetBytes(_sliceSize)) .Take(_totalLength) .ToArray() : null; return(solved); }
/// <summary> /// Tries to decode the original data from all <see cref="Slice"/>s previously given to /// <see cref="RememberAsync"/>. /// </summary> /// <remarks> /// Decoding the original data is akin to solving a set of linear equations. Each <see cref="Slice"/> /// represents one more equation. So enough <see cref="Slice"/>s have to be given to <see cref="RememberAsync"/> /// in order to be able to solve them. /// </remarks> public async Task <byte[]> TrySolveAsync() { using (await _guard.LockAsync()) { var solved = false; foreach (var step in GaussianEliminationHelpers.Solve(_coefficientsList)) { switch (step.Operation) { case Operation.Complete: solved = true; break; case Operation.Swap: Swap(step.From, step.To, _solutionsList); break; case Operation.Xor: Xor(step.From, step.To, _solutionsList); break; default: throw new NotImplementedException(); } } if (!solved) { return(null); } return (_solutionsList .SelectMany(x => x.GetBytes().Take(_sliceSize)) .Take(_totalLength) .ToArray()); } }
public void CanBeCalledWithoutEquations() { // ReSharper disable once ReturnValueOfPureMethodIsNotUsed GaussianEliminationHelpers.Solve(new List <long[]>(), 1).ToList(); }