Пример #1
0
        public static int EncodedLength(long input)
        {
            if (input == 0)
            {
                return(Zero.Length);
            }

            int length = (int)TritsHelper.RoundThird((long)PascalMinTrits((ulong)Math.Abs(input)));

            return(length + (int)PascalMinTrits((ulong)Math.Pow(2, (uint)(length / Constants.TritsPerTryte)) - 1));
        }
Пример #2
0
        private bool SearchCpu(sbyte[] state, int offset, int length, int security)
        {
            int count = Math.Min(length, CurlHashLength) - offset;

            // Ignore group incr
            int index = -1;

            while (index < 0)
            {
                int increaseLength = Increase(_stateLow, _stateHigh, offset + count * 2 / 3, count - count * 2 / 3);
                count =
                    (int)Math.Min(TritsHelper.RoundThird(offset + count * 2 / 3 + increaseLength), CurlHashLength) -
                    offset;

                // clone
                ulong[] stateLow  = new ulong[CurlStateLength];
                ulong[] stateHigh = new ulong[CurlStateLength];
                Array.Copy(_stateLow, stateLow, CurlStateLength);
                Array.Copy(_stateHigh, stateHigh, CurlStateLength);

                // transform
                Transform(stateLow, stateHigh);

                // check
                index = Check(security, stateLow, stateHigh);
            }

            // output
            for (int i = 0; i < count; i++)
            {
                ulong low  = (_stateLow[i] >> index) & 1;
                ulong high = (_stateHigh[i] >> index) & 1;

                if (low == 1 && high == 0)
                {
                    state[i] = -1;
                }
                else if (low == 0 && high == 1)
                {
                    state[i] = 1;
                }
                else if (low == 1 && high == 1)
                {
                    state[i] = 0;
                }
                else
                {
                    state[i] = 0;
                }
            }

            return(false);
        }
Пример #3
0
        public static void Encode(long input, sbyte[] trits)
        {
            if (input == 0)
            {
                Array.Copy(Zero, trits, Zero.Length);
            }
            else
            {
                int  length   = (int)TritsHelper.RoundThird((long)PascalMinTrits((ulong)Math.Abs(input)));
                long encoding = 0;
                TritsHelper.Long2Trits(input, trits);
                int index = 0;

                int end = length - Constants.TritsPerTryte;
                for (int i = 0; i < end; i += Constants.TritsPerTryte)
                {
                    int n = end - i > Constants.TritsPerTryte ? Constants.TritsPerTryte : end - i;
                    if (TritsHelper.Trits2Long(trits, i, n) > 0)
                    {
                        encoding |= (1L << index);
                        for (int j = 0; j < n; j++)
                        {
                            trits[i + j] = (sbyte)-trits[i + j];
                        }
                    }

                    index += 1;
                }

                if (TritsHelper.Trits2Long(trits, length - Constants.TritsPerTryte, Constants.TritsPerTryte) < 0)
                {
                    encoding |= (1L << index);
                    for (int j = 0; j < Constants.TritsPerTryte; j++)
                    {
                        trits[length - Constants.TritsPerTryte + j] =
                            (sbyte)-trits[length - Constants.TritsPerTryte + j];
                    }
                }

                sbyte[] encodingTrits = new sbyte[trits.Length - length];
                TritsHelper.Long2Trits(encoding, encodingTrits);

                Array.Copy(encodingTrits, 0, trits, length, encodingTrits.Length);
            }
        }