MCPcopy Create free account
hub / github.com/ElementsProject/lightning / derive_simple_privkey

Function derive_simple_privkey

common/key_derive.c:67–97  ·  view source on GitHub ↗

BOLT #3: * * The corresponding private keys can be similarly derived, if the basepoint * secrets are known (i.e. the private keys corresponding to `localpubkey`, * `local_htlcpubkey`, and `local_delayedpubkey` only): * * privkey = basepoint_secret + SHA256(per_commitment_point || basepoint) */

Source from the content-addressed store, hash-verified

65 * privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)
66 */
67bool derive_simple_privkey(const struct secret *base_secret,
68 const struct pubkey *basepoint,
69 const struct pubkey *per_commitment_point,
70 struct privkey *key)
71{
72 struct sha256 sha;
73 unsigned char der_keys[PUBKEY_CMPR_LEN * 2];
74
75 pubkey_to_der(der_keys, per_commitment_point);
76 pubkey_to_der(der_keys + PUBKEY_CMPR_LEN, basepoint);
77 sha256(&sha, der_keys, sizeof(der_keys));
78#ifdef SUPERVERBOSE
79 printf("# SHA256(per_commitment_point || basepoint)\n");
80 printf("# => SHA256(0x%s || 0x%s)\n",
81 tal_hexstr(tmpctx, der_keys, PUBKEY_CMPR_LEN),
82 tal_hexstr(tmpctx, der_keys + PUBKEY_CMPR_LEN, PUBKEY_CMPR_LEN));
83 printf("# = 0x%s\n", tal_hexstr(tmpctx, &sha, sizeof(sha)));
84#endif
85
86 key->secret = *base_secret;
87 if (secp256k1_ec_seckey_tweak_add(secp256k1_ctx, key->secret.data,
88 sha.u.u8) != 1)
89 return false;
90#ifdef SUPERVERBOSE
91 printf("# + basepoint_secret (0x%s)\n",
92 tal_hexstr(tmpctx, base_secret, sizeof(*base_secret)));
93 printf("# = 0x%s\n",
94 tal_hexstr(tmpctx, key, sizeof(*key)));
95#endif
96 return true;
97}
98
99/* BOLT #3:
100 *

Callers 8

mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85

Calls 3

tal_hexstrFunction · 0.85
sha256Class · 0.70
pubkey_to_derFunction · 0.50

Tested by 2

mainFunction · 0.68
mainFunction · 0.68