Skip to content

Commit f4aba16

Browse files
committed
Add ElligatorSwift ctime tests
1 parent f75ada4 commit f4aba16

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/modules/ellswift/main_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ int secp256k1_ellswift_create(const secp256k1_context* ctx, unsigned char *ell64
354354

355355
/* Compute (affine) public key */
356356
ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &seckey_scalar, &p, seckey32);
357+
secp256k1_declassify(ctx, &p, sizeof(p)); /* not constant time in produced pubkey */
357358
secp256k1_fe_normalize_var(&p.x);
358359
secp256k1_fe_normalize_var(&p.y);
359360

@@ -363,6 +364,7 @@ int secp256k1_ellswift_create(const secp256k1_context* ctx, unsigned char *ell64
363364
secp256k1_sha256_write(&hash, seckey32, 32);
364365
secp256k1_sha256_write(&hash, rnd32 ? rnd32 : ZERO, 32);
365366
secp256k1_sha256_write(&hash, ZERO, 32 - 9 - 4);
367+
secp256k1_declassify(ctx, &hash, sizeof(hash)); /* hasher gets to declassify private key */
366368

367369
/* Compute ElligatorSwift encoding and construct output. */
368370
secp256k1_ellswift_elligatorswift_var(&u, &t, &p, &hash);

src/valgrind_ctime_test.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#include "../include/secp256k1_schnorrsig.h"
2828
#endif
2929

30+
#ifdef ENABLE_MODULE_ELLSWIFT
31+
#include "../include/secp256k1_ellswift.h"
32+
#endif
33+
3034
void run_tests(secp256k1_context *ctx, unsigned char *key);
3135

3236
int main(void) {
@@ -77,6 +81,9 @@ void run_tests(secp256k1_context *ctx, unsigned char *key) {
7781
#ifdef ENABLE_MODULE_EXTRAKEYS
7882
secp256k1_keypair keypair;
7983
#endif
84+
#ifdef ENABLE_MODULE_ELLSWIFT
85+
unsigned char ellswift[64];
86+
#endif
8087

8188
for (i = 0; i < 32; i++) {
8289
msg[i] = i + 1;
@@ -168,4 +175,22 @@ void run_tests(secp256k1_context *ctx, unsigned char *key) {
168175
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
169176
CHECK(ret == 1);
170177
#endif
178+
179+
#ifdef ENABLE_MODULE_ELLSWIFT
180+
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
181+
ret = secp256k1_ellswift_create(ctx, ellswift, key, NULL);
182+
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
183+
CHECK(ret == 1);
184+
185+
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
186+
ret = secp256k1_ellswift_create(ctx, ellswift, key, key);
187+
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
188+
CHECK(ret == 1);
189+
190+
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
191+
VALGRIND_MAKE_MEM_DEFINED(&ellswift, sizeof(ellswift));
192+
ret = secp256k1_ellswift_xdh(ctx, msg, ellswift, ellswift, key, NULL, NULL);
193+
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
194+
CHECK(ret == 1);
195+
#endif
171196
}

0 commit comments

Comments
 (0)