From 1dd68bcd47b053673a34cd83490d79d787da1585 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Sat, 11 Apr 2020 09:59:44 +0200 Subject: [PATCH] Avoid if, like upstream --- src/modules/ecdh/main_impl.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/modules/ecdh/main_impl.h b/src/modules/ecdh/main_impl.h index 5f09292..b635529 100644 --- a/src/modules/ecdh/main_impl.h +++ b/src/modules/ecdh/main_impl.h @@ -81,23 +81,23 @@ int secp256k1_ecdh_raw(const secp256k1_context* ctx, unsigned char *result, cons secp256k1_pubkey_load(ctx, &pt, point); secp256k1_scalar_set_b32(&s, scalar, &overflow); - if (overflow || secp256k1_scalar_is_zero(&s)) { - ret = 0; - } else { - secp256k1_ecmult_const(&res, &pt, &s); - secp256k1_ge_set_gej(&pt, &res); - /* Output the point in compressed form. - * Note we cannot use secp256k1_eckey_pubkey_serialize here since it does not - * expect its output to be secret and has a timing sidechannel. */ - secp256k1_fe_normalize(&pt.x); - secp256k1_fe_normalize(&pt.y); - result[0] = 0x02 | secp256k1_fe_is_odd(&pt.y); - secp256k1_fe_get_b32(&result[1], &pt.x); - ret = 1; - } + + overflow |= secp256k1_scalar_is_zero(&s); + secp256k1_scalar_cmov(&s, &secp256k1_scalar_one, overflow); + + secp256k1_ecmult_const(&res, &pt, &s, 256); + secp256k1_ge_set_gej(&pt, &res); + + /* Output the point in compressed form. + * Note we cannot use secp256k1_eckey_pubkey_serialize here since it does not + * expect its output to be secret and has a timing sidechannel. */ + secp256k1_fe_normalize(&pt.x); + secp256k1_fe_normalize(&pt.y); + result[0] = 0x02 | secp256k1_fe_is_odd(&pt.y); + secp256k1_fe_get_b32(&result[1], &pt.x); secp256k1_scalar_clear(&s); - return ret; + return !overflow; } #endif /* SECP256K1_MODULE_ECDH_MAIN_H */