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:
makejian 2023-06-25 20:35:21 +08:00 committed by Xiang Xiao
parent 12935f2d33
commit aeac109e50
2 changed files with 37 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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 */