ring_elem RingZZ::remainderAndQuotient(const ring_elem f, const ring_elem g, ring_elem ") const { mpz_ptr q = new_elem(); mpz_ptr r = new_elem(); int gsign = mpz_sgn(g.get_mpz()); mpz_t gg, ghalf; mpz_init(gg); mpz_init(ghalf); mpz_abs(gg,g.get_mpz()); mpz_fdiv_qr(q, r, f.get_mpz(), gg); mpz_tdiv_q_2exp(ghalf, gg, 1); if (mpz_cmp(r,ghalf) > 0) // r > ghalf { mpz_sub(r,r,gg); mpz_add_ui(q,q,1); } if (gsign < 0) mpz_neg(q,q); mpz_clear(gg); mpz_clear(ghalf); quot = ring_elem(q); return ring_elem(r); }
ring_elem RingZZ::gcd_extended(const ring_elem f, const ring_elem g, ring_elem &u, ring_elem &v) const { mpz_ptr result = new_elem(); mpz_ptr u1 = new_elem(); mpz_ptr v1 = new_elem(); mpz_gcdext(result, u1, v1, f.get_mpz(), g.get_mpz()); u = ring_elem(u1); v = ring_elem(v1); return ring_elem(result); }
ring_elem GF::from_int(int n) const { int m = n % P; if (m < 0) m += P; m = _from_int_table[m]; return ring_elem(m); }
ring_elem RingZZ::from_int(mpz_ptr n) const { mpz_ptr result = new_elem(); mpz_set(result, n); return ring_elem(result); }
ring_elem Z_mod::invert(const ring_elem f) const { // MES: error if f == _ZERO int a = f.int_val; if (a == 0) return 0; // this is the case f == ONE return ring_elem(P - 1 - a); }
ring_elem Z_mod::from_long(long n) const { int m = static_cast<int>(n % P); if (m < 0) m += P; m = _log_table[m]; return ring_elem(m); }
ring_elem Z_mod::from_int(int n) const { int m = n % P; if (m < 0) m += P; m = _log_table[m]; return ring_elem(m); }
ring_elem RingZZ::from_long(long n) const { mpz_ptr result = new_elem(); mpz_set_si(result, n); return ring_elem(result); }
ring_elem RingZZ::copy(const ring_elem f) const { mpz_ptr a = f.get_mpz(); mpz_ptr result = new_elem(); mpz_set(result, a); return ring_elem(result); }
ring_elem GF::from_long(long n) const { long m1 = n % characteristic(); if (m1 < 0) m1 += characteristic(); int m = static_cast<int>(m1); m = _from_int_table[m]; return ring_elem(m); }
ring_elem GF::from_int(mpz_ptr n) const { mpz_t result; mpz_init(result); mpz_mod_ui(result, n, P); int m = static_cast<int>(mpz_get_si(result)); if (m < 0) m += P; m = _from_int_table[m]; return ring_elem(m); }
ring_elem RingZZ::power(const ring_elem f, mpz_t n) const { mpz_ptr result = new_elem(); int n1; if (!get_si(n1, n)) { ERROR("exponent too large"); } else mpz_pow_ui(result, f.get_mpz(), n1); return ring_elem(result); }
ring_elem GF::from_int(mpz_ptr n) const { mpz_t result; mpz_init(result); mpz_mod_ui(result, n, characteristic()); long m1 = mpz_get_si(result); mpz_clear(result); if (m1 < 0) m1 += characteristic(); int m = static_cast<int>(m1); m = _from_int_table[m]; return ring_elem(m); }
ring_elem Z_mod::from_int(mpz_ptr n) const { // cout << "from_int("; // bignum_text_out(cout, n); // cout << ") = " << endl; mpz_t result; mpz_init(result); mpz_mod_ui(result, n, P); int m = static_cast<int>(mpz_get_si(result)); // cout << m << endl; if (m < 0) m += P; m = _log_table[m]; return ring_elem(m); }
void RingZZ::lower_content(ring_elem &c, ring_elem g) const // c is a content elem, g is in ring { if (is_zero(c)) { c = g; return; } gmp_ZZ result = RingZZ::new_elem(); mpz_srcptr a = c.get_mpz(); mpz_srcptr b = g.get_mpz(); mpz_gcd(result, a, b); if(mpz_sgn(a) == -1) mpz_neg(result, result); c = ring_elem(result); }
bool RingZZ::lower_associate_divisor(ring_elem &f, const ring_elem g) const { // This sets f to either 0, 1 or -1. // if f is 0, do f=sign(g), else f=sign(f) // return whether f is zero gmp_ZZ result = RingZZ::new_elem(); mpz_ptr a = f.get_mpz(); mpz_ptr b = g.get_mpz(); int sa = mpz_sgn(a); int sb = mpz_sgn(b); int s = (sa == 0 ? sb : sa); mpz_set_si(result,s); f = ring_elem(result); return !RingZZ::is_zero(f); }
void RingZZ::lower_content(ring_elem &c, ring_elem g) const // c is a content elem, g is in ring { // if f is 0, do f=sign(g), else f=sign(f) // return whether f is zero if (is_zero(c)) { c = g; return; } gmp_ZZ result = RingZZ::new_elem(); mpz_ptr a = c.get_mpz(); mpz_ptr b = g.get_mpz(); mpz_gcd(result,a,b); c = ring_elem(result); }
ring_elem GF::invert(const ring_elem f) const { // error if f == _ZERO if (f == _ONE) return _ONE; return ring_elem(Q1_ - f); }
ring_elem RingZZ::negate(const ring_elem f) const { mpz_ptr result = new_elem(); mpz_sub(result, _zero_elem, f.get_mpz()); return ring_elem(result); }
ring_elem Z_mod::random() const { int exp = rawRandomInt((int32_t)P); return ring_elem(exp); }
ring_elem GF::random() const { int exp = rawRandomInt((int32_t)Q_); return ring_elem(exp); }
ring_elem RingZZ::subtract(const ring_elem f, const ring_elem g) const { mpz_ptr result = new_elem(); mpz_sub(result, f.get_mpz(), g.get_mpz()); return ring_elem(result); }
ring_elem RingZZ::gcd(const ring_elem f, const ring_elem g) const { mpz_ptr result = new_elem(); mpz_gcd(result, f.get_mpz(), g.get_mpz()); return ring_elem(result); }
ring_elem RingZZ::power(const ring_elem f, int n) const { mpz_ptr result = new_elem(); mpz_pow_ui(result, f.get_mpz(), n); return ring_elem(result); }
ring_elem RingZZ::divide(const ring_elem f, const ring_elem g) const { mpz_ptr result = new_elem(); mpz_fdiv_q(result, f.get_mpz(), g.get_mpz()); return ring_elem(result); }