daemon: use siphash for hashes.

Remove ccan/hash (aka Jenkins lookup3) altogether.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2016-06-29 06:49:20 +09:30
parent 92246c427b
commit 04b3e8f91d
10 changed files with 38 additions and 1734 deletions

View File

@@ -1,28 +1,42 @@
#include "pseudorand.h"
#include <assert.h>
#include <ccan/crypto/siphash24/siphash24.h>
#include <ccan/err/err.h>
#include <ccan/isaac/isaac64.h>
#include <ccan/likely/likely.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <sodium/randombytes.h>
#include <stdbool.h>
#include <string.h>
static struct isaac64_ctx isaac64;
static struct siphash_seed siphashseed;
static bool pseudorand_initted = false;
uint64_t pseudorand(uint64_t max)
static void init_if_needed(void)
{
if (unlikely(!pseudorand_initted)) {
unsigned char seedbuf[16];
/* PRNG */
if (RAND_bytes(seedbuf, sizeof(seedbuf)) != 1)
errx(1, "Could not seed PRNG: %s",
ERR_error_string(ERR_get_error(), NULL));
randombytes_buf(seedbuf, sizeof(seedbuf));
isaac64_init(&isaac64, seedbuf, sizeof(seedbuf));
memcpy(&siphashseed, seedbuf, sizeof(siphashseed));
pseudorand_initted = true;
}
}
uint64_t pseudorand(uint64_t max)
{
init_if_needed();
assert(max);
return isaac64_next_uint(&isaac64, max);
}
const struct siphash_seed *siphash_seed(void)
{
init_if_needed();
return &siphashseed;
}

View File

@@ -7,4 +7,10 @@
* pseudorand - pseudo (guessable!) random number between 0 and max-1.
*/
uint64_t pseudorand(uint64_t max);
/**
* Get the siphash seed for hash tables.
*/
const struct siphash_seed *siphash_seed(void);
#endif /* LIGHTNING_DAEMON_PSEUDORAND_H */

View File

@@ -33,9 +33,10 @@
#include "lightningd.h"
#include "log.h"
#include "peer.h"
#include "pseudorand.h"
#include "timeout.h"
#include "watch.h"
#include <ccan/hash/hash.h>
#include <ccan/crypto/siphash24/siphash24.h>
#include <ccan/ptrint/ptrint.h>
#include <ccan/structeq/structeq.h>
@@ -46,7 +47,11 @@ const struct txwatch_output *txowatch_keyof(const struct txowatch *w)
size_t txo_hash(const struct txwatch_output *out)
{
return hash(&out->txid, 1, out->index);
/* This hash-in-one-go trick only works if they're consecutive. */
BUILD_ASSERT(offsetof(struct txwatch_output, index)
== sizeof(((struct txwatch_output *)NULL)->txid));
return siphash24(siphash_seed(), &out->txid,
sizeof(out->txid) + sizeof(out->index));
}
bool txowatch_eq(const struct txowatch *w, const struct txwatch_output *out)
@@ -67,7 +72,7 @@ const struct sha256_double *txwatch_keyof(const struct txwatch *w)
size_t txid_hash(const struct sha256_double *txid)
{
return hash(txid->sha.u.u8, sizeof(txid->sha.u.u8), 0);
return siphash24(siphash_seed(), txid->sha.u.u8, sizeof(txid->sha.u.u8));
}
bool txwatch_eq(const struct txwatch *w, const struct sha256_double *txid)