gmp.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. An example of wrapping a C library in Go. This is the GNU
  6. multiprecision library gmp's integer type mpz_t wrapped to look like
  7. the Go package big's integer type Int.
  8. This is a syntactically valid Go program—it can be parsed with the Go
  9. parser and processed by godoc—but it is not compiled directly by gc.
  10. Instead, a separate tool, cgo, processes it to produce three output
  11. files. The first two, 6g.go and 6c.c, are a Go source file for 6g and
  12. a C source file for 6c; both compile as part of the named package
  13. (gmp, in this example). The third, gcc.c, is a C source file for gcc;
  14. it compiles into a shared object (.so) that is dynamically linked into
  15. any 6.out that imports the first two files.
  16. The stanza
  17. // #include <gmp.h>
  18. import "C"
  19. is a signal to cgo. The doc comment on the import of "C" provides
  20. additional context for the C file. Here it is just a single #include
  21. but it could contain arbitrary C definitions to be imported and used.
  22. Cgo recognizes any use of a qualified identifier C.xxx and uses gcc to
  23. find the definition of xxx. If xxx is a type, cgo replaces C.xxx with
  24. a Go translation. C arithmetic types translate to precisely-sized Go
  25. arithmetic types. A C struct translates to a Go struct, field by
  26. field; unrepresentable fields are replaced with opaque byte arrays. A
  27. C union translates into a struct containing the first union member and
  28. perhaps additional padding. C arrays become Go arrays. C pointers
  29. become Go pointers. C function pointers become Go's uintptr.
  30. C void pointers become Go's unsafe.Pointer.
  31. For example, mpz_t is defined in <gmp.h> as:
  32. typedef unsigned long int mp_limb_t;
  33. typedef struct
  34. {
  35. int _mp_alloc;
  36. int _mp_size;
  37. mp_limb_t *_mp_d;
  38. } __mpz_struct;
  39. typedef __mpz_struct mpz_t[1];
  40. Cgo generates:
  41. type _C_int int32
  42. type _C_mp_limb_t uint64
  43. type _C___mpz_struct struct {
  44. _mp_alloc _C_int;
  45. _mp_size _C_int;
  46. _mp_d *_C_mp_limb_t;
  47. }
  48. type _C_mpz_t [1]_C___mpz_struct
  49. and then replaces each occurrence of a type C.xxx with _C_xxx.
  50. If xxx is data, cgo arranges for C.xxx to refer to the C variable,
  51. with the type translated as described above. To do this, cgo must
  52. introduce a Go variable that points at the C variable (the linker can
  53. be told to initialize this pointer). For example, if the gmp library
  54. provided
  55. mpz_t zero;
  56. then cgo would rewrite a reference to C.zero by introducing
  57. var _C_zero *C.mpz_t
  58. and then replacing all instances of C.zero with (*_C_zero).
  59. Cgo's most interesting translation is for functions. If xxx is a C
  60. function, then cgo rewrites C.xxx into a new function _C_xxx that
  61. calls the C xxx in a standard pthread. The new function translates
  62. its arguments, calls xxx, and translates the return value.
  63. Translation of parameters and the return value follows the type
  64. translation above except that arrays passed as parameters translate
  65. explicitly in Go to pointers to arrays, as they do (implicitly) in C.
  66. Garbage collection is the big problem. It is fine for the Go world to
  67. have pointers into the C world and to free those pointers when they
  68. are no longer needed. To help, the Go code can define Go objects
  69. holding the C pointers and use runtime.SetFinalizer on those Go objects.
  70. It is much more difficult for the C world to have pointers into the Go
  71. world, because the Go garbage collector is unaware of the memory
  72. allocated by C. The most important consideration is not to
  73. constrain future implementations, so the rule is that Go code can
  74. hand a Go pointer to C code but must separately arrange for
  75. Go to hang on to a reference to the pointer until C is done with it.
  76. */
  77. package gmp
  78. /*
  79. #cgo LDFLAGS: -lgmp
  80. #include <gmp.h>
  81. #include <stdlib.h>
  82. // gmp 5.0.0+ changed the type of the 3rd argument to mp_bitcnt_t,
  83. // so, to support older versions, we wrap these two functions.
  84. void _mpz_mul_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) {
  85. mpz_mul_2exp(a, b, n);
  86. }
  87. void _mpz_div_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) {
  88. mpz_div_2exp(a, b, n);
  89. }
  90. */
  91. import "C"
  92. import (
  93. "os"
  94. "unsafe"
  95. )
  96. /*
  97. * one of a kind
  98. */
  99. // An Int represents a signed multi-precision integer.
  100. // The zero value for an Int represents the value 0.
  101. type Int struct {
  102. i C.mpz_t
  103. init bool
  104. }
  105. // NewInt returns a new Int initialized to x.
  106. func NewInt(x int64) *Int { return new(Int).SetInt64(x) }
  107. // Int promises that the zero value is a 0, but in gmp
  108. // the zero value is a crash. To bridge the gap, the
  109. // init bool says whether this is a valid gmp value.
  110. // doinit initializes z.i if it needs it. This is not inherent
  111. // to FFI, just a mismatch between Go's convention of
  112. // making zero values useful and gmp's decision not to.
  113. func (z *Int) doinit() {
  114. if z.init {
  115. return
  116. }
  117. z.init = true
  118. C.mpz_init(&z.i[0])
  119. }
  120. // Bytes returns z's representation as a big-endian byte array.
  121. func (z *Int) Bytes() []byte {
  122. b := make([]byte, (z.Len()+7)/8)
  123. n := C.size_t(len(b))
  124. C.mpz_export(unsafe.Pointer(&b[0]), &n, 1, 1, 1, 0, &z.i[0])
  125. return b[0:n]
  126. }
  127. // Len returns the length of z in bits. 0 is considered to have length 1.
  128. func (z *Int) Len() int {
  129. z.doinit()
  130. return int(C.mpz_sizeinbase(&z.i[0], 2))
  131. }
  132. // Set sets z = x and returns z.
  133. func (z *Int) Set(x *Int) *Int {
  134. z.doinit()
  135. C.mpz_set(&z.i[0], &x.i[0])
  136. return z
  137. }
  138. // SetBytes interprets b as the bytes of a big-endian integer
  139. // and sets z to that value.
  140. func (z *Int) SetBytes(b []byte) *Int {
  141. z.doinit()
  142. if len(b) == 0 {
  143. z.SetInt64(0)
  144. } else {
  145. C.mpz_import(&z.i[0], C.size_t(len(b)), 1, 1, 1, 0, unsafe.Pointer(&b[0]))
  146. }
  147. return z
  148. }
  149. // SetInt64 sets z = x and returns z.
  150. func (z *Int) SetInt64(x int64) *Int {
  151. z.doinit()
  152. // TODO(rsc): more work on 32-bit platforms
  153. C.mpz_set_si(&z.i[0], C.long(x))
  154. return z
  155. }
  156. // SetString interprets s as a number in the given base
  157. // and sets z to that value. The base must be in the range [2,36].
  158. // SetString returns an error if s cannot be parsed or the base is invalid.
  159. func (z *Int) SetString(s string, base int) error {
  160. z.doinit()
  161. if base < 2 || base > 36 {
  162. return os.ErrInvalid
  163. }
  164. p := C.CString(s)
  165. defer C.free(unsafe.Pointer(p))
  166. if C.mpz_set_str(&z.i[0], p, C.int(base)) < 0 {
  167. return os.ErrInvalid
  168. }
  169. return nil
  170. }
  171. // String returns the decimal representation of z.
  172. func (z *Int) String() string {
  173. if z == nil {
  174. return "nil"
  175. }
  176. z.doinit()
  177. p := C.mpz_get_str(nil, 10, &z.i[0])
  178. s := C.GoString(p)
  179. C.free(unsafe.Pointer(p))
  180. return s
  181. }
  182. func (z *Int) destroy() {
  183. if z.init {
  184. C.mpz_clear(&z.i[0])
  185. }
  186. z.init = false
  187. }
  188. /*
  189. * arithmetic
  190. */
  191. // Add sets z = x + y and returns z.
  192. func (z *Int) Add(x, y *Int) *Int {
  193. x.doinit()
  194. y.doinit()
  195. z.doinit()
  196. C.mpz_add(&z.i[0], &x.i[0], &y.i[0])
  197. return z
  198. }
  199. // Sub sets z = x - y and returns z.
  200. func (z *Int) Sub(x, y *Int) *Int {
  201. x.doinit()
  202. y.doinit()
  203. z.doinit()
  204. C.mpz_sub(&z.i[0], &x.i[0], &y.i[0])
  205. return z
  206. }
  207. // Mul sets z = x * y and returns z.
  208. func (z *Int) Mul(x, y *Int) *Int {
  209. x.doinit()
  210. y.doinit()
  211. z.doinit()
  212. C.mpz_mul(&z.i[0], &x.i[0], &y.i[0])
  213. return z
  214. }
  215. // Div sets z = x / y, rounding toward zero, and returns z.
  216. func (z *Int) Div(x, y *Int) *Int {
  217. x.doinit()
  218. y.doinit()
  219. z.doinit()
  220. C.mpz_tdiv_q(&z.i[0], &x.i[0], &y.i[0])
  221. return z
  222. }
  223. // Mod sets z = x % y and returns z.
  224. // Like the result of the Go % operator, z has the same sign as x.
  225. func (z *Int) Mod(x, y *Int) *Int {
  226. x.doinit()
  227. y.doinit()
  228. z.doinit()
  229. C.mpz_tdiv_r(&z.i[0], &x.i[0], &y.i[0])
  230. return z
  231. }
  232. // Lsh sets z = x << s and returns z.
  233. func (z *Int) Lsh(x *Int, s uint) *Int {
  234. x.doinit()
  235. z.doinit()
  236. C._mpz_mul_2exp(&z.i[0], &x.i[0], C.ulong(s))
  237. return z
  238. }
  239. // Rsh sets z = x >> s and returns z.
  240. func (z *Int) Rsh(x *Int, s uint) *Int {
  241. x.doinit()
  242. z.doinit()
  243. C._mpz_div_2exp(&z.i[0], &x.i[0], C.ulong(s))
  244. return z
  245. }
  246. // Exp sets z = x^y % m and returns z.
  247. // If m == nil, Exp sets z = x^y.
  248. func (z *Int) Exp(x, y, m *Int) *Int {
  249. m.doinit()
  250. x.doinit()
  251. y.doinit()
  252. z.doinit()
  253. if m == nil {
  254. C.mpz_pow_ui(&z.i[0], &x.i[0], C.mpz_get_ui(&y.i[0]))
  255. } else {
  256. C.mpz_powm(&z.i[0], &x.i[0], &y.i[0], &m.i[0])
  257. }
  258. return z
  259. }
  260. func (z *Int) Int64() int64 {
  261. if !z.init {
  262. return 0
  263. }
  264. return int64(C.mpz_get_si(&z.i[0]))
  265. }
  266. // Neg sets z = -x and returns z.
  267. func (z *Int) Neg(x *Int) *Int {
  268. x.doinit()
  269. z.doinit()
  270. C.mpz_neg(&z.i[0], &x.i[0])
  271. return z
  272. }
  273. // Abs sets z to the absolute value of x and returns z.
  274. func (z *Int) Abs(x *Int) *Int {
  275. x.doinit()
  276. z.doinit()
  277. C.mpz_abs(&z.i[0], &x.i[0])
  278. return z
  279. }
  280. /*
  281. * functions without a clear receiver
  282. */
  283. // CmpInt compares x and y. The result is
  284. //
  285. // -1 if x < y
  286. // 0 if x == y
  287. // +1 if x > y
  288. //
  289. func CmpInt(x, y *Int) int {
  290. x.doinit()
  291. y.doinit()
  292. switch cmp := C.mpz_cmp(&x.i[0], &y.i[0]); {
  293. case cmp < 0:
  294. return -1
  295. case cmp == 0:
  296. return 0
  297. }
  298. return +1
  299. }
  300. // DivModInt sets q = x / y and r = x % y.
  301. func DivModInt(q, r, x, y *Int) {
  302. q.doinit()
  303. r.doinit()
  304. x.doinit()
  305. y.doinit()
  306. C.mpz_tdiv_qr(&q.i[0], &r.i[0], &x.i[0], &y.i[0])
  307. }
  308. // GcdInt sets d to the greatest common divisor of a and b,
  309. // which must be positive numbers.
  310. // If x and y are not nil, GcdInt sets x and y such that d = a*x + b*y.
  311. // If either a or b is not positive, GcdInt sets d = x = y = 0.
  312. func GcdInt(d, x, y, a, b *Int) {
  313. d.doinit()
  314. x.doinit()
  315. y.doinit()
  316. a.doinit()
  317. b.doinit()
  318. C.mpz_gcdext(&d.i[0], &x.i[0], &y.i[0], &a.i[0], &b.i[0])
  319. }
  320. // ProbablyPrime performs n Miller-Rabin tests to check whether z is prime.
  321. // If it returns true, z is prime with probability 1 - 1/4^n.
  322. // If it returns false, z is not prime.
  323. func (z *Int) ProbablyPrime(n int) bool {
  324. z.doinit()
  325. return int(C.mpz_probab_prime_p(&z.i[0], C.int(n))) > 0
  326. }