Skip to content

Commit

Permalink
work in progress on #147
Browse files Browse the repository at this point in the history
  • Loading branch information
vo-nil committed Mar 19, 2024
1 parent dfa5137 commit 83023dd
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 257 deletions.
6 changes: 3 additions & 3 deletions include/nil/crypto3/algebra/fields/alt_bn128/base_field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ namespace nil {
constexpr static const integral_type modulus =
0x30644E72E131A029B85045B68181585D97816A916871CA8D3C208C16D87CFD47_cppui254;

// constexpr static const integral_type group_order_minus_one_half =
// (modulus - 1) / 2;
constexpr static const integral_type group_order_minus_one_half =
(modulus - 1) / 2;
/*
constexpr static const integral_type group_order_minus_one_half =
0x183227397098D014DC2822DB40C0AC2ECBC0B548B438E5469E10460B6C3E7EA3_cppui254;
Expand Down Expand Up @@ -87,7 +87,7 @@ namespace nil {

constexpr typename alt_bn128_base_field<254>::integral_type const alt_bn128_base_field<254>::modulus;

// constexpr typename alt_bn128_base_field<254>::integral_type const alt_bn128_base_field<254>::group_order_minus_one_half;
constexpr typename alt_bn128_base_field<254>::integral_type const alt_bn128_base_field<254>::group_order_minus_one_half;

constexpr typename alt_bn128_base_field<254>::modular_params_type const
alt_bn128_base_field<254>::modulus_params;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,90 +36,6 @@
namespace nil {
namespace crypto3 {
namespace algebra {


namespace debug {
template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp<FieldParams> &e) {
// os << std::hex <<"0x"<< std::setw((FieldParams::modulus_bits+7)/4) << std::setfill('0') << e.data << "_cppui" << std::dec << FieldParams::modulus_bits << " ";
os << '"' << e.data << '"' ;
}

template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp2<FieldParams> &e) {
os << " [ ";
print_field_element(os, e.data[0]);
os << ", ";
print_field_element(os, e.data[1]);
os << "]";
}

template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp3<FieldParams> &e) {
os << "[";
print_field_element(os, e.data[0]);
os << ", ";
print_field_element(os, e.data[1]);
os << ", ";
print_field_element(os, e.data[2]);
os << "]";
}

template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp4<FieldParams> &e) {
os << "[";
print_field_element(os, e.data[0]);
os << ", ";
print_field_element(os, e.data[1]);
os << "]";
}

template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp6_2over3<FieldParams> &e) {
os << "[";
print_field_element(os, e.data[0]);
os << ", ";
print_field_element(os, e.data[1]);
os << "]";
}

template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp6_3over2<FieldParams> &e) {
os << " [" << std::endl;
print_field_element(os, e.data[0]);
os << ", " << std::endl;
print_field_element(os, e.data[1]);
os << ", " << std::endl;
print_field_element(os, e.data[2]);
os << std::endl << " ]";
}


template<typename FieldParams>
void print_field_element(std::ostream &os, const fields::detail::element_fp12_2over3over2<FieldParams> &e) {
os << "[" << std::endl;
print_field_element(os, e.data[0]);
os << ", " << std::endl;
print_field_element(os, e.data[1]);
os << std::endl << "]";
}

template<typename FieldParams>
void print_ell_coeffs(std::ostream &os,
fields::detail::element_fp2<FieldParams> const& ell_0,
fields::detail::element_fp2<FieldParams> const& ell_VW,
fields::detail::element_fp2<FieldParams> const& ell_VV)
{
os << "{" << std::endl;
os << "\"ell_0\" : "; print_field_element(os, ell_0 ); os << "," << std::endl;
os << "\"ell_VW\": "; print_field_element(os, ell_VW); os << "," << std::endl;
os << "\"ell_VV\": "; print_field_element(os, ell_VV); os << "" << std::endl;
os << "}" << std::endl;
}

}


namespace pairing {

template<typename CurveType>
Expand Down Expand Up @@ -162,52 +78,31 @@ void print_ell_coeffs(std::ostream &os,
++idx;
if (params_type::twist_type == twist_type::TWIST_TYPE_M) {
f = f.mul_by_014(c.ell_0, prec_P.PX * c.ell_VW, prec_P.PY * c.ell_VV);
debug::print_ell_coeffs(std::cout, c.ell_0, prec_P.PX * c.ell_VW, prec_P.PY * c.ell_VV);
} else {
f = f.mul_by_034(prec_P.PY * c.ell_0, prec_P.PX * c.ell_VW, c.ell_VV);
debug::print_ell_coeffs(std::cout, prec_P.PY * c.ell_0, prec_P.PX * c.ell_VW, c.ell_VV);
}
std::cout << "by bit " << std::setw(2) << (int)(*bit) <<" :";
debug::print_field_element(std::cout, f);
std::cout << std::endl;
}

std::cout << "-------" << std::endl;
}

std::cout << "~~~~~~~~~~~~~~FINAL~~~~~~~~~~~~~" << std::endl;
debug::print_field_element(std::cout, f); std::cout << std::endl;

if (params_type::final_exponent_is_z_neg) {
f = f.inversed();
}

std::cout << "~~~~~~~~~~~~~~INVERSED?~~~~~~~~~~~~~" << std::endl;
debug::print_field_element(std::cout, f); std::cout << std::endl;


std::cout << "~=~=~=~= Final two coefficeints ~=~=~=~=" << std::endl;
c = prec_Q.coeffs[idx];
++idx;
if (params_type::twist_type == twist_type::TWIST_TYPE_M) {
f = f.mul_by_014(c.ell_0, prec_P.PX * c.ell_VW, prec_P.PY * c.ell_VV);
debug::print_ell_coeffs(std::cout, c.ell_0, prec_P.PX * c.ell_VW, prec_P.PY * c.ell_VV);
} else {
f = f.mul_by_034(prec_P.PY * c.ell_0, prec_P.PX * c.ell_VW, c.ell_VV);
debug::print_ell_coeffs(std::cout, prec_P.PY * c.ell_0, prec_P.PX * c.ell_VW, c.ell_VV);
}
debug::print_field_element(std::cout, f); std::cout << std::endl;

c = prec_Q.coeffs[idx];
++idx;
if (params_type::twist_type == twist_type::TWIST_TYPE_M) {
f = f.mul_by_014(c.ell_0, prec_P.PX * c.ell_VW, prec_P.PY * c.ell_VV);
debug::print_ell_coeffs(std::cout, c.ell_0, prec_P.PX * c.ell_VW, prec_P.PY * c.ell_VV);
} else {
f = f.mul_by_034(prec_P.PY * c.ell_0, prec_P.PX * c.ell_VW, c.ell_VV);
debug::print_ell_coeffs(std::cout, prec_P.PY * c.ell_0, prec_P.PX * c.ell_VW, c.ell_VV);
}
debug::print_field_element(std::cout, f); std::cout << std::endl;

return f;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,6 @@ namespace nil {

using g2_field_type_value = typename g2_type::field_type::value_type;

static void print_g2(typename g2_type::value_type const& v)
{
std::cout << "{";
std::cout << "\"x\": [ \"" << v.X.data[0] << "\", \"" << v.X.data[1] << "\"], ";
std::cout << "\"y\": [ \"" << v.Y.data[0] << "\", \"" << v.Y.data[1] << "\"], ";
std::cout << "\"z\": [ \"" << v.Z.data[0] << "\", \"" << v.Z.data[1] << "\"]";
std::cout << "}";
}

static void print_ell(typename policy_type::ate_ell_coeffs const& c)
{
std::cout << "{";
std::cout << "\"ell_0\" : [ \""
<< c.ell_0.data[0] << "\", \"" << c.ell_0.data[1] << "\"], " << std::endl;
std::cout << "\"ell_VW\": [ \""
<< c.ell_VW.data[0] << "\", \"" << c.ell_VW.data[1] << "\"], " << std::endl;
std::cout << "\"ell_VV\": [ \""
<< c.ell_VV.data[0] << "\", \"" << c.ell_VV.data[1] << "\"]" << std::endl;
std::cout << "}";
}

/* https://eprint.iacr.org/2013/722.pdf
* Equations (11) at p.13
* current *= 2, output ell coefficients in c
Expand All @@ -84,9 +63,6 @@ namespace nil {
typename g2_type::value_type &current,
typename policy_type::ate_ell_coeffs &c)
{

// std::cout << "doubling: "; print_g2(current); std::cout << std::endl;

const g2_field_type_value X = current.X, Y = current.Y, Z = current.Z;

const g2_field_type_value A = two_inv * (X * Y); // A = X1 * Y1 / 2
Expand Down Expand Up @@ -115,9 +91,6 @@ namespace nil {
c.ell_VW = J.doubled()+J;
c.ell_VV = I;
}
// std::cout << "doubling: "; print_g2(current); std::cout << std::endl;
// print_ell(c); std::cout << std::endl;

}

/* https://eprint.iacr.org/2013/722.pdf
Expand All @@ -129,9 +102,6 @@ namespace nil {
typename g2_type::value_type &current,
typename policy_type::ate_ell_coeffs &c)
{

// std::cout << "adding: "; print_g2(current); std::cout << std::endl;

const g2_field_type_value X1 = current.X, Y1 = current.Y, Z1 = current.Z;
const g2_field_type_value &x2 = base.X, &y2 = base.Y;

Expand Down Expand Up @@ -159,8 +129,6 @@ namespace nil {
c.ell_VW = -theta;
c.ell_VV = J;
}
// std::cout << "adding: "; print_g2(current); std::cout << std::endl;
// print_ell(c); std::cout << std::endl;
}

static typename g2_affine_type::value_type mul_by_char(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,90 +32,6 @@
namespace nil {
namespace crypto3 {
namespace algebra {
/*
namespace debug {
template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp<FieldParams> &e) {
// os << std::hex <<"0x"<< std::setw((FieldParams::modulus_bits+7)/4) << std::setfill('0') << e.data << "_cppui" << std::dec << FieldParams::modulus_bits << " ";
os << '"' << e.data << '"' ;
}
template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp2<FieldParams> &e) {
os << "[";
print_field_element(os, e.data[0]);
os << ", ";
print_field_element(os, e.data[1]);
os << "]";
}
template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp3<FieldParams> &e) {
os << "[";
print_field_element(os, e.data[0]);
os << ", ";
print_field_element(os, e.data[1]);
os << ", ";
print_field_element(os, e.data[2]);
os << "]";
}
template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp4<FieldParams> &e) {
os << "[";
print_field_element(os, e.data[0]);
os << ", ";
print_field_element(os, e.data[1]);
os << "]";
}
template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp6_2over3<FieldParams> &e) {
os << "[";
print_field_element(os, e.data[0]);
os << ", ";
print_field_element(os, e.data[1]);
os << "]";
}
template<typename FieldParams>
void print_field_element(std::ostream &os, const typename fields::detail::element_fp6_3over2<FieldParams> &e) {
os << "[";
print_field_element(os, e.data[0]);
os << ", ";
print_field_element(os, e.data[1]);
os << ", ";
print_field_element(os, e.data[2]);
os << "]";
}
template<typename FieldParams>
void print_field_element(std::ostream &os, const fields::detail::element_fp12_2over3over2<FieldParams> &e) {
os << "[";
print_field_element(os, e.data[0]);
os << ", ";
print_field_element(os, e.data[1]);
os << "]";
}
template<typename FieldParams>
void print_ell_coeffs(std::ostream &os,
fields::detail::element_fp2<FieldParams> const& ell_0,
fields::detail::element_fp2<FieldParams> const& ell_VW,
fields::detail::element_fp2<FieldParams> const& ell_VV)
{
os << "{" << std::endl;
os << "\"ell_0\" : "; print_field_element(os, ell_0 ); os << "," << std::endl;
os << "\"ell_VW\": "; print_field_element(os, ell_VW); os << "," << std::endl;
os << "\"ell_VV\": "; print_field_element(os, ell_VV); os << "" << std::endl;
os << "}" << std::endl;
}
}
*/


namespace pairing {

template<typename CurveType>
Expand All @@ -141,68 +57,37 @@ void print_ell_coeffs(std::ostream &os,
* */
static typename gt_type::value_type process(const typename gt_type::value_type &elt) {

std::cout << "~~~~~ FINAL EXPONENTIATION ~~~~~~" << std::endl;
std::cout << "elt: "; debug::print_field_element(std::cout, elt); std::cout << std::endl;

#define DUMP_FP12(X) { std::cout << #X " :" ; debug::print_field_element(std::cout, X); std::cout << std::endl; }
/* TODO: check elt == 0 ? */
auto f1 = elt.unitary_inversed();
DUMP_FP12(f1);
auto f2 = elt.inversed();
DUMP_FP12(f2);

auto r = f1 * f2;
f2 = r;
r = r.Frobenius_map(2);
r *= f2;

DUMP_FP12(r);

auto y0 = exp_by_z(r);
DUMP_FP12(y0);
auto y1 = y0.cyclotomic_squared();
DUMP_FP12(y1);
auto y2 = y1.cyclotomic_squared();
DUMP_FP12(y2);
auto y3 = y2 * y1;
DUMP_FP12(y3);
auto y4 = exp_by_z(y3);
DUMP_FP12(y4);
auto y5 = y4.cyclotomic_squared();
DUMP_FP12(y5);
auto y6 = exp_by_z(y5);
DUMP_FP12(y6);
y3 = y3.unitary_inversed();
DUMP_FP12(y3);
y6 = y6.unitary_inversed();
DUMP_FP12(y6);
auto y7 = y6 * y4;
DUMP_FP12(y7);
auto y8 = y7 * y3;
DUMP_FP12(y8);
auto y9 = y8 * y1;
DUMP_FP12(y9);
auto y10 = y8 * y4;
DUMP_FP12(y10);
auto y11 = y10 * r;
DUMP_FP12(y11);
auto y12 = y9.Frobenius_map(1);
DUMP_FP12(y12);
auto y13 = y12 * y11;
DUMP_FP12(y13);
y8 = y8.Frobenius_map(2);
DUMP_FP12(y8);
auto y14 = y8 * y13;
DUMP_FP12(y14);
r = r.unitary_inversed();
DUMP_FP12(r);
auto y15 = r * y9;
DUMP_FP12(y15);
y15 = y15.Frobenius_map(3);
DUMP_FP12(y15);
auto result = y15 * y14;
DUMP_FP12(result);

return result;
}
};
Expand Down
Loading

0 comments on commit 83023dd

Please sign in to comment.