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

Merge pull request #66 from axic/bignum

Bignum: add bn_one
This commit is contained in:
Pavol Rusnak 2016-08-28 14:28:27 +02:00 committed by GitHub
commit 10a2a0014b
3 changed files with 53 additions and 4 deletions

View File

@ -204,6 +204,20 @@ void bn_zero(bignum256 *a)
}
}
// sets a bignum to one.
void bn_one(bignum256 *a)
{
a->val[0] = 1;
a->val[1] = 0;
a->val[2] = 0;
a->val[3] = 0;
a->val[4] = 0;
a->val[5] = 0;
a->val[6] = 0;
a->val[7] = 0;
a->val[8] = 0;
}
// checks that a bignum is zero.
// a must be normalized
// function is constant time (on some architectures, in particular ARM).
@ -459,7 +473,7 @@ void bn_sqrt(bignum256 *x, const bignum256 *prime)
// this method compute x^1/2 = x^(prime+1)/4
uint32_t i, j, limb;
bignum256 res, p;
bn_zero(&res); res.val[0] = 1;
bn_one(&res);
// compute p = (prime+1)/4
memcpy(&p, prime, sizeof(bignum256));
bn_addi(&p, 1);
@ -498,7 +512,7 @@ void bn_inverse(bignum256 *x, const bignum256 *prime)
// this method compute x^-1 = x^(prime-2)
uint32_t i, j, limb;
bignum256 res;
bn_zero(&res); res.val[0] = 1;
bn_one(&res);
for (i = 0; i < 9; i++) {
// invariants:
// x = old(x)^(2^(i*30))

View File

@ -85,6 +85,8 @@ void bn_zero(bignum256 *a);
int bn_is_zero(const bignum256 *a);
void bn_one(bignum256 *a);
static inline int bn_is_even(const bignum256 *a) {
return (a->val[0] & 1) == 0;
}

37
tests.c
View File

@ -111,7 +111,7 @@ START_TEST(test_bignum_write_be)
}
END_TEST
START_TEST(test_bignum_equal)
START_TEST(test_bignum_is_equal)
{
bignum256 a = { { 0x286d8bd5, 0x380c7c17, 0x3c6a2ec1, 0x2d787ef5, 0x14437cd3, 0x25a043f8, 0x1dd5263f, 0x33a162c3, 0x0000c55e } };
bignum256 b = { { 0x286d8bd5, 0x380c7c17, 0x3c6a2ec1, 0x2d787ef5, 0x14437cd3, 0x25a043f8, 0x1dd5263f, 0x33a162c3, 0x0000c55e } };
@ -153,6 +153,18 @@ START_TEST(test_bignum_is_zero)
}
END_TEST
START_TEST(test_bignum_one)
{
bignum256 a;
bignum256 b;
bn_read_be(fromhex("0000000000000000000000000000000000000000000000000000000000000001"), &a);
bn_one(&b);
ck_assert_int_eq(bn_is_equal(&a, &b), 1);
}
END_TEST
START_TEST(test_bignum_read_le)
{
bignum256 a;
@ -333,6 +345,25 @@ START_TEST(test_bignum_bitcount)
}
END_TEST
START_TEST(test_bignum_is_less)
{
bignum256 a;
bignum256 b;
bn_read_uint32(0x1234, &a);
bn_read_uint32(0x8765, &b);
ck_assert_int_eq(bn_is_less(&a, &b), 1);
ck_assert_int_eq(bn_is_less(&b, &a), 0);
bn_zero(&a);
bn_read_be(fromhex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), &b);
ck_assert_int_eq(bn_is_less(&a, &b), 1);
ck_assert_int_eq(bn_is_less(&b, &a), 0);
}
END_TEST
// from https://github.com/bitcoin/bitcoin/blob/master/src/test/data/base58_keys_valid.json
START_TEST(test_base58)
{
@ -2580,9 +2611,10 @@ Suite *test_suite(void)
tc = tcase_create("bignum");
tcase_add_test(tc, test_bignum_read_be);
tcase_add_test(tc, test_bignum_write_be);
tcase_add_test(tc, test_bignum_equal);
tcase_add_test(tc, test_bignum_is_equal);
tcase_add_test(tc, test_bignum_zero);
tcase_add_test(tc, test_bignum_is_zero);
tcase_add_test(tc, test_bignum_one);
tcase_add_test(tc, test_bignum_read_le);
tcase_add_test(tc, test_bignum_write_le);
tcase_add_test(tc, test_bignum_read_uint32);
@ -2593,6 +2625,7 @@ Suite *test_suite(void)
tcase_add_test(tc, test_bignum_is_even);
tcase_add_test(tc, test_bignum_is_odd);
tcase_add_test(tc, test_bignum_bitcount);
tcase_add_test(tc, test_bignum_is_less);
suite_add_tcase(s, tc);
tc = tcase_create("base58");