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

[WIP] add Groestl support for c-lightning #3

Merged
merged 14 commits into from
Sep 20, 2018
5 changes: 5 additions & 0 deletions bitcoin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ BITCOIN_SRC := \
bitcoin/block.c \
bitcoin/chainparams.c \
bitcoin/locktime.c \
bitcoin/groestl.c \
bitcoin/privkey.c \
bitcoin/pubkey.c \
bitcoin/pullpush.c \
bitcoin/script.c \
bitcoin/shadouble.c \
bitcoin/short_channel_id.c \
bitcoin/sph_groestl.c \
bitcoin/signature.c \
bitcoin/tx.c \
bitcoin/varint.c
Expand All @@ -22,6 +24,7 @@ BITCOIN_HEADERS := bitcoin/address.h \
bitcoin/block.h \
bitcoin/chainparams.h \
bitcoin/feerate.h \
bitcoin/groestl.h \
bitcoin/locktime.h \
bitcoin/preimage.h \
bitcoin/privkey.h \
Expand All @@ -31,6 +34,8 @@ BITCOIN_HEADERS := bitcoin/address.h \
bitcoin/shadouble.h \
bitcoin/short_channel_id.h \
bitcoin/signature.h \
bitcoin/sph_groestl.h \
bitcoin/sph_types.h \
bitcoin/tx.h \
bitcoin/varint.h

Expand Down
205 changes: 198 additions & 7 deletions bitcoin/base58.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,217 @@
// Copyright (c) 2009-2012 The Bitcoin Developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
/*
* Copyright 2012-2014 Luke Dashjr
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the standard MIT license. See COPYING for more details.
*/

#include "address.h"
#include "base58.h"
#include "privkey.h"
#include "pubkey.h"
#include "shadouble.h"
#include <arpa/inet.h>
#include <assert.h>
#include <bitcoin/base58.h>
#include <bitcoin/chainparams.h>
#include <bitcoin/groestl.h>
#include <ccan/build_assert/build_assert.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <libbase58.h>
#include <string.h>

bool (*b58_sha256_impl)(void *, const void *, size_t) = NULL;

static const int8_t b58digits_map[] = {
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1,
-1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1,
22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1,
-1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46,
47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1,
};

bool b58tobin(void *bin, size_t *binszp, const char *b58, size_t b58sz)
{
size_t binsz = *binszp;
const unsigned char *b58u = (void*)b58;
unsigned char *binu = bin;
size_t outisz = (binsz + 3) / 4;
uint32_t outi[outisz];
uint64_t t;
uint32_t c;
size_t i, j;
uint8_t bytesleft = binsz % 4;
uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0;
unsigned zerocount = 0;

if (!b58sz)
b58sz = strlen(b58);

memset(outi, 0, outisz * sizeof(*outi));

// Leading zeros, just count
for (i = 0; i < b58sz && b58u[i] == '1'; ++i)
++zerocount;

for ( ; i < b58sz; ++i)
{
if (b58u[i] & 0x80)
// High-bit set on invalid digit
return false;
if (b58digits_map[b58u[i]] == -1)
// Invalid base58 digit
return false;
c = (unsigned)b58digits_map[b58u[i]];
for (j = outisz; j--; )
{
t = ((uint64_t)outi[j]) * 58 + c;
c = (t & 0x3f00000000) >> 32;
outi[j] = t & 0xffffffff;
}
if (c)
// Output number too big (carry to the next int32)
return false;
if (outi[0] & zeromask)
// Output number too big (last int32 filled too far)
return false;
}

j = 0;
switch (bytesleft) {
case 3:
*(binu++) = (outi[0] & 0xff0000) >> 16;
case 2:
*(binu++) = (outi[0] & 0xff00) >> 8;
case 1:
*(binu++) = (outi[0] & 0xff);
++j;
default:
break;
}

for (; j < outisz; ++j)
{
*(binu++) = (outi[j] >> 0x18) & 0xff;
*(binu++) = (outi[j] >> 0x10) & 0xff;
*(binu++) = (outi[j] >> 8) & 0xff;
*(binu++) = (outi[j] >> 0) & 0xff;
}

// Count canonical base58 byte count
binu = bin;
for (i = 0; i < binsz; ++i)
{
if (binu[i])
break;
--*binszp;
}
*binszp += zerocount;

return true;
}

static
bool my_dblsha256(void *hash, const void *data, size_t datasz)
{
return b58_sha256_impl(hash, data, datasz);
}

int b58check(const void *bin, size_t binsz, const char *base58str, size_t b58sz)
{
unsigned char buf[32];
const uint8_t *binc = bin;
unsigned i;
if (binsz < 4)
return -4;
if (!my_dblsha256(buf, bin, binsz - 4))
return -2;
if (memcmp(&binc[binsz - 4], buf, 4))
return -1;

// Check number of zeros is correct AFTER verifying checksum (to avoid possibility of accessing base58str beyond the end)
for (i = 0; binc[i] == '\0' && base58str[i] == '1'; ++i)
{} // Just finding the end of zeros, nothing to do in loop
if (binc[i] == '\0' || base58str[i] == '1')
return -3;

return binc[0];
}

static const char b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
{
const uint8_t *bin = data;
int carry;
ssize_t i, j, high, zcount = 0;
size_t size;

while (zcount < binsz && !bin[zcount])
++zcount;

size = (binsz - zcount) * 138 / 100 + 1;
uint8_t buf[size];
memset(buf, 0, size);

for (i = zcount, high = size - 1; i < binsz; ++i, high = j)
{
for (carry = bin[i], j = size - 1; (j > high) || carry; --j)
{
carry += 256 * buf[j];
buf[j] = carry % 58;
carry /= 58;
}
}

for (j = 0; j < size && !buf[j]; ++j);

if (*b58sz <= zcount + size - j)
{
*b58sz = zcount + size - j + 1;
return false;
}

if (zcount)
memset(b58, '1', zcount);
for (i = zcount; j < size; ++i, ++j)
b58[i] = b58digits_ordered[buf[j]];
b58[i] = '\0';
*b58sz = i + 1;

return true;
}

bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz)
{
uint8_t buf[1 + datasz + 0x20];
uint8_t *hash = &buf[1 + datasz];

buf[0] = ver;
memcpy(&buf[1], data, datasz);
if (!my_dblsha256(hash, buf, datasz + 1))
{
*b58c_sz = 0;
return false;
}

return b58enc(b58c, b58c_sz, buf, 1 + datasz + 4);
}


static bool my_sha256(void *digest, const void *data, size_t datasz)
{
sha256(digest, data, datasz);
groestlhash((void *)digest, (void *)data, datasz);
return true;
}

static char *to_base58(const tal_t *ctx, u8 version,
const struct ripemd160 *rmd)
const struct ripemd160 *rmd)
{
char out[BASE58_ADDR_MAX_LEN + 1];
size_t outlen = sizeof(out);
Expand All @@ -42,7 +233,7 @@ char *bitcoin_to_base58(const tal_t *ctx, bool test_net,
}

char *p2sh_to_base58(const tal_t *ctx, bool test_net,
const struct ripemd160 *p2sh)
const struct ripemd160 *p2sh)
{
return to_base58(ctx, test_net ? 196 : 5, p2sh);
}
Expand Down Expand Up @@ -84,8 +275,8 @@ bool bitcoin_from_base58(bool *test_net,
}

bool p2sh_from_base58(bool *test_net,
struct ripemd160 *p2sh,
const char *base58, size_t len)
struct ripemd160 *p2sh,
const char *base58, size_t len)
{
u8 version;

Expand All @@ -102,7 +293,7 @@ bool p2sh_from_base58(bool *test_net,
}

bool key_from_base58(const char *base58, size_t base58_len,
bool *test_net, struct privkey *priv, struct pubkey *key)
bool *test_net, struct privkey *priv, struct pubkey *key)
{
// 1 byte version, 32 byte private key, 1 byte compressed, 4 byte checksum
u8 keybuf[1 + 32 + 1 + 4];
Expand Down
9 changes: 9 additions & 0 deletions bitcoin/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ bool key_from_base58(const char *base58, size_t base58_len,

void base58_get_checksum(u8 csum[4], const u8 buf[], size_t buflen);

extern bool (*b58_sha256_impl)(void *, const void *, size_t);

extern bool b58tobin(void *bin, size_t *binsz, const char *b58, size_t b58sz);
extern int b58check(const void *bin, size_t binsz, const char *b58, size_t b58sz);

extern bool b58enc(char *b58, size_t *b58sz, const void *bin, size_t binsz);
extern bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz);


#endif /* LIGHTNING_BITCOIN_BASE58_H */
66 changes: 3 additions & 63 deletions bitcoin/chainparams.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,6 @@

const struct chainparams networks[] = {
{.index = 0,
.network_name = "bitcoin",
.bip173_name = "bc",
.genesis_blockhash = {{{.u.u8 = {0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f, 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00}}}},
.rpc_port = 8332,
.cli = "bitcoin-cli",
.cli_args = NULL,
.dust_limit = 546,
/* "Lightning Charge Powers Developers & Blockstream Store" */
.when_lightning_became_cool = 504500,
.testnet = false},
{.index = 1,
.network_name = "regtest",
.bip173_name = "bcrt",
.genesis_blockhash = {{{.u.u8 = {0x06, 0x22, 0x6e, 0x46, 0x11, 0x1a, 0x0b, 0x59, 0xca, 0xaf, 0x12, 0x60, 0x43, 0xeb, 0x5b, 0xbf, 0x28, 0xc3, 0x4f, 0x3a, 0x5e, 0x33, 0x2a, 0x1f, 0xc7, 0xb2, 0xb7, 0x3c, 0xf1, 0x88, 0x91, 0x0f}}}},
.rpc_port = 18332,
.cli = "bitcoin-cli",
.cli_args = "-regtest",
.dust_limit = 546,
.when_lightning_became_cool = 1,
.testnet = true},
{.index = 2,
.network_name = "testnet",
.bip173_name = "tb",
.genesis_blockhash = {{{.u.u8 = {0x43, 0x49, 0x7f, 0xd7, 0xf8, 0x26, 0x95, 0x71, 0x08, 0xf4, 0xa3, 0x0f, 0xd9, 0xce, 0xc3, 0xae, 0xba, 0x79, 0x97, 0x20, 0x84, 0xe9, 0x0e, 0xad, 0x01, 0xea, 0x33, 0x09, 0x00, 0x00, 0x00, 0x00}}}},
.rpc_port = 18332,
.cli = "bitcoin-cli",
.cli_args = "-testnet",
.dust_limit = 546,
.testnet = true},
{.index = 3,
.network_name = "litecoin",
.bip173_name = "ltc",
.genesis_blockhash = {{{.u.u8 = {0xe2, 0xbf, 0x04, 0x7e, 0x7e, 0x5a, 0x19, 0x1a, 0xa4, 0xef, 0x34, 0xd3, 0x14, 0x97, 0x9d, 0xc9, 0x98, 0x6e, 0x0f, 0x19, 0x25, 0x1e, 0xda, 0xba, 0x59, 0x40, 0xfd, 0x1f, 0xe3, 0x65, 0xa7, 0x12}}}},
.rpc_port = 9332,
.cli = "litecoin-cli",
.cli_args = NULL,
.dust_limit = 100000,
.when_lightning_became_cool = 1320000,
.testnet = false},
{.index = 4,
.network_name = "litecoin-testnet",
.bip173_name = "tltc",
.genesis_blockhash = {{{.u.u8 = {0xa0, 0x29, 0x3e, 0x4e, 0xeb, 0x3d, 0xa6, 0xe6, 0xf5, 0x6f, 0x81, 0xed, 0x59, 0x5f, 0x57, 0x88, 0x0d, 0x1a, 0x21, 0x56, 0x9e, 0x13, 0xee, 0xfd, 0xd9, 0x51, 0x28, 0x4b, 0x5a, 0x62, 0x66, 0x49}}}},
.rpc_port = 19332,
.cli = "litecoin-cli",
.cli_args = "-testnet",
.dust_limit = 100000,
.when_lightning_became_cool = 1,
.testnet = true},
{.index = 5,
.network_name = "litecoin-regtest",
.bip173_name = "rltc",
.genesis_blockhash = {{{.u.u8 = {0xf9, 0x16, 0xc4, 0x56, 0xfc, 0x51, 0xdf, 0x62, 0x78, 0x85, 0xd7, 0xd6, 0x74, 0xed, 0x02, 0xdc, 0x88, 0xa2, 0x25, 0xad, 0xb3, 0xf0, 0x2a, 0xd1, 0x3e, 0xb4, 0x93, 0x8f, 0xf3, 0x27, 0x08, 0x53}}}},
.rpc_port = 19443,
.cli = "litecoin-cli",
.cli_args = "-regtest",
.dust_limit = 100000,
.when_lightning_became_cool = 1,
.testnet = true},
{.index = 6,
.network_name = "groestlcoin",
.bip173_name = "grs",
.genesis_blockhash = {{{.u.u8 = {0x23, 0x90, 0x63, 0x3b, 0x70, 0xf0, 0x62, 0xcb, 0x3a, 0x3d, 0x68, 0x14, 0xb6, 0x7e, 0x29, 0xa8, 0x0d, 0x9d, 0x75, 0x81, 0xdb, 0x0b, 0xcc, 0x49, 0x4d, 0x59, 0x7c, 0x92, 0xc5, 0x0a, 0x00, 0x00}}}},
Expand All @@ -74,8 +14,8 @@ const struct chainparams networks[] = {
.dust_limit = 546,
.when_lightning_became_cool = 5045000,
.testnet = false},
{.index = 7,
.network_name = "groestlcoin-testnet",
{.index = 1,
.network_name = "testnet",
.bip173_name = "tgrs",
.genesis_blockhash = {{{.u.u8 = {0x36, 0xcd, 0xf2, 0xdc, 0xb7, 0x55, 0x62, 0x87, 0x28, 0x2a, 0x05, 0xc0, 0x64, 0x01, 0x23, 0x23, 0xba, 0xe6, 0x63, 0xc1, 0x6e, 0xd3, 0xcd, 0x98, 0x98, 0xfc, 0x50, 0xbb, 0xff, 0x00, 0x00, 0x00}}}},
.rpc_port = 17766,
Expand All @@ -84,7 +24,7 @@ const struct chainparams networks[] = {
.dust_limit = 546,
.when_lightning_became_cool = 1,
.testnet = true},
{.index = 8,
{.index = 2,
.network_name = "groestlcoin-regtest",
gruve-p marked this conversation as resolved.
Show resolved Hide resolved
.bip173_name = "grsrt",
.genesis_blockhash = {{{.u.u8 = {0x36, 0xcd, 0xf2, 0xdc, 0xb7, 0x55, 0x62, 0x87, 0x28, 0x2a, 0x05, 0xc0, 0x64, 0x01, 0x23, 0x23, 0xba, 0xe6, 0x63, 0xc1, 0x6e, 0xd3, 0xcd, 0x98, 0x98, 0xfc, 0x50, 0xbb, 0xff, 0x00, 0x00, 0x00}}}},
Expand Down
Loading