int.go 3.9 KB

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