mirror of
https://github.com/aljazceru/lightning.git
synced 2026-02-08 15:44:19 +01:00
permute_tx: fix horrible thinko.
Map needs to be inverted, otherwise non-trivial shuffles get confused. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
19
permute_tx.c
19
permute_tx.c
@@ -45,6 +45,21 @@ static void init_map(size_t *map, size_t len)
|
|||||||
map[i] = i;
|
map[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* This map says where things ended up, eg. 0 might be in slot 3. we
|
||||||
|
* want to change it so map[0] = 3. */
|
||||||
|
static void invert_map(size_t *map, size_t len)
|
||||||
|
{
|
||||||
|
if (map) {
|
||||||
|
size_t i, newmap[len];
|
||||||
|
|
||||||
|
memset(newmap, 0, sizeof(newmap));
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
newmap[map[i]] = i;
|
||||||
|
}
|
||||||
|
memcpy(map, newmap, sizeof(newmap));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static bool input_better(const struct bitcoin_tx_input *a,
|
static bool input_better(const struct bitcoin_tx_input *a,
|
||||||
const struct bitcoin_tx_input *b)
|
const struct bitcoin_tx_input *b)
|
||||||
{
|
{
|
||||||
@@ -117,6 +132,8 @@ void permute_inputs(uint64_t seed1, uint64_t seed2, uint64_t tx_num,
|
|||||||
size_t r = get_next_rand(&h, &randidx) % (num_inputs - i - 1);
|
size_t r = get_next_rand(&h, &randidx) % (num_inputs - i - 1);
|
||||||
swap_inputs(inputs, map, i, i + 1 + r);
|
swap_inputs(inputs, map, i, i + 1 + r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
invert_map(map, num_inputs);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void swap_outputs(struct bitcoin_tx_output *outputs, size_t *map,
|
static void swap_outputs(struct bitcoin_tx_output *outputs, size_t *map,
|
||||||
@@ -183,4 +200,6 @@ void permute_outputs(uint64_t seed1, uint64_t seed2, size_t tx_num,
|
|||||||
size_t r = get_next_rand(&h, &randidx) % (num_outputs - i - 1);
|
size_t r = get_next_rand(&h, &randidx) % (num_outputs - i - 1);
|
||||||
swap_outputs(outputs, map, i, i + 1 + r);
|
swap_outputs(outputs, map, i, i + 1 + r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
invert_map(map, num_outputs);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user