Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unfinished patch for switching from isaac to siphash...
diff --git a/gossipd/routing.c b/gossipd/routing.c
index 69db3001..5798c239 100644
--- a/gossipd/routing.c
+++ b/gossipd/routing.c
@@ -5,7 +5,6 @@
#include <ccan/array_size/array_size.h>
#include <ccan/crypto/siphash24/siphash24.h>
#include <ccan/endian/endian.h>
-#include <ccan/isaac/isaac64.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/str/str.h>
#include <common/features.h>
@@ -334,28 +333,18 @@ static u64 risk_fee(u64 amount, u32 delay, double riskfactor)
/* We track totals, rather than costs. That's because the fee depends
* on the current amount passing through. */
static void bfg_one_edge(struct node *node, size_t edgenum, double riskfactor,
- double fuzz, const struct isaac64_ctx *baserng)
+ double fuzz, const struct siphash_seed *base_seed)
{
struct node_connection *c = node->in[edgenum];
size_t h;
- struct isaac64_ctx myrng;
double fee_scale = 1.0;
- u8 *scid;
if (fuzz != 0.0) {
- /* Copy RNG state */
- myrng = *baserng;
-
- /* Provide the channel short ID, as additional
- * entropy. */
- scid = tal_arr(NULL, u8, 0);
- towire_short_channel_id(&scid, &c->short_channel_id);
- isaac64_reseed(&myrng,
- (const unsigned char *) scid, tal_len(scid));
- tal_free(scid);
+ u64 scid = short_channel_id_to_uint(&c->short_channel_id);
+ u64 h = siphash24(base_seed, &scid, sizeof(scid));
/* Scale fees for this channel */
- fee_scale = 1.0 + fuzz * isaac64_next_signed_double(&myrng);
+ fee_scale = 1.0 + fuzz * h / UINT64_MAX;
}
assert(c->dst == node);
@@ -403,7 +392,7 @@ static struct node_connection *
find_route(const tal_t *ctx, struct routing_state *rstate,
const struct pubkey *from, const struct pubkey *to, u64 msatoshi,
double riskfactor,
- double fuzz, struct isaac64_ctx *baserng,
+ double fuzz, const struct siphash_seed *base_seed,
u64 *fee, struct node_connection ***route)
{
struct node *n, *src, *dst;
@@ -465,7 +454,7 @@ find_route(const tal_t *ctx, struct routing_state *rstate,
continue;
}
bfg_one_edge(n, i, riskfactor,
- fuzz, baserng);
+ fuzz, base_seed);
SUPERVERBOSE("...done");
}
}
@@ -1144,7 +1133,7 @@ struct route_hop *get_route(tal_t *ctx, struct routing_state *rstate,
const struct pubkey *destination,
const u32 msatoshi, double riskfactor,
u32 final_cltv,
- double fuzz, u8 *seed)
+ double fuzz, const struct siphash_seed *base_seed)
{
struct node_connection **route;
u64 total_amount;
@@ -1153,14 +1142,10 @@ struct route_hop *get_route(tal_t *ctx, struct routing_state *rstate,
struct route_hop *hops;
int i;
struct node_connection *first_conn;
- isaac64_ctx baserng;
-
- /* Load base RNG */
- isaac64_init(&baserng, (const unsigned char*) seed, tal_count(seed));
first_conn = find_route(ctx, rstate, source, destination, msatoshi,
riskfactor / BLOCKS_PER_YEAR / 10000,
- fuzz, &baserng,
+ fuzz, base_seed,
&fee, &route);
if (!first_conn) {
diff --git a/gossipd/routing.h b/gossipd/routing.h
index 1afd71b1..b68bfe66 100644
--- a/gossipd/routing.h
+++ b/gossipd/routing.h
@@ -187,7 +187,8 @@ struct route_hop *get_route(tal_t *ctx, struct routing_state *rstate,
const struct pubkey *destination,
const u32 msatoshi, double riskfactor,
u32 final_cltv,
- double fuzz, u8 *seed);
+ double fuzz,
+ const struct siphash_seed *base_seed);
/* Disable channel(s) based on the given routing failure. */
void routing_failure(struct routing_state *rstate,
const struct pubkey *erring_node,
diff --git a/gossipd/test/run-bench-find_route.c b/gossipd/test/run-bench-find_route.c
index 03643f7d..42a1abed 100644
--- a/gossipd/test/run-bench-find_route.c
+++ b/gossipd/test/run-bench-find_route.c
@@ -177,6 +167,7 @@ int main(int argc, char *argv[])
struct pubkey me = nodeid(0);
bool perfme = false;
const double riskfactor = 0.01 / BLOCKS_PER_YEAR / 10000;
+ struct siphash_seed base_seed;
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN);
@@ -194,6 +185,7 @@ int main(int argc, char *argv[])
if (argc > 3)
opt_usage_and_exit("[num_nodes [num_runs]]");
+ memset(&base_seed, 0, sizeof(base_seed));
for (size_t i = 0; i < num_nodes; i++)
populate_random_node(rstate, i);
@@ -212,7 +204,7 @@ int main(int argc, char *argv[])
nc = find_route(ctx, rstate, &from, &to,
pseudorand(100000),
riskfactor,
- 0.0, NULL,
+ 0.75, &base_seed,
&fee, &route);
num_success += (nc != NULL);
tal_free(route);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment