Skip to content

Commit

Permalink
add dblCTProj
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Feb 13, 2024
1 parent 7042a44 commit 3cf65bb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
54 changes: 44 additions & 10 deletions include/mcl/ec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,17 +505,23 @@ void addJacobi(E& R, const E& P, const E& Q)
/*
accept P == Q
https://github.com/apache/incubator-milagro-crypto-c/blob/fa0a45a3/src/ecp.c.in#L767-L976
// 14M
(x, y, z) is zero <=> x = 0, y = 1, z = 0
*/
template<class E>
void clearCTProj(E& P)
{
P.x.clear();
P.y = 1;
P.z.clear();
}

template<class E>
void addCTProj(E& R, const E& P, const E& Q)
{
typedef typename E::Fp F;
assert(E::a_ == 0);
F b3;
F::add(b3, E::b_, E::b_);
b3 += E::b_;
F t0, t1, t2, t3, t4, x3, y3, z3;
F t0, t1, t2, t3, t4, x3, y3;
F::mul(t0, P.x, Q.x);
F::mul(t1, P.y, Q.y);
F::mul(t2, P.z, Q.z);
Expand All @@ -536,19 +542,44 @@ void addCTProj(E& R, const E& P, const E& Q)
F::sub(y3, x3, y3);
F::add(x3, t0, t0);
F::add(t0, t0, x3);
t2 *= b3;
F::add(z3, t1, t2);
t2 *= E::b3_;
F::add(R.z, t1, t2);
F::sub(t1, t1, t2);
y3 *= b3;
y3 *= E::b3_;
F::mul(x3, y3, t4);
F::mul(t2, t3, t1);
F::sub(R.x, t2, x3);
F::mul(y3, y3, t0);
F::mul(t1, t1, z3);
F::mul(t1, t1, R.z);
F::add(R.y, y3, t1);
F::mul(t0, t0, t3);
F::mul(z3, z3, t4);
F::add(R.z, z3, t0);
F::mul(R.z, R.z, t4);
F::add(R.z, R.z, t0);
}
template<class E>
void dblCTProj(E& R, const E& P)
{
typedef typename E::Fp F;
assert(E::a_ == 0);
F t0, t1, t2, x3, y3;
F::sqr(t0, P.y);
F::mul(t1, P.y, P.z);
F::sqr(t2, P.z);
F::add(R.z, t0, t0);
F::add(R.z, R.z, R.z);
F::add(R.z, R.z, R.z);
F::mul(t2, t2, E::b3_);
F::mul(x3, t2, P.z);
F::add(y3, t0, t2);
F::mul(R.z, R.z, t1);
F::add(t1, t2, t2);
F::add(t2, t2, t1);
F::mul(t1, P.x, P.y);
F::sub(t0, t0, t2);
F::mul(R.y, y3, t0);
R.y += x3;
F::mul(R.x, t0, t1);
R.x += R.x;
}

template<class E>
Expand Down Expand Up @@ -1237,6 +1268,7 @@ class EcT : public fp::Serializable<EcT<_Fp> > {
static int mode_;
static Fp a_;
static Fp b_;
static Fp b3_;
static int specialA_;
static int ioMode_;
/*
Expand Down Expand Up @@ -1294,6 +1326,7 @@ class EcT : public fp::Serializable<EcT<_Fp> > {
{
a_ = a;
b_ = b;
b3_ = b * 3;
if (a_.isZero()) {
specialA_ = ec::Zero;
} else if (a_ == -3) {
Expand Down Expand Up @@ -2100,6 +2133,7 @@ class EcT : public fp::Serializable<EcT<_Fp> > {

template<class Fp> Fp EcT<Fp>::a_;
template<class Fp> Fp EcT<Fp>::b_;
template<class Fp> Fp EcT<Fp>::b3_;
template<class Fp> int EcT<Fp>::specialA_;
template<class Fp> int EcT<Fp>::ioMode_;
template<class Fp> bool EcT<Fp>::verifyOrder_;
Expand Down
13 changes: 13 additions & 0 deletions test/ec_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,19 @@ struct Test {
mcl::ec::addCTProj(Q, Zero, Zero);
Ec::add(R, Zero, Zero);
CYBOZU_TEST_EQUAL(Q, R);
mcl::ec::addCTProj(Q, Q, Q);
Ec::add(R, R, R);
CYBOZU_TEST_EQUAL(Q, R);

// dbl
mcl::ec::dblCTProj(P, Q);
Ec::dbl(R, R);
CYBOZU_TEST_EQUAL(Q, R);
mcl::ec::dblCTProj(Q, Q);
Ec::dbl(R, R);
CYBOZU_TEST_EQUAL(Q, R);
mcl::ec::dblCTProj(Q, Zero);
CYBOZU_TEST_EQUAL(Q, Zero);
}
void ProjJacobi() const
{
Expand Down

0 comments on commit 3cf65bb

Please sign in to comment.