Ai
1 Star 0 Fork 0

子安/libtommath

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bn_mp_rand.c 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
Daniel Mendler 提交于 2019-05-13 06:22 +08:00 . use enums mp_err, mp_ord, mp_bool, mp_sign
#include "tommath_private.h"
#ifdef BN_MP_RAND_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
mp_err(*s_mp_rand_source)(void *out, size_t size) = s_mp_rand_platform;
void mp_rand_source(mp_err(*source)(void *out, size_t size))
{
s_mp_rand_source = (source == NULL) ? s_mp_rand_platform : source;
}
/* makes a pseudo-random int of a given size */
mp_err mp_rand_digit(mp_digit *r)
{
mp_err ret = s_mp_rand_source(r, sizeof(mp_digit));
*r &= MP_MASK;
return ret;
}
mp_err mp_rand(mp_int *a, int digits)
{
int i;
mp_err ret;
mp_zero(a);
if (digits <= 0) {
return MP_OKAY;
}
if ((ret = mp_grow(a, digits)) != MP_OKAY) {
return ret;
}
if ((ret = s_mp_rand_source(a->dp, (size_t)digits * sizeof(mp_digit))) != MP_OKAY) {
return ret;
}
/* TODO: We ensure that the highest digit is nonzero. Should this be removed? */
while ((a->dp[digits - 1] & MP_MASK) == 0) {
if ((ret = s_mp_rand_source(a->dp + digits - 1, sizeof(mp_digit))) != MP_OKAY) {
return ret;
}
}
a->used = digits;
for (i = 0; i < digits; ++i) {
a->dp[i] &= MP_MASK;
}
return MP_OKAY;
}
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/andrewgithub/libtommath.git
git@gitee.com:andrewgithub/libtommath.git
andrewgithub
libtommath
libtommath
develop

搜索帮助