|
| 1 | +/*********************************************************************** |
| 2 | + * Copyright (c) 2021-2023 Jesse Posner * |
| 3 | + * Distributed under the MIT software license, see the accompanying * |
| 4 | + * file COPYING or https://www.opensource.org/licenses/mit-license.php.* |
| 5 | + ***********************************************************************/ |
| 6 | + |
| 7 | +/** |
| 8 | + * This file demonstrates how to use the FROST module to create a threshold |
| 9 | + * signature. Additionally, see the documentation in include/secp256k1_frost.h. |
| 10 | + */ |
| 11 | + |
| 12 | +#include <stdio.h> |
| 13 | +#include <assert.h> |
| 14 | +#include <string.h> |
| 15 | + |
| 16 | +#include <secp256k1.h> |
| 17 | +#include <secp256k1_schnorrsig.h> |
| 18 | +#include <secp256k1_frost.h> |
| 19 | + |
| 20 | +#include "examples_util.h" |
| 21 | +/* Number of public keys involved in creating the aggregate signature */ |
| 22 | +#define N_SIGNERS 5 |
| 23 | + |
| 24 | +/* Threshold required in creating the aggregate signature */ |
| 25 | +#define THRESHOLD 3 |
| 26 | + |
| 27 | +struct signer_secrets { |
| 28 | + secp256k1_frost_share share; |
| 29 | + secp256k1_frost_secnonce secnonce; |
| 30 | +}; |
| 31 | + |
| 32 | +struct signer { |
| 33 | + secp256k1_pubkey pubshare; |
| 34 | + secp256k1_frost_pubnonce pubnonce; |
| 35 | + secp256k1_frost_session session; |
| 36 | + secp256k1_frost_partial_sig partial_sig; |
| 37 | +}; |
| 38 | + |
| 39 | +/* Create shares and coefficient commitments */ |
| 40 | +int create_shares(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, struct signer *signers, secp256k1_xonly_pubkey *pk) { |
| 41 | + int i; |
| 42 | + secp256k1_frost_share shares[N_SIGNERS]; |
| 43 | + secp256k1_pubkey pubshares[N_SIGNERS]; |
| 44 | + unsigned char seed[32]; |
| 45 | + |
| 46 | + if (!fill_random(seed, sizeof(seed))) { |
| 47 | + return 0; |
| 48 | + } |
| 49 | + |
| 50 | + if (!secp256k1_frost_shares_trusted_gen(ctx, shares, pubshares, pk, seed, THRESHOLD, N_SIGNERS)) { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + for (i = 0; i < N_SIGNERS; i++) { |
| 55 | + signer_secrets[i].share = shares[i]; |
| 56 | + signers[i].pubshare = pubshares[i]; |
| 57 | + } |
| 58 | + |
| 59 | + return 1; |
| 60 | +} |
| 61 | + |
| 62 | +/* Tweak the pubkey corresponding to the provided tweak cache, update the cache |
| 63 | + * and return the tweaked aggregate pk. */ |
| 64 | +int tweak(const secp256k1_context* ctx, secp256k1_xonly_pubkey *pk, secp256k1_frost_tweak_cache *cache) { |
| 65 | + secp256k1_pubkey output_pk; |
| 66 | + unsigned char ordinary_tweak[32] = "this could be a BIP32 tweak...."; |
| 67 | + unsigned char xonly_tweak[32] = "this could be a taproot tweak.."; |
| 68 | + |
| 69 | + if (!secp256k1_frost_pubkey_tweak(ctx, cache, pk)) { |
| 70 | + return 0; |
| 71 | + } |
| 72 | + |
| 73 | + /* Ordinary tweaking which, for example, allows deriving multiple child |
| 74 | + * public keys from a single aggregate key using BIP32 */ |
| 75 | + if (!secp256k1_frost_pubkey_ec_tweak_add(ctx, NULL, cache, ordinary_tweak)) { |
| 76 | + return 0; |
| 77 | + } |
| 78 | + /* If one is not interested in signing, the same output_pk can be obtained |
| 79 | + * by calling `secp256k1_frost_pubkey_get` right after key aggregation to |
| 80 | + * get the full pubkey and then call `secp256k1_ec_pubkey_tweak_add`. */ |
| 81 | + |
| 82 | + /* Xonly tweaking which, for example, allows creating taproot commitments */ |
| 83 | + if (!secp256k1_frost_pubkey_xonly_tweak_add(ctx, &output_pk, cache, xonly_tweak)) { |
| 84 | + return 0; |
| 85 | + } |
| 86 | + /* Note that if we wouldn't care about signing, we can arrive at the same |
| 87 | + * output_pk by providing the untweaked public key to |
| 88 | + * `secp256k1_xonly_pubkey_tweak_add` (after converting it to an xonly pubkey |
| 89 | + * if necessary with `secp256k1_xonly_pubkey_from_pubkey`). */ |
| 90 | + |
| 91 | + /* Now we convert the output_pk to an xonly pubkey to allow to later verify |
| 92 | + * the Schnorr signature against it. For this purpose we can ignore the |
| 93 | + * `pk_parity` output argument; we would need it if we would have to open |
| 94 | + * the taproot commitment. */ |
| 95 | + if (!secp256k1_xonly_pubkey_from_pubkey(ctx, pk, NULL, &output_pk)) { |
| 96 | + return 0; |
| 97 | + } |
| 98 | + return 1; |
| 99 | +} |
| 100 | + |
| 101 | +/* Sign a message hash with the given threshold and aggregate shares and store |
| 102 | + * the result in sig */ |
| 103 | +int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, struct signer *signer, const unsigned char* msg32, secp256k1_xonly_pubkey *pk, unsigned char *sig64, const secp256k1_frost_tweak_cache *cache) { |
| 104 | + int i; |
| 105 | + size_t signer_id = 0; |
| 106 | + int signers[THRESHOLD]; |
| 107 | + int is_signer[N_SIGNERS]; |
| 108 | + const secp256k1_frost_pubnonce *pubnonces[THRESHOLD]; |
| 109 | + size_t ids[THRESHOLD]; |
| 110 | + const secp256k1_frost_partial_sig *partial_sigs[THRESHOLD]; |
| 111 | + |
| 112 | + for (i = 0; i < N_SIGNERS; i++) { |
| 113 | + unsigned char session_id[32]; |
| 114 | + |
| 115 | + /* Create random session ID. It is absolutely necessary that the session ID |
| 116 | + * is unique for every call of secp256k1_frost_nonce_gen. Otherwise |
| 117 | + * it's trivial for an attacker to extract the secret key! */ |
| 118 | + if (!fill_random(session_id, sizeof(session_id))) { |
| 119 | + return 0; |
| 120 | + } |
| 121 | + /* Initialize session and create secret nonce for signing and public |
| 122 | + * nonce to send to the other signers. */ |
| 123 | + if (!secp256k1_frost_nonce_gen(ctx, &signer_secrets[i].secnonce, &signer[i].pubnonce, session_id, &signer_secrets[i].share, msg32, pk, NULL)) { |
| 124 | + return 0; |
| 125 | + } |
| 126 | + is_signer[i] = 0; /* Initialize is_signer */ |
| 127 | + } |
| 128 | + /* Select a random subset of signers */ |
| 129 | + for (i = 0; i < THRESHOLD; i++) { |
| 130 | + size_t subset_seed; |
| 131 | + |
| 132 | + while (1) { |
| 133 | + if (!fill_random((unsigned char*)&subset_seed, sizeof(subset_seed))) { |
| 134 | + return 0; |
| 135 | + } |
| 136 | + signer_id = subset_seed % N_SIGNERS; |
| 137 | + /* Check if signer has already been assigned */ |
| 138 | + if (!is_signer[signer_id]) { |
| 139 | + is_signer[signer_id] = 1; |
| 140 | + signers[i] = signer_id; |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + /* Mark signer as assigned */ |
| 145 | + pubnonces[i] = &signer[signer_id].pubnonce; |
| 146 | + /* pubkeys[i] = &signer[signer_id].pubkey; */ |
| 147 | + ids[i] = signer_id + 1; |
| 148 | + } |
| 149 | + /* Signing communication round 1: Exchange nonces */ |
| 150 | + for (i = 0; i < THRESHOLD; i++) { |
| 151 | + signer_id = signers[i]; |
| 152 | + if (!secp256k1_frost_nonce_process(ctx, &signer[signer_id].session, pubnonces, THRESHOLD, msg32, pk, signer_id + 1, ids, cache, NULL)) { |
| 153 | + return 0; |
| 154 | + } |
| 155 | + /* partial_sign will clear the secnonce by setting it to 0. That's because |
| 156 | + * you must _never_ reuse the secnonce (or use the same session_id to |
| 157 | + * create a secnonce). If you do, you effectively reuse the nonce and |
| 158 | + * leak the secret key. */ |
| 159 | + if (!secp256k1_frost_partial_sign(ctx, &signer[signer_id].partial_sig, &signer_secrets[signer_id].secnonce, &signer_secrets[signer_id].share, &signer[signer_id].session, cache)) { |
| 160 | + return 0; |
| 161 | + } |
| 162 | + partial_sigs[i] = &signer[signer_id].partial_sig; |
| 163 | + } |
| 164 | + /* Communication round 2: A production system would exchange |
| 165 | + * partial signatures here before moving on. */ |
| 166 | + for (i = 0; i < THRESHOLD; i++) { |
| 167 | + signer_id = signers[i]; |
| 168 | + /* To check whether signing was successful, it suffices to either verify |
| 169 | + * the aggregate signature with the aggregate public key using |
| 170 | + * secp256k1_schnorrsig_verify, or verify all partial signatures of all |
| 171 | + * signers individually. Verifying the aggregate signature is cheaper but |
| 172 | + * verifying the individual partial signatures has the advantage that it |
| 173 | + * can be used to determine which of the partial signatures are invalid |
| 174 | + * (if any), i.e., which of the partial signatures cause the aggregate |
| 175 | + * signature to be invalid and thus the protocol run to fail. It's also |
| 176 | + * fine to first verify the aggregate sig, and only verify the individual |
| 177 | + * sigs if it does not work. |
| 178 | + */ |
| 179 | + if (!secp256k1_frost_partial_sig_verify(ctx, &signer[signer_id].partial_sig, &signer[signer_id].pubnonce, &signer[signer_id].pubshare, &signer[signer_id].session, cache)) { |
| 180 | + return 0; |
| 181 | + } |
| 182 | + } |
| 183 | + return secp256k1_frost_partial_sig_agg(ctx, sig64, &signer[signer_id].session, partial_sigs, THRESHOLD); |
| 184 | +} |
| 185 | + |
| 186 | +int main(void) { |
| 187 | + secp256k1_context* ctx; |
| 188 | + struct signer_secrets signer_secrets[N_SIGNERS]; |
| 189 | + struct signer signers[N_SIGNERS]; |
| 190 | + secp256k1_xonly_pubkey pk; |
| 191 | + secp256k1_frost_tweak_cache cache; |
| 192 | + unsigned char msg[32] = "this_could_be_the_hash_of_a_msg!"; |
| 193 | + unsigned char sig[64]; |
| 194 | + |
| 195 | + /* Create a context for signing and verification */ |
| 196 | + ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE); |
| 197 | + printf("Creating shares........."); |
| 198 | + if (!create_shares(ctx, signer_secrets, signers, &pk)) { |
| 199 | + printf("FAILED\n"); |
| 200 | + return 1; |
| 201 | + } |
| 202 | + printf("ok\n"); |
| 203 | + printf("Tweaking................"); |
| 204 | + /* Optionally tweak the key */ |
| 205 | + if (!tweak(ctx, &pk, &cache)) { |
| 206 | + printf("FAILED\n"); |
| 207 | + return 1; |
| 208 | + } |
| 209 | + printf("ok\n"); |
| 210 | + printf("Signing message........."); |
| 211 | + if (!sign(ctx, signer_secrets, signers, msg, &pk, sig, &cache)) { |
| 212 | + printf("FAILED\n"); |
| 213 | + return 1; |
| 214 | + } |
| 215 | + printf("ok\n"); |
| 216 | + printf("Verifying signature....."); |
| 217 | + if (!secp256k1_schnorrsig_verify(ctx, sig, msg, 32, &pk)) { |
| 218 | + printf("FAILED\n"); |
| 219 | + return 1; |
| 220 | + } |
| 221 | + printf("ok\n"); |
| 222 | + secp256k1_context_destroy(ctx); |
| 223 | + return 0; |
| 224 | +} |
0 commit comments