Esempio n. 1
0
        public static byte[] ToBytesPadded(BigInteger value, int length)
        {
            byte[] result = new byte[length];
            byte[] bytes  = value.BigIntegerToBytes();
            int    bytesLength;
            byte   srcOffset;

            if (bytes[0] == 0)
            {
                bytesLength = bytes.Length - 1;
                srcOffset   = 1;
            }
            else
            {
                bytesLength = bytes.Length;
                srcOffset   = 0;
            }

            if (bytesLength > length)
            {
                throw new ArgumentOutOfRangeException(nameof(length), $"Input is too large to put in byte array of size; {bytesLength}>{length}");
            }

            int destOffset = length - bytesLength;

            Unsafe.CopyBlock(ref result[destOffset], ref bytes[srcOffset], (uint)bytesLength);
            //Array.Copy(bytes, srcOffset, result, destOffset, bytesLength);
            return(result);
        }
Esempio n. 2
0
        public static RlpString Create(BigInteger value)
        {
            // RLP encoding only supports positive integer values
            if (value.SignValue < 1)
            {
                return(new RlpString(EMPTY));
            }
            var bytes = value.BigIntegerToBytes();

            return(new RlpString(bytes.SkipWhile(element => element == 0).ToArray()));
        }
Esempio n. 3
0
        public static byte[] BigIntToBytesWithPadding(BigInteger bigint, int size)
        {
            byte[] asBytes  = bigint.BigIntegerToBytes();
            var    newArray = new byte[size];

            var startAt = newArray.Length - asBytes.Length;

            Unsafe.CopyBlock(ref newArray[startAt], ref asBytes[0], (uint)asBytes.Length);
            // Array.Copy(asBytes, 0, newArray, startAt, asBytes.Length);

            return(newArray);
        }