mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 01:43:36 +01:00
ccan: update to latest shachain.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
8ffdeea522
commit
1ffb9f07c1
@ -1,3 +1,3 @@
|
||||
CCAN imported from http://ccodearchive.net.
|
||||
|
||||
CCAN version: init-2382-g3ffb94e9
|
||||
CCAN version: init-2388-ge6abb93d
|
||||
|
@ -10,7 +10,7 @@ static void change_bit(unsigned char *arr, size_t index)
|
||||
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
|
||||
return index ? (unsigned int)__builtin_ctzll(index) : SHACHAIN_BITS;
|
||||
@ -25,24 +25,24 @@ static unsigned int count_trailing_zeroes(shachain_index_t index)
|
||||
#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. */
|
||||
if (from == 0)
|
||||
return true;
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
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,
|
||||
struct sha256 *hash)
|
||||
{
|
||||
shachain_index_t branches;
|
||||
uint64_t branches;
|
||||
int i;
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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,
|
||||
shachain_index_t index, const struct sha256 *hash)
|
||||
uint64_t index, const struct sha256 *hash)
|
||||
{
|
||||
unsigned int i, pos;
|
||||
|
||||
/* You have to insert them in order! */
|
||||
assert(index == chain->min_index - 1 ||
|
||||
(index == (shachain_index_t)(UINT64_MAX >> (64 - SHACHAIN_BITS))
|
||||
&& chain->num_valid == 0));
|
||||
assert(index == shachain_next_index(chain));
|
||||
|
||||
pos = count_trailing_zeroes(index);
|
||||
|
||||
@ -104,7 +108,7 @@ bool shachain_add_hash(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;
|
||||
|
||||
|
@ -6,13 +6,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* Useful for testing. */
|
||||
#ifndef shachain_index_t
|
||||
#define shachain_index_t uint64_t
|
||||
#endif
|
||||
|
||||
#ifndef SHACHAIN_BITS
|
||||
#define SHACHAIN_BITS (sizeof(shachain_index_t) * 8)
|
||||
#define SHACHAIN_BITS (sizeof(uint64_t) * 8)
|
||||
#endif
|
||||
|
||||
/**
|
||||
@ -42,7 +37,7 @@
|
||||
* 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);
|
||||
|
||||
/**
|
||||
@ -55,10 +50,10 @@ void shachain_from_seed(const struct sha256 *seed, shachain_index_t index,
|
||||
* added.
|
||||
*/
|
||||
struct shachain {
|
||||
shachain_index_t min_index;
|
||||
uint64_t min_index;
|
||||
unsigned int num_valid;
|
||||
struct {
|
||||
shachain_index_t index;
|
||||
uint64_t index;
|
||||
struct sha256 hash;
|
||||
} known[SHACHAIN_BITS + 1];
|
||||
};
|
||||
@ -71,20 +66,28 @@ struct shachain {
|
||||
*/
|
||||
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.
|
||||
* @chain: the chain to add to
|
||||
* @index: the index of the hash
|
||||
* @hash: the hash value.
|
||||
*
|
||||
* You can only add index 0xFFFFFFFFFFFFFFFF (for a freshly
|
||||
* initialized chain), or one less than the previously successfully
|
||||
* added value.
|
||||
* You can only add shachain_next_index(@chain).
|
||||
*
|
||||
* This can fail (return false without altering @chain) if the hash
|
||||
* for this index isn't consistent with previous hashes (ie. wasn't
|
||||
* 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.
|
||||
*
|
||||
* Example:
|
||||
@ -98,7 +101,7 @@ void shachain_init(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.
|
||||
@ -129,5 +132,5 @@ bool shachain_add_hash(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 */
|
||||
|
@ -1,4 +1,4 @@
|
||||
#define shachain_index_t uint8_t
|
||||
#define SHACHAIN_BITS 8
|
||||
|
||||
#include <ccan/crypto/shachain/shachain.h>
|
||||
/* Include the C files directly. */
|
||||
|
@ -7,13 +7,13 @@
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
|
@ -13,7 +13,7 @@ int main(void)
|
||||
uint64_t i, j;
|
||||
|
||||
/* 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));
|
||||
/* Generate a whole heap. */
|
||||
@ -34,6 +34,7 @@ int main(void)
|
||||
i--) {
|
||||
struct sha256 hash;
|
||||
int expidx = 0xFFFFFFFFFFFFFFFFULL - i;
|
||||
ok1(shachain_next_index(&chain) == i);
|
||||
ok1(shachain_add_hash(&chain, i, &expect[expidx]));
|
||||
for (j = i; j != 0; j++) {
|
||||
ok1(shachain_get_hash(&chain, j, &hash));
|
||||
|
@ -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 */
|
||||
static unsigned int count_trailing_zeroes(shachain_index_t index)
|
||||
static unsigned int count_trailing_zeroes(uint64_t index)
|
||||
{
|
||||
#if HAVE_BUILTIN_CTZLL
|
||||
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,
|
||||
struct wallet_shachain *chain,
|
||||
shachain_index_t index,
|
||||
uint64_t index,
|
||||
const struct sha256 *hash)
|
||||
{
|
||||
tal_t *tmpctx = tal_tmpctx(wallet);
|
||||
|
@ -142,7 +142,7 @@ bool wallet_shachain_init(struct wallet *wallet, struct wallet_shachain *chain);
|
||||
*/
|
||||
bool wallet_shachain_add_hash(struct wallet *wallet,
|
||||
struct wallet_shachain *chain,
|
||||
shachain_index_t index,
|
||||
uint64_t index,
|
||||
const struct sha256 *hash);
|
||||
|
||||
/* Simply passes through to shachain_get_hash since it doesn't touch
|
||||
|
@ -81,7 +81,7 @@ static bool test_shachain_crud(void)
|
||||
int fd = mkstemp(filename);
|
||||
struct wallet *w = tal(NULL, struct wallet);
|
||||
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);
|
||||
CHECK_MSG(w->db, "Failed opening the db");
|
||||
@ -99,7 +99,8 @@ static bool test_shachain_crud(void)
|
||||
|
||||
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++) {
|
||||
shachain_from_seed(&seed, index, &hash);
|
||||
|
Loading…
Reference in New Issue
Block a user