big.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package common
  2. import "math/big"
  3. // Common big integers often used
  4. var (
  5. Big1 = big.NewInt(1)
  6. Big2 = big.NewInt(2)
  7. Big3 = big.NewInt(3)
  8. Big0 = big.NewInt(0)
  9. BigTrue = Big1
  10. BigFalse = Big0
  11. Big32 = big.NewInt(32)
  12. Big256 = big.NewInt(0xff)
  13. Big257 = big.NewInt(257)
  14. MaxBig = String2Big("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
  15. )
  16. // Big pow
  17. //
  18. // Returns the power of two big integers
  19. func BigPow(a, b int) *big.Int {
  20. c := new(big.Int)
  21. c.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), big.NewInt(0))
  22. return c
  23. }
  24. // Big
  25. //
  26. // Shortcut for new(big.Int).SetString(..., 0)
  27. func Big(num string) *big.Int {
  28. n := new(big.Int)
  29. n.SetString(num, 0)
  30. return n
  31. }
  32. // Bytes2Big
  33. //
  34. func BytesToBig(data []byte) *big.Int {
  35. n := new(big.Int)
  36. n.SetBytes(data)
  37. return n
  38. }
  39. func Bytes2Big(data []byte) *big.Int { return BytesToBig(data) }
  40. func BigD(data []byte) *big.Int { return BytesToBig(data) }
  41. func String2Big(num string) *big.Int {
  42. n := new(big.Int)
  43. n.SetString(num, 0)
  44. return n
  45. }
  46. func BitTest(num *big.Int, i int) bool {
  47. return num.Bit(i) > 0
  48. }
  49. // To256
  50. //
  51. // "cast" the big int to a 256 big int (i.e., limit to)
  52. var tt256 = new(big.Int).Lsh(big.NewInt(1), 256)
  53. var tt256m1 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
  54. var tt255 = new(big.Int).Lsh(big.NewInt(1), 255)
  55. func U256(x *big.Int) *big.Int {
  56. //if x.Cmp(Big0) < 0 {
  57. // return new(big.Int).Add(tt256, x)
  58. // }
  59. x.And(x, tt256m1)
  60. return x
  61. }
  62. func S256(x *big.Int) *big.Int {
  63. if x.Cmp(tt255) < 0 {
  64. return x
  65. } else {
  66. // We don't want to modify x, ever
  67. return new(big.Int).Sub(x, tt256)
  68. }
  69. }
  70. func FirstBitSet(v *big.Int) int {
  71. for i := 0; i < v.BitLen(); i++ {
  72. if v.Bit(i) > 0 {
  73. return i
  74. }
  75. }
  76. return v.BitLen()
  77. }
  78. // Big to bytes
  79. //
  80. // Returns the bytes of a big integer with the size specified by **base**
  81. // Attempts to pad the byte array with zeros.
  82. func BigToBytes(num *big.Int, base int) []byte {
  83. ret := make([]byte, base/8)
  84. if len(num.Bytes()) > base/8 {
  85. return num.Bytes()
  86. }
  87. return append(ret[:len(ret)-len(num.Bytes())], num.Bytes()...)
  88. }
  89. // Big copy
  90. //
  91. // Creates a copy of the given big integer
  92. func BigCopy(src *big.Int) *big.Int {
  93. return new(big.Int).Set(src)
  94. }
  95. // Big max
  96. //
  97. // Returns the maximum size big integer
  98. func BigMax(x, y *big.Int) *big.Int {
  99. if x.Cmp(y) < 0 {
  100. return y
  101. }
  102. return x
  103. }
  104. // Big min
  105. //
  106. // Returns the minimum size big integer
  107. func BigMin(x, y *big.Int) *big.Int {
  108. if x.Cmp(y) > 0 {
  109. return y
  110. }
  111. return x
  112. }