diff --git a/src/bench_ecmult.c b/src/bench_ecmult.c index 12d550a6ae..76d8b29202 100644 --- a/src/bench_ecmult.c +++ b/src/bench_ecmult.c @@ -269,8 +269,8 @@ static void generate_scalar(const secp256k1_context *ctx, uint32_t num, secp256k c[8] = num >> 16; c[9] = num >> 24; secp256k1_sha256_initialize(&sha256); - secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &sha256, c, sizeof(c)); - secp256k1_sha256_finalize(secp256k1_get_hash_context(ctx), &sha256, buf); + secp256k1_sha256_write(&ctx->hash_ctx, &sha256, c, sizeof(c)); + secp256k1_sha256_finalize(&ctx->hash_ctx, &sha256, buf); secp256k1_scalar_set_b32(scalar, buf, &overflow); CHECK(!overflow); } diff --git a/src/bench_internal.c b/src/bench_internal.c index 453f3bb3f8..d1ee1a7824 100644 --- a/src/bench_internal.c +++ b/src/bench_internal.c @@ -359,7 +359,7 @@ static void bench_sha256(void* arg, int iters) { int i; bench_inv *data = (bench_inv*)arg; secp256k1_sha256 sha; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(data->ctx); + const secp256k1_hash_ctx *hash_ctx = &data->ctx->hash_ctx; for (i = 0; i < iters; i++) { secp256k1_sha256_initialize(&sha); @@ -372,7 +372,7 @@ static void bench_hmac_sha256(void* arg, int iters) { int i; bench_inv *data = (bench_inv*)arg; secp256k1_hmac_sha256 hmac; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(data->ctx); + const secp256k1_hash_ctx *hash_ctx = &data->ctx->hash_ctx; for (i = 0; i < iters; i++) { secp256k1_hmac_sha256_initialize(hash_ctx, &hmac, data->data, 32); @@ -385,7 +385,7 @@ static void bench_rfc6979_hmac_sha256(void* arg, int iters) { int i; bench_inv *data = (bench_inv*)arg; secp256k1_rfc6979_hmac_sha256 rng; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(data->ctx); + const secp256k1_hash_ctx *hash_ctx = &data->ctx->hash_ctx; for (i = 0; i < iters; i++) { secp256k1_rfc6979_hmac_sha256_initialize(hash_ctx, &rng, data->data, 64); diff --git a/src/modules/ecdh/main_impl.h b/src/modules/ecdh/main_impl.h index 699a0a8e72..bd16947fc8 100644 --- a/src/modules/ecdh/main_impl.h +++ b/src/modules/ecdh/main_impl.h @@ -25,7 +25,7 @@ static int ecdh_hash_function_sha256_impl(const secp256k1_hash_ctx *hash_ctx, un } static int ecdh_hash_function_sha256(unsigned char *output, const unsigned char *x32, const unsigned char *y32, void *data) { - return ecdh_hash_function_sha256_impl(secp256k1_get_hash_context(secp256k1_context_static), output, x32, y32, data); + return ecdh_hash_function_sha256_impl(&secp256k1_context_static->hash_ctx, output, x32, y32, data); } const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_sha256 = ecdh_hash_function_sha256; @@ -60,7 +60,7 @@ int secp256k1_ecdh(const secp256k1_context* ctx, unsigned char *output, const se if (hashfp == NULL || hashfp == secp256k1_ecdh_hash_function_sha256) { /* Use ctx-aware function by default */ - ret = ecdh_hash_function_sha256_impl(secp256k1_get_hash_context(ctx), output, x, y, data); + ret = ecdh_hash_function_sha256_impl(&ctx->hash_ctx, output, x, y, data); } else { ret = hashfp(output, x, y, data); } diff --git a/src/modules/ecdh/tests_impl.h b/src/modules/ecdh/tests_impl.h index c75ce9ff67..1333f057dc 100644 --- a/src/modules/ecdh/tests_impl.h +++ b/src/modules/ecdh/tests_impl.h @@ -84,8 +84,8 @@ static void test_ecdh_generator_basepoint(void) { /* compute "explicitly" */ CHECK(secp256k1_ec_pubkey_serialize(CTX, point_ser, &point_ser_len, &point[1], SECP256K1_EC_COMPRESSED) == 1); secp256k1_sha256_initialize(&sha); - secp256k1_sha256_write(secp256k1_get_hash_context(CTX), &sha, point_ser, point_ser_len); - secp256k1_sha256_finalize(secp256k1_get_hash_context(CTX), &sha, output_ser); + secp256k1_sha256_write(&CTX->hash_ctx, &sha, point_ser, point_ser_len); + secp256k1_sha256_finalize(&CTX->hash_ctx, &sha, output_ser); /* compare */ CHECK(secp256k1_memcmp_var(output_ecdh, output_ser, 32) == 0); } diff --git a/src/modules/ellswift/main_impl.h b/src/modules/ellswift/main_impl.h index d17530c695..8297f11f55 100644 --- a/src/modules/ellswift/main_impl.h +++ b/src/modules/ellswift/main_impl.h @@ -346,14 +346,14 @@ static void secp256k1_ellswift_xelligatorswift_var(const secp256k1_context *ctx, secp256k1_fe u; /* If the pool of branch values is empty, populate it. */ if (branches_left == 0) { - secp256k1_ellswift_prng(secp256k1_get_hash_context(ctx), branch_hash, hasher, cnt++); + secp256k1_ellswift_prng(&ctx->hash_ctx, branch_hash, hasher, cnt++); branches_left = 64; } /* Take a 3-bit branch value from the branch pool (top bit is discarded). */ --branches_left; branch = (branch_hash[branches_left >> 1] >> ((branches_left & 1) << 2)) & 7; /* Compute a new u value by hashing. */ - secp256k1_ellswift_prng(secp256k1_get_hash_context(ctx), u32, hasher, cnt++); + secp256k1_ellswift_prng(&ctx->hash_ctx, u32, hasher, cnt++); /* overflow is not a problem (we prefer uniform u32 over uniform u). */ secp256k1_fe_set_b32_mod(&u, u32); /* Since u is the output of a hash, it should practically never be 0. We could apply the @@ -406,8 +406,8 @@ int secp256k1_ellswift_encode(const secp256k1_context *ctx, unsigned char *ell64 * BIP340 tagged hash with tag "secp256k1_ellswift_encode". */ secp256k1_ellswift_sha256_init_encode(&hash); secp256k1_ge_serialize33(&p, p64); - secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &hash, p64, sizeof(p64)); - secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &hash, rnd32, 32); + secp256k1_sha256_write(&ctx->hash_ctx, &hash, p64, sizeof(p64)); + secp256k1_sha256_write(&ctx->hash_ctx, &hash, rnd32, 32); /* Compute ElligatorSwift encoding and construct output. */ secp256k1_ellswift_elligatorswift_var(ctx, ell64, &t, &p, &hash); /* puts u in ell64[0..32] */ @@ -452,11 +452,11 @@ int secp256k1_ellswift_create(const secp256k1_context *ctx, unsigned char *ell64 /* Set up hasher state. The used RNG is H(seckey32 || "\x00"*32 [|| auxrnd32] || cnt++), * using BIP340 tagged hash with tag "secp256k1_ellswift_create". */ secp256k1_ellswift_sha256_init_create(&hash); - secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &hash, seckey32, 32); - secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &hash, zero32, sizeof(zero32)); + secp256k1_sha256_write(&ctx->hash_ctx, &hash, seckey32, 32); + secp256k1_sha256_write(&ctx->hash_ctx, &hash, zero32, sizeof(zero32)); /* Declassify only hash state. seckey32 has been hashed, but copy remains in the hash buffer */ secp256k1_declassify(ctx, &hash.s, sizeof(hash.s)); - if (auxrnd32) secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &hash, auxrnd32, 32); + if (auxrnd32) secp256k1_sha256_write(&ctx->hash_ctx, &hash, auxrnd32, 32); /* Compute ElligatorSwift encoding and construct output. */ secp256k1_ellswift_elligatorswift_var(ctx, ell64, &t, &p, &hash); /* puts u in ell64[0..32] */ @@ -499,7 +499,7 @@ static int ellswift_xdh_hash_function_prefix_impl(const secp256k1_hash_ctx *hash } static int ellswift_xdh_hash_function_prefix(unsigned char *output, const unsigned char *x32, const unsigned char *ell_a64, const unsigned char *ell_b64, void *data) { - return ellswift_xdh_hash_function_prefix_impl(secp256k1_get_hash_context(secp256k1_context_static), output, x32, ell_a64, ell_b64, data); + return ellswift_xdh_hash_function_prefix_impl(&secp256k1_context_static->hash_ctx, output, x32, ell_a64, ell_b64, data); } /** Set hash state to the BIP340 tagged hash midstate for "bip324_ellswift_xonly_ecdh". */ @@ -527,7 +527,7 @@ static int ellswift_xdh_hash_function_bip324_impl(const secp256k1_hash_ctx *hash } static int ellswift_xdh_hash_function_bip324(unsigned char* output, const unsigned char *x32, const unsigned char *ell_a64, const unsigned char *ell_b64, void *data) { - return ellswift_xdh_hash_function_bip324_impl(secp256k1_get_hash_context(secp256k1_context_static), output, x32, ell_a64, ell_b64, data); + return ellswift_xdh_hash_function_bip324_impl(&secp256k1_context_static->hash_ctx, output, x32, ell_a64, ell_b64, data); } const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_prefix = ellswift_xdh_hash_function_prefix; @@ -565,9 +565,9 @@ int secp256k1_ellswift_xdh(const secp256k1_context *ctx, unsigned char *output, /* Invoke hasher. Use ctx-aware function by default */ if (hashfp == secp256k1_ellswift_xdh_hash_function_bip324) { - ret = ellswift_xdh_hash_function_bip324_impl(secp256k1_get_hash_context(ctx), output, sx, ell_a64, ell_b64, data); + ret = ellswift_xdh_hash_function_bip324_impl(&ctx->hash_ctx, output, sx, ell_a64, ell_b64, data); } else if (hashfp == secp256k1_ellswift_xdh_hash_function_prefix) { - ret = ellswift_xdh_hash_function_prefix_impl(secp256k1_get_hash_context(ctx), output, sx, ell_a64, ell_b64, data); + ret = ellswift_xdh_hash_function_prefix_impl(&ctx->hash_ctx, output, sx, ell_a64, ell_b64, data); } else { ret = hashfp(output, sx, ell_a64, ell_b64, data); } diff --git a/src/modules/ellswift/tests_impl.h b/src/modules/ellswift/tests_impl.h index 71788c515f..4e1d2b1e0a 100644 --- a/src/modules/ellswift/tests_impl.h +++ b/src/modules/ellswift/tests_impl.h @@ -473,7 +473,7 @@ void ellswift_xdh_ctx_sha256_tests(void) { /* Test hash initializers */ void ellswift_hash_init_tests(void) { secp256k1_sha256 sha_optimized; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; /* "secp256k1_ellswift_encode" */ static const unsigned char encode_tag[] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'e', 'n', 'c', 'o', 'd', 'e'}; /* "secp256k1_ellswift_create" */ diff --git a/src/modules/musig/keyagg_impl.h b/src/modules/musig/keyagg_impl.h index b4ffbd5e3c..144a19873e 100644 --- a/src/modules/musig/keyagg_impl.h +++ b/src/modules/musig/keyagg_impl.h @@ -82,9 +82,9 @@ static int secp256k1_musig_compute_pks_hash(const secp256k1_context *ctx, unsign return 0; } VERIFY_CHECK(ser_len == sizeof(ser)); - secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &sha, ser, sizeof(ser)); + secp256k1_sha256_write(&ctx->hash_ctx, &sha, ser, sizeof(ser)); } - secp256k1_sha256_finalize(secp256k1_get_hash_context(ctx), &sha, pks_hash); + secp256k1_sha256_finalize(&ctx->hash_ctx, &sha, pks_hash); return 1; } @@ -149,7 +149,7 @@ static int secp256k1_musig_pubkey_agg_callback(secp256k1_scalar *sc, secp256k1_g #else (void) ret; #endif - secp256k1_musig_keyaggcoef_internal(secp256k1_get_hash_context(ctx->ctx), sc, ctx->pks_hash, pt, &ctx->second_pk); + secp256k1_musig_keyaggcoef_internal(&ctx->ctx->hash_ctx, sc, ctx->pks_hash, pt, &ctx->second_pk); return 1; } diff --git a/src/modules/musig/session_impl.h b/src/modules/musig/session_impl.h index 4d1c5cf69b..e20eaacf1c 100644 --- a/src/modules/musig/session_impl.h +++ b/src/modules/musig/session_impl.h @@ -381,7 +381,7 @@ static int secp256k1_musig_nonce_gen_internal(const secp256k1_context* ctx, secp /* A pubkey cannot be the point at infinity */ secp256k1_ge_serialize33(&pk, pk_ser); - secp256k1_nonce_function_musig(secp256k1_get_hash_context(ctx), k, input_nonce, msg32, seckey, pk_ser, aggpk_ser_ptr, extra_input32); + secp256k1_nonce_function_musig(&ctx->hash_ctx, k, input_nonce, msg32, seckey, pk_ser, aggpk_ser_ptr, extra_input32); VERIFY_CHECK(!secp256k1_scalar_is_zero(&k[0])); VERIFY_CHECK(!secp256k1_scalar_is_zero(&k[1])); secp256k1_musig_secnonce_save(secnonce, k, &pk); @@ -542,7 +542,7 @@ static void secp256k1_musig_nonce_process_internal(const secp256k1_context *ctx, secp256k1_ge fin_nonce_pt; secp256k1_gej fin_nonce_ptj; - secp256k1_musig_compute_noncehash(secp256k1_get_hash_context(ctx), noncehash, aggnonce_pts, agg_pk32, msg); + secp256k1_musig_compute_noncehash(&ctx->hash_ctx, noncehash, aggnonce_pts, agg_pk32, msg); secp256k1_scalar_set_b32(b, noncehash, NULL); /* fin_nonce = aggnonce_pts[0] + b*aggnonce_pts[1] */ secp256k1_effective_nonce(&fin_nonce_ptj, aggnonce_pts, b); @@ -580,7 +580,7 @@ int secp256k1_musig_nonce_process(const secp256k1_context* ctx, secp256k1_musig_ } secp256k1_musig_nonce_process_internal(ctx, &session_i.fin_nonce_parity, fin_nonce, &session_i.noncecoef, aggnonce_pts, agg_pk32, msg32); - secp256k1_schnorrsig_challenge(secp256k1_get_hash_context(ctx), &session_i.challenge, fin_nonce, msg32, 32, agg_pk32); + secp256k1_schnorrsig_challenge(&ctx->hash_ctx, &session_i.challenge, fin_nonce, msg32, 32, agg_pk32); /* If there is a tweak then set `challenge` times `tweak` to the `s`-part.*/ secp256k1_scalar_set_int(&session_i.s_part, 0); @@ -650,7 +650,7 @@ int secp256k1_musig_partial_sign(const secp256k1_context* ctx, secp256k1_musig_p } /* Multiply KeyAgg coefficient */ - secp256k1_musig_keyaggcoef(secp256k1_get_hash_context(ctx), &mu, &cache_i, &pk); + secp256k1_musig_keyaggcoef(&ctx->hash_ctx, &mu, &cache_i, &pk); secp256k1_scalar_mul(&sk, &sk, &mu); if (!secp256k1_musig_session_load(ctx, &session_i, session)) { @@ -710,7 +710,7 @@ int secp256k1_musig_partial_sig_verify(const secp256k1_context* ctx, const secp2 /* Multiplying the challenge by the KeyAgg coefficient is equivalent * to multiplying the signer's public key by the coefficient, except * much easier to do. */ - secp256k1_musig_keyaggcoef(secp256k1_get_hash_context(ctx), &mu, &cache_i, &pkp); + secp256k1_musig_keyaggcoef(&ctx->hash_ctx, &mu, &cache_i, &pkp); secp256k1_scalar_mul(&e, &session_i.challenge, &mu); /* Negate e if secp256k1_fe_is_odd(&cache_i.pk.y)) XOR cache_i.parity_acc. diff --git a/src/modules/musig/tests_impl.h b/src/modules/musig/tests_impl.h index 7b54afdfe0..7349ba0481 100644 --- a/src/modules/musig/tests_impl.h +++ b/src/modules/musig/tests_impl.h @@ -526,7 +526,7 @@ static void musig_nonce_test(void) { int i, j; secp256k1_scalar k[6][2]; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; testrand_bytes_test(session_secrand, sizeof(session_secrand)); testrand_bytes_test(sk, sizeof(sk)); testrand_bytes_test(pk, sizeof(pk)); @@ -575,7 +575,7 @@ static void musig_nonce_test(void) { * state. */ static void sha256_tag_test(void) { secp256k1_sha256 sha; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; { /* "KeyAgg list" */ static const unsigned char tag[] = {'K', 'e', 'y', 'A', 'g', 'g', ' ', 'l', 'i', 's', 't'}; diff --git a/src/modules/schnorrsig/main_impl.h b/src/modules/schnorrsig/main_impl.h index 785bc4af37..6224d65c03 100644 --- a/src/modules/schnorrsig/main_impl.h +++ b/src/modules/schnorrsig/main_impl.h @@ -88,7 +88,7 @@ static int nonce_function_bip340_impl(const secp256k1_hash_ctx *hash_ctx, unsign } static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *msg, size_t msglen, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo, size_t algolen, void *data) { - return nonce_function_bip340_impl(secp256k1_get_hash_context(secp256k1_context_static), nonce32, msg, msglen, key32, xonly_pk32, algo, algolen, data); + return nonce_function_bip340_impl(&secp256k1_context_static->hash_ctx, nonce32, msg, msglen, key32, xonly_pk32, algo, algolen, data); } const secp256k1_nonce_function_hardened secp256k1_nonce_function_bip340 = nonce_function_bip340; @@ -150,7 +150,7 @@ static int secp256k1_schnorrsig_sign_internal(const secp256k1_context* ctx, unsi /* Compute nonce */ if (noncefp == NULL || noncefp == secp256k1_nonce_function_bip340) { /* Use context-aware nonce function by default */ - ret &= nonce_function_bip340_impl(secp256k1_get_hash_context(ctx), nonce32, msg, msglen, seckey, pk_buf, bip340_algo, sizeof(bip340_algo), ndata); + ret &= nonce_function_bip340_impl(&ctx->hash_ctx, nonce32, msg, msglen, seckey, pk_buf, bip340_algo, sizeof(bip340_algo), ndata); } else { ret &= !!noncefp(nonce32, msg, msglen, seckey, pk_buf, bip340_algo, sizeof(bip340_algo), ndata); } @@ -171,7 +171,7 @@ static int secp256k1_schnorrsig_sign_internal(const secp256k1_context* ctx, unsi secp256k1_fe_normalize_var(&r.x); secp256k1_fe_get_b32(&sig64[0], &r.x); - secp256k1_schnorrsig_challenge(secp256k1_get_hash_context(ctx), &e, &sig64[0], msg, msglen, pk_buf); + secp256k1_schnorrsig_challenge(&ctx->hash_ctx, &e, &sig64[0], msg, msglen, pk_buf); secp256k1_scalar_mul(&e, &e, &sk); secp256k1_scalar_add(&e, &e, &k); secp256k1_scalar_get_b32(&sig64[32], &e); @@ -236,7 +236,7 @@ int secp256k1_schnorrsig_verify(const secp256k1_context* ctx, const unsigned cha /* Compute e. */ secp256k1_fe_get_b32(buf, &pk.x); - secp256k1_schnorrsig_challenge(secp256k1_get_hash_context(ctx), &e, &sig64[0], msg, msglen, buf); + secp256k1_schnorrsig_challenge(&ctx->hash_ctx, &e, &sig64[0], msg, msglen, buf); /* Compute rj = s*G + (-e)*pkj */ secp256k1_scalar_negate(&e, &e); diff --git a/src/modules/schnorrsig/tests_exhaustive_impl.h b/src/modules/schnorrsig/tests_exhaustive_impl.h index 57efe348b1..fce34fee1b 100644 --- a/src/modules/schnorrsig/tests_exhaustive_impl.h +++ b/src/modules/schnorrsig/tests_exhaustive_impl.h @@ -105,7 +105,7 @@ static void test_exhaustive_schnorrsig_verify(const secp256k1_context *ctx, cons secp256k1_scalar e; unsigned char msg32[32]; testrand256(msg32); - secp256k1_schnorrsig_challenge(secp256k1_get_hash_context(ctx), &e, sig64, msg32, sizeof(msg32), pk32); + secp256k1_schnorrsig_challenge(&ctx->hash_ctx, &e, sig64, msg32, sizeof(msg32), pk32); /* Only do work if we hit a challenge we haven't tried before. */ if (!e_done[e]) { /* Iterate over the possible valid last 32 bytes in the signature. @@ -162,7 +162,7 @@ static void test_exhaustive_schnorrsig_sign(const secp256k1_context *ctx, unsign while (e_count_done < EXHAUSTIVE_TEST_ORDER) { secp256k1_scalar e; testrand256(msg32); - secp256k1_schnorrsig_challenge(secp256k1_get_hash_context(ctx), &e, xonly_pubkey_bytes[k - 1], msg32, sizeof(msg32), xonly_pubkey_bytes[d - 1]); + secp256k1_schnorrsig_challenge(&ctx->hash_ctx, &e, xonly_pubkey_bytes[k - 1], msg32, sizeof(msg32), xonly_pubkey_bytes[d - 1]); /* Only do work if we hit a challenge we haven't tried before. */ if (!e_done[e]) { secp256k1_scalar expected_s = (actual_k + e * actual_d) % EXHAUSTIVE_TEST_ORDER; diff --git a/src/modules/schnorrsig/tests_impl.h b/src/modules/schnorrsig/tests_impl.h index 12403c5bdf..d6b54a8ba9 100644 --- a/src/modules/schnorrsig/tests_impl.h +++ b/src/modules/schnorrsig/tests_impl.h @@ -38,7 +38,7 @@ static void run_nonce_function_bip340_tests(void) { unsigned char *args[5]; int i; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; /* Check that hash initialized by * secp256k1_nonce_function_bip340_sha256_tagged has the expected @@ -164,7 +164,7 @@ static void test_schnorrsig_sha256_tagged(void) { unsigned char tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'c', 'h', 'a', 'l', 'l', 'e', 'n', 'g', 'e'}; secp256k1_sha256 sha; secp256k1_sha256 sha_optimized; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; secp256k1_sha256_initialize_tagged(hash_ctx, &sha, (unsigned char *) tag, sizeof(tag)); secp256k1_schnorrsig_sha256_tagged(&sha_optimized); diff --git a/src/modules/silentpayments/main_impl.h b/src/modules/silentpayments/main_impl.h index 9b595c17d2..415f2d235e 100644 --- a/src/modules/silentpayments/main_impl.h +++ b/src/modules/silentpayments/main_impl.h @@ -116,7 +116,7 @@ static void secp256k1_silentpayments_sha256_init_sharedsecret(secp256k1_sha256* } static int secp256k1_silentpayments_create_output_tweak(const secp256k1_context *ctx, secp256k1_scalar *t_k_scalar, const unsigned char *shared_secret33, uint32_t k) { - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(ctx); + const secp256k1_hash_ctx *hash_ctx = &ctx->hash_ctx; secp256k1_sha256 hash; unsigned char hash_ser[32]; unsigned char k_serialized[4]; @@ -281,7 +281,7 @@ int secp256k1_silentpayments_sender_create_outputs( * curve order, which is statistically improbable. Returning an error here results in an untestable branch in the * code, but we do this anyways to ensure strict compliance with BIP0352. */ - if (!secp256k1_silentpayments_calculate_input_hash_scalar(secp256k1_get_hash_context(ctx), &input_hash_scalar, outpoint_smallest36, &prevouts_pubkey_sum_ge)) { + if (!secp256k1_silentpayments_calculate_input_hash_scalar(&ctx->hash_ctx, &input_hash_scalar, outpoint_smallest36, &prevouts_pubkey_sum_ge)) { secp256k1_scalar_clear(&seckey_sum_scalar); return 0; } @@ -410,10 +410,10 @@ int secp256k1_silentpayments_recipient_label_create(const secp256k1_context *ctx /* Compute hash(ser_256(b_scan) || ser_32(m)) [sha256 with tag "BIP0352/Label"] */ secp256k1_silentpayments_sha256_init_label(&hash); - secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &hash, scan_key32, 32); + secp256k1_sha256_write(&ctx->hash_ctx, &hash, scan_key32, 32); secp256k1_write_be32(m_serialized, m); - secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &hash, m_serialized, sizeof(m_serialized)); - secp256k1_sha256_finalize(secp256k1_get_hash_context(ctx), &hash, label_tweak32); + secp256k1_sha256_write(&ctx->hash_ctx, &hash, m_serialized, sizeof(m_serialized)); + secp256k1_sha256_finalize(&ctx->hash_ctx, &hash, label_tweak32); ret &= secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &label_tweak_scalar, &label_ge, label_tweak32); secp256k1_silentpayments_label_save(label, &label_ge); @@ -552,7 +552,7 @@ int secp256k1_silentpayments_recipient_prevouts_summary_create( * curve order, which is statistically improbable. Returning an error here results in an untestable branch in the * code, but we do this anyways to ensure strict compliance with BIP0352. */ - if (!secp256k1_silentpayments_calculate_input_hash_scalar(secp256k1_get_hash_context(ctx), &input_hash_scalar, outpoint_smallest36, &prevouts_pubkey_sum_ge)) { + if (!secp256k1_silentpayments_calculate_input_hash_scalar(&ctx->hash_ctx, &input_hash_scalar, outpoint_smallest36, &prevouts_pubkey_sum_ge)) { return 0; } memcpy(&prevouts_summary->data[0], secp256k1_silentpayments_prevouts_summary_magic, 4); diff --git a/src/secp256k1.c b/src/secp256k1.c index 7c14b7dcec..cf138625aa 100644 --- a/src/secp256k1.c +++ b/src/secp256k1.c @@ -234,10 +234,6 @@ void secp256k1_context_set_sha256_compression(secp256k1_context *ctx, secp256k1_ ctx->hash_ctx.fn_sha256_compression = fn_compression; } -static SECP256K1_INLINE const secp256k1_hash_ctx* secp256k1_get_hash_context(const secp256k1_context *ctx) { - return &ctx->hash_ctx; -} - static secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) { VERIFY_CHECK(ctx != NULL); return secp256k1_scratch_create(&ctx->error_callback, max_size); @@ -532,7 +528,7 @@ static int nonce_function_rfc6979_impl(const secp256k1_hash_ctx *hash_ctx, unsig } static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) { - return nonce_function_rfc6979_impl(secp256k1_get_hash_context(secp256k1_context_static), nonce32, msg32, key32, algo16, data, counter); + return nonce_function_rfc6979_impl(&secp256k1_context_static->hash_ctx, nonce32, msg32, key32, algo16, data, counter); } const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979; @@ -560,7 +556,7 @@ static int secp256k1_ecdsa_sign_inner(const secp256k1_context* ctx, secp256k1_sc if (noncefp == NULL || noncefp == secp256k1_nonce_function_rfc6979) { /* Use ctx-aware function by default */ - ret = nonce_function_rfc6979_impl(secp256k1_get_hash_context(ctx), nonce32, msg32, seckey, NULL, (void*)noncedata, count); + ret = nonce_function_rfc6979_impl(&ctx->hash_ctx, nonce32, msg32, seckey, NULL, (void*)noncedata, count); } else { ret = !!noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count); } @@ -781,7 +777,7 @@ int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *see ARG_CHECK(secp256k1_context_is_proper(ctx)); if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) { - secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, secp256k1_get_hash_context(ctx), seed32); + secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, &ctx->hash_ctx, seed32); } return 1; } @@ -819,9 +815,9 @@ int secp256k1_tagged_sha256(const secp256k1_context* ctx, unsigned char *hash32, ARG_CHECK(tag != NULL); ARG_CHECK(msg != NULL); - secp256k1_sha256_initialize_tagged(secp256k1_get_hash_context(ctx), &sha, tag, taglen); - secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &sha, msg, msglen); - secp256k1_sha256_finalize(secp256k1_get_hash_context(ctx), &sha, hash32); + secp256k1_sha256_initialize_tagged(&ctx->hash_ctx, &sha, tag, taglen); + secp256k1_sha256_write(&ctx->hash_ctx, &sha, msg, msglen); + secp256k1_sha256_finalize(&ctx->hash_ctx, &sha, hash32); secp256k1_sha256_clear(&sha); return 1; } diff --git a/src/testrand_impl.h b/src/testrand_impl.h index 58e71e1071..5d7976f163 100644 --- a/src/testrand_impl.h +++ b/src/testrand_impl.h @@ -22,7 +22,7 @@ SECP256K1_INLINE static void testrand_seed(const unsigned char *seed16) { unsigned char out32[32]; secp256k1_sha256 hash; int i; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(secp256k1_context_static); + const secp256k1_hash_ctx *hash_ctx = &secp256k1_context_static->hash_ctx; /* Use SHA256(PREFIX || seed16) as initial state. */ secp256k1_sha256_initialize(&hash); diff --git a/src/tests.c b/src/tests.c index 13ef295670..da471980ad 100644 --- a/src/tests.c +++ b/src/tests.c @@ -480,8 +480,8 @@ static void run_plug_sha256_compression_tests(void) { /* 5) A hash operation should invoke the installed callback */ own_transform_called = 0; secp256k1_sha256_initialize(&sha); - secp256k1_sha256_write(secp256k1_get_hash_context(ctx), &sha, (const unsigned char*)"a", 1); - secp256k1_sha256_finalize(secp256k1_get_hash_context(ctx), &sha, sha_out); + secp256k1_sha256_write(&ctx->hash_ctx, &sha, (const unsigned char*)"a", 1); + secp256k1_sha256_finalize(&ctx->hash_ctx, &sha, sha_out); CHECK(own_transform_called); /* 6) Unset sha256 and verify the default one is set again */ @@ -680,7 +680,7 @@ static void run_ctz_tests(void) { /***** HASH TESTS *****/ static void run_sha256_known_output_tests(void) { - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; static const char *inputs[] = { "", "abc", "message digest", "secure hash algorithm", "SHA256 is considered to be safe", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", @@ -829,7 +829,7 @@ static void run_sha256_counter_tests(void) { {0x2c, 0xf3, 0xa9, 0xf6, 0x15, 0x25, 0x80, 0x70, 0x76, 0x99, 0x7d, 0xf1, 0xc3, 0x2f, 0xa3, 0x31, 0xff, 0x92, 0x35, 0x2e, 0x8d, 0x04, 0x13, 0x33, 0xd8, 0x0d, 0xdb, 0x4a, 0xf6, 0x8c, 0x03, 0x34}, {0xec, 0x12, 0x24, 0x9f, 0x35, 0xa4, 0x29, 0x8b, 0x9e, 0x4a, 0x95, 0xf8, 0x61, 0xaf, 0x61, 0xc5, 0x66, 0x55, 0x3e, 0x3f, 0x2a, 0x98, 0xea, 0x71, 0x16, 0x6b, 0x1c, 0xd9, 0xe4, 0x09, 0xd2, 0x8e}, }; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; unsigned int i; for (i = 0; i < ARRAY_SIZE(midstates); i++) { unsigned char out[32]; @@ -884,7 +884,7 @@ static void run_hmac_sha256_tests(void) { {0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63, 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93, 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2} }; int i; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; for (i = 0; i < 6; i++) { secp256k1_hmac_sha256 hasher; unsigned char out[32]; @@ -918,7 +918,7 @@ static void run_rfc6979_hmac_sha256_tests(void) { {0x75, 0x97, 0x88, 0x7c, 0xbd, 0x76, 0x32, 0x1f, 0x32, 0xe3, 0x04, 0x40, 0x67, 0x9a, 0x22, 0xcf, 0x7f, 0x8d, 0x9d, 0x2e, 0xac, 0x39, 0x0e, 0x58, 0x1f, 0xea, 0x09, 0x1c, 0xe2, 0x02, 0xba, 0x94} }; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; secp256k1_rfc6979_hmac_sha256 rng; unsigned char out[32]; int i; @@ -976,7 +976,7 @@ static void run_sha256_initialize_midstate_tests(void) { 0xa9ec59eaul, 0x9b4c2ffful, 0x400821e2ul, 0x0dcf3847ul, 0xbe7ea179ul, 0xa5772bdcul, 0x7d29bfe3ul, 0xa486b855ul }; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; secp256k1_sha256 sha; secp256k1_sha256_initialize_midstate(&sha, 64, midstate); @@ -5750,11 +5750,11 @@ static void test_ecmult_accumulate(secp256k1_sha256* acc, const secp256k1_scalar if (secp256k1_ge_is_infinity(&r)) { /* Store infinity as 0x00 */ const unsigned char zerobyte[1] = {0}; - secp256k1_sha256_write(secp256k1_get_hash_context(CTX), acc, zerobyte, 1); + secp256k1_sha256_write(&CTX->hash_ctx, acc, zerobyte, 1); } else { /* Store other points using their uncompressed serialization. */ secp256k1_ge_serialize65(&r, bytes); - secp256k1_sha256_write(secp256k1_get_hash_context(CTX), acc, bytes, sizeof(bytes)); + secp256k1_sha256_write(&CTX->hash_ctx, acc, bytes, sizeof(bytes)); } } @@ -5796,7 +5796,7 @@ static void test_ecmult_constants_2bit(void) { test_ecmult_accumulate(&acc, &x, scratch); } } - secp256k1_sha256_finalize(secp256k1_get_hash_context(CTX), &acc, b32); + secp256k1_sha256_finalize(&CTX->hash_ctx, &acc, b32); CHECK(secp256k1_memcmp_var(b32, expected32, 32) == 0); secp256k1_scratch_space_destroy(CTX, scratch); @@ -5815,7 +5815,7 @@ static void test_ecmult_constants_sha(uint32_t prefix, size_t iter, const unsign unsigned char b32[32]; unsigned char inp[6]; size_t i; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; secp256k1_scratch_space *scratch = secp256k1_scratch_space_create(CTX, 65536); inp[0] = prefix & 0xFF; @@ -5913,7 +5913,7 @@ static void test_ecmult_gen_blind(void) { testrand256(seed32); b = CTX->ecmult_gen_ctx.scalar_offset; p = CTX->ecmult_gen_ctx.ge_offset; - secp256k1_ecmult_gen_blind(&CTX->ecmult_gen_ctx, secp256k1_get_hash_context(CTX), seed32); + secp256k1_ecmult_gen_blind(&CTX->ecmult_gen_ctx, &CTX->hash_ctx, seed32); CHECK(!secp256k1_scalar_eq(&b, &CTX->ecmult_gen_ctx.scalar_offset)); secp256k1_ecmult_gen_gej(&CTX->ecmult_gen_ctx, &pgej2, &key); CHECK(!gej_xyz_equals_gej(&pgej, &pgej2)); @@ -5926,10 +5926,10 @@ static void test_ecmult_gen_blind_reset(void) { /* Test ecmult_gen() blinding reset and confirm that the blinding is consistent. */ secp256k1_scalar b; secp256k1_ge p1, p2; - secp256k1_ecmult_gen_blind(&CTX->ecmult_gen_ctx, secp256k1_get_hash_context(CTX), 0); + secp256k1_ecmult_gen_blind(&CTX->ecmult_gen_ctx, &CTX->hash_ctx, 0); b = CTX->ecmult_gen_ctx.scalar_offset; p1 = CTX->ecmult_gen_ctx.ge_offset; - secp256k1_ecmult_gen_blind(&CTX->ecmult_gen_ctx, secp256k1_get_hash_context(CTX), 0); + secp256k1_ecmult_gen_blind(&CTX->ecmult_gen_ctx, &CTX->hash_ctx, 0); CHECK(secp256k1_scalar_eq(&b, &CTX->ecmult_gen_ctx.scalar_offset)); p2 = CTX->ecmult_gen_ctx.ge_offset; CHECK(secp256k1_ge_eq_var(&p1, &p2)); @@ -7749,7 +7749,7 @@ static void test_ecdsa_wycheproof(void) { #include "wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.h" int t; - const secp256k1_hash_ctx *hash_ctx = secp256k1_get_hash_context(CTX); + const secp256k1_hash_ctx *hash_ctx = &CTX->hash_ctx; for (t = 0; t < SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS; t++) { secp256k1_ecdsa_signature signature; secp256k1_sha256 hasher;