|
| 1 | +/********************************************************************** |
| 2 | + * Copyright (c) 2019 Marko Bencun, Jonas Nick * |
| 3 | + * Distributed under the MIT software license, see the accompanying * |
| 4 | + * file COPYING or http://www.opensource.org/licenses/mit-license.php.* |
| 5 | + **********************************************************************/ |
| 6 | + |
| 7 | +#ifndef SECP256K1_MODULE_ECDSA_SIGN_TO_CONTRACT_MAIN_H |
| 8 | +#define SECP256K1_MODULE_ECDSA_SIGN_TO_CONTRACT_MAIN_H |
| 9 | + |
| 10 | +#include "include/secp256k1_ecdsa_sign_to_contract.h" |
| 11 | + |
| 12 | +int secp256k1_ecdsa_s2c_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *signature, secp256k1_s2c_opening *s2c_opening, const unsigned char *msg32, const unsigned char *seckey, const unsigned char* s2c_data32, secp256k1_nonce_function noncefp, const void* noncedata) { |
| 13 | + secp256k1_scalar r, s; |
| 14 | + secp256k1_scalar sec, non, msg; |
| 15 | + secp256k1_sha256 sha; |
| 16 | + int ret = 0; |
| 17 | + int overflow = 0; |
| 18 | + int is_zero = 0; |
| 19 | + unsigned char ndata[32]; |
| 20 | + VERIFY_CHECK(ctx != NULL); |
| 21 | + ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)); |
| 22 | + ARG_CHECK(msg32 != NULL); |
| 23 | + ARG_CHECK(signature != NULL); |
| 24 | + ARG_CHECK(seckey != NULL); |
| 25 | + if (noncefp == NULL) { |
| 26 | + noncefp = secp256k1_nonce_function_default; |
| 27 | + } |
| 28 | + /* sign-to-contract commitments only work with the default nonce function, |
| 29 | + * because we need to ensure that s2c_data is actually hashed into the nonce and |
| 30 | + * not just ignored. */ |
| 31 | + ARG_CHECK(s2c_data32 == NULL || noncefp == secp256k1_nonce_function_default); |
| 32 | + /* s2c_opening and s2c_data32 should be either both non-NULL or both NULL. */ |
| 33 | + ARG_CHECK((s2c_opening != NULL) == (s2c_data32 != NULL)); |
| 34 | + |
| 35 | + if (s2c_opening != NULL) { |
| 36 | + secp256k1_s2c_opening_init(s2c_opening); |
| 37 | + } |
| 38 | + |
| 39 | + if(s2c_data32 != NULL) { |
| 40 | + /* Provide s2c_data32 and ndata (if not NULL) to the the nonce function |
| 41 | + * as additional data to derive the nonce from. If both pointers are |
| 42 | + * not NULL, they need to be hashed to get the nonce data 32 bytes. |
| 43 | + * Even if only s2c_data32 is not NULL, it's hashed because it should |
| 44 | + * be possible to derive nonces even if only a SHA256 commitment to the |
| 45 | + * data is known. This is for example important in the |
| 46 | + * anti-nonce-sidechannel protocol. |
| 47 | + */ |
| 48 | + secp256k1_sha256_initialize(&sha); |
| 49 | + secp256k1_sha256_write(&sha, s2c_data32, 32); |
| 50 | + if (noncedata != NULL) { |
| 51 | + secp256k1_sha256_write(&sha, noncedata, 32); |
| 52 | + } |
| 53 | + secp256k1_sha256_finalize(&sha, ndata); |
| 54 | + noncedata = &ndata; |
| 55 | + } |
| 56 | + |
| 57 | + secp256k1_scalar_set_b32(&sec, seckey, &overflow); |
| 58 | + /* Fail if the secret key is invalid. */ |
| 59 | + if (!overflow && !secp256k1_scalar_is_zero(&sec)) { |
| 60 | + unsigned char nonce32[32]; |
| 61 | + unsigned int count = 0; |
| 62 | + secp256k1_scalar_set_b32(&msg, msg32, NULL); |
| 63 | + while (1) { |
| 64 | + ret = noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count); |
| 65 | + if (!ret) { |
| 66 | + break; |
| 67 | + } |
| 68 | + secp256k1_scalar_set_b32(&non, nonce32, &overflow); |
| 69 | + is_zero = secp256k1_scalar_is_zero(&non); |
| 70 | + if (!overflow && !is_zero) { |
| 71 | + if (s2c_data32 != NULL) { |
| 72 | + secp256k1_gej nonce_pj; |
| 73 | + secp256k1_ge nonce_p; |
| 74 | + |
| 75 | + /* Compute original nonce commitment/pubkey */ |
| 76 | + secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &nonce_pj, &non); |
| 77 | + secp256k1_ge_set_gej(&nonce_p, &nonce_pj); |
| 78 | + secp256k1_pubkey_save(&s2c_opening->original_pubnonce, &nonce_p); |
| 79 | + |
| 80 | + /* Tweak nonce with s2c commitment. */ |
| 81 | + if (!secp256k1_ec_commit_seckey(ctx, nonce32, &s2c_opening->original_pubnonce, s2c_data32, 32)) { |
| 82 | + return 0; |
| 83 | + } |
| 84 | + secp256k1_scalar_set_b32(&non, nonce32, &overflow); |
| 85 | + is_zero = secp256k1_scalar_is_zero(&non); |
| 86 | + } |
| 87 | + |
| 88 | + if (!overflow && !is_zero) { |
| 89 | + if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, NULL)) { |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + count++; |
| 95 | + } |
| 96 | + memset(nonce32, 0, 32); |
| 97 | + secp256k1_scalar_clear(&msg); |
| 98 | + secp256k1_scalar_clear(&non); |
| 99 | + secp256k1_scalar_clear(&sec); |
| 100 | + } |
| 101 | + if (ret) { |
| 102 | + secp256k1_ecdsa_signature_save(signature, &r, &s); |
| 103 | + } else { |
| 104 | + memset(signature, 0, sizeof(*signature)); |
| 105 | + } |
| 106 | + return ret; |
| 107 | +} |
| 108 | + |
| 109 | +int secp256k1_ecdsa_s2c_verify_commit(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *data32, const secp256k1_s2c_opening *opening) { |
| 110 | + secp256k1_pubkey commitment; |
| 111 | + secp256k1_ge commitment_ge; |
| 112 | + unsigned char x_bytes1[32]; |
| 113 | + unsigned char x_bytes2[32]; |
| 114 | + secp256k1_scalar sigr, sigs; |
| 115 | + |
| 116 | + VERIFY_CHECK(ctx != NULL); |
| 117 | + ARG_CHECK(sig != NULL); |
| 118 | + ARG_CHECK(data32 != NULL); |
| 119 | + ARG_CHECK(opening != NULL); |
| 120 | + ARG_CHECK(secp256k1_s2c_commit_is_init(opening)); |
| 121 | + |
| 122 | + if (!secp256k1_ec_commit(ctx, &commitment, &opening->original_pubnonce, data32, 32)) { |
| 123 | + return 0; |
| 124 | + } |
| 125 | + |
| 126 | + /* Check that sigr (x coordinate of R) matches the x coordinate of the commitment. */ |
| 127 | + secp256k1_ecdsa_signature_load(ctx, &sigr, &sigs, sig); |
| 128 | + |
| 129 | + if (!secp256k1_pubkey_load(ctx, &commitment_ge, &commitment)) { |
| 130 | + return 0; |
| 131 | + } |
| 132 | + secp256k1_fe_normalize(&commitment_ge.x); |
| 133 | + secp256k1_fe_get_b32(x_bytes1, &commitment_ge.x); |
| 134 | + secp256k1_scalar_get_b32(x_bytes2, &sigr); |
| 135 | + return memcmp(x_bytes1, x_bytes2, 32) == 0; |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | +#endif /* SECP256K1_ECDSA_SIGN_TO_CONTRACT_MAIN_H */ |
0 commit comments