Browse Source

bigsize: make it a proper first-class type.

It doesn't belong in bitcoin, and should not be confused with varint_t.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
pull/2938/head
Rusty Russell 5 years ago
parent
commit
3fa375881a
  1. 62
      bitcoin/varint.c
  2. 8
      bitcoin/varint.h
  3. 1
      channeld/Makefile
  4. 1
      common/Makefile
  5. 75
      common/bigsize.c
  6. 21
      common/bigsize.h
  7. 10
      common/sphinx.c
  8. 15
      common/test/run-sphinx.c
  9. 1
      devtools/Makefile
  10. 1
      lightningd/Makefile
  11. 3
      wire/wire.h

62
bitcoin/varint.c

@ -60,65 +60,3 @@ size_t varint_get(const u8 *p, size_t max, varint_t *val)
return 1;
}
}
size_t bigsize_put(u8 buf[VARINT_MAX_LEN], varint_t v)
{
u8 *p = buf;
if (v < 0xfd) {
*(p++) = v;
} else if (v <= 0xffff) {
(*p++) = 0xfd;
(*p++) = v >> 8;
(*p++) = v;
} else if (v <= 0xffffffff) {
(*p++) = 0xfe;
(*p++) = v >> 24;
(*p++) = v >> 16;
(*p++) = v >> 8;
(*p++) = v;
} else {
(*p++) = 0xff;
(*p++) = v >> 56;
(*p++) = v >> 48;
(*p++) = v >> 40;
(*p++) = v >> 32;
(*p++) = v >> 24;
(*p++) = v >> 16;
(*p++) = v >> 8;
(*p++) = v;
}
return p - buf;
}
size_t bigsize_get(const u8 *p, size_t max, varint_t *val)
{
if (max < 1)
return 0;
switch (*p) {
case 0xfd:
if (max < 3)
return 0;
*val = ((u64)p[1] << 8) + p[2];
return 3;
case 0xfe:
if (max < 5)
return 0;
*val = ((u64)p[1] << 24) + ((u64)p[2] << 16)
+ ((u64)p[3] << 8) + p[4];
return 5;
case 0xff:
if (max < 9)
return 0;
*val = ((u64)p[1] << 56) + ((u64)p[2] << 48)
+ ((u64)p[3] << 40) + ((u64)p[4] << 32)
+ ((u64)p[5] << 24) + ((u64)p[6] << 16)
+ ((u64)p[7] << 8) + p[8];
return 9;
default:
*val = *p;
return 1;
}
}

8
bitcoin/varint.h

@ -15,12 +15,4 @@ size_t varint_put(u8 buf[VARINT_MAX_LEN], varint_t v);
/* Returns bytes used: 0 if max_len too small. */
size_t varint_get(const u8 *p, size_t max_len, varint_t *val);
/* Big-endian variant of varint_put, used in lightning */
size_t bigsize_put(u8 buf[VARINT_MAX_LEN], varint_t v);
/* Big-endian variant of varint_get, used in lightning */
size_t bigsize_get(const u8 *p, size_t max, varint_t *val);
#endif /* LIGHTNING_BITCOIN_VARINT_H */

1
channeld/Makefile

@ -35,6 +35,7 @@ ALL_GEN_HEADERS += $(LIGHTNINGD_CHANNEL_HEADERS_GEN)
CHANNELD_COMMON_OBJS := \
common/amount.o \
common/base32.o \
common/bigsize.o \
common/bip32.o \
common/channel_config.o \
common/crypto_state.o \

1
common/Makefile

@ -4,6 +4,7 @@ COMMON_SRC_NOGEN := \
common/base32.c \
common/bech32.c \
common/bech32_util.c \
common/bigsize.c \
common/bip32.c \
common/bolt11.c \
common/channel_config.c \

75
common/bigsize.c

@ -0,0 +1,75 @@
#include <common/bigsize.h>
size_t bigsize_len(bigsize_t v)
{
if (v < 0xfd) {
return 1;
} else if (v <= 0xffff) {
return 3;
} else if (v <= 0xffffffff) {
return 5;
} else {
return 9;
}
}
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN], bigsize_t v)
{
u8 *p = buf;
if (v < 0xfd) {
*(p++) = v;
} else if (v <= 0xffff) {
(*p++) = 0xfd;
(*p++) = v >> 8;
(*p++) = v;
} else if (v <= 0xffffffff) {
(*p++) = 0xfe;
(*p++) = v >> 24;
(*p++) = v >> 16;
(*p++) = v >> 8;
(*p++) = v;
} else {
(*p++) = 0xff;
(*p++) = v >> 56;
(*p++) = v >> 48;
(*p++) = v >> 40;
(*p++) = v >> 32;
(*p++) = v >> 24;
(*p++) = v >> 16;
(*p++) = v >> 8;
(*p++) = v;
}
return p - buf;
}
size_t bigsize_get(const u8 *p, size_t max, bigsize_t *val)
{
if (max < 1)
return 0;
switch (*p) {
case 0xfd:
if (max < 3)
return 0;
*val = ((u64)p[1] << 8) + p[2];
return 3;
case 0xfe:
if (max < 5)
return 0;
*val = ((u64)p[1] << 24) + ((u64)p[2] << 16)
+ ((u64)p[3] << 8) + p[4];
return 5;
case 0xff:
if (max < 9)
return 0;
*val = ((u64)p[1] << 56) + ((u64)p[2] << 48)
+ ((u64)p[3] << 40) + ((u64)p[4] << 32)
+ ((u64)p[5] << 24) + ((u64)p[6] << 16)
+ ((u64)p[7] << 8) + p[8];
return 9;
default:
*val = *p;
return 1;
}
}

21
common/bigsize.h

@ -0,0 +1,21 @@
#ifndef LIGHTNING_COMMON_BIGSIZE_H
#define LIGHTNING_COMMON_BIGSIZE_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <stddef.h>
/* typedef for clarity. */
typedef u64 bigsize_t;
#define BIGSIZE_MAX_LEN 9
/* Returns length of buf used. */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN], bigsize_t v);
/* Returns 0 on failure, otherwise length (<= max) used. */
size_t bigsize_get(const u8 *p, size_t max, bigsize_t *val);
/* How many bytes does it take to encode v? */
size_t bigsize_len(bigsize_t v);
#endif /* LIGHTNING_COMMON_BIGSIZE_H */

10
common/sphinx.c

@ -1,7 +1,5 @@
#include <assert.h>
#include <bitcoin/varint.h>
#include <ccan/array_size/array_size.h>
#include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/crypto/sha256/sha256.h>
@ -96,7 +94,7 @@ static size_t sphinx_hop_size(const struct sphinx_hop *hop)
/* There is no point really in trying to serialize something that is
* larger than the maximum length we can fit into the payload region
* anyway. 3 here is the maximum varint size that we allow. */
* anyway. 3 here is the maximum bigsize size that we allow. */
assert(size < ROUTING_INFO_SIZE - 3 - HMAC_SIZE);
/* Backwards compatibility: realm 0 is the legacy hop_data format and
@ -112,7 +110,7 @@ static size_t sphinx_hop_size(const struct sphinx_hop *hop)
else
vsize = 3;
/* The hop must accomodate the hop_payload, as well as the varint
/* The hop must accomodate the hop_payload, as well as the bigsize
* describing the length and HMAC. */
return vsize + size + HMAC_SIZE;
}
@ -471,7 +469,7 @@ static bool sphinx_write_frame(u8 *dest, const struct sphinx_hop *hop)
static void sphinx_parse_payload(struct route_step *step, const u8 *src)
{
size_t hop_size, vsize;
varint_t raw_size;
bigsize_t raw_size;
#if !EXPERIMENTAL_FEATURES
if (src[0] != 0x00) {
step->type = SPHINX_INVALID_PAYLOAD;
@ -588,7 +586,7 @@ struct route_step *process_onionpacket(
u8 stream[NUM_STREAM_BYTES];
u8 paddedheader[2*ROUTING_INFO_SIZE];
size_t vsize;
varint_t shift_size;
bigsize_t shift_size;
step->next = talz(step, struct onionpacket);
step->next->version = msg->version;

15
common/test/run-sphinx.c

@ -12,6 +12,12 @@
#include <unistd.h>
/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for fromwire */
const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED)
{ fprintf(stderr, "fromwire called!\n"); abort(); }
@ -21,9 +27,6 @@ struct amount_msat fromwire_amount_msat(const u8 **cursor UNNEEDED, size_t *max
/* Generated stub for fromwire_fail */
const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_fail called!\n"); abort(); }
/* Generated stub for fromwire_pad */
void fromwire_pad(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED)
{ fprintf(stderr, "fromwire_pad called!\n"); abort(); }
/* Generated stub for fromwire_short_channel_id */
void fromwire_short_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct short_channel_id *short_channel_id UNNEEDED)
@ -40,9 +43,6 @@ u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for towire */
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "towire called!\n"); abort(); }
/* Generated stub for towire_amount_msat */
void towire_amount_msat(u8 **pptr UNNEEDED, const struct amount_msat msat UNNEEDED)
{ fprintf(stderr, "towire_amount_msat called!\n"); abort(); }
/* Generated stub for towire_pad */
void towire_pad(u8 **pptr UNNEEDED, size_t num UNNEEDED)
{ fprintf(stderr, "towire_pad called!\n"); abort(); }
@ -59,9 +59,6 @@ void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED)
/* Generated stub for towire_u64 */
void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED)
{ fprintf(stderr, "towire_u64 called!\n"); abort(); }
/* Generated stub for towire_u8 */
void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED)
{ fprintf(stderr, "towire_u8 called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
secp256k1_context *secp256k1_ctx;

1
devtools/Makefile

@ -11,6 +11,7 @@ DEVTOOLS_COMMON_OBJS := \
common/base32.o \
common/bech32.o \
common/bech32_util.o \
common/bigsize.o \
common/bolt11.o \
common/crypto_state.o \
common/decode_short_channel_ids.o \

1
lightningd/Makefile

@ -19,6 +19,7 @@ LIGHTNINGD_COMMON_OBJS := \
common/base32.o \
common/bech32.o \
common/bech32_util.o \
common/bigsize.o \
common/bip32.o \
common/bolt11.o \
common/channel_config.o \

3
wire/wire.h

@ -12,6 +12,7 @@
#include <ccan/short_types/short_types.h>
#include <ccan/structeq/structeq.h>
#include <common/amount.h>
#include <common/bigsize.h>
#include <secp256k1_recovery.h>
#include <stdlib.h>
@ -31,7 +32,7 @@ struct siphash_seed;
/* Makes generate-wire.py work */
typedef char wirestring;
typedef u64 bigsize;
typedef bigsize_t bigsize;
/* FIXME: Some versions of spec using 'varint' for bigsize' */
typedef bigsize varint;

Loading…
Cancel
Save