Skip to content

Commit c7f37f8

Browse files
author
Chandra Pratap
committed
fuzz-tests: test 8-to-5 bit conversion
Currently, the test only verifies the 5-to-8 bit conversion. Replace it with a roundtrip check that verifies 8-to-5 bit conversion as well.
1 parent bfb072e commit c7f37f8

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

tests/fuzz/fuzz-bech32.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,22 @@ void run(const uint8_t *data, size_t size)
3939
assert(memcmp(data_out, data + 1, data_out_len) == 0);
4040
}
4141

42-
data_out = tal_arr(tmpctx, uint8_t, size);
42+
data_out = tal_arr(tmpctx, uint8_t, size * 2);
4343

4444
/* This is also used as part of sign and check message. */
4545
data_out_len = 0;
46-
bech32_convert_bits(data_out, &data_out_len, 8, data, size, 5, 1);
47-
data_out_len = 0;
48-
bech32_convert_bits(data_out, &data_out_len, 8, data, size, 5, 0);
46+
/* First conversion uses pad=1 to ensure all bits are captured. */
47+
if (bech32_convert_bits(data_out, &data_out_len, 5, data, size, 8, 1)) {
48+
uint8_t *deconv_data_out = tal_arr(tmpctx, uint8_t, size);
49+
size_t deconv_data_out_len = 0;
50+
51+
/* Second uses pad=0 to discard padding bits during reconstruction. */
52+
if (bech32_convert_bits(deconv_data_out, &deconv_data_out_len, 8,
53+
data_out, data_out_len, 5, 0)) {
54+
assert(deconv_data_out_len == size);
55+
assert(memcmp(data, deconv_data_out, size) == 0);
56+
}
57+
}
4958

5059
addr = tal_arr(tmpctx, char, 73 + strlen(hrp_addr));
5160
for (int wit_version = 0; wit_version <= 16; ++wit_version) {

0 commit comments

Comments
 (0)