2019-02-27 20:55:48 +00:00
|
|
|
package stack
|
|
|
|
|
2019-04-02 20:38:41 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
)
|
2019-02-27 20:55:48 +00:00
|
|
|
|
|
|
|
// Int represents an integer on the stack
|
|
|
|
type Int struct {
|
|
|
|
*abstractItem
|
|
|
|
val *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInt will convert a big integer into
|
|
|
|
// a StackInteger
|
|
|
|
func NewInt(val *big.Int) (*Int, error) {
|
|
|
|
return &Int{
|
|
|
|
abstractItem: &abstractItem{},
|
|
|
|
val: val,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal will check if two integers hold equal value
|
|
|
|
func (i *Int) Equal(s *Int) bool {
|
2019-08-12 15:05:04 +00:00
|
|
|
return i.val.Cmp(s.val) == 0
|
2019-02-27 20:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add will add two stackIntegers together
|
|
|
|
func (i *Int) Add(s *Int) (*Int, error) {
|
2019-02-27 21:38:11 +00:00
|
|
|
return &Int{
|
|
|
|
val: new(big.Int).Add(i.val, s.val),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sub will subtract two stackIntegers together
|
|
|
|
func (i *Int) Sub(s *Int) (*Int, error) {
|
2019-02-27 20:55:48 +00:00
|
|
|
return &Int{
|
|
|
|
val: new(big.Int).Sub(i.val, s.val),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-27 21:38:11 +00:00
|
|
|
// Mul will multiply two stackIntegers together
|
|
|
|
func (i *Int) Mul(s *Int) (*Int, error) {
|
|
|
|
return &Int{
|
|
|
|
val: new(big.Int).Mul(i.val, s.val),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:32:32 +00:00
|
|
|
// Div will divide one stackInteger by an other.
|
|
|
|
func (i *Int) Div(s *Int) (*Int, error) {
|
|
|
|
return &Int{
|
|
|
|
val: new(big.Int).Div(i.val, s.val),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-27 21:38:11 +00:00
|
|
|
// Mod will take the mod of two stackIntegers together
|
|
|
|
func (i *Int) Mod(s *Int) (*Int, error) {
|
|
|
|
return &Int{
|
|
|
|
val: new(big.Int).Mod(i.val, s.val),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rsh will shift the integer b to the right by `n` bits
|
|
|
|
func (i *Int) Rsh(n *Int) (*Int, error) {
|
|
|
|
return &Int{
|
|
|
|
val: new(big.Int).Rsh(i.val, uint(n.val.Int64())),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lsh will shift the integer b to the left by `n` bits
|
|
|
|
func (i *Int) Lsh(n *Int) (*Int, error) {
|
|
|
|
return &Int{
|
|
|
|
val: new(big.Int).Lsh(i.val, uint(n.val.Int64())),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-27 20:55:48 +00:00
|
|
|
// Integer will overwrite the default implementation
|
|
|
|
// to allow go to cast this item as an integer.
|
|
|
|
func (i *Int) Integer() (*Int, error) {
|
|
|
|
return i, nil
|
|
|
|
}
|
2019-02-27 22:41:46 +00:00
|
|
|
|
|
|
|
// ByteArray override the default ByteArray method
|
|
|
|
// to convert a Integer into a byte Array
|
|
|
|
func (i *Int) ByteArray() (*ByteArray, error) {
|
2019-03-15 22:30:25 +00:00
|
|
|
b := i.val.Bytes()
|
|
|
|
dest := reverse(b)
|
|
|
|
return NewByteArray(dest), nil
|
2019-02-27 22:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Boolean override the default Boolean method
|
|
|
|
// to convert an Integer into a Boolean StackItem
|
|
|
|
func (i *Int) Boolean() (*Boolean, error) {
|
|
|
|
boolean := (i.val.Int64() != 0)
|
2019-03-16 21:44:03 +00:00
|
|
|
return NewBoolean(boolean), nil
|
2019-03-15 22:30:25 +00:00
|
|
|
}
|
2019-02-27 22:41:46 +00:00
|
|
|
|
2019-03-15 22:30:25 +00:00
|
|
|
//Value returns the underlying big.Int
|
|
|
|
func (i *Int) Value() *big.Int {
|
|
|
|
return i.val
|
2019-02-27 22:41:46 +00:00
|
|
|
}
|
2019-03-28 19:26:55 +00:00
|
|
|
|
|
|
|
// Abs returns a stack integer whose underlying value is
|
|
|
|
// the absolute value of the original stack integer.
|
|
|
|
func (i *Int) Abs() (*Int, error) {
|
|
|
|
a := big.NewInt(0).Abs(i.Value())
|
|
|
|
b, err := NewInt(a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
2019-03-29 19:35:16 +00:00
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
// Lte returns a bool value from the comparison of two integers, a and b.
|
|
|
|
// value is true if a <= b.
|
|
|
|
// value is false if a > b.
|
|
|
|
func (i *Int) Lte(s *Int) bool {
|
|
|
|
return i.Value().Cmp(s.Value()) != 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gte returns a bool value from the comparison of two integers, a and b.
|
|
|
|
// value is true if a >= b.
|
|
|
|
// value is false if a < b.
|
|
|
|
func (i *Int) Gte(s *Int) bool {
|
|
|
|
return i.Value().Cmp(s.Value()) != -1
|
|
|
|
}
|
|
|
|
|
2019-03-29 19:35:16 +00:00
|
|
|
// Lt returns a bool value from the comparison of two integers, a and b.
|
|
|
|
// value is true if a < b.
|
|
|
|
// value is false if a >= b.
|
|
|
|
func (i *Int) Lt(s *Int) bool {
|
|
|
|
return i.Value().Cmp(s.Value()) == -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gt returns a bool value from the comparison of two integers, a and b.
|
|
|
|
// value is true if a > b.
|
|
|
|
// value is false if a <= b.
|
|
|
|
func (i *Int) Gt(s *Int) bool {
|
|
|
|
return i.Value().Cmp(s.Value()) == 1
|
|
|
|
}
|
2019-04-03 22:34:21 +00:00
|
|
|
|
|
|
|
// Invert returns an Integer whose underlying value is the bitwise complement
|
|
|
|
// of the original value.
|
|
|
|
func (i *Int) Invert() (*Int, error) {
|
|
|
|
res := new(big.Int).Not(i.Value())
|
|
|
|
return NewInt(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// And returns an Integer whose underlying value is the result of the
|
|
|
|
// application of the bitwise AND operator to the two original integers'
|
|
|
|
// values.
|
|
|
|
func (i *Int) And(s *Int) (*Int, error) {
|
|
|
|
res := new(big.Int).And(i.Value(), s.Value())
|
|
|
|
return NewInt(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Or returns an Integer whose underlying value is the result of the
|
|
|
|
// application of the bitwise OR operator to the two original integers'
|
|
|
|
// values.
|
|
|
|
func (i *Int) Or(s *Int) (*Int, error) {
|
|
|
|
res := new(big.Int).Or(i.Value(), s.Value())
|
|
|
|
return NewInt(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Xor returns an Integer whose underlying value is the result of the
|
|
|
|
// application of the bitwise XOR operator to the two original integers'
|
|
|
|
// values.
|
|
|
|
func (i *Int) Xor(s *Int) (*Int, error) {
|
|
|
|
res := new(big.Int).Xor(i.Value(), s.Value())
|
|
|
|
return NewInt(res)
|
|
|
|
}
|
2019-08-12 09:21:52 +00:00
|
|
|
|
2019-04-02 20:38:41 +00:00
|
|
|
// Hash overrides the default abstract hash method.
|
|
|
|
func (i *Int) Hash() (string, error) {
|
|
|
|
data := fmt.Sprintf("%T %v", i, i.Value())
|
|
|
|
return KeyGenerator([]byte(data))
|
2019-08-12 09:21:52 +00:00
|
|
|
}
|
2019-08-12 09:11:29 +00:00
|
|
|
|
2019-08-12 09:21:52 +00:00
|
|
|
// Min returns the mininum between two integers.
|
2019-03-29 16:43:16 +00:00
|
|
|
func Min(a *Int, b *Int) *Int {
|
2019-04-03 18:36:54 +00:00
|
|
|
if a.Lte(b) {
|
2019-03-29 16:43:16 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Max returns the maximun between two integers.
|
|
|
|
func Max(a *Int, b *Int) *Int {
|
2019-04-03 18:36:54 +00:00
|
|
|
if a.Gte(b) {
|
2019-03-29 16:43:16 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Within returns a bool whose value is true
|
|
|
|
// iff the value of the integer i is within the specified
|
|
|
|
// range [a,b) (left-inclusive).
|
|
|
|
func (i *Int) Within(a *Int, b *Int) bool {
|
|
|
|
// i >= a && i < b
|
2019-04-03 18:36:54 +00:00
|
|
|
return i.Gte(a) && i.Lt(b)
|
2019-04-02 20:38:41 +00:00
|
|
|
}
|