Skip to content

Commit 1e251cf

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 1e251cf

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

tests/fuzz/fuzz-bech32.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,28 @@ 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+
/* Convert data to 5-bit values (0-31) */
43+
u8 *five_bit_data = tal_dup_arr(tmpctx, u8, data, size, 0);
44+
for (size_t i = 0; i < size; i++)
45+
five_bit_data[i] &= 0x1F;
4346

44-
/* This is also used as part of sign and check message. */
45-
data_out_len = 0;
46-
bech32_convert_bits(data_out, &data_out_len, 8, data, size, 5, 1);
47+
u8 *eight_bit_data = tal_arr(tmpctx, u8, size);
48+
size_t eight_bit_len = 0;
49+
/* Convert 5-to-8 without padding */
50+
if (bech32_convert_bits(eight_bit_data, &eight_bit_len, 8,
51+
five_bit_data, size, 5, 0)) {
52+
u8 *five_bit_deconv = tal_arr(tmpctx, u8, size);
53+
size_t five_bit_deconv_len = 0;
54+
/* Convert 8-to-5 with padding */
55+
if (bech32_convert_bits(five_bit_deconv, &five_bit_deconv_len, 5,
56+
eight_bit_data, eight_bit_len, 8, 1)) {
57+
assert(five_bit_deconv_len == size);
58+
assert(memcmp(five_bit_data, five_bit_deconv, five_bit_deconv_len) == 0);
59+
}
60+
}
61+
62+
data_out = tal_arr(tmpctx, uint8_t, size);
4763
data_out_len = 0;
48-
bech32_convert_bits(data_out, &data_out_len, 8, data, size, 5, 0);
4964

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

0 commit comments

Comments
 (0)