本文整理匯總了C++中BN_lshift函數的典型用法代碼示例。如果您正苦於以下問題:C++ BN_lshift函數的具體用法?C++ BN_lshift怎麽用?C++ BN_lshift使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了BN_lshift函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。
示例1: BN_solinas2bn
int BN_solinas2bn(const BN_SOLINAS *solinas, BIGNUM *bn)
{
int ret = 0;
BIGNUM *tmp = NULL;
if (!solinas || !bn) {
BNerr(BN_F_BN_SOLINAS2BN, ERR_R_MALLOC_FAILURE);
return 0;
}
if (solinas->b <= 0 || solinas->a <= solinas->b
|| (solinas->s != 1 && solinas->s != -1)
|| (solinas->c != 1 && solinas->c != -1)) {
BNerr(BN_F_BN_SOLINAS2BN, BN_R_INVALID_SOLINAS_PARAMETERS);
return 0;
}
if (!(tmp = BN_new())) {
BNerr(BN_F_BN_SOLINAS2BN, ERR_R_MALLOC_FAILURE);
goto end;
}
BN_one(tmp);
if (!BN_lshift(bn, tmp, solinas->a)) {
BNerr(BN_F_BN_SOLINAS2BN, ERR_R_BN_LIB);
goto end;
}
if (!BN_lshift(tmp, tmp, solinas->b)) {
BNerr(BN_F_BN_SOLINAS2BN, ERR_R_BN_LIB);
goto end;
}
if (!BN_add_word(tmp, solinas->c)) {
BNerr(BN_F_BN_SOLINAS2BN, ERR_R_BN_LIB);
goto end;
}
if (solinas->s > 0) {
if (!BN_add(bn, bn, tmp)) {
BNerr(BN_F_BN_SOLINAS2BN, ERR_R_BN_LIB);
goto end;
}
} else {
if (!BN_sub(bn, bn, tmp)) {
BNerr(BN_F_BN_SOLINAS2BN, ERR_R_BN_LIB);
goto end;
}
}
/* check if it is a prime */
ret = 1;
end:
BN_free(tmp);
return ret;
}
示例2: BN_mod
/* rem != m */
int BN_mod(BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
{
#if 0 /* The old slow way */
int i, nm, nd;
BIGNUM *dv;
if(BN_ucmp(m, d) < 0)
{ return ((BN_copy(rem, m) == NULL) ? 0 : 1); }
BN_CTX_start(ctx);
dv = BN_CTX_get(ctx);
if(!BN_copy(rem, m)) { goto err; }
nm = BN_num_bits(rem);
nd = BN_num_bits(d);
if(!BN_lshift(dv, d, nm - nd)) { goto err; }
for(i = nm - nd; i >= 0; i--)
{
if(BN_cmp(rem, dv) >= 0)
{
if(!BN_sub(rem, rem, dv)) { goto err; }
}
if(!BN_rshift1(dv, dv)) { goto err; }
}
BN_CTX_end(ctx);
return (1);
err:
BN_CTX_end(ctx);
return (0);
#else
return (BN_div(NULL, rem, m, d, ctx));
#endif
}
示例3: BN_div_word
BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
{
BN_ULONG ret = 0;
int i, j;
bn_check_top(a);
w &= BN_MASK2;
if (!w)
/* actually this an error (division by zero) */
return (BN_ULONG)-1;
if (a->top == 0)
return 0;
/* normalize input (so bn_div_words doesn't complain) */
j = BN_BITS2 - BN_num_bits_word(w);
w <<= j;
if (!BN_lshift(a, a, j))
return (BN_ULONG)-1;
for (i=a->top-1; i>=0; i--)
{
BN_ULONG l,d;
l=a->d[i];
d=bn_div_words(ret,l,w);
ret=(l-((d*w)&BN_MASK2))&BN_MASK2;
a->d[i]=d;
}
if ((a->top > 0) && (a->d[a->top-1] == 0))
a->top--;
ret >>= j;
bn_check_top(a);
return(ret);
}
示例4: BN_div
int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
BN_CTX *ctx)
{
int i,nm,nd;
int ret = 0;
BIGNUM *D;
bn_check_top(m);
bn_check_top(d);
if (BN_is_zero(d))
{
BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);
return(0);
}
if (BN_ucmp(m,d) < 0)
{
if (rem != NULL)
{ if (BN_copy(rem,m) == NULL) return(0); }
if (dv != NULL) BN_zero(dv);
return(1);
}
BN_CTX_start(ctx);
D = BN_CTX_get(ctx);
if (dv == NULL) dv = BN_CTX_get(ctx);
if (rem == NULL) rem = BN_CTX_get(ctx);
if (D == NULL || dv == NULL || rem == NULL)
goto end;
nd=BN_num_bits(d);
nm=BN_num_bits(m);
if (BN_copy(D,d) == NULL) goto end;
if (BN_copy(rem,m) == NULL) goto end;
/* The next 2 are needed so we can do a dv->d[0]|=1 later
* since BN_lshift1 will only work once there is a value :-) */
BN_zero(dv);
if(bn_wexpand(dv,1) == NULL) goto end;
dv->top=1;
if (!BN_lshift(D,D,nm-nd)) goto end;
for (i=nm-nd; i>=0; i--)
{
if (!BN_lshift1(dv,dv)) goto end;
if (BN_ucmp(rem,D) >= 0)
{
dv->d[0]|=1;
if (!BN_usub(rem,rem,D)) goto end;
}
/* CAN IMPROVE (and have now :=) */
if (!BN_rshift1(D,D)) goto end;
}
rem->neg=BN_is_zero(rem)?0:m->neg;
dv->neg=m->neg^d->neg;
ret = 1;
end:
BN_CTX_end(ctx);
return(ret);
}
示例5: test_div
int
test_div(BIO *bp, BN_CTX *ctx)
{
BIGNUM a, b,c, d, e;
int i;
int rc = 1;
BN_init(&a);
BN_init(&b);
BN_init(&c);
BN_init(&d);
BN_init(&e);
for (i = 0; i < num0 + num1; i++) {
if (i < num1) {
BN_bntest_rand(&a, 400, 0, 0);
BN_copy(&b, &a);
BN_lshift(&a, &a, i);
BN_add_word(&a, i);
} else
BN_bntest_rand(&b, 50 + 3*(i - num1), 0, 0);
a.neg = rand_neg();
b.neg = rand_neg();
BN_div(&d, &c, &a, &b, ctx);
if (bp != NULL) {
if (!results) {
BN_print(bp, &a);
BIO_puts(bp, " / ");
BN_print(bp, &b);
BIO_puts(bp, " - ");
}
BN_print(bp, &d);
BIO_puts(bp, "\n");
if (!results) {
BN_print(bp, &a);
BIO_puts(bp, " % ");
BN_print(bp, &b);
BIO_puts(bp, " - ");
}
BN_print(bp, &c);
BIO_puts(bp, "\n");
}
BN_mul(&e, &d, &b, ctx);
BN_add(&d, &e, &c);
BN_sub(&d, &d, &a);
if (!BN_is_zero(&d)) {
fprintf(stderr, "Division test failed!\n");
rc = 0;
break;
}
}
BN_free(&a);
BN_free(&b);
BN_free(&c);
BN_free(&d);
BN_free(&e);
return (rc);
}
示例6: hit2hit_key
/*
* \fn hit2hit_key
*
* \param hit 128-bit Host Identity Tag
* \param hit_key buffer for storing HIT_KEY; should be DHT_VAL_SIZE long
*
* \brief Create a HIT_KEY from a HIT by taking the middle 100 bits and adding
* padding.
*/
void hit2hit_key(hip_hit *hit, __u8 *hit_key)
{
BIGNUM *hk = BN_bin2bn((const unsigned char *)hit, HIT_SIZE, NULL);
BN_lshift(hk, hk, 28); /* truncate to 100-bit number */
memset(hit_key, 0, DHT_KEY_SIZE);
bn2bin_safe(hk, hit_key, 16); /* lower 28-bits now zeroes */
BN_free(hk);
}
示例7: test_lshift
int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
{
BIGNUM *a,*b,*c,*d;
int i;
b=BN_new();
c=BN_new();
d=BN_new();
BN_one(c);
if(a_)
a=a_;
else
{
a=BN_new();
BN_bntest_rand(a,200,0,0); /**/
a->neg=rand_neg();
}
for (i=0; i<num0; i++)
{
BN_lshift(b,a,i+1);
BN_add(c,c,c);
if (bp != NULL)
{
if (!results)
{
BN_print(bp,a);
BIO_puts(bp," * ");
BN_print(bp,c);
BIO_puts(bp," - ");
}
BN_print(bp,b);
BIO_puts(bp,"\n");
}
BN_mul(d,a,c,ctx);
BN_sub(d,d,b);
if(!BN_is_zero(d))
{
fprintf(stderr,"Left shift test failed!\n");
fprintf(stderr,"a=");
BN_print_fp(stderr,a);
fprintf(stderr,"\nb=");
BN_print_fp(stderr,b);
fprintf(stderr,"\nc=");
BN_print_fp(stderr,c);
fprintf(stderr,"\nd=");
BN_print_fp(stderr,d);
fprintf(stderr,"\n");
return 0;
}
}
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
return(1);
}
示例8: Java_java_math_NativeBN_BN_1shift
extern "C" void Java_java_math_NativeBN_BN_1shift(JNIEnv* env, jclass, jlong r, jlong a, int n) {
if (!twoValidHandles(env, r, a)) return;
if (n >= 0) {
BN_lshift(toBigNum(r), toBigNum(a), n);
} else {
BN_rshift(toBigNum(r), toBigNum(a), -n);
}
throwExceptionIfNecessary(env);
}
示例9: bn_check_top
static BIGNUM *euclid(BIGNUM *a, BIGNUM *b)
{
BIGNUM *t;
int shifts=0;
bn_check_top(a);
bn_check_top(b);
/* 0 <= b <= a */
while (!BN_is_zero(b))
{
/* 0 < b <= a */
if (BN_is_odd(a))
{
if (BN_is_odd(b))
{
if (!BN_sub(a,a,b)) goto err;
if (!BN_rshift1(a,a)) goto err;
if (BN_cmp(a,b) < 0)
{ t=a; a=b; b=t; }
}
else /* a odd - b even */
{
if (!BN_rshift1(b,b)) goto err;
if (BN_cmp(a,b) < 0)
{ t=a; a=b; b=t; }
}
}
else /* a is even */
{
if (BN_is_odd(b))
{
if (!BN_rshift1(a,a)) goto err;
if (BN_cmp(a,b) < 0)
{ t=a; a=b; b=t; }
}
else /* a even - b even */
{
if (!BN_rshift1(a,a)) goto err;
if (!BN_rshift1(b,b)) goto err;
shifts++;
}
}
/* 0 <= b <= a */
}
if (shifts)
{
if (!BN_lshift(a,a,shifts)) goto err;
}
bn_check_top(a);
return(a);
err:
return(NULL);
}
示例10: test_div
int test_div(BIO *bp, BN_CTX *ctx)
{
BIGNUM *a, *b, *c, *d, *e;
int i;
a = BN_new();
b = BN_new();
c = BN_new();
d = BN_new();
e = BN_new();
for (i = 0; i < num0 + num1; i++) {
if (i < num1) {
BN_bntest_rand(a, 400, 0, 0);
BN_copy(b, a);
BN_lshift(a, a, i);
BN_add_word(a, i);
} else
BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);
a->neg = rand_neg();
b->neg = rand_neg();
BN_div(d, c, a, b, ctx);
if (bp != NULL) {
if (!results) {
BN_print(bp, a);
BIO_puts(bp, " / ");
BN_print(bp, b);
BIO_puts(bp, " - ");
}
BN_print(bp, d);
BIO_puts(bp, "\n");
if (!results) {
BN_print(bp, a);
BIO_puts(bp, " % ");
BN_print(bp, b);
BIO_puts(bp, " - ");
}
BN_print(bp, c);
BIO_puts(bp, "\n");
}
BN_mul(e, d, b, ctx);
BN_add(d, e, c);
BN_sub(d, d, a);
if (!BN_is_zero(d)) {
fprintf(stderr, "Division test failed!\n");
return 0;
}
}
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
return (1);
}
示例11: fermat_question_ask
static RSA *
fermat_question_ask(const RSA *rsa)
{
BIGNUM
*a = BN_new(),
*b = BN_new(),
*a2 = BN_new(),
*b2 = BN_new();
BIGNUM *n = rsa->n;
BIGNUM
*tmp = BN_new(),
*rem = BN_new(),
*dssdelta = BN_new();
BN_CTX *ctx = BN_CTX_new();
RSA *ret = NULL;
BN_sqrtmod(tmp, rem, n, ctx);
/* Δ = |p - q| = |a + b - a + b| = |2b| > √N 2⁻¹⁰⁰ */
/* BN_rshift(dssdelta, tmp, 101); */
BN_one(dssdelta);
BN_lshift(dssdelta, dssdelta, BN_num_bits(n) / 4 + 10);
BN_copy(a, tmp);
BN_sqr(a2, a, ctx);
do {
/* a² += 2a + 1 */
BN_lshift1(tmp, a);
BN_uiadd1(tmp);
BN_add(a2, a2, tmp);
/* a += 1 */
BN_uiadd1(a);
/* b² = a² - N */
BN_usub(b2, a2, n);
/* b */
BN_sqrtmod(b, rem, b2, ctx);
} while (!BN_is_zero(rem) && BN_cmp(b, dssdelta) < 1);
if (BN_is_zero(rem)) {
BN_uadd(a, a, b);
ret = qa_RSA_recover(rsa, a, ctx);
}
BN_CTX_free(ctx);
BN_free(a);
BN_free(b);
BN_free(a2);
BN_free(b2);
BN_free(dssdelta);
BN_free(tmp);
BN_free(rem);
return ret;
}
示例12: BN_mod_lshift_quick
/* BN_mod_lshift variant that may be used if a is non-negative
* and less than m */
int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)
{
if (r != a)
{
if (BN_copy(r, a) == NULL) return 0;
}
while (n > 0)
{
int max_shift;
/* 0 < r < m */
max_shift = BN_num_bits(m) - BN_num_bits(r);
/* max_shift >= 0 */
if (max_shift < 0)
{
BNerr(BN_F_BN_MOD_LSHIFT_QUICK, BN_R_INPUT_NOT_REDUCED);
return 0;
}
if (max_shift > n)
max_shift = n;
if (max_shift)
{
if (!BN_lshift(r, r, max_shift)) return 0;
n -= max_shift;
}
else
{
if (!BN_lshift1(r, r)) return 0;
--n;
}
/* BN_num_bits(r) <= BN_num_bits(m) */
if (BN_cmp(r, m) >= 0)
{
if (!BN_sub(r, r, m)) return 0;
}
}
bn_check_top(r);
return 1;
}
示例13: bu256_bn
void bu256_bn(BIGNUM *vo, const bu256_t *vi)
{
BN_zero(vo);
BIGNUM tmp;
BN_init(&tmp);
unsigned int i;
for (i = 0; i < 8; i++) {
BN_set_word(&tmp, GUINT32_FROM_LE(vi->dword[i]));
BN_lshift(&tmp, &tmp, (i * 32));
BN_add(vo, vo, &tmp);
}
BN_free(&tmp);
}
示例14: BN_bin2bn
EC_POINT *embed(const polypseud_ctx *ctx, const unsigned char *data, const size_t len) {
BIGNUM *t1 = BN_bin2bn(data, len, NULL);
BIGNUM *x = BN_new();
BN_mod(x, t1, ctx->p, ctx->bn_ctx);
EC_POINT *point = EC_POINT_new(ctx->ec_group);
unsigned char counter = 0;
int success = 0;
while(!success) {
success = EC_POINT_set_compressed_coordinates_GFp(ctx->ec_group, point, x, 1, ctx->bn_ctx);
if(!success) {
if(counter == 0) {
BN_lshift(x, x, 8);
}
BN_add(x, x, BN_value_one());
}
}
BN_free(x);
BN_free(t1);
return point;
}
示例15: get_size
static ssize_t
get_size(const uint8_t *buf, size_t len, BIGNUM *out)
{
int sz = -1;
if (len == 0)
return 1;
if (BN_set_word(out, 0) <= 0)
return -ENOMEM;
for (size_t i = 0; i < len; i++) {
if (sz > 0) {
if (BN_lshift(out, out, 8) <= 0)
return -ENOMEM;
if (BN_add_word(out, buf[i]) <= 0)
return -ENOMEM;
} else if (sz < 0) {
if (i == 0) {
if (IS_ID_SHORT(buf[i]))
sz++;
} else if (IS_ID_LAST(buf[i]))
sz++;
} else if (sz == 0) {
sz = buf[i] & ~BIT8;
if (IS_LEN_SHORT(buf[i]))
return BN_set_word(out, sz) > 0 ? 0 : -ENOMEM;
if (i + 1 == len)
return sz;
}
}
return sz <= 0 ? 1 : 0;
}