static P generic_mul(const P &p, const td::RefInt256 &x) {
  CHECK(x.not_null() && x->is_valid());
  if (x->sgn() == 0) {
    return generic_zero();
  }
  td::uint8 x_bytes[32];
  CHECK((x % get_r())->export_bytes(x_bytes, 32, false));
  try {
    blst_P point(p.data(), p.size() / 8);
    blst::Scalar scalar;
    scalar.from_bendian(x_bytes, 32);
    point.mult(scalar);
    P result;
    point.compress(result.data());
    return result;
  } catch (BLST_ERROR e) {
    throw VmError{Excno::unknown, PSTRING() << "blst error " << e};
  }
}
template 
static P generic_multiexp(const std::vector> &ps) {
  if (ps.size() == 1) {
    return generic_mul(ps[0].first, ps[0].second);
  }
  try {
    std::vector points(ps.size());
    std::vector scalars(ps.size());
    std::vector scalar_ptrs(ps.size());
    for (size_t i = 0; i < ps.size(); ++i) {
      points[i] = blst_P_Affine(ps[i].first.data(), ps[i].first.size() / 8);
      CHECK(ps[i].second.not_null() && ps[i].second->is_valid());
      CHECK((ps[i].second % get_r())->export_bytes_lsb(scalars[i].data(), 32));
      scalar_ptrs[i] = (const byte *)&scalars[i];
    }
    blst_P point =
        ps.empty() ? blst_P() : blst_P_Affines::mult_pippenger(points.data(), points.size(), scalar_ptrs.data(), 256);
    P result;
    point.compress(result.data());
    return result;
  } catch (BLST_ERROR e) {
    throw VmError{Excno::unknown, PSTRING() << "blst error " << e};
  }
}
template 
static bool generic_in_group(const P &a) {
  try {
    blst_P point = blst_P(a.data(), a.size() / 8);
    return point.in_group();
  } catch (BLST_ERROR e) {
    return false;
  }
}
template 
static bool generic_is_zero(const P &a) {
  return a == generic_zero();
}
P1 g1_add(const P1 &a, const P1 &b) {
  return generic_add(a, b);
}
P1 g1_sub(const P1 &a, const P1 &b) {
  return generic_sub(a, b);
}
P1 g1_neg(const P1 &a) {
  return generic_neg(a);
}
P1 g1_mul(const P1 &p, const td::RefInt256 &x) {
  return generic_mul(p, x);
}
P1 g1_multiexp(const std::vector> &ps) {
  return generic_multiexp(ps);
}
P1 g1_zero() {
  return generic_zero();
}
P1 map_to_g1(const FP &a) {
  blst_fp fp;
  blst_fp_from_bendian(&fp, a.data());
  blst_p1 point;
  blst_map_to_g1(&point, &fp, nullptr);
  P1 result;
  blst_p1_compress(result.data(), &point);
  return result;
}
bool g1_in_group(const P1 &a) {
  return generic_in_group(a);
}
bool g1_is_zero(const P1 &a) {
  return generic_is_zero(a);
}
P2 g2_add(const P2 &a, const P2 &b) {
  return generic_add(a, b);
}
P2 g2_sub(const P2 &a, const P2 &b) {
  return generic_sub(a, b);
}
P2 g2_neg(const P2 &a) {
  return generic_neg(a);
}
P2 g2_mul(const P2 &p, const td::RefInt256 &x) {
  return generic_mul(p, x);
}
P2 g2_multiexp(const std::vector> &ps) {
  return generic_multiexp(ps);
}
P2 g2_zero() {
  return generic_zero();
}
P2 map_to_g2(const FP2 &a) {
  blst_fp2 fp2;
  blst_fp_from_bendian(&fp2.fp[0], a.data());
  blst_fp_from_bendian(&fp2.fp[1], a.data() + FP_SIZE);
  blst_p2 point;
  blst_map_to_g2(&point, &fp2, nullptr);
  P2 result;
  blst_p2_compress(result.data(), &point);
  return result;
}
bool g2_in_group(const P2 &a) {
  return generic_in_group(a);
}
bool g2_is_zero(const P2 &a) {
  return generic_is_zero(a);
}
bool pairing(const std::vector> &ps) {
  try {
    std::unique_ptr pairing = std::make_unique(true, DST);
    for (const auto &p : ps) {
      blst::P1_Affine point1(p.first.data(), P1_SIZE);
      blst::P2_Affine point2(p.second.data(), P2_SIZE);
      pairing->raw_aggregate(&point2, &point1);
    }
    pairing->commit();
    return pairing->finalverify();
  } catch (BLST_ERROR e) {
    throw VmError{Excno::unknown, PSTRING() << "blst error " << e};
  }
}
td::RefInt256 get_r() {
  static td::RefInt256 r = td::dec_string_to_int256(
      td::Slice{"52435875175126190479447740508185965837690552500527637822603658699938581184513"});
  return r;
}
}  // namespace bls
}  // namespace vm