예제 #1
0
        private static ulong End(sbyte[] trits, int index)
        {
            if (TritsHelper.Trits2Long(trits, index, Constants.TritsPerTryte) > 0)
            {
                return(Constants.TritsPerTryte);
            }

            return(Constants.TritsPerTryte + End(trits, index + Constants.TritsPerTryte));
        }
예제 #2
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));
        }
예제 #3
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);
        }
예제 #4
0
        public static Tuple <long, int> Decode(sbyte[] trits)
        {
            // check Zero
            ulong index;

            for (index = 0; index < 4; index++)
            {
                if (trits[index] != Zero[index])
                {
                    break;
                }
            }

            if (index == 4)
            {
                return(new Tuple <long, int>(0, 4));
            }

            //
            var encodersStart = End(trits, 0);
            var inputEnd      = encodersStart +
                                PascalMinTrits((ulong)Math.Pow(2, (long)(encodersStart / Constants.TritsPerTryte)) - 1);

            var encoder = TritsHelper.Trits2Long(trits, (int)encodersStart, (int)(inputEnd - encodersStart));

            long acc = 0;
            int  i   = 0;

            index = 0;
            for (; index < encodersStart; index += Constants.TritsPerTryte, i++)
            {
                ulong left   = encodersStart - index;
                int   length = left > Constants.TritsPerTryte ? Constants.TritsPerTryte : (int)left;

                long temp = TritsHelper.Trits2Long(trits, (int)index, length);
                if (((encoder >> i) & 0x1L) != 0x0L)
                {
                    temp = -temp;
                }

                acc += (long)(Math.Pow(27, i)) * temp;
            }

            return(new Tuple <long, int>(acc, (int)inputEnd));
        }
예제 #5
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);
            }
        }