From 7042a444c8454981f7020bc9db2402a935280ee0 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Tue, 13 Feb 2024 12:27:53 +0900 Subject: [PATCH] add conversion between Proj and Jacobi --- include/mcl/ec.hpp | 23 +++++++++++++++++++++++ test/ec_test.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index 2cd5a87a..4bba163b 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -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 +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 +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 void normalizeJacobi(E& P) { diff --git a/test/ec_test.cpp b/test/ec_test.cpp index c461b00f..160d0239 100644 --- a/test/ec_test.cpp +++ b/test/ec_test.cpp @@ -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 void test(F f, const char *msg) const @@ -672,6 +695,7 @@ mul 499.00usec mulCT(); compare(); addCT(); + ProjJacobi(); } private: Test(const Test&);