Skip to content

Commit

Permalink
add conversion between Proj and Jacobi
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Feb 13, 2024
1 parent e8efab8 commit 7042a44
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
23 changes: 23 additions & 0 deletions include/mcl/ec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,29 @@ void normalizeVecT(Eout& Q, Ein& P, size_t n, size_t N = 256)

} // mcl::ec::local

// [X:Y:Z] as Proj = (X/Z, Y/Z) as Affine = [XZ:YZ^2:Z] as Jacobi
template<class E>
void ProjToJacobi(E& Q, const E& P)
{
typedef typename E::Fp F;
F::mul(Q.x, P.x, P.z);
F::mul(Q.y, P.y, P.z);
Q.y *= P.z;
Q.z = P.z;
}

// [X:Y:Z] as Jacobi = (X/Z^2, Y/Z^3) as Affine = [XZ:Y:Z^3] as Proj
template<class E>
void JacobiToProj(E& Q, const E& P)
{
typedef typename E::Fp F;
F::mul(Q.x, P.x, P.z);
Q.y = P.y;
F t;
F::sqr(t, P.z);
F::mul(Q.z, P.z, t);
}

template<class E>
void normalizeJacobi(E& P)
{
Expand Down
24 changes: 24 additions & 0 deletions test/ec_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,29 @@ struct Test {
Ec::add(R, Zero, Zero);
CYBOZU_TEST_EQUAL(Q, R);
}
void ProjJacobi() const
{
if (Ec::getMode() == mcl::ec::Affine) return;
Fp x(para.gx);
Fp y(para.gy);
Ec P(x, y), Q, R;
P *= 123;
if (Ec::getMode() == mcl::ec::Proj) {
mcl::ec::ProjToJacobi(Q, P);
mcl::ec::normalizeJacobi(Q);
CYBOZU_TEST_EQUAL(Q, P);
mcl::ec::ProjToJacobi(Q, P);
mcl::ec::JacobiToProj(R, Q);
CYBOZU_TEST_EQUAL(R, P);
} else {
mcl::ec::JacobiToProj(Q, P);
mcl::ec::normalizeProj(Q);
CYBOZU_TEST_EQUAL(Q, P);
mcl::ec::JacobiToProj(Q, P);
mcl::ec::ProjToJacobi(R, Q);
CYBOZU_TEST_EQUAL(R, P);
}
}

template<class F>
void test(F f, const char *msg) const
Expand Down Expand Up @@ -672,6 +695,7 @@ mul 499.00usec
mulCT();
compare();
addCT();
ProjJacobi();
}
private:
Test(const Test&);
Expand Down

0 comments on commit 7042a44

Please sign in to comment.