diff options
author | Adam Langley <agl@golang.org> | 2009-10-14 10:56:19 -0700 |
---|---|---|
committer | Adam Langley <agl@golang.org> | 2009-10-14 10:56:19 -0700 |
commit | aa504f65f0fd6d386b01744265fb1eeaf36194f0 (patch) | |
tree | 6a9fad3da67656defdde603d1e89d0e831b96eb6 | |
parent | 9ff17cd4d61cb13063a5db3158a200ce920d28f5 (diff) | |
download | golang-aa504f65f0fd6d386b01744265fb1eeaf36194f0.tar.gz |
Minor fixes and additions to the GMP wrapping.
R=rsc
APPROVED=rsc
DELTA=12 (11 added, 0 deleted, 1 changed)
OCL=35715
CL=35715
-rw-r--r-- | misc/cgo/gmp/gmp.go | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/misc/cgo/gmp/gmp.go b/misc/cgo/gmp/gmp.go index 5cda6dc73..a31f7407c 100644 --- a/misc/cgo/gmp/gmp.go +++ b/misc/cgo/gmp/gmp.go @@ -197,6 +197,9 @@ func (z *Int) SetString(s string, base int) os.Error { // String returns the decimal representation of z. func (z *Int) String() string { + if z == nil { + return "nil"; + } z.doinit(); p := C.mpz_get_str(nil, 10, &z.i[0]); s := C.GoString(p); @@ -253,7 +256,7 @@ func (z *Int) Div(x, y *Int) *Int { } // Mod sets z = x % y and returns z. -// XXX Unlike in Go, the result is always positive. +// Like the result of the Go % operator, z has the same sign as x. func (z *Int) Mod(x, y *Int) *Int { x.doinit(); y.doinit(); @@ -361,3 +364,11 @@ func GcdInt(d, x, y, a, b *Int) { b.doinit(); C.mpz_gcdext(&d.i[0], &x.i[0], &y.i[0], &a.i[0], &b.i[0]); } + +// 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 { + z.doinit(); + return int(C.mpz_probab_prime_p(&z.i[0], C.int(n))) > 0; +} |