Skip to content

Commit

Permalink
ccan: update to latest shachain.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Aug 20, 2017
1 parent 8ffdeea commit 1ffb9f0
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 38 deletions.
2 changes: 1 addition & 1 deletion ccan/README
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CCAN imported from http://ccodearchive.net.

CCAN version: init-2382-g3ffb94e9
CCAN version: init-2388-ge6abb93d
30 changes: 17 additions & 13 deletions ccan/ccan/crypto/shachain/shachain.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -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);

Expand All @@ -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;

Expand Down
33 changes: 18 additions & 15 deletions ccan/ccan/crypto/shachain/shachain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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);

/**
Expand All @@ -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];
};
Expand All @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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 */
2 changes: 1 addition & 1 deletion ccan/ccan/crypto/shachain/test/run-8bit.c
Original file line number Diff line number Diff line change
@@ -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. */
Expand Down
4 changes: 2 additions & 2 deletions ccan/ccan/crypto/shachain/test/run-can_derive.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion ccan/ccan/crypto/shachain/test/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions wallet/wallet_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down

0 comments on commit 1ffb9f0

Please sign in to comment.