Skip to content

Commit

Permalink
affine zero point for short-weierstrass added
Browse files Browse the repository at this point in the history
  • Loading branch information
tshchelovek committed Jun 9, 2022
1 parent 701472d commit 1394dc1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
61 changes: 59 additions & 2 deletions example/curves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,27 @@
//#include <nil/crypto3/algebra/curves/wei25519.hpp>
#include <nil/crypto3/algebra/curves/secp_k1.hpp>
#include <nil/crypto3/algebra/curves/pallas.hpp>
#include <nil/crypto3/algebra/curves/vesta.hpp>
#include <nil/crypto3/algebra/curves/secp_r1.hpp>

using namespace nil::crypto3::algebra;

template<typename FpCurveGroupElement>
void print_affine_fp_curve_group_element(FpCurveGroupElement e) {
if (e.is_zero()) {
std::cout << "zero\n";
} else {
std::cout << e.X.data << " " << e.Y.data << std::endl;
}
}

template<typename FpCurveGroupElement>
void print_fp_curve_group_element(FpCurveGroupElement e) {
std::cout << e.X.data << " " << e.Y.data << " " << e.Z.data << std::endl;
if (e.is_zero()) {
std::cout << "zero\n";
} else {
std::cout << e.X.data << " " << e.Y.data << " " << e.Z.data << std::endl;
}
}

template<typename Fp2CurveGroupElement>
Expand All @@ -65,12 +79,45 @@ void print_fp3_curve_group_element(Fp3CurveGroupElement e) {

// print dunctions can be made using arity in fields

template<typename FpCurveGroup>
void fp_curve_group_affine_math_examples() {
typedef typename FpCurveGroup::value_type group_value_type;
typedef typename FpCurveGroup::field_type::value_type field_value_type;

field_value_type e1 = field_value_type(2), e2(5);
group_value_type c1 = group_value_type::zero();
group_value_type c2(e1, e2);

std::cout << "Curve element values: " << std::endl;
std::cout << "c1 value: ";
print_affine_fp_curve_group_element(c1);

std::cout << "c2 value: ";
print_affine_fp_curve_group_element(c2);

std::cout << "c1 + c2 value: ";
print_affine_fp_curve_group_element(c1 + c2);

std::cout << "c1 - c2 value: ";
print_affine_fp_curve_group_element(c1 - c2);

std::cout << "Doubled c1 value: ";
print_affine_fp_curve_group_element(c1.doubled());

group_value_type cd = c1.doubled();

// group_value_type cn = c1.normalize();

// std::cout << "c1 normalized value: ";
// print_fp_curve_group_element(cn);
}

template<typename FpCurveGroup>
void fp_curve_group_basic_math_examples() {
typedef typename FpCurveGroup::value_type group_value_type;
typedef typename FpCurveGroup::field_type::value_type field_value_type;

field_value_type e1 = field_value_type(2), e2(3), e3(5), e4(3), e5(5), e6(7);
field_value_type e1 = field_value_type(0), e2(1), e3(0), e4(3), e5(5), e6(7);
group_value_type c1(e1, e2, e3), c2(e4, e5, e6);

std::cout << "Curve element values: " << std::endl;
Expand Down Expand Up @@ -246,5 +293,15 @@ int main() {

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

std::cout << "Pallas curve g1 group affine math:" << std::endl;
fp_curve_group_affine_math_examples<curves::pallas::g1_type<curves::coordinates::affine>>();

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

std::cout << "Vesta curve g1 group affine math:" << std::endl;
fp_curve_group_affine_math_examples<curves::vesta::g1_type<curves::coordinates::affine>>();

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

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace nil {

field_value_type X;
field_value_type Y;
bool is_zero_flag;

/************************* Constructors and zero/one ***********************************/

Expand All @@ -75,7 +76,7 @@ namespace nil {
*
*/
constexpr curve_element() :
curve_element(params_type::zero_fill[0], params_type::zero_fill[1]) {};
is_zero_flag(true) {};

/** @brief
* @return the selected point $(X:Y)$ in the affine coordinates
Expand All @@ -84,6 +85,7 @@ namespace nil {
constexpr curve_element(field_value_type in_X, field_value_type in_Y) {
this->X = in_X;
this->Y = in_Y;
is_zero_flag = false;
};

/** @brief Get the point at infinity
Expand Down Expand Up @@ -133,7 +135,7 @@ namespace nil {
* @return true if element from group G1 is the point at infinity
*/
constexpr bool is_zero() const {
return X == params_type::zero_fill[0] && Y == params_type::zero_fill[1];
return is_zero_flag;
}

/************************* Reducing operations ***********************************/
Expand Down

0 comments on commit 1394dc1

Please sign in to comment.