1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 14:28:07 +00:00

bignum: introduce load uint32/uint64

This commit is contained in:
Alex Beregszaszi 2016-08-26 00:12:02 +01:00
parent 339d2f44a9
commit d061139da9
3 changed files with 82 additions and 0 deletions

View File

@ -156,6 +156,32 @@ void bn_write_le(const bignum256 *in_number, uint8_t *out_number)
}
}
void bn_load_uint32(uint32_t in_number, bignum256 *out_number)
{
out_number->val[0] = in_number & 0x3FFFFFFF;
out_number->val[1] = in_number >> 30;
out_number->val[2] = 0;
out_number->val[3] = 0;
out_number->val[4] = 0;
out_number->val[5] = 0;
out_number->val[6] = 0;
out_number->val[7] = 0;
out_number->val[8] = 0;
}
void bn_load_uint64(uint64_t in_number, bignum256 *out_number)
{
out_number->val[0] = in_number & 0x3FFFFFFF;
out_number->val[1] = (in_number >>= 30) & 0x3FFFFFFF;
out_number->val[2] = in_number >>= 30;
out_number->val[3] = 0;
out_number->val[4] = 0;
out_number->val[5] = 0;
out_number->val[6] = 0;
out_number->val[7] = 0;
out_number->val[8] = 0;
}
// copies number a to b
void bn_copy(bignum256 *a, bignum256 *b)
{

View File

@ -54,6 +54,10 @@ void bn_read_le(const uint8_t *in_number, bignum256 *out_number);
void bn_write_le(const bignum256 *in_number, uint8_t *out_number);
void bn_load_uint32(uint32_t in_number, bignum256 *out_number);
void bn_load_uint64(uint64_t in_number, bignum256 *out_number);
void bn_copy(bignum256 *a, bignum256 *b);
void bn_zero(bignum256 *a);

52
tests.c
View File

@ -153,6 +153,56 @@ START_TEST(test_bignum_write_le)
}
END_TEST
START_TEST(test_bignum_load_uint32)
{
bignum256 a;
bignum256 b;
// lowest 30 bits set
bn_read_be(fromhex("000000000000000000000000000000000000000000000000000000003fffffff"), &a);
bn_load_uint32(0x3fffffff, &b);
ck_assert_int_eq(bn_is_equal(&a, &b), 1);
// bit 31 set
bn_read_be(fromhex("0000000000000000000000000000000000000000000000000000000040000000"), &a);
bn_load_uint32(0x40000000, &b);
ck_assert_int_eq(bn_is_equal(&a, &b), 1);
}
END_TEST
START_TEST(test_bignum_load_uint64)
{
bignum256 a;
bignum256 b;
// lowest 30 bits set
bn_read_be(fromhex("000000000000000000000000000000000000000000000000000000003fffffff"), &a);
bn_load_uint64(0x3fffffff, &b);
ck_assert_int_eq(bn_is_equal(&a, &b), 1);
// bit 31 set
bn_read_be(fromhex("0000000000000000000000000000000000000000000000000000000040000000"), &a);
bn_load_uint64(0x40000000, &b);
ck_assert_int_eq(bn_is_equal(&a, &b), 1);
// bit 33 set
bn_read_be(fromhex("0000000000000000000000000000000000000000000000000000000100000000"), &a);
bn_load_uint64(0x100000000LL, &b);
ck_assert_int_eq(bn_is_equal(&a, &b), 1);
// bit 61 set
bn_read_be(fromhex("0000000000000000000000000000000000000000000000002000000000000000"), &a);
bn_load_uint64(0x2000000000000000LL, &b);
ck_assert_int_eq(bn_is_equal(&a, &b), 1);
// all 64 bits set
bn_read_be(fromhex("000000000000000000000000000000000000000000000000ffffffffffffffff"), &a);
bn_load_uint64(0xffffffffffffffffLL, &b);
ck_assert_int_eq(bn_is_equal(&a, &b), 1);
}
END_TEST
START_TEST(test_bignum_copy)
{
bignum256 a;
@ -2445,6 +2495,8 @@ Suite *test_suite(void)
tcase_add_test(tc, test_bignum_equal);
tcase_add_test(tc, test_bignum_read_le);
tcase_add_test(tc, test_bignum_write_le);
tcase_add_test(tc, test_bignum_load_uint32);
tcase_add_test(tc, test_bignum_load_uint64);
tcase_add_test(tc, test_bignum_copy);
tcase_add_test(tc, test_bignum_is_even);
tcase_add_test(tc, test_bignum_is_odd);