integer.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package integer
  2. /*
  3. RSAInt: uses big ints to handle big numbers,
  4. and adapts usage to a more fuctional level
  5. against encryption-keys
  6. */
  7. import (
  8. "fmt"
  9. "time"
  10. "math/big"
  11. "math/rand"
  12. )
  13. type inttype int
  14. const (
  15. ANY inttype = 1 << iota
  16. ODD inttype = 1 << iota
  17. EVEN inttype = 1 << iota
  18. )
  19. var (
  20. ZERO = New(0).setProtected(true)
  21. ONE = New(1).setProtected(true)
  22. TWO= New(2).setProtected(true)
  23. THREE = New(3).setProtected(true)
  24. )
  25. type RSAInt struct {
  26. *big.Int
  27. p bool
  28. }
  29. func randInt() *rand.Rand {
  30. return rand.New(rand.NewSource(time.Now().UnixNano()))
  31. }
  32. func New(i int64) *RSAInt {
  33. k := new(RSAInt)
  34. k.Int = big.NewInt(i)
  35. return k.setProtected(false)
  36. }
  37. func (k *RSAInt) Copy() *RSAInt {
  38. return New(0).Set(k).setProtected(k.p)
  39. }
  40. func (k *RSAInt) IsProtected() bool {
  41. return k.p
  42. }
  43. func (k *RSAInt) isProtected() bool {
  44. if k.IsProtected() {
  45. fmt.Printf("Using protected value %p: %s\n", k, k.Text(10))
  46. return true
  47. }
  48. return false
  49. }
  50. func (k *RSAInt) setProtected(p bool) *RSAInt {
  51. k.p = p
  52. return k
  53. }
  54. func (k *RSAInt) Set(o *RSAInt) *RSAInt {
  55. k.isProtected()
  56. k.Int.Set(o.Int)
  57. return k
  58. }
  59. func (k *RSAInt) Set64(i int64) *RSAInt {
  60. k.isProtected()
  61. k.Int.SetInt64(i)
  62. return k
  63. }
  64. func (k *RSAInt) SetString(s string) (*RSAInt, bool) {
  65. k.isProtected()
  66. _, e := k.Int.SetString(s, 10)
  67. return k, e
  68. }
  69. func (k *RSAInt) Next(s int64) *RSAInt {
  70. k.isProtected()
  71. return k.Add(New(s))
  72. }
  73. func (k *RSAInt) Add(b *RSAInt) *RSAInt {
  74. k.isProtected()
  75. k.Int.Add(k.Int, b.Int)
  76. return k
  77. }
  78. func (k *RSAInt) Sub(b *RSAInt) *RSAInt {
  79. k.isProtected()
  80. k.Int.Sub(k.Int, b.Int)
  81. return k
  82. }
  83. func (k *RSAInt) Mul(b *RSAInt) *RSAInt {
  84. k.isProtected()
  85. k.Int.Mul(k.Int, b.Int)
  86. return k
  87. }
  88. func (k *RSAInt) Div(b *RSAInt) *RSAInt {
  89. k.isProtected()
  90. k.Int.Div(k.Int, b.Int)
  91. return k
  92. }
  93. func (k *RSAInt) Mod(b *RSAInt) *RSAInt {
  94. k.isProtected()
  95. k.Int.Mod(k.Int, b.Int)
  96. return k
  97. }
  98. func (k *RSAInt) Pow(e *RSAInt) *RSAInt {
  99. k.isProtected()
  100. k.Exp(k.Int, e.Int, nil)
  101. return k
  102. }
  103. func (k *RSAInt) Pow64(e int64) *RSAInt {
  104. k.isProtected()
  105. k.Exp(k.Int, New(e).Int, nil)
  106. return k
  107. }
  108. func (k *RSAInt) PowMod(e, m *RSAInt) *RSAInt {
  109. k.isProtected()
  110. k.Exp(k.Int, e.Int, m.Int)
  111. return k
  112. }
  113. func (k *RSAInt) GCD(b *RSAInt) *RSAInt {
  114. k.isProtected()
  115. k.Int.GCD(nil, nil, k.Int, b.Int)
  116. return k
  117. }
  118. func (k *RSAInt) Rand() *RSAInt {
  119. k.isProtected()
  120. k.Int.Rand(randInt(), k.Int)
  121. return k
  122. }
  123. func (k *RSAInt) Eq(ot *RSAInt) bool {
  124. return k.Cmp(ot.Int) == 0
  125. }
  126. func (k *RSAInt) Neq(ot *RSAInt) bool {
  127. return !k.Eq(ot)
  128. }
  129. func (k *RSAInt) Lt(ot *RSAInt) bool {
  130. return k.Cmp(ot.Int) < 0
  131. }
  132. func (k *RSAInt) LtEq(ot *RSAInt) bool {
  133. return k.Cmp(ot.Int) <= 0
  134. }
  135. func (k *RSAInt) Gt(ot *RSAInt) bool {
  136. return !k.LtEq(ot)
  137. }
  138. func (k *RSAInt) GtEq(ot *RSAInt) bool {
  139. return !k.Lt(ot)
  140. }
  141. func (k *RSAInt) Or(ot *RSAInt) *RSAInt {
  142. k.Int.Or(k.Int, ot.Int)
  143. return k
  144. }
  145. func (k *RSAInt) Len() int {
  146. return len(k.String())
  147. }
  148. func (k *RSAInt) IsPrime() bool {
  149. return k.ProbablyPrime(100)
  150. }
  151. func (k *RSAInt) isPrime(p chan *RSAInt) {
  152. if k.IsPrime() {
  153. p <- k
  154. }
  155. }
  156. func (k *RSAInt) RandRange(r inttype) *RSAInt {
  157. s, e := "", New(0)
  158. for l, i := k.Len(), 0; i < l; i++ {
  159. s += "0"
  160. }
  161. if _, r := e.SetString(s); !r {
  162. return nil
  163. }
  164. e.Sub(k).Rand().Add(k)
  165. if c := e.Copy().Mod(TWO); r == EVEN && c.Neq(ZERO) {
  166. e.Add(ONE)
  167. } else if r == ODD && c.Eq(ZERO) {
  168. e.Add(ONE)
  169. }
  170. return e
  171. }
  172. func (k *RSAInt) NextRandPrime() {
  173. p := make(chan *RSAInt)
  174. var r *RSAInt
  175. for run, e := true, k.RandRange(ODD); run; e.Next(2) {
  176. select {
  177. case r = <-p:
  178. run = false
  179. *k = *r
  180. default:
  181. go e.Copy().isPrime(p)
  182. }
  183. }
  184. }
  185. func (k *RSAInt) PollardPm1(base int64) (*RSAInt, *RSAInt, *RSAInt) {
  186. n, b, q, p := New(0), New(base), New(0), New(base).GCD(k)
  187. for f, i := New(1), New(1); p.Eq(ONE) && n.Lt(k); n.Next(1) {
  188. f.Mul(i)
  189. p = b.Copy().PowMod(f, k).Sub(ONE).GCD(k)
  190. i.Next(1)
  191. }
  192. q.Set(k).Div(p)
  193. fmt.Printf(
  194. "POLLARD: n: %s\tb: %s\tp: %s\tq: %s\n",
  195. n.Set(p).Mul(q), b.Text(10), p.Text(10), q.Text(10),
  196. )
  197. return n, p, q
  198. }
  199. func (k *RSAInt) EulerTot(p, q *RSAInt) *RSAInt {
  200. return k.Add(New(1)).Sub(p.Copy().Add(q))
  201. }
  202. func (k *RSAInt) RelativePrime() *RSAInt {
  203. for x := New(65537); x.Lt(k); x.Next(1) {
  204. if x.Copy().GCD(k).Eq(ONE) {
  205. return k.Set(x)
  206. }
  207. }
  208. return nil
  209. }
  210. func (k *RSAInt) ModMulInv(e, etot *RSAInt) *RSAInt {
  211. r := etot.Copy()
  212. nr := e.Copy().Mod(etot)
  213. for t, nt := New(0), New(1); nr.Neq(ZERO); {
  214. q := r.Copy().Div(nr).Or(ZERO)
  215. nt, t = t.Sub(q.Copy().Mul(nt)), nt
  216. nr, r = r.Sub(q.Copy().Mul(nr)), nr
  217. k.Set(t)
  218. }
  219. if r.Gt(ONE) {
  220. k.SetInt64(-1)
  221. } else if k.Lt(ZERO) {
  222. k.Add(etot)
  223. }
  224. return k
  225. }