Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTLC test vector update #4493

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion bitcoin/signature.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ static bool sig_has_low_r(const secp256k1_ecdsa_signature* sig)
return compact_sig[0] < 0x80;
}

#if DEVELOPER
/* Some of the spec test vectors assume no sig grinding. */
extern bool dev_no_grind;

bool dev_no_grind = false;
#endif

void sign_hash(const struct privkey *privkey,
const struct sha256_double *h,
secp256k1_ecdsa_signature *s)
Expand All @@ -106,8 +113,13 @@ void sign_hash(const struct privkey *privkey,
ok = secp256k1_ecdsa_sign(secp256k1_ctx,
s,
h->sha.u.u8,
privkey->secret.data, NULL, extra_entropy);
privkey->secret.data, NULL,
IFDEV(dev_no_grind ? NULL
: extra_entropy,
extra_entropy));
((u32 *)extra_entropy)[0]++;
if (IFDEV(dev_no_grind, false))
break;
} while (!sig_has_low_r(s));

assert(ok);
Expand Down
26 changes: 12 additions & 14 deletions channeld/commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
option_anchor_outputs);
p2wsh = scriptpubkey_p2wsh(tx, wscript);
bitcoin_tx_add_output(tx, p2wsh, wscript, amount);
SUPERVERBOSE("# HTLC %" PRIu64 " offered %s wscript %s\n", htlc->id,
type_to_string(tmpctx, struct amount_sat, &amount),
SUPERVERBOSE("# HTLC #%" PRIu64 " offered amount %"PRIu64" wscript %s\n", htlc->id,
amount.satoshis, /* Raw: BOLT 3 output match */
tal_hex(wscript, wscript));
tal_free(wscript);
}
Expand All @@ -75,10 +75,9 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,

bitcoin_tx_add_output(tx, p2wsh, wscript, amount);

SUPERVERBOSE("# HTLC %"PRIu64" received %s wscript %s\n",
SUPERVERBOSE("# HTLC #%"PRIu64" received amount %"PRIu64" wscript %s\n",
htlc->id,
type_to_string(tmpctx, struct amount_sat,
&amount),
amount.satoshis, /* Raw: BOLT 3 output match */
tal_hex(wscript, wscript));
tal_free(wscript);
}
Expand Down Expand Up @@ -138,8 +137,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed,
option_anchor_outputs);

SUPERVERBOSE("# base commitment transaction fee = %s\n",
type_to_string(tmpctx, struct amount_sat, &base_fee));
SUPERVERBOSE("# base commitment transaction fee = %"PRIu64"\n",
base_fee.satoshis /* Raw: spec uses raw numbers */);

/* BOLT #3:
* If `option_anchor_outputs` applies to the commitment
Expand Down Expand Up @@ -168,9 +167,9 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
option_anchor_outputs, side))
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(htlcs[i]->amount));
}
if (amount_msat_greater_sat(self_pay, dust_limit))
if (amount_msat_greater_eq_sat(self_pay, dust_limit))
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(self_pay));
if (amount_msat_greater_sat(other_pay, dust_limit))
if (amount_msat_greater_eq_sat(other_pay, dust_limit))
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(other_pay));
assert(ok);
SUPERVERBOSE("# actual commitment transaction fee = %"PRIu64"\n",
Expand Down Expand Up @@ -244,8 +243,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
(*htlcmap)[n] = direct_outputs ? dummy_to_local : NULL;
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
* However, valgrind will warn us something wierd is happening */
SUPERVERBOSE("# to-local amount %s wscript %s\n",
type_to_string(tmpctx, struct amount_sat, &amount),
SUPERVERBOSE("# to_local amount %"PRIu64" wscript %s\n",
amount.satoshis, /* Raw: BOLT 3 output match */
tal_hex(tmpctx, wscript));
n++;
to_local = true;
Expand Down Expand Up @@ -287,9 +286,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
(*htlcmap)[n] = direct_outputs ? dummy_to_remote : NULL;
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
* However, valgrind will warn us something wierd is happening */
SUPERVERBOSE("# to-remote amount %s key %s\n",
type_to_string(tmpctx, struct amount_sat,
&amount),
SUPERVERBOSE("# to_remote amount %"PRIu64" P2WPKH(%s)\n",
amount.satoshis, /* Raw: BOLT 3 output match */
type_to_string(tmpctx, struct pubkey,
&keyset->other_payment_key));
n++;
Expand Down
59 changes: 33 additions & 26 deletions channeld/test/run-commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ static void report_htlcs(const struct bitcoin_tx *tx,
keyset.other_htlc_key = *remote_htlckey;

for (i = 0; i < tal_count(htlc_map); i++) {
struct bitcoin_signature localhtlcsig;
const struct htlc *htlc = htlc_map[i];

if (!htlc)
Expand Down Expand Up @@ -281,29 +282,21 @@ static void report_htlcs(const struct bitcoin_tx *tx,
x_remote_htlcsecretkey, remote_htlckey,
SIGHASH_ALL,
&remotehtlcsig[i]);
printf("# signature for output %zi (htlc %"PRIu64")\n", i, htlc->id);
printf("# signature for output #%zi (%s for htlc #%"PRIu64")\n",
i, htlc_owner(htlc) == LOCAL ? "htlc-timeout" : "htlc-success", htlc->id);
printf("remote_htlc_signature = %s\n",
type_to_string(tmpctx, struct bitcoin_signature,
&remotehtlcsig[i]));
}

/* For any HTLC outputs, produce htlc_tx */
for (i = 0; i < tal_count(htlc_map); i++) {
struct bitcoin_signature localhtlcsig;
const struct htlc *htlc = htlc_map[i];

if (!htlc)
continue;
type_to_string(tmpctx, secp256k1_ecdsa_signature,
&remotehtlcsig[i].s));

sign_tx_input(htlc_tx[i], 0,
NULL,
wscript[i],
local_htlcsecretkey, local_htlckey,
SIGHASH_ALL,
&localhtlcsig);
printf("# local_signature = %s\n",
type_to_string(tmpctx, struct bitcoin_signature,
&localhtlcsig));
printf("# local_htlc_signature = %s\n",
type_to_string(tmpctx, secp256k1_ecdsa_signature,
&localhtlcsig.s));
if (htlc_owner(htlc) == LOCAL) {
htlc_timeout_tx_add_witness(htlc_tx[i],
local_htlckey,
Expand All @@ -324,7 +317,7 @@ static void report_htlcs(const struct bitcoin_tx *tx,
remote_revocation_key,
option_anchor_outputs);
}
printf("output htlc_%s_tx %"PRIu64": %s\n",
printf("htlc_%s_tx (htlc #%"PRIu64"): %s\n",
htlc_owner(htlc) == LOCAL ? "timeout" : "success",
htlc->id,
tal_hex(tmpctx, linearize_tx(tmpctx, htlc_tx[i])));
Expand Down Expand Up @@ -361,15 +354,15 @@ static void report(struct bitcoin_tx *tx,
SIGHASH_ALL,
&remotesig);
printf("remote_signature = %s\n",
type_to_string(tmpctx, struct bitcoin_signature, &remotesig));
type_to_string(tmpctx, secp256k1_ecdsa_signature, &remotesig.s));
sign_tx_input(tx, 0,
NULL,
wscript,
local_funding_privkey, local_funding_pubkey,
SIGHASH_ALL,
&localsig);
printf("# local_signature = %s\n",
type_to_string(tmpctx, struct bitcoin_signature, &localsig));
type_to_string(tmpctx, secp256k1_ecdsa_signature, &localsig.s));

witness =
bitcoin_witness_2of2(tx, &localsig, &remotesig,
Expand Down Expand Up @@ -801,7 +794,7 @@ int main(int argc, const char *argv[])
to_remote.millisatoshis = 3000000000;
feerate_per_kw = 0;
printf("\n"
"name: commitment tx with all 5 htlcs untrimmed (minimum feerate)\n"
"name: commitment tx with all five HTLCs untrimmed (minimum feerate)\n"
"to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
Expand Down Expand Up @@ -901,12 +894,19 @@ int main(int argc, const char *argv[])
}
#endif
printf("\n"
"name: commitment tx with %zu output%s untrimmed (maximum feerate)\n"
"name: commitment tx with %s untrimmed (maximum feerate)\n"
"to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
tx->wtx->num_outputs,
tx->wtx->num_outputs > 1 ? "s" : "",
/* Spec was "neatened" to change these numbers to words! */
tx->wtx->num_outputs == 7 ? "seven outputs"
: tx->wtx->num_outputs == 6 ? "six outputs"
: tx->wtx->num_outputs == 5 ? "five outputs"
: tx->wtx->num_outputs == 4 ? "four outputs"
: tx->wtx->num_outputs == 3 ? "three outputs"
: tx->wtx->num_outputs == 2 ? "two outputs"
: tx->wtx->num_outputs == 1 ? "one output"
: "no outputs???",
Comment on lines +957 to +965
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh 😤

to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw-1);
/* Recalc with verbosity on */
print_superverbose = true;
Expand Down Expand Up @@ -942,12 +942,19 @@ int main(int argc, const char *argv[])
htlc_map);

printf("\n"
"name: commitment tx with %zu output%s untrimmed (minimum feerate)\n"
"name: commitment tx with %s untrimmed (minimum feerate)\n"
"to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
newtx->wtx->num_outputs,
newtx->wtx->num_outputs > 1 ? "s" : "",
/* Spec was "neatened" to change these numbers to words! */
newtx->wtx->num_outputs == 7 ? "seven outputs"
: newtx->wtx->num_outputs == 6 ? "six outputs"
: newtx->wtx->num_outputs == 5 ? "five outputs"
: newtx->wtx->num_outputs == 4 ? "four outputs"
: newtx->wtx->num_outputs == 3 ? "three outputs"
: newtx->wtx->num_outputs == 2 ? "two outputs"
: newtx->wtx->num_outputs == 1 ? "one output"
: "no outputs???",
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw);
/* Recalc with verbosity on */
print_superverbose = true;
Expand Down Expand Up @@ -1020,7 +1027,7 @@ int main(int argc, const char *argv[])
assert(feerate_per_kw == 9651936);

printf("\n"
"name: commitment tx with fee greater than opener amount\n"
"name: commitment tx with fee greater than funder amount\n"
"to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
Expand Down