Skip to content

Commit 6b4e4c8

Browse files
committed
f add magic to s2c context
1 parent 6acc03b commit 6b4e4c8

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

include/secp256k1.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@ typedef struct {
9292
*
9393
* The exact representation of data inside is implementation defined and not
9494
* guaranteed to be portable between different platforms or versions. It is however
95-
* guaranteed to be 128 bytes in size, and can be safely copied/moved.
95+
* guaranteed to be 136 bytes in size, and can be safely copied/moved.
9696
*/
9797
typedef struct {
98+
/* magic is set during initialization. It allows functions casting to
99+
* s2c_commit_contexts from a void pointer to check if they actually got an
100+
* s2c_commit_context and if it has been initialized. */
101+
unsigned char magic[8];
98102
unsigned char data[32];
99103
unsigned char data_hash[32];
100104
secp256k1_pubkey original_pubnonce;

src/secp256k1.c

+3
Original file line numberDiff line numberDiff line change
@@ -686,13 +686,15 @@ static int secp256k1_ec_commit_verify(const secp256k1_context* ctx, const secp25
686686
return secp256k1_gej_is_infinity(&pj);
687687
}
688688

689+
static uint64_t s2c_commit_context_magic = 0xd5bafd089f7e1c63;
689690
int secp256k1_s2c_commit_context_create(secp256k1_context *ctx, secp256k1_s2c_commit_context *s2c_ctx, const unsigned char *data32) {
690691
secp256k1_sha256 sha;
691692

692693
VERIFY_CHECK(ctx != NULL);
693694
ARG_CHECK(s2c_ctx != NULL);
694695
ARG_CHECK(data32 != NULL);
695696

697+
memcpy(s2c_ctx->magic, &s2c_commit_context_magic, sizeof(s2c_ctx->magic));
696698
memcpy(s2c_ctx->data, data32, 32);
697699
secp256k1_sha256_initialize(&sha);
698700
secp256k1_sha256_write(&sha, data32, 32);
@@ -733,6 +735,7 @@ static int secp256k1_nonce_function_bipschnorr_no_s2c_tweak(const secp256k1_cont
733735
} else {
734736
/* Prepare for a sign-to-contract commitment if data is provided */
735737
secp256k1_s2c_commit_context *s2c_ctx = (secp256k1_s2c_commit_context *)data;
738+
ARG_CHECK(memcmp(s2c_ctx->magic, &s2c_commit_context_magic, sizeof(s2c_ctx->magic)) == 0);
736739
secp256k1_sha256_write(&sha, s2c_ctx->data_hash, 32);
737740
secp256k1_sha256_finalize(&sha, nonce32);
738741

src/tests.c

+7
Original file line numberDiff line numberDiff line change
@@ -4112,9 +4112,12 @@ void test_nonce_function_bipschnorr_s2c(void) {
41124112
unsigned char algo16[16];
41134113
unsigned char data32[32];
41144114
secp256k1_s2c_commit_context s2c_ctx;
4115+
secp256k1_s2c_commit_context s2c_ctx_2;
41154116
secp256k1_pubkey pubnonce;
41164117
secp256k1_pubkey original_nonce;
4118+
int32_t ecount = 0;
41174119

4120+
secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount);
41184121
secp256k1_rand256(msg32);
41194122
secp256k1_rand256(key32);
41204123
secp256k1_rand256(data32);
@@ -4125,6 +4128,10 @@ void test_nonce_function_bipschnorr_s2c(void) {
41254128
CHECK(secp256k1_s2c_commit_get_original_nonce(ctx, &original_nonce, &s2c_ctx) == 1);
41264129
CHECK(secp256k1_ec_pubkey_create(ctx, &pubnonce, nonce32) == 1);
41274130
CHECK(secp256k1_ec_commit_verify(ctx, &pubnonce, &original_nonce, data32, 32) == 1);
4131+
4132+
CHECK(ecount == 0);
4133+
CHECK(secp256k1_nonce_function_bipschnorr(ctx, nonce32, msg32, key32, algo16, &s2c_ctx_2, 0) == 0);
4134+
CHECK(ecount == 1);
41284135
}
41294136

41304137
void run_nonce_function_bipschnorr_tests(void) {

0 commit comments

Comments
 (0)