public override void Init(List <byte> arg) // from interfaces
        {
            const int bias = 0;                    //S-Block record type: 'some args, Line1, Line2, ..., LineN'. No some args => Bias=0.

            if (arg.Count >= bias)
            {
                if (arg.Count - bias != 0)
                {
                    VarCount = (int)Math.Log(arg.Count - bias, 2); // Matrix: 2**n x n
                }
                if (VarCount == this._length)
                {
                    FuncMatrix = WayConverter.ListToMatrix(arg.Skip(bias).ToList(), VarCount);
                    //Line0:        y0..yn
                    //Line1:        y0..yn
                    //....................
                    //Line2**n-1:   y0..yn
                    if (_database == null)
                    {
                        _database = new SBlockDB();
                    }
                    var Note = _database.GetNoteFromDB(FuncMatrix);
                    CorMatrix = Note.CorMatrix;
                    DifMatrix = Note.DifMatrix;
                    LStates   = Note.LStates;
                    DStates   = Note.DStates;
                }
                else
                {
                    throw new Exception("Argument length error");
                }
            }
        }
Beispiel #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="Param">Value from Cor or Dif Matrix</param>
 /// <param name="inputs"></param>
 /// <param name="outputs"></param>
 /// <param name="length"></param>
 public BlockState(Int64 MatrixValue, int inputs, int outputs, int length)
 {
     _length          = length;
     this.MatrixValue = MatrixValue;
     _inputs          = WayConverter.ToList(inputs, _length);
     _outputs         = WayConverter.ToList(outputs, _length);
 }
Beispiel #3
0
        protected override bool SLayer(SolverParams SolParams)
        {
            //var LIndex = SolParams.lastNotEmptyLayerIndex;
            var ret = true;
            int ActiveBlocksCount = 0;

            //var BIndex = SolParams.BIndex;
            if (SolParams.BIndex == -1)
            {
                SolParams.BIndex = 0;
                for (; SolParams.BIndex < SolParams.Way.layers[SolParams.lastNotEmptyLayerIndex].blocks.Count; SolParams.BIndex++)
                {
                    var WayBlock = SolParams.Way.layers[SolParams.lastNotEmptyLayerIndex].blocks[SolParams.BIndex];
                    if (!WayBlock.active_inputs.All(x => !x))
                    {
                        ActiveBlocksCount++;
                        continue;
                    }
                }
                if (ActiveBlocksCount > SolParams.MaxActiveBlocksOnLayer)
                {
                    return(false);
                }
                SolParams.BIndex = 0;
            }
            for (; SolParams.BIndex < SolParams.Way.layers[SolParams.lastNotEmptyLayerIndex].blocks.Count; SolParams.BIndex++)
            {
                var WayBlock = SolParams.Way.layers[SolParams.lastNotEmptyLayerIndex].blocks[SolParams.BIndex];
                if (WayBlock.active_inputs.All(x => !x))
                {
                    continue;
                }
                if (!WayBlock.active_outputs.All(x => !x))
                {
                    //ActiveBlocksCount++;
                    continue; //already solved block
                }
                ret = false;
                var NetBlock = SolParams.Net.GetLayers()[SolParams.lastNotEmptyLayerIndex].GetBlocks()[SolParams.BIndex];
                var Params   = new BlockStateExtrParams(WayBlock.active_inputs, null, SolParams.Net.GetMultiThreadPrevalence(), SolParams.P, SolParams.Type, true);
                var States   = NetBlock.ExtractStates(Params);
                if (States.Count > 0)
                {
                    var State    = States[0];
                    var NewWay   = WayConverter.CloneWay(SolParams.Way);
                    var NewBLock = NewWay.layers[SolParams.lastNotEmptyLayerIndex].blocks[SolParams.BIndex];
                    NewBLock.active_outputs = State._outputs;
                    NewWay.layers[SolParams.lastNotEmptyLayerIndex].blocks[SolParams.BIndex] = NewBLock;
                    var NewSolParams = SolParams;
                    NewSolParams.P  *= State.MatrixValue;
                    NewSolParams.Way = NewWay;
                    NewSolParams.BIndex++;
                    Solve(NewSolParams);
                }
                break;
            }
            return(ret);
        }
Beispiel #4
0
        public virtual void Solve(SolverParams SolParams)
        {
            var layersCount = SolParams.Way.layers.Count();
            var roundsCount = layersCount / 3;

            #region FindLastNotEmptyLayer
            if (SolParams.lastNotEmptyLayerIndex == -1)
            {
                SolParams.lastNotEmptyLayerIndex = WayConverter.SearchLastNotEmptyLayer(SolParams.Way);
            }
            #endregion
            #region FullRounds
            if ((SolParams.lastNotEmptyLayerIndex >= 0) && (SolParams.lastNotEmptyLayerIndex / 3 < roundsCount - 1))
            {
                for (int i = SolParams.lastNotEmptyLayerIndex / 3; i < roundsCount - 1; i++)
                {
                    #region K-layer
                    if (SolParams.Way.layers[SolParams.lastNotEmptyLayerIndex].type == LayerType.KLayer)
                    {
                        KLayer(SolParams);
                        WayConverter.CopyOutToIn(SolParams.Way, SolParams.lastNotEmptyLayerIndex, SolParams.lastNotEmptyLayerIndex + 1);
                        SolParams.lastNotEmptyLayerIndex++;
                        SolParams.BIndex = -1;
                    }
                    #endregion
                    #region S-layer
                    if (SolParams.Way.layers[SolParams.lastNotEmptyLayerIndex].type == LayerType.SLayer)
                    {
                        if (!(SLayer(SolParams)))
                        {
                            return;
                        }
                        WayConverter.CopyOutToIn(SolParams.Way, SolParams.lastNotEmptyLayerIndex, SolParams.lastNotEmptyLayerIndex + 1);
                        SolParams.lastNotEmptyLayerIndex++;
                    }
                    #endregion
                    #region P-layer
                    if (SolParams.Way.layers[SolParams.lastNotEmptyLayerIndex].type == LayerType.PLayer)
                    {
                        PLayer(SolParams);
                        WayConverter.CopyOutToIn(SolParams.Way, SolParams.lastNotEmptyLayerIndex, SolParams.lastNotEmptyLayerIndex + 1);
                        SolParams.lastNotEmptyLayerIndex++;
                    }
                    #endregion
                }
            }
            #endregion
            #region LastRound
            //No need to process LastRound, because LastRound must be reversed.
            #endregion
            SolParams.Net.GetCallbackAddSolution()(new Solution(SolParams.P, SolParams.Way));
            if (SolParams.P > SolParams.Net.GetMultiThreadPrevalence())
            {
                SolParams.Net.SetMultiThreadPrevalence(SolParams.P);
            }
        }
Beispiel #5
0
 public void Init(TaskerParams Params)
 {
     this.Params          = Params;
     _rounds_count        = Params.Net.GetLayers().Count / 3;
     _tempEmptyWay        = WayConverter.ToWay(Params.Net);
     IsBruteForceTurnedOn = false;
     Iter   = new InputsIterator(Params.Net.GetSettings().SBoxCount, Params.Net.GetSettings().SBoxSize);
     _tasks = new ConcurrentQueue <Task>();
     InitSolvers();
     ProcessRules();
 }
        public DBNote GetNoteFromDB(List <List <bool> > funcMatrix)
        {
            DBNote Note;
            var    key = WayConverter.MatrixToString(funcMatrix);

            if (!FuncDB.TryGetValue(key, out Note))
            {
                Note = AddToFuncDB(key, funcMatrix);
            }
            else
            {
                ;
            }
            return(Note);
        }
Beispiel #7
0
        void ProcessRules()
        {
            //throw new NotImplementedException();
            for (int i = 0; i < Params.Alg.Rules.Count; i++)
            {
                var Rule = Params.Alg.Rules[i];
                if (Rule.UseCustomInput == true)
                {
                    var SolParam = new SolverParams(WayConverter.ToWay(Params.Net, Rule.Input), Params.Net, Params.Alg.Type, Rule.MaxActiveBlocksOnLayer);
                    var T        = new Task(Solvers[Rule.SolverType].S, SolParam, new ExtraParams());
                    _tasks.Enqueue(T);
                }
                else
                {
                    var S = Solvers[Rule.SolverType];
                    S.IsUsedForBruteForce    = true;
                    S.MaxActiveBlocksOnLayer = Rule.MaxActiveBlocksOnLayer;
                    Solvers[Rule.SolverType] = S;
                    IsBruteForceTurnedOn     = true;
                }
            }
            //Warning! Unoptimized code!!!
            if (IsBruteForceTurnedOn)
            {
                var          temp = Iter;
                SolverInputs NextInput;
                foreach (var S in Solvers)
                {
                    if (S.Value.IsUsedForBruteForce)
                    {
                        Iter = new InputsIterator(Params.Net.GetSettings().SBoxCount, Params.Net.GetSettings().SBoxSize);
                        while (!Iter.IsFinished())
                        {
                            NextInput = Iter.NextState();
                            var ws = WayConverter.ToWay(Params.Net, NextInput);

                            _tasks.Enqueue(new Task(S.Value.S, new SolverParams(ws, Params.Net, Params.Alg.Type, S.Value.MaxActiveBlocksOnLayer)));
                        }
                    }
                }
                Iter = temp;
            }
        }
        public List <List <short> > GetDifMatrix(List <List <bool> > funcMatrix)
        {
            var funcList = WayConverter.MatrixToList(funcMatrix);
            var ret      = new List <List <short> >();

            ret.AddRange(Enumerable.Range(0, funcMatrix.Count).Select(i => new List <short>(funcMatrix.Count).ToList()));
            for (int a = 0; a < funcMatrix.Count; a++)
            {
                for (int b = 0; b < funcMatrix.Count; b++)
                {
                    int Counter = 0;
                    foreach (var x in Enumerable.Range(0, funcMatrix.Count))
                    {
                        if ((funcList[x] ^ funcList[x ^ a]) == b)
                        {
                            Counter++;
                        }
                    }
                    ret[a].Add((short)Counter);
                }
            }
            return(ret);
        }
 public DBNote GetNoteFromDB(List <byte> funcMatrix, int VarCount)
 {
     return(GetNoteFromDB(WayConverter.ListToMatrix(funcMatrix, VarCount)));
 }
Beispiel #10
0
 public SolverInputs(long Input, int length)
 {
     input  = WayConverter.ToList(Input, length);
     weight = 0;
 }