Browse Source

ccan: update to latest shachain.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
parent
commit
1ffb9f07c1
  1. 2
      ccan/README
  2. 30
      ccan/ccan/crypto/shachain/shachain.c
  3. 33
      ccan/ccan/crypto/shachain/shachain.h
  4. 2
      ccan/ccan/crypto/shachain/test/run-8bit.c
  5. 4
      ccan/ccan/crypto/shachain/test/run-can_derive.c
  6. 3
      ccan/ccan/crypto/shachain/test/run.c
  7. 4
      wallet/wallet.c
  8. 2
      wallet/wallet.h
  9. 5
      wallet/wallet_tests.c

2
ccan/README

@ -1,3 +1,3 @@
CCAN imported from http://ccodearchive.net. CCAN imported from http://ccodearchive.net.
CCAN version: init-2382-g3ffb94e9 CCAN version: init-2388-ge6abb93d

30
ccan/ccan/crypto/shachain/shachain.c

@ -10,7 +10,7 @@ static void change_bit(unsigned char *arr, size_t index)
arr[index / CHAR_BIT] ^= (1 << (index % CHAR_BIT)); arr[index / CHAR_BIT] ^= (1 << (index % CHAR_BIT));
} }
static unsigned int count_trailing_zeroes(shachain_index_t index) static unsigned int count_trailing_zeroes(uint64_t index)
{ {
#if HAVE_BUILTIN_CTZLL #if HAVE_BUILTIN_CTZLL
return index ? (unsigned int)__builtin_ctzll(index) : SHACHAIN_BITS; return index ? (unsigned int)__builtin_ctzll(index) : SHACHAIN_BITS;
@ -25,24 +25,24 @@ static unsigned int count_trailing_zeroes(shachain_index_t index)
#endif #endif
} }
static bool can_derive(shachain_index_t from, shachain_index_t to) static bool can_derive(uint64_t from, uint64_t to)
{ {
shachain_index_t mask; uint64_t mask;
/* Corner case: can always derive from seed. */ /* Corner case: can always derive from seed. */
if (from == 0) if (from == 0)
return true; return true;
/* Leading bits must be the same */ /* Leading bits must be the same */
mask = ~((1ULL << count_trailing_zeroes(from))-1); mask = ~(((uint64_t)1 << count_trailing_zeroes(from))-1);
return ((from ^ to) & mask) == 0; return ((from ^ to) & mask) == 0;
} }
static void derive(shachain_index_t from, shachain_index_t to, static void derive(uint64_t from, uint64_t to,
const struct sha256 *from_hash, const struct sha256 *from_hash,
struct sha256 *hash) struct sha256 *hash)
{ {
shachain_index_t branches; uint64_t branches;
int i; int i;
assert(can_derive(from, to)); assert(can_derive(from, to));
@ -60,27 +60,31 @@ static void derive(shachain_index_t from, shachain_index_t to,
} }
} }
void shachain_from_seed(const struct sha256 *seed, shachain_index_t index, void shachain_from_seed(const struct sha256 *seed, uint64_t index,
struct sha256 *hash) struct sha256 *hash)
{ {
derive(0, index, seed, hash); derive(0, index, seed, hash);
} }
uint64_t shachain_next_index(const struct shachain *chain)
{
return chain->min_index - 1;
}
void shachain_init(struct shachain *chain) void shachain_init(struct shachain *chain)
{ {
chain->num_valid = 0; chain->num_valid = 0;
chain->min_index = 0; /* This is 0 in the case where SHACHAIN_BITS is 64. */
chain->min_index = (UINT64_MAX >> (64 - SHACHAIN_BITS)) + 1;
} }
bool shachain_add_hash(struct shachain *chain, bool shachain_add_hash(struct shachain *chain,
shachain_index_t index, const struct sha256 *hash) uint64_t index, const struct sha256 *hash)
{ {
unsigned int i, pos; unsigned int i, pos;
/* You have to insert them in order! */ /* You have to insert them in order! */
assert(index == chain->min_index - 1 || assert(index == shachain_next_index(chain));
(index == (shachain_index_t)(UINT64_MAX >> (64 - SHACHAIN_BITS))
&& chain->num_valid == 0));
pos = count_trailing_zeroes(index); pos = count_trailing_zeroes(index);
@ -104,7 +108,7 @@ bool shachain_add_hash(struct shachain *chain,
} }
bool shachain_get_hash(const struct shachain *chain, bool shachain_get_hash(const struct shachain *chain,
shachain_index_t index, struct sha256 *hash) uint64_t index, struct sha256 *hash)
{ {
unsigned int i; unsigned int i;

33
ccan/ccan/crypto/shachain/shachain.h

@ -6,13 +6,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
/* Useful for testing. */
#ifndef shachain_index_t
#define shachain_index_t uint64_t
#endif
#ifndef SHACHAIN_BITS #ifndef SHACHAIN_BITS
#define SHACHAIN_BITS (sizeof(shachain_index_t) * 8) #define SHACHAIN_BITS (sizeof(uint64_t) * 8)
#endif #endif
/** /**
@ -42,7 +37,7 @@
* shachain_from_seed(&seed, index--, hash); * shachain_from_seed(&seed, index--, hash);
* } * }
*/ */
void shachain_from_seed(const struct sha256 *seed, shachain_index_t index, void shachain_from_seed(const struct sha256 *seed, uint64_t index,
struct sha256 *hash); struct sha256 *hash);
/** /**
@ -55,10 +50,10 @@ void shachain_from_seed(const struct sha256 *seed, shachain_index_t index,
* added. * added.
*/ */
struct shachain { struct shachain {
shachain_index_t min_index; uint64_t min_index;
unsigned int num_valid; unsigned int num_valid;
struct { struct {
shachain_index_t index; uint64_t index;
struct sha256 hash; struct sha256 hash;
} known[SHACHAIN_BITS + 1]; } known[SHACHAIN_BITS + 1];
}; };
@ -71,20 +66,28 @@ struct shachain {
*/ */
void shachain_init(struct shachain *chain); void shachain_init(struct shachain *chain);
/**
* shachain_next_index - what's the next index I can add to the shachain?
* @chain: the chain
*
* This returns 0xFFFFFFFFFFFFFFFF (for a freshly
* initialized chain), or one less than the previously successfully
* added value.
*/
uint64_t shachain_next_index(const struct shachain *chain);
/** /**
* shachain_add_hash - record the hash for the next index. * shachain_add_hash - record the hash for the next index.
* @chain: the chain to add to * @chain: the chain to add to
* @index: the index of the hash * @index: the index of the hash
* @hash: the hash value. * @hash: the hash value.
* *
* You can only add index 0xFFFFFFFFFFFFFFFF (for a freshly * You can only add shachain_next_index(@chain).
* initialized chain), or one less than the previously successfully
* added value.
* *
* This can fail (return false without altering @chain) if the hash * This can fail (return false without altering @chain) if the hash
* for this index isn't consistent with previous hashes (ie. wasn't * for this index isn't consistent with previous hashes (ie. wasn't
* generated from the same seed), though it can't always detect that. * generated from the same seed), though it can't always detect that.
* If the hash is inconsistent yet undetected, the next addition will * If the hash is inconsistent yet undetected, a future addition will
* fail. * fail.
* *
* Example: * Example:
@ -98,7 +101,7 @@ void shachain_init(struct shachain *chain);
* } * }
*/ */
bool shachain_add_hash(struct shachain *chain, bool shachain_add_hash(struct shachain *chain,
shachain_index_t index, const struct sha256 *hash); uint64_t index, const struct sha256 *hash);
/** /**
* shachain_get_hash - get the hash for a given index. * shachain_get_hash - get the hash for a given index.
@ -129,5 +132,5 @@ bool shachain_add_hash(struct shachain *chain,
* } * }
*/ */
bool shachain_get_hash(const struct shachain *chain, bool shachain_get_hash(const struct shachain *chain,
shachain_index_t index, struct sha256 *hash); uint64_t index, struct sha256 *hash);
#endif /* CCAN_CRYPTO_SHACHAIN_H */ #endif /* CCAN_CRYPTO_SHACHAIN_H */

2
ccan/ccan/crypto/shachain/test/run-8bit.c

@ -1,4 +1,4 @@
#define shachain_index_t uint8_t #define SHACHAIN_BITS 8
#include <ccan/crypto/shachain/shachain.h> #include <ccan/crypto/shachain/shachain.h>
/* Include the C files directly. */ /* Include the C files directly. */

4
ccan/ccan/crypto/shachain/test/run-can_derive.c

@ -7,13 +7,13 @@
#include <stdio.h> #include <stdio.h>
static bool bit_set(shachain_index_t index, int bit) static bool bit_set(uint64_t index, int bit)
{ {
return index & (1ULL << bit); return index & (1ULL << bit);
} }
/* As per design.txt */ /* As per design.txt */
static bool naive_can_derive(shachain_index_t from, shachain_index_t to) static bool naive_can_derive(uint64_t from, shachain_index_t to)
{ {
int i; int i;

3
ccan/ccan/crypto/shachain/test/run.c

@ -13,7 +13,7 @@ int main(void)
uint64_t i, j; uint64_t i, j;
/* This is how many tests you plan to run */ /* This is how many tests you plan to run */
plan_tests(NUM_TESTS * 3 + NUM_TESTS * (NUM_TESTS + 1) - 1); plan_tests(NUM_TESTS * 4 + NUM_TESTS * (NUM_TESTS + 1) - 1);
memset(&seed, 0, sizeof(seed)); memset(&seed, 0, sizeof(seed));
/* Generate a whole heap. */ /* Generate a whole heap. */
@ -34,6 +34,7 @@ int main(void)
i--) { i--) {
struct sha256 hash; struct sha256 hash;
int expidx = 0xFFFFFFFFFFFFFFFFULL - i; int expidx = 0xFFFFFFFFFFFFFFFFULL - i;
ok1(shachain_next_index(&chain) == i);
ok1(shachain_add_hash(&chain, i, &expect[expidx])); ok1(shachain_add_hash(&chain, i, &expect[expidx]));
for (j = i; j != 0; j++) { for (j = i; j != 0; j++) {
ok1(shachain_get_hash(&chain, j, &hash)); ok1(shachain_get_hash(&chain, j, &hash));

4
wallet/wallet.c

@ -252,7 +252,7 @@ bool wallet_shachain_init(struct wallet *wallet, struct wallet_shachain *chain)
} }
/* TODO(cdecker) Stolen from shachain, move to some appropriate location */ /* TODO(cdecker) Stolen from shachain, move to some appropriate location */
static unsigned int count_trailing_zeroes(shachain_index_t index) static unsigned int count_trailing_zeroes(uint64_t index)
{ {
#if HAVE_BUILTIN_CTZLL #if HAVE_BUILTIN_CTZLL
return index ? (unsigned int)__builtin_ctzll(index) : SHACHAIN_BITS; return index ? (unsigned int)__builtin_ctzll(index) : SHACHAIN_BITS;
@ -269,7 +269,7 @@ static unsigned int count_trailing_zeroes(shachain_index_t index)
bool wallet_shachain_add_hash(struct wallet *wallet, bool wallet_shachain_add_hash(struct wallet *wallet,
struct wallet_shachain *chain, struct wallet_shachain *chain,
shachain_index_t index, uint64_t index,
const struct sha256 *hash) const struct sha256 *hash)
{ {
tal_t *tmpctx = tal_tmpctx(wallet); tal_t *tmpctx = tal_tmpctx(wallet);

2
wallet/wallet.h

@ -142,7 +142,7 @@ bool wallet_shachain_init(struct wallet *wallet, struct wallet_shachain *chain);
*/ */
bool wallet_shachain_add_hash(struct wallet *wallet, bool wallet_shachain_add_hash(struct wallet *wallet,
struct wallet_shachain *chain, struct wallet_shachain *chain,
shachain_index_t index, uint64_t index,
const struct sha256 *hash); const struct sha256 *hash);
/* Simply passes through to shachain_get_hash since it doesn't touch /* Simply passes through to shachain_get_hash since it doesn't touch

5
wallet/wallet_tests.c

@ -81,7 +81,7 @@ static bool test_shachain_crud(void)
int fd = mkstemp(filename); int fd = mkstemp(filename);
struct wallet *w = tal(NULL, struct wallet); struct wallet *w = tal(NULL, struct wallet);
struct sha256 seed, hash; struct sha256 seed, hash;
shachain_index_t index = UINT64_MAX >> (64 - SHACHAIN_BITS); uint64_t index = UINT64_MAX >> (64 - SHACHAIN_BITS);
w->db = db_open(w, filename); w->db = db_open(w, filename);
CHECK_MSG(w->db, "Failed opening the db"); CHECK_MSG(w->db, "Failed opening the db");
@ -99,7 +99,8 @@ static bool test_shachain_crud(void)
CHECK(a.id == 1); CHECK(a.id == 1);
CHECK(a.chain.num_valid == 0 && a.chain.min_index == 0); CHECK(a.chain.num_valid == 0);
CHECK(shachain_next_index(&a.chain) == index);
for (int i=0; i<100; i++) { for (int i=0; i<100; i++) {
shachain_from_seed(&seed, index, &hash); shachain_from_seed(&seed, index, &hash);

Loading…
Cancel
Save