Skip to content

Commit

Permalink
Add ctime tests for ellswift module
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Jun 20, 2023
1 parent df633cd commit 2d1d41a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/ctime_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#include "../include/secp256k1_schnorrsig.h"
#endif

#ifdef ENABLE_MODULE_ELLSWIFT
#include "../include/secp256k1_ellswift.h"
#endif

static void run_tests(secp256k1_context *ctx, unsigned char *key);

int main(void) {
Expand Down Expand Up @@ -80,6 +84,10 @@ static void run_tests(secp256k1_context *ctx, unsigned char *key) {
#ifdef ENABLE_MODULE_EXTRAKEYS
secp256k1_keypair keypair;
#endif
#ifdef ENABLE_MODULE_ELLSWIFT
unsigned char ellswift[64];
static const unsigned char prefix[64] = {'t', 'e', 's', 't'};
#endif

for (i = 0; i < 32; i++) {
msg[i] = i + 1;
Expand Down Expand Up @@ -171,4 +179,31 @@ static void run_tests(secp256k1_context *ctx, unsigned char *key) {
SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
CHECK(ret == 1);
#endif

#ifdef ENABLE_MODULE_ELLSWIFT
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
ret = secp256k1_ellswift_create(ctx, ellswift, key, NULL);
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
CHECK(ret == 1);

VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
ret = secp256k1_ellswift_create(ctx, ellswift, key, ellswift);
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
CHECK(ret == 1);

for (i = 0; i < 2; i++) {
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
VALGRIND_MAKE_MEM_DEFINED(&ellswift, sizeof(ellswift));
ret = secp256k1_ellswift_xdh(ctx, msg, ellswift, ellswift, key, i, secp256k1_ellswift_xdh_hash_function_bip324, NULL);
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
CHECK(ret == 1);

VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
VALGRIND_MAKE_MEM_DEFINED(&ellswift, sizeof(ellswift));
ret = secp256k1_ellswift_xdh(ctx, msg, ellswift, ellswift, key, i, secp256k1_ellswift_xdh_hash_function_prefix, (void *)prefix);
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
CHECK(ret == 1);
}

#endif
}
2 changes: 2 additions & 0 deletions src/modules/ellswift/main_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ int secp256k1_ellswift_create(const secp256k1_context *ctx, unsigned char *ell64

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

Expand All @@ -471,6 +472,7 @@ int secp256k1_ellswift_create(const secp256k1_context *ctx, unsigned char *ell64
secp256k1_ellswift_sha256_init_create(&hash);
secp256k1_sha256_write(&hash, seckey32, 32);
secp256k1_sha256_write(&hash, zero32, sizeof(zero32));
secp256k1_declassify(ctx, &hash, sizeof(hash)); /* private key is hashed now */
if (auxrnd32) secp256k1_sha256_write(&hash, auxrnd32, 32);

/* Compute ElligatorSwift encoding and construct output. */
Expand Down

0 comments on commit 2d1d41a

Please sign in to comment.