int.go 4.8 KB

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