Skip to content

Commit

Permalink
Add EC point add/dbl to speed.cc (#1545)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkostic committed Apr 24, 2024
1 parent d7ddfc4 commit 0f3548a
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions tool/speed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ static bool SpeedEvpEcdh(const std::string &selected) {
return SpeedEvpEcdhCurve("EVP ECDH X25519", NID_X25519, selected);
}

static bool SpeedECMULCurve(const std::string &name, int nid,
static bool SpeedECPOINTCurve(const std::string &name, int nid,
const std::string &selected) {
if (!selected.empty() && name.find(selected) == std::string::npos) {
return true;
Expand All @@ -1624,6 +1624,7 @@ static bool SpeedECMULCurve(const std::string &name, int nid,
BM_NAMESPACE::UniquePtr<BIGNUM> scalar0(BN_new());
BM_NAMESPACE::UniquePtr<BIGNUM> scalar1(BN_new());
BM_NAMESPACE::UniquePtr<EC_POINT> pin0(EC_POINT_new(group.get()));
BM_NAMESPACE::UniquePtr<EC_POINT> pin1(EC_POINT_new(group.get()));
BM_NAMESPACE::UniquePtr<EC_POINT> pout(EC_POINT_new(group.get()));


Expand All @@ -1633,11 +1634,36 @@ static bool SpeedECMULCurve(const std::string &name, int nid,
return false;
}

// Generate one random EC point.
// Generate two random EC point.
EC_POINT_mul(group.get(), pin0.get(), scalar0.get(), nullptr, nullptr, ctx.get());
EC_POINT_mul(group.get(), pin1.get(), scalar1.get(), nullptr, nullptr, ctx.get());

TimeResults results;

// Measure point doubling.
if (!TimeFunction(&results, [&group, &pout, &ctx, &pin0]() -> bool {
if (!EC_POINT_dbl(group.get(), pout.get(), pin0.get(), ctx.get())) {
return false;
}

return true;
})) {
return false;
}
results.Print(name + " dbl");

// Measure point addition.
if (!TimeFunction(&results, [&group, &pout, &ctx, &pin0, &pin1]() -> bool {
if (!EC_POINT_add(group.get(), pout.get(), pin0.get(), pin1.get(), ctx.get())) {
return false;
}

return true;
})) {
return false;
}
results.Print(name + " add");

// Measure scalar multiplication of an arbitrary curve point.
if (!TimeFunction(&results, [&group, &pout, &ctx, &pin0, &scalar0]() -> bool {
if (!EC_POINT_mul(group.get(), pout.get(), nullptr, pin0.get(), scalar0.get(), ctx.get())) {
Expand Down Expand Up @@ -1677,10 +1703,10 @@ static bool SpeedECMULCurve(const std::string &name, int nid,
return true;
}

static bool SpeedECMUL(const std::string &selected) {
static bool SpeedECPOINT(const std::string &selected) {
for (const auto& config : supported_curves) {
std::string message = "ECMUL " + config.name;
if(!SpeedECMULCurve(message, config.nid, selected)) {
std::string message = "EC POINT " + config.name;
if(!SpeedECPOINTCurve(message, config.nid, selected)) {
return false;
}
}
Expand Down Expand Up @@ -2699,7 +2725,7 @@ bool Speed(const std::vector<std::string> &args) {
// OpenSSL 1.0.2 is missing functions e.g. |EVP_PKEY_get0_EC_KEY| and
// doesn't implement X255519 either.
!SpeedEvpEcdh(selected) ||
!SpeedECMUL(selected) ||
!SpeedECPOINT(selected) ||
// OpenSSL 1.0 doesn't support Scrypt
!SpeedScrypt(selected) ||
#endif
Expand Down

0 comments on commit 0f3548a

Please sign in to comment.