Exemple #1
0
// 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 {
	m.doinit()
	x.doinit()
	y.doinit()
	z.doinit()
	if m == nil {
		C.mpz_pow_ui(&z.i[0], &x.i[0], C.mpz_get_ui(&y.i[0]))
	} else {
		C.mpz_powm(&z.i[0], &x.i[0], &y.i[0], &m.i[0])
	}
	return z
}
Exemple #2
0
// Uint64 returns the uint64 representation of x.
// If x cannot be represented in a uint64, the result is undefined.
func (x *Int) Uint64() (y uint64) {
	if !x.init {
		return
	}
	if C.mpz_fits_ulong_p(&x.i[0]) != 0 {
		return uint64(C.mpz_get_ui(&x.i[0]))
	}
	// Undefined result if > 64 bits
	if x.BitLen() > 64 {
		return
	}
	C.mpz_export(unsafe.Pointer(&y), nil, -1, 8, 0, 0, &x.i[0])
	return
}
Exemple #3
0
// Exp sets z = x^y % m and returns z. If m != nil, negative exponents are
// allowed if x^-1 mod m exists. If the inverse doesn't exist then a
// division-by-zero run-time panic occurs.
//
// If m == nil, Exp sets z = x^y for positive y and 1 for negative y.
func (z *Int) Exp(x, y, m *Int) *Int {
	x.doinit()
	y.doinit()
	z.doinit()
	if m == nil || m.Cmp(intZero) == 0 {
		if y.Sign() == -1 {
			z := NewInt(1)
			return z
		}
		C.mpz_pow_ui(z.ptr, x.ptr, C.mpz_get_ui(y.ptr))
	} else {
		m.doinit()
		C.mpz_powm(z.ptr, x.ptr, y.ptr, m.ptr)
	}
	return z
}
Exemple #4
0
// Exp sets z = x**y mod |m| (i.e. the sign of m is ignored), and returns z.
// If y <= 0, the result is 1; if m == nil or m == 0, z = x**y.
// See Knuth, volume 2, section 4.6.3.
func (z *Int) Exp(x, y, m *Int) *Int {
	x.doinit()
	y.doinit()
	z.doinit()
	if y.Sign() <= 0 {
		z.SetInt64(1)
		return z
	}
	if m == nil || m.Sign() == 0 {
		C.mpz_pow_ui(&z.i[0], &x.i[0], C.mpz_get_ui(&y.i[0]))
	} else {
		m.doinit()
		C.mpz_powm(&z.i[0], &x.i[0], &y.i[0], &m.i[0])
	}
	return z
}
Exemple #5
0
// Uint32 returns the uint32 representation of z, if z fits into a uint32.
// If z is too big then the least significant bits that do fit are returned.
// The sign of z is ignored, only the absolute value is used.
//
// NB This is not part of big.Int
func (z *Int) Uint32() uint32 {
	z.doinit()
	return uint32(C.mpz_get_ui(&z.i[0]))
}
Exemple #6
0
// Uint64 returns the uint64 representation of x. If x cannot be
// represented in an uint64, the result is undefined.
func (z *Int) Uint64() uint64 {
	if !z.init {
		return 0
	}
	return uint64(C.mpz_get_ui(z.ptr))
}