10#include <boost/throw_exception.hpp>
18 static bool s_initialized = []()
noexcept {
30 mpz_import(m, be.size(), 1, 1, 0, 0, &be[0]);
38 static bool valid_element_of_fp(
const Scalar& x)
noexcept {
43 if(bytes64_be.size() != 64) {
47 bytes sub1(bytes64_be.begin(), bytes64_be.begin()+32);
48 bytes sub2(bytes64_be.begin()+32, bytes64_be.begin()+64);
53 if (!valid_element_of_fp(x) || !valid_element_of_fp(
y)) {
57 if (x.is_zero() &&
y.is_zero()) {
62 if (!
point.is_well_formed()) {
69 if(bytes64_be.size() != 64) {
74 bytes sub1(bytes64_be.begin()+32, bytes64_be.begin()+64);
75 bytes sub2(bytes64_be.begin(), bytes64_be.begin()+32);
80 if (!valid_element_of_fp(c0) || !valid_element_of_fp(c1)) {
88 if(bytes128_be.size() != 128) {
92 bytes sub1(bytes128_be.begin(), bytes128_be.begin()+64);
94 if (std::holds_alternative<alt_bn128_error>(maybe_x)) {
95 return std::get<alt_bn128_error>(maybe_x);
98 bytes sub2(bytes128_be.begin()+64, bytes128_be.begin()+128);
100 if (std::holds_alternative<alt_bn128_error>(maybe_y)) {
101 return std::get<alt_bn128_error>(maybe_y);
104 const auto& x = std::get<libff::alt_bn128_Fq2>(maybe_x);
105 const auto&
y = std::get<libff::alt_bn128_Fq2>(maybe_y);
107 if (x.is_zero() &&
y.is_zero()) {
112 if (!
point.is_well_formed()) {
130 p.to_affine_coordinates();
132 auto x{
p.X.as_bigint()};
133 auto y{
p.Y.as_bigint()};
135 std::memcpy(&out[0],
y.data, 32);
136 std::memcpy(&out[32], x.data, 32);
138 std::reverse(out.begin(), out.end());
146 if (std::holds_alternative<alt_bn128_error>(maybe_x)) {
147 return std::get<alt_bn128_error>(maybe_x);
151 if (std::holds_alternative<alt_bn128_error>(maybe_y)) {
152 return std::get<alt_bn128_error>(maybe_y);
155 const auto& x = std::get<libff::alt_bn128_G1>(maybe_x);
156 const auto&
y = std::get<libff::alt_bn128_G1>(maybe_y);
166 if (std::holds_alternative<alt_bn128_error>(maybe_x)) {
167 return std::get<alt_bn128_error>(maybe_x);
170 auto& x = std::get<libff::alt_bn128_G1>(maybe_x);
172 if(scalar.size() != 32) {
182 static constexpr size_t kSnarkvStride{192};
185 if (g1_g2_pairs.size() % kSnarkvStride != 0) {
189 size_t k{g1_g2_pairs.size() / kSnarkvStride};
192 using namespace libff;
194 static const auto one{alt_bn128_Fq12::one()};
195 auto accumulator{
one};
197 for (
size_t i{0}; i < k; ++i) {
198 auto offset = i * kSnarkvStride;
200 bytes sub1(g1_g2_pairs.begin()+offset, g1_g2_pairs.begin()+offset+64);
202 if (std::holds_alternative<alt_bn128_error>(maybe_a)) {
203 return std::get<alt_bn128_error>(maybe_a);
206 bytes sub2(g1_g2_pairs.begin()+offset+64, g1_g2_pairs.begin()+offset+64+128);
208 if (std::holds_alternative<alt_bn128_error>(maybe_b)) {
209 return std::get<alt_bn128_error>(maybe_b);
212 const auto&
a = std::get<libff::alt_bn128_G1>(maybe_a);
213 const auto& b = std::get<libff::alt_bn128_G2>(maybe_b);
215 if (
a.is_zero() || b.is_zero()) {
219 accumulator = accumulator * alt_bn128_miller_loop(alt_bn128_precompute_G1(
a), alt_bn128_precompute_G2(b));
223 bool pair_result =
false;
224 if (alt_bn128_final_exponentiation(accumulator) ==
one) {
static Fp2_model< n, modulus > one()
static Fp_model< n, modulus > one()
static alt_bn128_G1 zero()
static alt_bn128_G2 zero()
static bigint< scalar_field::num_limbs > order()
static void init_public_params()
Scalar to_scalar(const bytes &be) noexcept
std::variant< alt_bn128_error, libff::alt_bn128_G2 > decode_g2_element(const bytes &bytes128_be) noexcept
bytes encode_g1_element(libff::alt_bn128_G1 p) noexcept
std::vector< char > bytes
void initLibSnark() noexcept
std::variant< alt_bn128_error, libff::alt_bn128_Fq2 > decode_fp2_element(const bytes &bytes64_be) noexcept
std::variant< alt_bn128_error, bytes > alt_bn128_add(const bytes &op1, const bytes &op2)
@ pairing_list_size_error
@ operand_component_invalid
std::variant< alt_bn128_error, bytes > alt_bn128_mul(const bytes &g1_point, const bytes &scalar)
libff::bigint< libff::alt_bn128_q_limbs > Scalar
std::variant< alt_bn128_error, libff::alt_bn128_G1 > decode_g1_element(const bytes &bytes64_be) noexcept
std::variant< alt_bn128_error, bool > alt_bn128_pair(const bytes &g1_g2_pairs, const yield_function_t &yield)
bigint< alt_bn128_q_limbs > alt_bn128_modulus_q
const mp_size_t alt_bn128_q_limbs
bool inhibit_profiling_info
bool inhibit_profiling_counters
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a