예제 #1
0
        /// <summary>
        /// Pushes the number of stack items onto the stack. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            opData.Push(OpHelper.IntToByteArray(opData.ItemCount));

            error = null;
            return(true);
        }
        /// <summary>
        /// Removes top three stack items and converts them to <see cref="long"/>s: value, Min, Max.
        /// Pushes 1 if value was in range, otherwise 0.
        /// Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 3)
            {
                error = Err.OpNotEnoughItems;
                return(false);
            }

            // Stack is: x then min then max => pop max first
            byte[][] numbers = opData.Pop(3);
            if (!TryConvertToLong(numbers[2], out long max, opData.StrictNumberEncoding))
            {
                error = "Invalid number format (max).";
                return(false);
            }
            if (!TryConvertToLong(numbers[1], out long min, opData.StrictNumberEncoding))
            {
                error = "Invalid number format (min).";
                return(false);
            }
            if (!TryConvertToLong(numbers[0], out long x, opData.StrictNumberEncoding))
            {
                error = "Invalid number format (x).";
                return(false);
            }

            opData.Push(x >= min && x < max);

            error = null;
            return(true);
        }
        /// <summary>
        /// Removes top three stack items and converts them to <see cref="long"/>s: value, Min, Max.
        /// Pushes 1 if value was in range, otherwise 0.
        /// Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 3)
            {
                error = "Invalid number of elements in stack.";
                return(false);
            }

            // Stack is: x then min then max so we pop max first
            if (!OpHelper.TryConvertByteArrayToInt(opData.Pop(), out long max, true))
            {
                error = "Couldn't convert to number.";
                return(false);
            }
            if (!OpHelper.TryConvertByteArrayToInt(opData.Pop(), out long min, true))
            {
                error = "Couldn't convert to number.";
                return(false);
            }
            if (!OpHelper.TryConvertByteArrayToInt(opData.Pop(), out long x, true))
            {
                error = "Couldn't convert to number.";
                return(false);
            }

            int c = (x >= min && x < max) ? 1 : 0;

            opData.Push(OpHelper.IntToByteArray(c));

            error = null;
            return(true);
        }
        /// <summary>
        /// Duplicates the top stack item. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 1)
            {
                error = Err.OpNotEnoughItems;
                return(false);
            }

            opData.Push(opData.Peek());
            return(CheckItemCount(opData, out error));
        }
예제 #5
0
 /// <summary>
 /// Pushes the specified data of this instance at the top of the stack.
 /// </summary>
 /// <param name="opData">Stack to use</param>
 /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
 /// <returns>True if operation was successful, false if otherwise</returns>
 public override bool Run(IOpData opData, out string error)
 {
     if (OpValue == OP._0)
     {
         opData.Push(new byte[0]);
     }
     else if (OpValue == OP.Negative1)
     {
         opData.Push(new byte[1] {
             0b1000_0001
         });
        /// <summary>
        /// Replaces top two stack items interpreted as <see cref="long"/>s with their subtract result. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (!TrySetValues(opData, out error))
            {
                return(false);
            }

            opData.Push(OpHelper.IntToByteArray(a - b));

            error = null;
            return(true);
        }
        /// <summary>
        /// Replaces top two stack items interpreted as <see cref="long"/>s with 1 if first one was bigger or equal, otherwise with 0.
        /// Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (!TrySetValues(opData, out error))
            {
                return(false);
            }

            opData.Push(a >= b);

            error = null;
            return(true);
        }
        /// <summary>
        /// Removes top alt-stack item and puts it in stack. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.AltItemCount < 1)
            {
                error = Err.OpNotEnoughItems + "(alt stack)";
                return(false);
            }

            opData.Push(opData.AltPop());

            error = null;
            return(true);
        }
        /// <summary>
        /// Copies the second item from top of the stack to the top. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 2)
            {
                error = Err.OpNotEnoughItems;
                return(false);
            }

            byte[] data = opData.PeekAtIndex(1);
            opData.Push(data);

            return(CheckItemCount(opData, out error));
        }
예제 #10
0
        /// <summary>
        /// Pushes the size of the top stack item to the stack without removing the item. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 1)
            {
                error = Err.OpNotEnoughItems;
                return(false);
            }

            byte[] temp = opData.Peek();
            opData.Push(IntToByteArray(temp.Length));

            return(CheckItemCount(opData, out error));
        }
예제 #11
0
        /// <summary>
        /// Duplicates the top stack item. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 1)
            {
                error = "There was no item left in the stack to duplicate.";
                return(false);
            }

            opData.Push(opData.Peek());

            error = null;
            return(true);
        }
예제 #12
0
        /// <summary>
        /// Replaces top stack item with its hash digest. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 1)
            {
                error = "Not enough items in stack.";
                return(false);
            }

            opData.Push(Hash.ComputeHash(opData.Pop()));

            error = null;
            return(true);
        }
예제 #13
0
        /// <summary>
        /// Removes top alt-stack item and puts it in stack. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.AltItemCount < 1)
            {
                error = "There was not enough items left in the alt-stack to copy to stack.";
                return(false);
            }

            opData.Push(opData.AltPop());

            error = null;
            return(true);
        }
예제 #14
0
        /// <summary>
        /// Removes top two stack item and pushes (true for equality and false otherwiwe) onto the stack. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 2)
            {
                error = "There was not enough items left in the stack to check.";
                return(false);
            }

            byte[] item1 = opData.Pop();
            byte[] item2 = opData.Pop();

            if (item1.IsEqualTo(item2))
            {
                opData.Push(new byte[] { 1 });
            }
            else
            {
                opData.Push(new byte[0]);
            }

            error = null;
            return(true);
        }
예제 #15
0
        /// <summary>
        /// Removes top two stack items (signature and public key) and verifies the transaction signature.
        /// </summary>
        /// <param name="opData">Stack to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            bool b = ExtractAndVerify(opData, out error);

            if (error is null)
            {
                opData.Push(b);
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #16
0
        /// <summary>
        /// Pushes the size of the top stack item to the stack. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 1)
            {
                error = "There was not enough items left in the stack.";
                return(false);
            }

            byte[] temp = opData.Peek();
            opData.Push(OpHelper.IntToByteArray(temp.Length));

            error = null;
            return(true);
        }
        /// <summary>
        /// Swaps the position of top 2 items on top of the stack. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 2)
            {
                error = Err.OpNotEnoughItems;
                return(false);
            }

            byte[][] data = opData.Pop(2);
            opData.Push(new byte[2][] { data[1], data[0] });

            error = null;
            return(true);
        }
        /// <summary>
        /// Copies 2 items from stack to top of the stack like this: x1 x2 x3 x4 -> x1 x2 x3 x4 x1 x2
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 4)
            {
                error = Err.OpNotEnoughItems;
                return(false);
            }

            byte[] data1 = opData.PeekAtIndex(3);
            byte[] data2 = opData.PeekAtIndex(2);

            opData.Push(new byte[2][] { data1, data2 });
            return(CheckItemCount(opData, out error));
        }
예제 #19
0
        /// <summary>
        /// Copies the second item from top of the stack to the top. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 2)
            {
                error = "There was not enough items left in the stack to copy over.";
                return(false);
            }

            byte[] data = opData.PeekAtIndex(1);
            opData.Push(data);

            error = null;
            return(true);
        }
예제 #20
0
        /// <summary>
        /// Swaps the position of top 2 items on top of the stack. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 2)
            {
                error = "There was not enough items left in the stack to swap.";
                return(false);
            }

            byte[][] data = opData.Pop(2);
            opData.Push(new byte[2][] { data[1], data[0] });

            error = null;
            return(true);
        }
예제 #21
0
        /// <summary>
        /// Replaces top stack item with its hash digest. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 1)
            {
                error = Err.OpNotEnoughItems;
                return(false);
            }

            using Sha256 hash = new Sha256();
            opData.Push(hash.ComputeHash(opData.Pop()));

            error = null;
            return(true);
        }
        /// <summary>
        /// Replaces top two stack items interpreted as <see cref="long"/>s with the bigger of the two.
        /// Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (!TrySetValues(opData, out error))
            {
                return(false);
            }

            long c = (a > b) ? a : b;

            opData.Push(IntToByteArray(c));

            error = null;
            return(true);
        }
        /// <summary>
        /// Replaces top two stack items interpreted as <see cref="long"/>s with 1 if either one is not 0, otherwise with 0.
        /// Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (!TrySetValues(opData, out error))
            {
                return(false);
            }

            int c = (a != 0 || b != 0) ? 1 : 0;

            opData.Push(OpHelper.IntToByteArray(c));

            error = null;
            return(true);
        }
        /// <summary>
        /// Swaps top two item pairs on top of the stack: x1 x2 x3 x4 -> x3 x4 x1 x2
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 4)
            {
                error = Err.OpNotEnoughItems;
                return(false);
            }

            byte[][] data = opData.Pop(4);
            // x0 x1 x2 x3 -> x2 x3 x0 x1
            opData.Push(new byte[4][] { data[2], data[3], data[0], data[1] });

            error = null;
            return(true);
        }
예제 #25
0
        /// <summary>
        /// Swaps top two item pairs on top of the stack: x1 x2 x3 x4 -> x3 x4 x1 x2
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 4)
            {
                error = "There was not enough items left in the stack to swap.";
                return(false);
            }

            byte[][] data = opData.Pop(4);
            // x0 x1 x2 x3 -> x2 x3 x0 x1
            opData.Push(new byte[4][] { data[2], data[3], data[0], data[1] });

            error = null;
            return(true);
        }
예제 #26
0
        /// <summary>
        /// Rotates top 3 items on top of the stack to the left. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 3)
            {
                error = "There was not enough items left in the stack to rotate.";
                return(false);
            }

            byte[][] data = opData.Pop(3);
            // (x0 x1 x2 -> x1 x2 x0)
            opData.Push(new byte[3][] { data[1], data[2], data[0] });

            error = null;
            return(true);
        }
        /// <summary>
        /// Rotates top 3 items on top of the stack to the left. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 3)
            {
                error = Err.OpNotEnoughItems;
                return(false);
            }

            byte[][] data = opData.Pop(3);
            // (x0 x1 x2 -> x1 x2 x0)
            opData.Push(new byte[3][] { data[1], data[2], data[0] });

            error = null;
            return(true);
        }
        /// <summary>
        /// Removes top two stack item and pushes the result of their equality check (true for equality and false otherwiwe)
        /// onto the stack. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 2)
            {
                error = Err.OpNotEnoughItems;
                return(false);
            }

            ReadOnlySpan <byte> item1 = opData.Pop();
            ReadOnlySpan <byte> item2 = opData.Pop();

            opData.Push(item1.SequenceEqual(item2));

            error = null;
            return(true);
        }
예제 #29
0
        /// <summary>
        /// Copies 2 items from stack to top of the stack like this: x1 x2 x3 x4 -> x1 x2 x3 x4 x1 x2
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 4)
            {
                error = "There was not enough items left in the stack to copy over.";
                return(false);
            }

            byte[] data1 = opData.PeekAtIndex(3);
            byte[] data2 = opData.PeekAtIndex(2);

            opData.Push(new byte[2][] { data1, data2 });

            error = null;
            return(true);
        }
        /// <summary>
        /// Replaces top stack item interpreted as a <see cref="long"/> with its absolute value. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (!TrySetValue(opData, out error))
            {
                return(false);
            }

            if (a < 0)
            {
                a = -a;
            }
            opData.Push(IntToByteArray(a));

            error = null;
            return(true);
        }