summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2022-12-11 23:54:08 +0100
committerMitya Selivanov <automainint@guattari.tech>2022-12-11 23:54:08 +0100
commit3729638138e3154e6672ade78d177d0b098b8236 (patch)
treeb2332e70c0d52ffddd4d90a14ec2a652ef4961b8 /source
parent0223e2faaebcb4065d9c37f55e8798196f911ae9 (diff)
downloadkit-3729638138e3154e6672ade78d177d0b098b8236.zip
[bigint] Fix hex parsing
Diffstat (limited to 'source')
-rw-r--r--source/kit/bigint.h2
-rw-r--r--source/test/unittests/bigint.test.c11
2 files changed, 12 insertions, 1 deletions
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"),