int.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package number
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. )
  21. var tt256 = new(big.Int).Lsh(big.NewInt(1), 256)
  22. var tt256m1 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
  23. var tt255 = new(big.Int).Lsh(big.NewInt(1), 255)
  24. func limitUnsigned256(x *Number) *Number {
  25. x.num.And(x.num, tt256m1)
  26. return x
  27. }
  28. func limitSigned256(x *Number) *Number {
  29. if x.num.Cmp(tt255) < 0 {
  30. return x
  31. } else {
  32. x.num.Sub(x.num, tt256)
  33. return x
  34. }
  35. }
  36. // Number function
  37. type Initialiser func(n int64) *Number
  38. // A Number represents a generic integer with a bounding function limiter. Limit is called after each operations
  39. // to give "fake" bounded integers. New types of Number can be created through NewInitialiser returning a lambda
  40. // with the new Initialiser.
  41. type Number struct {
  42. num *big.Int
  43. limit func(n *Number) *Number
  44. }
  45. // Returns a new initialiser for a new *Number without having to expose certain fields
  46. func NewInitialiser(limiter func(*Number) *Number) Initialiser {
  47. return func(n int64) *Number {
  48. return &Number{big.NewInt(n), limiter}
  49. }
  50. }
  51. // Return a Number with a UNSIGNED limiter up to 256 bits
  52. func Uint256(n int64) *Number {
  53. return &Number{big.NewInt(n), limitUnsigned256}
  54. }
  55. // Return a Number with a SIGNED limiter up to 256 bits
  56. func Int256(n int64) *Number {
  57. return &Number{big.NewInt(n), limitSigned256}
  58. }
  59. // Returns a Number with a SIGNED unlimited size
  60. func Big(n int64) *Number {
  61. return &Number{big.NewInt(n), func(x *Number) *Number { return x }}
  62. }
  63. // Sets i to sum of x+y
  64. func (i *Number) Add(x, y *Number) *Number {
  65. i.num.Add(x.num, y.num)
  66. return i.limit(i)
  67. }
  68. // Sets i to difference of x-y
  69. func (i *Number) Sub(x, y *Number) *Number {
  70. i.num.Sub(x.num, y.num)
  71. return i.limit(i)
  72. }
  73. // Sets i to product of x*y
  74. func (i *Number) Mul(x, y *Number) *Number {
  75. i.num.Mul(x.num, y.num)
  76. return i.limit(i)
  77. }
  78. // Sets i to the quotient prodject of x/y
  79. func (i *Number) Div(x, y *Number) *Number {
  80. i.num.Div(x.num, y.num)
  81. return i.limit(i)
  82. }
  83. // Sets i to x % y
  84. func (i *Number) Mod(x, y *Number) *Number {
  85. i.num.Mod(x.num, y.num)
  86. return i.limit(i)
  87. }
  88. // Sets i to x << s
  89. func (i *Number) Lsh(x *Number, s uint) *Number {
  90. i.num.Lsh(x.num, s)
  91. return i.limit(i)
  92. }
  93. // Sets i to x^y
  94. func (i *Number) Pow(x, y *Number) *Number {
  95. i.num.Exp(x.num, y.num, big.NewInt(0))
  96. return i.limit(i)
  97. }
  98. // Setters
  99. // Set x to i
  100. func (i *Number) Set(x *Number) *Number {
  101. i.num.Set(x.num)
  102. return i.limit(i)
  103. }
  104. // Set x bytes to i
  105. func (i *Number) SetBytes(x []byte) *Number {
  106. i.num.SetBytes(x)
  107. return i.limit(i)
  108. }
  109. // Cmp compares x and y and returns:
  110. //
  111. // -1 if x < y
  112. // 0 if x == y
  113. // +1 if x > y
  114. func (i *Number) Cmp(x *Number) int {
  115. return i.num.Cmp(x.num)
  116. }
  117. // Getters
  118. // Returns the string representation of i
  119. func (i *Number) String() string {
  120. return i.num.String()
  121. }
  122. // Returns the byte representation of i
  123. func (i *Number) Bytes() []byte {
  124. return i.num.Bytes()
  125. }
  126. // Uint64 returns the Uint64 representation of x. If x cannot be represented in an int64, the result is undefined.
  127. func (i *Number) Uint64() uint64 {
  128. return i.num.Uint64()
  129. }
  130. // Int64 returns the int64 representation of x. If x cannot be represented in an int64, the result is undefined.
  131. func (i *Number) Int64() int64 {
  132. return i.num.Int64()
  133. }
  134. // Returns the signed version of i
  135. func (i *Number) Int256() *Number {
  136. return Int(0).Set(i)
  137. }
  138. // Returns the unsigned version of i
  139. func (i *Number) Uint256() *Number {
  140. return Uint(0).Set(i)
  141. }
  142. // Returns the index of the first bit that's set to 1
  143. func (i *Number) FirstBitSet() int {
  144. for j := 0; j < i.num.BitLen(); j++ {
  145. if i.num.Bit(j) > 0 {
  146. return j
  147. }
  148. }
  149. return i.num.BitLen()
  150. }
  151. // Variables
  152. var (
  153. Zero = Uint(0)
  154. One = Uint(1)
  155. Two = Uint(2)
  156. MaxUint256 = Uint(0).SetBytes(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))
  157. MinOne = Int(-1)
  158. // "typedefs"
  159. Uint = Uint256
  160. Int = Int256
  161. )