bigint.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package tools
  2. import (
  3. "math/big"
  4. )
  5. var (
  6. valMin = big.NewInt(1187)
  7. //valMin = big.NewInt(1)
  8. maxTest = big.NewInt(111187000)
  9. //maxTest = big.NewInt(87)
  10. max = Exp64(big.NewInt(2), 63)
  11. )
  12. func And(a, b *big.Int) *big.Int {
  13. return new(big.Int).And(a, b)
  14. }
  15. func Exp(a, b, mod *big.Int) *big.Int {
  16. return new(big.Int).Exp(a, b, mod)
  17. }
  18. func Exp64(a *big.Int, b int64) *big.Int {
  19. return new(big.Int).Exp(a, big.NewInt(b), nil)
  20. }
  21. func Mul(a, b *big.Int) *big.Int {
  22. return new(big.Int).Mul(a, b)
  23. }
  24. func Mul64(a *big.Int, b int64) *big.Int {
  25. return Mul(a, big.NewInt(b))
  26. }
  27. func Add(a, b *big.Int) *big.Int {
  28. return new(big.Int).Add(a, b)
  29. }
  30. func Add64(a *big.Int, b int64) *big.Int {
  31. return Add(a, big.NewInt(b))
  32. }
  33. func Mod(a, b *big.Int) *big.Int {
  34. return new(big.Int).Mod(a, b)
  35. }
  36. func Mod64(a *big.Int, b int64) *big.Int {
  37. return Mod(a, big.NewInt(b))
  38. }
  39. func Neg(a *big.Int) *big.Int {
  40. return new(big.Int).Neg(a)
  41. }
  42. func Sub(a, b *big.Int) *big.Int {
  43. return new(big.Int).Sub(a, b)
  44. }
  45. func Sub64(a *big.Int, b int64) *big.Int {
  46. return Sub(a, big.NewInt(b))
  47. }
  48. func Cmp(a, b *big.Int) bool {
  49. return a.Cmp(b) == 0
  50. }
  51. func Div(a, b *big.Int) *big.Int {
  52. return new(big.Int).Div(a, b)
  53. }
  54. func Div64(a *big.Int, b int64) *big.Int {
  55. return Div(a, big.NewInt(b))
  56. }
  57. func Rem(a, b *big.Int) *big.Int {
  58. return new(big.Int).Rem(a, b)
  59. }
  60. func Split(x *big.Int, parts int, firstOffset, lastOffset *big.Int) [][]*big.Int {
  61. res := make([][]*big.Int, parts)
  62. div := Div64(x, int64(parts))
  63. var last []*big.Int
  64. for i := 0; i < parts; i++ {
  65. res[i] = []*big.Int{
  66. Mul(big.NewInt(int64(i)), div),
  67. Sub64(Add(Mul(big.NewInt(int64(i)), div), div), 1),
  68. }
  69. last = res[i]
  70. }
  71. last[1] = Add(last[1], Add64(Mod64(x, int64(parts)), 1))
  72. res[0][0] = Add(res[0][0], firstOffset)
  73. last[1] = Sub(last[1], lastOffset)
  74. return res
  75. }
  76. func Bytes32(x *big.Int) []byte {
  77. xb := x.Bytes()
  78. ball := make([]byte, 32-len(xb))
  79. return append(ball, xb...)
  80. }