github.com/etherite/go-etherite@v0.0.0-20171015192807-5f4dd87b2f6e/common/number/int.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package number
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/etherite/go-etherite/common"
    23  )
    24  
    25  var tt256 = new(big.Int).Lsh(big.NewInt(1), 256)
    26  var tt256m1 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
    27  var tt255 = new(big.Int).Lsh(big.NewInt(1), 255)
    28  
    29  func limitUnsigned256(x *Number) *Number {
    30  	x.num.And(x.num, tt256m1)
    31  	return x
    32  }
    33  
    34  func limitSigned256(x *Number) *Number {
    35  	if x.num.Cmp(tt255) < 0 {
    36  		return x
    37  	} else {
    38  		x.num.Sub(x.num, tt256)
    39  		return x
    40  	}
    41  }
    42  
    43  // Number function
    44  type Initialiser func(n int64) *Number
    45  
    46  // A Number represents a generic integer with a bounding function limiter. Limit is called after each operations
    47  // to give "fake" bounded integers. New types of Number can be created through NewInitialiser returning a lambda
    48  // with the new Initialiser.
    49  type Number struct {
    50  	num   *big.Int
    51  	limit func(n *Number) *Number
    52  }
    53  
    54  // Returns a new initialiser for a new *Number without having to expose certain fields
    55  func NewInitialiser(limiter func(*Number) *Number) Initialiser {
    56  	return func(n int64) *Number {
    57  		return &Number{big.NewInt(n), limiter}
    58  	}
    59  }
    60  
    61  // Return a Number with a UNSIGNED limiter up to 256 bits
    62  func Uint256(n int64) *Number {
    63  	return &Number{big.NewInt(n), limitUnsigned256}
    64  }
    65  
    66  // Return a Number with a SIGNED limiter up to 256 bits
    67  func Int256(n int64) *Number {
    68  	return &Number{big.NewInt(n), limitSigned256}
    69  }
    70  
    71  // Returns a Number with a SIGNED unlimited size
    72  func Big(n int64) *Number {
    73  	return &Number{big.NewInt(n), func(x *Number) *Number { return x }}
    74  }
    75  
    76  // Sets i to sum of x+y
    77  func (i *Number) Add(x, y *Number) *Number {
    78  	i.num.Add(x.num, y.num)
    79  	return i.limit(i)
    80  }
    81  
    82  // Sets i to difference of x-y
    83  func (i *Number) Sub(x, y *Number) *Number {
    84  	i.num.Sub(x.num, y.num)
    85  	return i.limit(i)
    86  }
    87  
    88  // Sets i to product of x*y
    89  func (i *Number) Mul(x, y *Number) *Number {
    90  	i.num.Mul(x.num, y.num)
    91  	return i.limit(i)
    92  }
    93  
    94  // Sets i to the quotient prodject of x/y
    95  func (i *Number) Div(x, y *Number) *Number {
    96  	i.num.Div(x.num, y.num)
    97  	return i.limit(i)
    98  }
    99  
   100  // Sets i to x % y
   101  func (i *Number) Mod(x, y *Number) *Number {
   102  	i.num.Mod(x.num, y.num)
   103  	return i.limit(i)
   104  }
   105  
   106  // Sets i to x << s
   107  func (i *Number) Lsh(x *Number, s uint) *Number {
   108  	i.num.Lsh(x.num, s)
   109  	return i.limit(i)
   110  }
   111  
   112  // Sets i to x^y
   113  func (i *Number) Pow(x, y *Number) *Number {
   114  	i.num.Exp(x.num, y.num, big.NewInt(0))
   115  	return i.limit(i)
   116  }
   117  
   118  // Setters
   119  
   120  // Set x to i
   121  func (i *Number) Set(x *Number) *Number {
   122  	i.num.Set(x.num)
   123  	return i.limit(i)
   124  }
   125  
   126  // Set x bytes to i
   127  func (i *Number) SetBytes(x []byte) *Number {
   128  	i.num.SetBytes(x)
   129  	return i.limit(i)
   130  }
   131  
   132  // Cmp compares x and y and returns:
   133  //
   134  //     -1 if x <  y
   135  //     0 if x == y
   136  //     +1 if x >  y
   137  func (i *Number) Cmp(x *Number) int {
   138  	return i.num.Cmp(x.num)
   139  }
   140  
   141  // Getters
   142  
   143  // Returns the string representation of i
   144  func (i *Number) String() string {
   145  	return i.num.String()
   146  }
   147  
   148  // Returns the byte representation of i
   149  func (i *Number) Bytes() []byte {
   150  	return i.num.Bytes()
   151  }
   152  
   153  // Uint64 returns the Uint64 representation of x. If x cannot be represented in an int64, the result is undefined.
   154  func (i *Number) Uint64() uint64 {
   155  	return i.num.Uint64()
   156  }
   157  
   158  // Int64 returns the int64 representation of x. If x cannot be represented in an int64, the result is undefined.
   159  func (i *Number) Int64() int64 {
   160  	return i.num.Int64()
   161  }
   162  
   163  // Returns the signed version of i
   164  func (i *Number) Int256() *Number {
   165  	return Int(0).Set(i)
   166  }
   167  
   168  // Returns the unsigned version of i
   169  func (i *Number) Uint256() *Number {
   170  	return Uint(0).Set(i)
   171  }
   172  
   173  // Returns the index of the first bit that's set to 1
   174  func (i *Number) FirstBitSet() int {
   175  	for j := 0; j < i.num.BitLen(); j++ {
   176  		if i.num.Bit(j) > 0 {
   177  			return j
   178  		}
   179  	}
   180  
   181  	return i.num.BitLen()
   182  }
   183  
   184  // Variables
   185  
   186  var (
   187  	Zero       = Uint(0)
   188  	One        = Uint(1)
   189  	Two        = Uint(2)
   190  	MaxUint256 = Uint(0).SetBytes(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))
   191  
   192  	MinOne = Int(-1)
   193  
   194  	// "typedefs"
   195  	Uint = Uint256
   196  	Int  = Int256
   197  )