bigint.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package curve
  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 Add(a, b *big.Int) *big.Int {
  25. return new(big.Int).Add(a, b)
  26. }
  27. func Add64(a *big.Int, b int64) *big.Int {
  28. return Add(a, big.NewInt(b))
  29. }
  30. func Mod(a, b *big.Int) *big.Int {
  31. return new(big.Int).Mod(a, b)
  32. }
  33. func Mod64(a *big.Int, b int64) *big.Int {
  34. return Mod(a, big.NewInt(b))
  35. }
  36. func Neg(a *big.Int) *big.Int {
  37. return new(big.Int).Neg(a)
  38. }
  39. func Sub(a, b *big.Int) *big.Int {
  40. return new(big.Int).Sub(a, b)
  41. }
  42. func Sub64(a *big.Int, b int64) *big.Int {
  43. return Sub(a, big.NewInt(b))
  44. }
  45. func Cmp(a, b *big.Int) bool {
  46. return a.Cmp(b) == 0
  47. }
  48. func Div(a, b *big.Int) *big.Int {
  49. return new(big.Int).Div(a, b)
  50. }
  51. func Div64(a *big.Int, b int64) *big.Int {
  52. return Div(a, big.NewInt(b))
  53. }
  54. func Rem(a, b *big.Int) *big.Int {
  55. return new(big.Int).Rem(a, b)
  56. }
  57. func Split(x *big.Int, parts int, firstOffset, lastOffset *big.Int) [][]*big.Int {
  58. res := make([][]*big.Int, parts)
  59. div := Div64(x, int64(parts))
  60. var last []*big.Int
  61. for i := 0; i < parts; i++ {
  62. res[i] = []*big.Int{
  63. Mul(big.NewInt(int64(i)), div),
  64. Sub64(Add(Mul(big.NewInt(int64(i)), div), div), 1),
  65. }
  66. last = res[i]
  67. }
  68. last[1] = Add(last[1], Add64(Mod64(x, int64(parts)), 1))
  69. res[0][0] = Add(res[0][0], firstOffset)
  70. last[1] = Sub(last[1], lastOffset)
  71. return res
  72. }