2 *****************************************************************************
3 * @author This file is part of libff, developed by SCIPR Lab
4 * and contributors (see AUTHORS).
5 * @copyright MIT license (see LICENSE file)
6 *****************************************************************************/
8 #ifndef CURVE_UTILS_TCC_
9 #define CURVE_UTILS_TCC_
14 template<typename GroupT, mp_size_t m>
15 GroupT scalar_mul(const GroupT &base, const bigint<m> &scalar)
17 GroupT result = GroupT::zero();
19 bool found_one = false;
20 for (long i = static_cast<long>(scalar.max_bits() - 1); i >= 0; --i) {
22 result = result.dbl();
25 if (scalar.test_bit(i)) {
27 result = result + base;
34 template<typename GroupT>
35 decltype(((GroupT *)nullptr)->X) curve_point_y_at_x(
36 const decltype(((GroupT *)nullptr)->X) &x)
38 using base_field = decltype(((GroupT *)nullptr)->X);
39 const base_field x_squared = x * x;
40 const base_field x_cubed = x_squared * x;
41 const base_field y_squared =
42 x_cubed + (GroupT::coeff_a * x) + GroupT::coeff_b;
43 // TODO Check that y_squared is a quadratic residue (ensuring that sqrt()
45 return y_squared.sqrt();
48 template<typename GroupT>
49 GroupT g1_curve_point_at_x(const typename GroupT::base_field &x)
51 const typename GroupT::base_field x_squared = x * x;
52 const typename GroupT::base_field x_cubed = x_squared * x;
53 const typename GroupT::base_field y_squared =
54 x_cubed + (GroupT::coeff_a * x) + GroupT::coeff_b;
55 // Check that y_squared is a quadratic residue (ensuring that sqrt()
57 if ((y_squared ^ GroupT::base_field::euler) != GroupT::base_field::one()) {
58 throw std::runtime_error("curve eqn has no solution at x");
61 const typename GroupT::base_field y = y_squared.sqrt();
62 return GroupT(x, y, GroupT::base_field::one());
65 template<typename GroupT>
66 GroupT g2_curve_point_at_x(const typename GroupT::twist_field &x)
68 // TODO: Generic check (over all fields) that y_squared.sqrt() terminates.
69 return GroupT(x, curve_point_y_at_x<GroupT>(x), GroupT::twist_field::one());
73 #endif // CURVE_UTILS_TCC_