1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-21 23:18:13 +00:00

add setbit, clearbit, testbit and xor to bignum

This commit is contained in:
Pavol Rusnak 2016-10-31 17:26:24 +01:00
parent 1259c36f80
commit 459f4a5e7a
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 35 additions and 0 deletions

View File

@ -296,6 +296,33 @@ void bn_rshift(bignum256 *a)
a->val[8] >>= 1;
}
// sets bit in bignum
void bn_setbit(bignum256 *a, uint8_t bit)
{
a->val[bit / 30] |= (1 << (bit % 30));
}
// clears bit in bignum
void bn_clearbit(bignum256 *a, uint8_t bit)
{
a->val[bit / 30] &= ~(1 << (bit % 30));
}
// tests bit in bignum
uint32_t bn_testbit(bignum256 *a, uint8_t bit)
{
return a->val[bit / 30] & (1 << (bit % 30));
}
// a = b ^ c
void bn_xor(bignum256 *a, const bignum256 *b, const bignum256 *c)
{
int i;
for (i = 0; i < 9; i++) {
a->val[i] = b->val[i] ^ c->val[i];
}
}
// multiply x by 1/2 modulo prime.
// it computes x = (x & 1) ? (x + prime) >> 1 : x >> 1.
// assumes x is normalized.

View File

@ -105,6 +105,14 @@ void bn_lshift(bignum256 *a);
void bn_rshift(bignum256 *a);
void bn_setbit(bignum256 *a, uint8_t bit);
void bn_clearbit(bignum256 *a, uint8_t bit);
uint32_t bn_testbit(bignum256 *a, uint8_t bit);
void bn_xor(bignum256 *a, const bignum256 *b, const bignum256 *c);
void bn_mult_half(bignum256 *x, const bignum256 *prime);
void bn_mult_k(bignum256 *x, uint8_t k, const bignum256 *prime);