mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-22 22:38:08 +00:00
add setbit, clearbit, testbit and xor to bignum
This commit is contained in:
parent
1259c36f80
commit
459f4a5e7a
27
bignum.c
27
bignum.c
@ -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.
|
||||
|
8
bignum.h
8
bignum.h
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user