crypto/bn:Add exponentiation algorithm in bignum
add exponentiation algorithm: pow_mod_faster Signed-off-by: makejian <makejian@xiaomi.com>
This commit is contained in:
parent
12935f2d33
commit
aeac109e50
32
crypto/bn.c
32
crypto/bn.c
@ -762,3 +762,35 @@ void bignum_assign(FAR struct bn *dst, FAR struct bn *src)
|
||||
dst->array[i] = src->array[i];
|
||||
}
|
||||
}
|
||||
|
||||
void pow_mod_faster(FAR struct bn *a, FAR struct bn *b,
|
||||
FAR struct bn *n, FAR struct bn *res)
|
||||
{
|
||||
struct bn tmpa;
|
||||
struct bn tmpb;
|
||||
struct bn tmp;
|
||||
bignum_assign(&tmpa, a);
|
||||
bignum_assign(&tmpb, b);
|
||||
|
||||
bignum_from_int(res, 1); /* r = 1 */
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (tmpb.array[0] & 1) /* if (b % 2) */
|
||||
{
|
||||
bignum_mul(res, &tmpa, &tmp); /* r = r * a % m */
|
||||
bignum_mod(&tmp, n, res);
|
||||
}
|
||||
|
||||
bignum_rshift(&tmpb, &tmp, 1); /* b /= 2 */
|
||||
bignum_assign(&tmpb, &tmp);
|
||||
|
||||
if (bignum_is_zero(&tmpb))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
bignum_mul(&tmpa, &tmpa, &tmp);
|
||||
bignum_mod(&tmp, n, &tmpa);
|
||||
}
|
||||
}
|
||||
|
@ -204,4 +204,9 @@ void bignum_isqrt(FAR struct bn *a, FAR struct bn *b);
|
||||
|
||||
void bignum_assign(FAR struct bn *dst, FAR struct bn *src);
|
||||
|
||||
/* CRK_EXP_MOD algorithm */
|
||||
|
||||
void pow_mod_faster(FAR struct bn *a, FAR struct bn *b,
|
||||
FAR struct bn *n, FAR struct bn *res);
|
||||
|
||||
#endif /* __INCLUDE_CRYPTO_BIGNUM_H */
|
||||
|
Loading…
Reference in New Issue
Block a user