From 3729638138e3154e6672ade78d177d0b098b8236 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Sun, 11 Dec 2022 23:54:08 +0100 Subject: [bigint] Fix hex parsing --- source/kit/bigint.h | 2 +- source/test/unittests/bigint.test.c | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/source/kit/bigint.h b/source/kit/bigint.h index 7bb6a24..b2fa49f 100644 --- a/source/kit/bigint.h +++ b/source/kit/bigint.h @@ -380,7 +380,7 @@ static kit_bigint_t kit_bi_hex(kit_str_t const hex) { for (ptrdiff_t i = 0; i < hex.size && i / 2 < KIT_BIGINT_SIZE; i++) { uint8_t const digit = kit_hex_digit(hex.values[hex.size - i - 1]); - z.v[i / 8] |= digit << (8 * (i % 8)); + z.v[i / 8] |= digit << (4 * (i % 8)); } return z; diff --git a/source/test/unittests/bigint.test.c b/source/test/unittests/bigint.test.c index 5ca49b5..0b45d7b 100644 --- a/source/test/unittests/bigint.test.c +++ b/source/test/unittests/bigint.test.c @@ -6,6 +6,17 @@ static_assert(sizeof(bigint_t) == 256, "KIT_BIGINT_SIZE check"); +TEST("bigint bin hex") { + REQUIRE(bi_equal(HEX("10"), BIN("10000"))); + REQUIRE(bi_equal(HEX("20"), BIN("100000"))); + REQUIRE(bi_equal(HEX("40"), BIN("1000000"))); + REQUIRE(bi_equal(HEX("80"), BIN("10000000"))); + REQUIRE(bi_equal(HEX("310"), BIN("1100010000"))); + REQUIRE(bi_equal(HEX("420"), BIN("10000100000"))); + REQUIRE(bi_equal(HEX("540"), BIN("10101000000"))); + REQUIRE(bi_equal(HEX("780"), BIN("11110000000"))); +} + TEST("bigint hex add") { REQUIRE(bi_equal( bi_add(HEX("4242424242424242424242424242424242424242"), -- cgit v1.2.3