Skip to content

Commit 031361e

Browse files
committed
Optimize EC point addition in homogeneous & affine coordinates
1 parent bb327c5 commit 031361e

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

include/ack/ec.hpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ namespace ack {
485485
// Calculate new x and y
486486
auto x3 = s.sqr() - x - a.x;
487487
auto y3 = s * ( x - x3 ) - y;
488-
return ec_point_fp( this->curve(), x3, y3 );
488+
return ec_point_fp( this->curve(), std::move( x3 ), std::move( y3 ) );
489489
}
490490

491491
/**
@@ -501,13 +501,12 @@ namespace ack {
501501
}
502502

503503
// Calculate tangent slope
504-
auto x_sqr = x.sqr();
505-
auto s = ( x_sqr + x_sqr + x_sqr + this->curve().a ) / ( y + y ) ;
504+
const auto s = ( 3 * x.sqr() + this->curve().a ) / ( 2 * y ) ;
506505

507506
// Calculate new x and y
508-
auto x2 = s.sqr() - x - x;
507+
auto x2 = s.sqr() - 2 * x;
509508
auto y2 = s * ( x - x2 ) - y;
510-
return ec_point_fp( this->curve(), x2, y2 );
509+
return ec_point_fp( this->curve(), std::move( x2 ), std::move( y2 ) );
511510
}
512511

513512
/**
@@ -792,10 +791,10 @@ namespace ack {
792791
return p;
793792
}
794793

795-
auto t0 = p.y * q.z;
796-
auto t1 = q.y * p.z;
797-
auto u0 = p.x * q.z;
798-
auto u1 = q.x * p.z;
794+
const auto t0 = p.y * q.z;
795+
const auto t1 = q.y * p.z;
796+
const auto u0 = p.x * q.z;
797+
const auto u1 = q.x * p.z;
799798
if ( u0 == u1 ) {
800799
if ( t0 == t1 ) {
801800
return doubled();
@@ -805,14 +804,14 @@ namespace ack {
805804
}
806805

807806
// Note: Wrapping the following code in 3 lambdas
808-
// can make a little bit faster execution time (few 10s of us)
809-
auto t = t0 - t1;
810-
auto u = u0 - u1;
811-
auto u2 = u.sqr();
812-
auto u3 = u * u2;
807+
// can make slightly faster execution time (few 10s of us)
808+
const auto t = t0 - t1;
809+
const auto u = u0 - u1;
810+
const auto u2 = u.sqr();
811+
const auto u3 = u * u2;
813812

814-
auto v = p.z * q.z;
815-
auto w = t * t * v - u2 * ( u0 + u1 );
813+
const auto v = p.z * q.z;
814+
const auto w = t.sqr() * v - u2 * ( u0 + u1 );
816815

817816
auto rx = u * w;
818817
auto ry = t * ( u0 * u2 - w ) - t0 * u3;
@@ -838,13 +837,14 @@ namespace ack {
838837
}
839838

840839
auto t = p.x.sqr() * 3 + this->curve().a * p.z.sqr();
841-
auto u = p.y * p.z * 2;
842-
auto v = u * p.x * p.y * 2;
843-
auto w = t.sqr() - v * 2;
840+
const auto dy = 2 * p.y;
841+
const auto u = dy * p.z;
842+
const auto v = u * p.x * dy;
843+
const auto w = t.sqr() - v * 2;
844844

845845
auto rx = u * w;
846846

847-
auto u2 = u.sqr();
847+
const auto u2 = u.sqr();
848848
auto ry = t * ( v - w ) - u2 * p.y.sqr() * 2;
849849

850850
auto rz = u2 * u;
@@ -1365,7 +1365,7 @@ namespace ack {
13651365
__attribute__((always_inline))
13661366
static ec_point_fp_jacobi add_ne(const ec_point_fp_jacobi& p, const ec_point_fp_jacobi& q)
13671367
{
1368-
// This extra function, although inlined, produces a little bit more efficient code than
1368+
// This extra function, although inlined, produces slightly more efficient code than
13691369
// it would if put directly into the calling scope.
13701370
const auto pz2 = p.z.sqr();
13711371
const auto qz2 = q.z.sqr();

0 commit comments

Comments
 (0)