Browse Source

sha256: Add primitive to add a varint-encoded int to a hash

We're about to push a couple of varints as part of the dynafed blockparsing,
so we might as well make it easier for us.
travis-debug
Christian Decker 5 years ago
parent
commit
8839be641a
  1. 13
      bitcoin/block.c

13
bitcoin/block.c

@ -4,6 +4,14 @@
#include <ccan/str/hex/hex.h> #include <ccan/str/hex/hex.h>
#include <common/type_to_string.h> #include <common/type_to_string.h>
static void sha256_varint(struct sha256_ctx *ctx, u64 val)
{
u8 vt[VARINT_MAX_LEN];
size_t vtlen;
vtlen = varint_put(vt, val);
sha256_update(ctx, vt, vtlen);
}
/* Encoding is <blockhdr> <varint-num-txs> <tx>... */ /* Encoding is <blockhdr> <varint-num-txs> <tx>... */
struct bitcoin_block * struct bitcoin_block *
bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams, bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
@ -81,8 +89,6 @@ void bitcoin_block_blkid(const struct bitcoin_block *b,
struct bitcoin_blkid *out) struct bitcoin_blkid *out)
{ {
struct sha256_ctx shactx; struct sha256_ctx shactx;
u8 vt[VARINT_MAX_LEN];
size_t vtlen;
sha256_init(&shactx); sha256_init(&shactx);
sha256_le32(&shactx, b->hdr.version); sha256_le32(&shactx, b->hdr.version);
@ -94,8 +100,7 @@ void bitcoin_block_blkid(const struct bitcoin_block *b,
size_t clen = tal_bytelen(b->elements_hdr->proof.challenge); size_t clen = tal_bytelen(b->elements_hdr->proof.challenge);
sha256_le32(&shactx, b->elements_hdr->block_height); sha256_le32(&shactx, b->elements_hdr->block_height);
vtlen = varint_put(vt, clen); sha256_varint(&shactx, clen);
sha256_update(&shactx, vt, vtlen);
sha256_update(&shactx, b->elements_hdr->proof.challenge, clen); sha256_update(&shactx, b->elements_hdr->proof.challenge, clen);
/* The solution is skipped, since that'd create a circular /* The solution is skipped, since that'd create a circular
* dependency apparently */ * dependency apparently */

Loading…
Cancel
Save