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

simplify divmod58

This commit is contained in:
Pavol Rusnak 2013-09-25 12:39:23 +02:00
parent 74a5b04b81
commit 7ed18947ba
3 changed files with 16 additions and 37 deletions

View File

@ -502,41 +502,23 @@ void bn_substract_noprime(const bignum256 *a, const bignum256 *b, bignum256 *res
res->val[8] = a->val[8] - b->val[8] - carry;
}
// a / 58 = q (+r)
void bn_divmod58(const bignum256 *a, bignum256 *q, uint32_t *r)
// a / 58 = a (+r)
void bn_divmod58(bignum256 *a, uint32_t *r)
{
bignum256 i58, rem;
int na, i;
bn_zero(q);
bn_zero(&i58); i58.val[0] = 58;
if (bn_is_less(a, &i58)) {
*r = a->val[0];
int i;
uint32_t rem, tmp;
rem = a->val[8] % 58;
a->val[8] /= 58;
for (i = 7; i >= 0; i--) {
// 2^30 == 18512790*58 + 4
tmp = rem * 4 + a->val[i];
a->val[i] = rem * 18512790 + (tmp / 58);
rem = tmp % 58;
}
na = bn_bitlen(a);
for (i = 0; i < 9; i++) {
rem.val[i] = a->val[i];
}
for (i = 0; i <= na - 6; i++) {
bn_lshift(&i58);
}
for (i = na - 5; i >= 0; --i) {
bn_lshift(q);
if (!bn_is_less(&rem, &i58)) {
bn_substract_noprime(&rem, &i58, &rem);
q->val[0] |= 1;
}
bn_rshift(&i58);
}
*r = rem.val[0];
*r = rem;
}
#if 0
void bn_print(const bignum256 *a)
{

View File

@ -82,7 +82,7 @@ void bn_substract(const bignum256 *a, const bignum256 *b, bignum256 *res);
void bn_substract_noprime(const bignum256 *a, const bignum256 *b, bignum256 *res);
void bn_divmod58(const bignum256 *a, bignum256 *q, uint32_t *r);
void bn_divmod58(bignum256 *a, uint32_t *r);
#if 0
void bn_print(const bignum256 *a);

View File

@ -279,7 +279,7 @@ void ecdsa_get_address(const uint8_t *pub_key, char version, char *addr)
char *p = addr, s;
uint8_t a[32], b[21];
uint32_t r;
bignum256 c, q;
bignum256 c;
int i, l;
SHA256_Raw(pub_key, 33, a);
@ -296,12 +296,9 @@ void ecdsa_get_address(const uint8_t *pub_key, char version, char *addr)
bn_read_be(a, &c);
while (!bn_is_zero(&c)) {
bn_divmod58(&c, &q, &r);
bn_divmod58(&c, &r);
*p = code[r];
p++;
for (i = 0; i < 9; i++) {
c.val[i] = q.val[i];
}
}
if (a[0] == 0) {