2009-09-16 19:14:18 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
/*
|
|
|
|
An example of wrapping a C library in Go. This is the GNU
|
|
|
|
multiprecision library gmp's integer type mpz_t wrapped to look like
|
|
|
|
the Go package big's integer type Int.
|
|
|
|
|
|
|
|
This is a syntactically valid Go program—it can be parsed with the Go
|
2015-06-23 17:50:12 -06:00
|
|
|
parser and processed by godoc—but it is not compiled directly by gc.
|
2009-09-16 19:14:18 -06:00
|
|
|
Instead, a separate tool, cgo, processes it to produce three output
|
|
|
|
files. The first two, 6g.go and 6c.c, are a Go source file for 6g and
|
|
|
|
a C source file for 6c; both compile as part of the named package
|
|
|
|
(gmp, in this example). The third, gcc.c, is a C source file for gcc;
|
|
|
|
it compiles into a shared object (.so) that is dynamically linked into
|
|
|
|
any 6.out that imports the first two files.
|
|
|
|
|
|
|
|
The stanza
|
|
|
|
|
|
|
|
// #include <gmp.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
is a signal to cgo. The doc comment on the import of "C" provides
|
|
|
|
additional context for the C file. Here it is just a single #include
|
|
|
|
but it could contain arbitrary C definitions to be imported and used.
|
|
|
|
|
|
|
|
Cgo recognizes any use of a qualified identifier C.xxx and uses gcc to
|
|
|
|
find the definition of xxx. If xxx is a type, cgo replaces C.xxx with
|
|
|
|
a Go translation. C arithmetic types translate to precisely-sized Go
|
|
|
|
arithmetic types. A C struct translates to a Go struct, field by
|
|
|
|
field; unrepresentable fields are replaced with opaque byte arrays. A
|
|
|
|
C union translates into a struct containing the first union member and
|
|
|
|
perhaps additional padding. C arrays become Go arrays. C pointers
|
2009-09-30 12:51:08 -06:00
|
|
|
become Go pointers. C function pointers become Go's uintptr.
|
2013-03-30 05:02:14 -06:00
|
|
|
C void pointers become Go's unsafe.Pointer.
|
2009-09-16 19:14:18 -06:00
|
|
|
|
|
|
|
For example, mpz_t is defined in <gmp.h> as:
|
|
|
|
|
|
|
|
typedef unsigned long int mp_limb_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int _mp_alloc;
|
|
|
|
int _mp_size;
|
|
|
|
mp_limb_t *_mp_d;
|
|
|
|
} __mpz_struct;
|
|
|
|
|
|
|
|
typedef __mpz_struct mpz_t[1];
|
|
|
|
|
|
|
|
Cgo generates:
|
|
|
|
|
|
|
|
type _C_int int32
|
|
|
|
type _C_mp_limb_t uint64
|
|
|
|
type _C___mpz_struct struct {
|
|
|
|
_mp_alloc _C_int;
|
|
|
|
_mp_size _C_int;
|
|
|
|
_mp_d *_C_mp_limb_t;
|
|
|
|
}
|
|
|
|
type _C_mpz_t [1]_C___mpz_struct
|
|
|
|
|
|
|
|
and then replaces each occurrence of a type C.xxx with _C_xxx.
|
|
|
|
|
|
|
|
If xxx is data, cgo arranges for C.xxx to refer to the C variable,
|
|
|
|
with the type translated as described above. To do this, cgo must
|
|
|
|
introduce a Go variable that points at the C variable (the linker can
|
|
|
|
be told to initialize this pointer). For example, if the gmp library
|
|
|
|
provided
|
|
|
|
|
|
|
|
mpz_t zero;
|
|
|
|
|
|
|
|
then cgo would rewrite a reference to C.zero by introducing
|
|
|
|
|
|
|
|
var _C_zero *C.mpz_t
|
|
|
|
|
|
|
|
and then replacing all instances of C.zero with (*_C_zero).
|
|
|
|
|
|
|
|
Cgo's most interesting translation is for functions. If xxx is a C
|
|
|
|
function, then cgo rewrites C.xxx into a new function _C_xxx that
|
|
|
|
calls the C xxx in a standard pthread. The new function translates
|
|
|
|
its arguments, calls xxx, and translates the return value.
|
|
|
|
|
|
|
|
Translation of parameters and the return value follows the type
|
2009-09-30 12:51:08 -06:00
|
|
|
translation above except that arrays passed as parameters translate
|
|
|
|
explicitly in Go to pointers to arrays, as they do (implicitly) in C.
|
2009-09-16 19:14:18 -06:00
|
|
|
|
|
|
|
Garbage collection is the big problem. It is fine for the Go world to
|
|
|
|
have pointers into the C world and to free those pointers when they
|
2011-05-31 12:41:24 -06:00
|
|
|
are no longer needed. To help, the Go code can define Go objects
|
|
|
|
holding the C pointers and use runtime.SetFinalizer on those Go objects.
|
2009-09-16 19:14:18 -06:00
|
|
|
|
|
|
|
It is much more difficult for the C world to have pointers into the Go
|
|
|
|
world, because the Go garbage collector is unaware of the memory
|
2009-09-30 12:51:08 -06:00
|
|
|
allocated by C. The most important consideration is not to
|
|
|
|
constrain future implementations, so the rule is that Go code can
|
|
|
|
hand a Go pointer to C code but must separately arrange for
|
2009-09-16 19:14:18 -06:00
|
|
|
Go to hang on to a reference to the pointer until C is done with it.
|
|
|
|
*/
|
|
|
|
package gmp
|
|
|
|
|
2012-03-20 10:51:48 -06:00
|
|
|
/*
|
|
|
|
#cgo LDFLAGS: -lgmp
|
|
|
|
#include <gmp.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
// gmp 5.0.0+ changed the type of the 3rd argument to mp_bitcnt_t,
|
|
|
|
// so, to support older versions, we wrap these two functions.
|
|
|
|
void _mpz_mul_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) {
|
|
|
|
mpz_mul_2exp(a, b, n);
|
|
|
|
}
|
|
|
|
void _mpz_div_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) {
|
|
|
|
mpz_div_2exp(a, b, n);
|
|
|
|
}
|
|
|
|
*/
|
2009-09-16 19:14:18 -06:00
|
|
|
import "C"
|
|
|
|
|
2009-09-24 12:43:19 -06:00
|
|
|
import (
|
2009-12-15 16:33:31 -07:00
|
|
|
"os"
|
|
|
|
"unsafe"
|
2009-09-24 12:43:19 -06:00
|
|
|
)
|
2009-09-16 19:14:18 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* one of a kind
|
|
|
|
*/
|
|
|
|
|
|
|
|
// An Int represents a signed multi-precision integer.
|
|
|
|
// The zero value for an Int represents the value 0.
|
|
|
|
type Int struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
i C.mpz_t
|
|
|
|
init bool
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInt returns a new Int initialized to x.
|
2009-12-15 16:33:31 -07:00
|
|
|
func NewInt(x int64) *Int { return new(Int).SetInt64(x) }
|
2009-09-16 19:14:18 -06:00
|
|
|
|
|
|
|
// Int promises that the zero value is a 0, but in gmp
|
|
|
|
// the zero value is a crash. To bridge the gap, the
|
|
|
|
// init bool says whether this is a valid gmp value.
|
|
|
|
// doinit initializes z.i if it needs it. This is not inherent
|
|
|
|
// to FFI, just a mismatch between Go's convention of
|
|
|
|
// making zero values useful and gmp's decision not to.
|
|
|
|
func (z *Int) doinit() {
|
|
|
|
if z.init {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
z.init = true
|
|
|
|
C.mpz_init(&z.i[0])
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns z's representation as a big-endian byte array.
|
|
|
|
func (z *Int) Bytes() []byte {
|
2009-12-15 16:33:31 -07:00
|
|
|
b := make([]byte, (z.Len()+7)/8)
|
|
|
|
n := C.size_t(len(b))
|
|
|
|
C.mpz_export(unsafe.Pointer(&b[0]), &n, 1, 1, 1, 0, &z.i[0])
|
|
|
|
return b[0:n]
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the length of z in bits. 0 is considered to have length 1.
|
|
|
|
func (z *Int) Len() int {
|
2009-12-15 16:33:31 -07:00
|
|
|
z.doinit()
|
|
|
|
return int(C.mpz_sizeinbase(&z.i[0], 2))
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets z = x and returns z.
|
|
|
|
func (z *Int) Set(x *Int) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
z.doinit()
|
|
|
|
C.mpz_set(&z.i[0], &x.i[0])
|
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetBytes interprets b as the bytes of a big-endian integer
|
|
|
|
// and sets z to that value.
|
|
|
|
func (z *Int) SetBytes(b []byte) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
z.doinit()
|
2009-09-16 19:14:18 -06:00
|
|
|
if len(b) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
z.SetInt64(0)
|
2009-09-16 19:14:18 -06:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
C.mpz_import(&z.i[0], C.size_t(len(b)), 1, 1, 1, 0, unsafe.Pointer(&b[0]))
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetInt64 sets z = x and returns z.
|
|
|
|
func (z *Int) SetInt64(x int64) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
z.doinit()
|
2009-09-16 19:14:18 -06:00
|
|
|
// TODO(rsc): more work on 32-bit platforms
|
2009-12-15 16:33:31 -07:00
|
|
|
C.mpz_set_si(&z.i[0], C.long(x))
|
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetString interprets s as a number in the given base
|
|
|
|
// and sets z to that value. The base must be in the range [2,36].
|
|
|
|
// SetString returns an error if s cannot be parsed or the base is invalid.
|
2011-11-01 20:06:05 -06:00
|
|
|
func (z *Int) SetString(s string, base int) error {
|
2009-12-15 16:33:31 -07:00
|
|
|
z.doinit()
|
2009-09-16 19:14:18 -06:00
|
|
|
if base < 2 || base > 36 {
|
2012-03-20 10:51:48 -06:00
|
|
|
return os.ErrInvalid
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
p := C.CString(s)
|
|
|
|
defer C.free(unsafe.Pointer(p))
|
2009-09-24 12:43:19 -06:00
|
|
|
if C.mpz_set_str(&z.i[0], p, C.int(base)) < 0 {
|
2012-03-20 10:51:48 -06:00
|
|
|
return os.ErrInvalid
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
2010-05-01 14:10:01 -06:00
|
|
|
return nil
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the decimal representation of z.
|
|
|
|
func (z *Int) String() string {
|
2009-10-14 11:56:19 -06:00
|
|
|
if z == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "nil"
|
2009-10-14 11:56:19 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
z.doinit()
|
|
|
|
p := C.mpz_get_str(nil, 10, &z.i[0])
|
|
|
|
s := C.GoString(p)
|
|
|
|
C.free(unsafe.Pointer(p))
|
|
|
|
return s
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (z *Int) destroy() {
|
|
|
|
if z.init {
|
2009-11-09 13:07:39 -07:00
|
|
|
C.mpz_clear(&z.i[0])
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
z.init = false
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* arithmetic
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Add sets z = x + y and returns z.
|
|
|
|
func (z *Int) Add(x, y *Int) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
x.doinit()
|
|
|
|
y.doinit()
|
|
|
|
z.doinit()
|
|
|
|
C.mpz_add(&z.i[0], &x.i[0], &y.i[0])
|
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sub sets z = x - y and returns z.
|
|
|
|
func (z *Int) Sub(x, y *Int) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
x.doinit()
|
|
|
|
y.doinit()
|
|
|
|
z.doinit()
|
|
|
|
C.mpz_sub(&z.i[0], &x.i[0], &y.i[0])
|
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mul sets z = x * y and returns z.
|
|
|
|
func (z *Int) Mul(x, y *Int) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
x.doinit()
|
|
|
|
y.doinit()
|
|
|
|
z.doinit()
|
|
|
|
C.mpz_mul(&z.i[0], &x.i[0], &y.i[0])
|
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Div sets z = x / y, rounding toward zero, and returns z.
|
|
|
|
func (z *Int) Div(x, y *Int) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
x.doinit()
|
|
|
|
y.doinit()
|
|
|
|
z.doinit()
|
|
|
|
C.mpz_tdiv_q(&z.i[0], &x.i[0], &y.i[0])
|
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mod sets z = x % y and returns z.
|
2009-10-14 11:56:19 -06:00
|
|
|
// Like the result of the Go % operator, z has the same sign as x.
|
2009-09-16 19:14:18 -06:00
|
|
|
func (z *Int) Mod(x, y *Int) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
x.doinit()
|
|
|
|
y.doinit()
|
|
|
|
z.doinit()
|
|
|
|
C.mpz_tdiv_r(&z.i[0], &x.i[0], &y.i[0])
|
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lsh sets z = x << s and returns z.
|
|
|
|
func (z *Int) Lsh(x *Int, s uint) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
x.doinit()
|
|
|
|
z.doinit()
|
2012-03-20 10:51:48 -06:00
|
|
|
C._mpz_mul_2exp(&z.i[0], &x.i[0], C.ulong(s))
|
2009-12-15 16:33:31 -07:00
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rsh sets z = x >> s and returns z.
|
2009-09-24 12:43:19 -06:00
|
|
|
func (z *Int) Rsh(x *Int, s uint) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
x.doinit()
|
|
|
|
z.doinit()
|
2012-03-20 10:51:48 -06:00
|
|
|
C._mpz_div_2exp(&z.i[0], &x.i[0], C.ulong(s))
|
2009-12-15 16:33:31 -07:00
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exp sets z = x^y % m and returns z.
|
|
|
|
// If m == nil, Exp sets z = x^y.
|
|
|
|
func (z *Int) Exp(x, y, m *Int) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
m.doinit()
|
|
|
|
x.doinit()
|
|
|
|
y.doinit()
|
|
|
|
z.doinit()
|
2009-09-16 19:14:18 -06:00
|
|
|
if m == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
C.mpz_pow_ui(&z.i[0], &x.i[0], C.mpz_get_ui(&y.i[0]))
|
2009-09-16 19:14:18 -06:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
C.mpz_powm(&z.i[0], &x.i[0], &y.i[0], &m.i[0])
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
2009-09-24 12:43:19 -06:00
|
|
|
func (z *Int) Int64() int64 {
|
|
|
|
if !z.init {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0
|
2009-09-24 12:43:19 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return int64(C.mpz_get_si(&z.i[0]))
|
2009-09-24 12:43:19 -06:00
|
|
|
}
|
|
|
|
|
2009-09-16 19:14:18 -06:00
|
|
|
// Neg sets z = -x and returns z.
|
|
|
|
func (z *Int) Neg(x *Int) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
x.doinit()
|
|
|
|
z.doinit()
|
|
|
|
C.mpz_neg(&z.i[0], &x.i[0])
|
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Abs sets z to the absolute value of x and returns z.
|
|
|
|
func (z *Int) Abs(x *Int) *Int {
|
2009-12-15 16:33:31 -07:00
|
|
|
x.doinit()
|
|
|
|
z.doinit()
|
|
|
|
C.mpz_abs(&z.i[0], &x.i[0])
|
|
|
|
return z
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* functions without a clear receiver
|
|
|
|
*/
|
|
|
|
|
|
|
|
// CmpInt compares x and y. The result is
|
|
|
|
//
|
|
|
|
// -1 if x < y
|
|
|
|
// 0 if x == y
|
|
|
|
// +1 if x > y
|
|
|
|
//
|
|
|
|
func CmpInt(x, y *Int) int {
|
2009-12-15 16:33:31 -07:00
|
|
|
x.doinit()
|
|
|
|
y.doinit()
|
2009-09-24 12:43:19 -06:00
|
|
|
switch cmp := C.mpz_cmp(&x.i[0], &y.i[0]); {
|
|
|
|
case cmp < 0:
|
2009-11-09 13:07:39 -07:00
|
|
|
return -1
|
2009-09-24 12:43:19 -06:00
|
|
|
case cmp == 0:
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0
|
2009-09-24 12:43:19 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return +1
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// DivModInt sets q = x / y and r = x % y.
|
|
|
|
func DivModInt(q, r, x, y *Int) {
|
2009-12-15 16:33:31 -07:00
|
|
|
q.doinit()
|
|
|
|
r.doinit()
|
|
|
|
x.doinit()
|
|
|
|
y.doinit()
|
|
|
|
C.mpz_tdiv_qr(&q.i[0], &r.i[0], &x.i[0], &y.i[0])
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GcdInt sets d to the greatest common divisor of a and b,
|
|
|
|
// which must be positive numbers.
|
|
|
|
// If x and y are not nil, GcdInt sets x and y such that d = a*x + b*y.
|
|
|
|
// If either a or b is not positive, GcdInt sets d = x = y = 0.
|
|
|
|
func GcdInt(d, x, y, a, b *Int) {
|
2009-12-15 16:33:31 -07:00
|
|
|
d.doinit()
|
|
|
|
x.doinit()
|
|
|
|
y.doinit()
|
|
|
|
a.doinit()
|
|
|
|
b.doinit()
|
|
|
|
C.mpz_gcdext(&d.i[0], &x.i[0], &y.i[0], &a.i[0], &b.i[0])
|
2009-09-16 19:14:18 -06:00
|
|
|
}
|
2009-10-14 11:56:19 -06:00
|
|
|
|
|
|
|
// ProbablyPrime performs n Miller-Rabin tests to check whether z is prime.
|
|
|
|
// If it returns true, z is prime with probability 1 - 1/4^n.
|
|
|
|
// If it returns false, z is not prime.
|
|
|
|
func (z *Int) ProbablyPrime(n int) bool {
|
2009-12-15 16:33:31 -07:00
|
|
|
z.doinit()
|
|
|
|
return int(C.mpz_probab_prime_p(&z.i[0], C.int(n))) > 0
|
2009-10-14 11:56:19 -06:00
|
|
|
}
|