return cln::the<cln::cl_I>(ex_to<numeric>(e).to_cl_N());
}
+/**
+ * Generate random integer -p/2 < m <= p/2.
+ */
struct random_modint
{
typedef long value_type;
const value_type p;
- const value_type p_2;
+ const value_type pm1_2; // (p-1)/2
- random_modint(const value_type& p_) : p(p_), p_2((p >> 1))
+ random_modint(const value_type& p_) : p(p_), pm1_2((p-1) >> 1)
{ }
value_type operator()() const
{
- do {
- cln::cl_I tmp_ = cln::random_I(p);
- value_type tmp = cln::cl_I_to_long(tmp_);
- if (tmp > p_2)
- tmp -= p;
- return tmp;
- } while (true);
+ cln::cl_I m = cln::random_I(p) - pm1_2;
+ return cln::cl_I_to_long(m);
}
};