• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ BN_sqr函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中BN_sqr函数的典型用法代码示例。如果您正苦于以下问题:C++ BN_sqr函数的具体用法?C++ BN_sqr怎么用?C++ BN_sqr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了BN_sqr函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: pr_fact

/*
 * pr_fact - print the factors of a number
 *
 * Print the factors of the number, from the lowest to the highest.
 * A factor will be printed multiple times if it divides the value
 * multiple times.
 *
 * Factors are printed with leading tabs.
 */
static void
pr_fact(BIGNUM *val)
{
	const ubig *fact;	/* The factor found. */

	/* Firewall - catch 0 and 1. */
	if (BN_is_zero(val))	/* Historical practice; 0 just exits. */
		exit(0);
	if (BN_is_one(val)) {
		printf("1: 1\n");
		return;
	}

	/* Factor value. */

	if (hflag) {
		fputs("0x", stdout);
		BN_print_fp(stdout, val);
	} else
		BN_print_dec_fp(stdout, val);
	putchar(':');
	for (fact = &prime[0]; !BN_is_one(val); ++fact) {
		/* Look for the smallest factor. */
		do {
			if (BN_mod_word(val, (BN_ULONG)*fact) == 0)
				break;
		} while (++fact <= pr_limit);

		/* Watch for primes larger than the table. */
		if (fact > pr_limit) {
#ifdef HAVE_OPENSSL
			BIGNUM *bnfact;

			bnfact = BN_new();
			BN_set_word(bnfact, *(fact - 1));
			if (!BN_sqr(bnfact, bnfact, ctx))
				errx(1, "error in BN_sqr()");
			if (BN_cmp(bnfact, val) > 0 ||
			    BN_is_prime(val, PRIME_CHECKS,
					NULL, NULL, NULL) == 1)
				pr_print(val);
			else
				pollard_pminus1(val);
#else
			pr_print(val);
#endif
			break;
		}

		/* Divide factor out until none are left. */
		do {
			printf(hflag ? " 0x%lx" : " %lu", *fact);
			BN_div_word(val, (BN_ULONG)*fact);
		} while (BN_mod_word(val, (BN_ULONG)*fact) == 0);

		/* Let the user know we're doing something. */
		fflush(stdout);
	}
	putchar('\n');
}
开发者ID:edgar-pek,项目名称:PerspicuOS,代码行数:69,代码来源:factor.c


示例2: BN_mod_mul

/* slow but works */
int
BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
    BN_CTX *ctx)
{
	BIGNUM *t;
	int ret = 0;

	bn_check_top(a);
	bn_check_top(b);
	bn_check_top(m);

	BN_CTX_start(ctx);
	if ((t = BN_CTX_get(ctx)) == NULL)
		goto err;
	if (a == b) {
		if (!BN_sqr(t, a, ctx))
			goto err;
	} else {
		if (!BN_mul(t, a,b, ctx))
			goto err;
	}
	if (!BN_nnmod(r, t,m, ctx))
		goto err;
	bn_check_top(r);
	ret = 1;

err:
	BN_CTX_end(ctx);
	return (ret);
}
开发者ID:Basskrapfen,项目名称:openbsd,代码行数:31,代码来源:bn_mod.c


示例3: BN_mod_sqr

int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
{
    if (!BN_sqr(r, a, ctx))
        return 0;
    /* r->neg == 0,  thus we don't need BN_nnmod */
    return BN_mod(r, r, m, ctx);
}
开发者ID:1234-,项目名称:openssl,代码行数:7,代码来源:bn_mod.c


示例4: BN_mod_mul_montgomery

int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
			  BN_MONT_CTX *mont, BN_CTX *ctx)
	{
	BIGNUM *tmp;
	int ret=0;

	BN_CTX_start(ctx);
	tmp = BN_CTX_get(ctx);
	if (tmp == NULL) goto err;

	bn_check_top(tmp);
	if (a == b)
		{
		if (!BN_sqr(tmp,a,ctx)) goto err;
		}
	else
		{
		if (!BN_mul(tmp,a,b,ctx)) goto err;
		}
	/* reduce from aRR to aR */
	if (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;
	bn_check_top(r);
	ret=1;
err:
	BN_CTX_end(ctx);
	return(ret);
	}
开发者ID:siredblood,项目名称:tree-bumpkin-project,代码行数:27,代码来源:bn_mont.c


示例5: ec_GFp_nist_field_sqr

int 
ec_GFp_nist_field_sqr(const EC_GROUP * group, BIGNUM * r, const BIGNUM * a,
    BN_CTX * ctx)
{
	int ret = 0;
	BN_CTX *ctx_new = NULL;

	if (!group || !r || !a) {
		ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER);
		goto err;
	}
	if (!ctx)
		if ((ctx_new = ctx = BN_CTX_new()) == NULL)
			goto err;

	if (!BN_sqr(r, a, ctx))
		goto err;
	if (!group->field_mod_func(r, r, &group->field, ctx))
		goto err;

	ret = 1;
err:
	if (ctx_new)
		BN_CTX_free(ctx_new);
	return ret;
}
开发者ID:DiamondLovesYou,项目名称:libressl-pnacl-sys,代码行数:26,代码来源:ecp_nist.c


示例6: BN_mod_mul_reciprocal

int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
                          BN_RECP_CTX *recp, BN_CTX *ctx)
{
    int ret = 0;
    BIGNUM *a;
    const BIGNUM *ca;

    BN_CTX_start(ctx);
    if ((a = BN_CTX_get(ctx)) == NULL)
        goto err;
    if (y != NULL) {
        if (x == y) {
            if (!BN_sqr(a, x, ctx))
                goto err;
        } else {
            if (!BN_mul(a, x, y, ctx))
                goto err;
        }
        ca = a;
    } else
        ca = x;                 /* Just do the mod */

    ret = BN_div_recp(NULL, r, ca, recp, ctx);
 err:
    BN_CTX_end(ctx);
    bn_check_top(r);
    return ret;
}
开发者ID:isaracorp,项目名称:openssl,代码行数:28,代码来源:bn_recp.c


示例7: BN_exp

int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) {
  int i, bits, ret = 0;
  BIGNUM *v, *rr;

  if ((p->flags & BN_FLG_CONSTTIME) != 0) {
    /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
    OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    return 0;
  }

  BN_CTX_start(ctx);
  if (r == a || r == p) {
    rr = BN_CTX_get(ctx);
  } else {
    rr = r;
  }

  v = BN_CTX_get(ctx);
  if (rr == NULL || v == NULL) {
    goto err;
  }

  if (BN_copy(v, a) == NULL) {
    goto err;
  }
  bits = BN_num_bits(p);

  if (BN_is_odd(p)) {
    if (BN_copy(rr, a) == NULL) {
      goto err;
    }
  } else {
    if (!BN_one(rr)) {
      goto err;
    }
  }

  for (i = 1; i < bits; i++) {
    if (!BN_sqr(v, v, ctx)) {
      goto err;
    }
    if (BN_is_bit_set(p, i)) {
      if (!BN_mul(rr, rr, v, ctx)) {
        goto err;
      }
    }
  }

  if (r != rr && !BN_copy(r, rr)) {
    goto err;
  }
  ret = 1;

err:
  BN_CTX_end(ctx);
  return ret;
}
开发者ID:DemiMarie,项目名称:ring,代码行数:57,代码来源:bn_test_lib.c


示例8: openssl_bioBN_math

void openssl_bioBN_math()
{
	BIO *outs;
	BN_CTX *ctx;
	char num1[8], num2[8];
	BIGNUM *bg1, *bg2, *tmp, *stp;

	bg1 = BN_new();
	bg2 = BN_new();
	tmp = BN_new();
	ctx = BN_CTX_new();
	strcpy(num1, "84");
	strcpy(num2, "3");
	BN_hex2bn(&bg1, num1);
	BN_hex2bn(&bg2, num2);
	outs = BIO_new(BIO_s_file());
	BIO_set_fp(outs, stdout, BIO_NOCLOSE);

	printf("\nBIO_MATH as follow:\n");
	BN_add(tmp, bg1, bg2);
	BIO_puts(outs, "\tbn(0x84 + 0x3) = 0x");
	BN_print(outs, tmp);
	BIO_puts(outs, "\n");

	BN_sub(tmp, bg1, bg2);
	BIO_puts(outs, "\tbn(0x84 - 0x3) = 0x");
	BN_print(outs, tmp);
	BIO_puts(outs, "\n");

	BN_mul(tmp, bg1, bg2, ctx);
	BIO_puts(outs, "\tbn(0x84 * 0x3) = 0x");
	BN_print(outs, tmp);
	BIO_puts(outs, "\n");

	BN_sqr(tmp, bg1, ctx);
	BIO_puts(outs, "\tbn(sqr(0x84))  = 0x");
	BN_print(outs, tmp);
	BIO_puts(outs, "\n");

	BN_div(tmp, stp, bg1, bg2, ctx);
	BIO_puts(outs, "\tbn(0x84 / 0x3) = 0x");
	BN_print(outs, tmp);
	BIO_puts(outs, "\n");

	BN_exp(tmp, bg1, bg2, ctx);
	BIO_puts(outs, "\tbn(0x84 e 0x03)= 0x");
	BN_print(outs, tmp);
	BIO_puts(outs, "\n");

	BN_free(bg1);
	BN_free(bg2);
	BN_free(tmp);
	BIO_free(outs);
}
开发者ID:beike2020,项目名称:source,代码行数:54,代码来源:openssl_base.c


示例9: pr_fact

/*
 * pr_fact - print the factors of a number
 *
 * If the number is 0 or 1, then print the number and return.
 * If the number is < 0, print -1, negate the number and continue
 * processing.
 *
 * Print the factors of the number, from the lowest to the highest.
 * A factor will be printed numtiple times if it divides the value
 * multiple times.
 *
 * Factors are printed with leading tabs.
 */
static void
pr_fact(BIGNUM *val)
{
	const ubig *fact;		/* The factor found. */

	/* Firewall - catch 0 and 1. */
	if (BN_is_zero(val) || BN_is_one(val))
		errx(1, "numbers <= 1 aren't permitted.");

	/* Factor value. */

	BN_print_dec_fp(stdout, val);
	putchar(':');
	for (fact = &prime[0]; !BN_is_one(val); ++fact) {
		/* Look for the smallest factor. */
		while (fact <= pr_limit) {
			if (BN_mod_word(val, (BN_ULONG)*fact) == 0)
				break;
			fact++;
		}

		/* Watch for primes larger than the table. */
		if (fact > pr_limit) {
#ifdef HAVE_OPENSSL
			BIGNUM *bnfact;

			bnfact = BN_new();
			BN_set_word(bnfact, (BN_ULONG)*(fact - 1));
			BN_sqr(bnfact, bnfact, ctx);
			if (BN_cmp(bnfact, val) > 0
			    || BN_is_prime(val, PRIME_CHECKS, NULL, NULL,
					   NULL) == 1) {
				putchar(' ');
				BN_print_dec_fp(stdout, val);
			} else
				pollard_rho(val);
#else
			printf(" %s", BN_bn2dec(val));
#endif
			break;
		}

		/* Divide factor out until none are left. */
		do {
			printf(" %lu", *fact);
			BN_div_word(val, (BN_ULONG)*fact);
		} while (BN_mod_word(val, (BN_ULONG)*fact) == 0);

		/* Let the user know we're doing something. */
		fflush(stdout);
	}
	putchar('\n');
}
开发者ID:Hooman3,项目名称:minix,代码行数:66,代码来源:factor.c


示例10: 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;
}
开发者ID:fxfactorial,项目名称:bachelor,代码行数:53,代码来源:fermat.c


示例11: do_mul

void do_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
{
    int i, j, k;
    double tm;
    long num;

    for (i = 0; i < NUM_SIZES; i++) {
        num = BASENUM;
        if (i)
            num /= (i * 3);
        BN_rand(a, sizes[i], 1, 0);
        for (j = i; j < NUM_SIZES; j++) {
            BN_rand(b, sizes[j], 1, 0);
            Time_F(START);
            for (k = 0; k < num; k++)
                BN_mul(r, b, a, ctx);
            tm = Time_F(STOP);
            /*printf("mul %4d x %4d -> %8.3fms\n", sizes[i], sizes[j],
                   tm * 1000.0 / num);*//* LEVANCIO S10 comment delete R.Miura 2016/02/03 */
        }
    }

    for (i = 0; i < NUM_SIZES; i++) {
        num = BASENUM;
        if (i)
            num /= (i * 3);
        BN_rand(a, sizes[i], 1, 0);
        Time_F(START);
        for (k = 0; k < num; k++)
            BN_sqr(r, a, ctx);
        tm = Time_F(STOP);
        /*printf("sqr %4d x %4d -> %8.3fms\n", sizes[i], sizes[i],
               tm * 1000.0 / num);*//* LEVANCIO S10 comment delete R.Miura 2016/02/03 */
    }

    for (i = 0; i < NUM_SIZES; i++) {
        num = BASENUM / 10;
        if (i)
            num /= (i * 3);
        BN_rand(a, sizes[i] - 1, 1, 0);
        for (j = i; j < NUM_SIZES; j++) {
            BN_rand(b, sizes[j], 1, 0);
            Time_F(START);
            for (k = 0; k < 100000; k++)
                BN_div(r, NULL, b, a, ctx);
            tm = Time_F(STOP);
            /*printf("div %4d / %4d -> %8.3fms\n", sizes[j], sizes[i] - 1,
                   tm * 1000.0 / num);*//* LEVANCIO S10 comment delete R.Miura 2016/02/03 */
        }
    }
}
开发者ID:neominds,项目名称:iwe194030,代码行数:51,代码来源:bnspeed.c


示例12: do_mul

void do_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
	{
	int i,j,k;
	double tm;
	long num;

	for (i=0; i<NUM_SIZES; i++)
		{
		num=BASENUM;
		if (i) num/=(i*3);
		BN_rand(a,sizes[i],1,0);
		for (j=i; j<NUM_SIZES; j++)
			{
			BN_rand(b,sizes[j],1,0);
			Time_F(START);
			for (k=0; k<num; k++)
				BN_mul(r,b,a,ctx);
			tm=Time_F(STOP);
			TINYCLR_SSL_FPRINTF("mul %4d x %4d -> %8.3fms\n",sizes[i],sizes[j],tm*1000.0/num);
			}
		}

	for (i=0; i<NUM_SIZES; i++)
		{
		num=BASENUM;
		if (i) num/=(i*3);
		BN_rand(a,sizes[i],1,0);
		Time_F(START);
		for (k=0; k<num; k++)
			BN_sqr(r,a,ctx);
		tm=Time_F(STOP);
		TINYCLR_SSL_FPRINTF("sqr %4d x %4d -> %8.3fms\n",sizes[i],sizes[i],tm*1000.0/num);
		}

	for (i=0; i<NUM_SIZES; i++)
		{
		num=BASENUM/10;
		if (i) num/=(i*3);
		BN_rand(a,sizes[i]-1,1,0);
		for (j=i; j<NUM_SIZES; j++)
			{
			BN_rand(b,sizes[j],1,0);
			Time_F(START);
			for (k=0; k<100000; k++)
				BN_div(r, NULL, b, a,ctx);
			tm=Time_F(STOP);
			TINYCLR_SSL_FPRINTF("div %4d / %4d -> %8.3fms\n",sizes[j],sizes[i]-1,tm*1000.0/num);
			}
		}
	}
开发者ID:Sorcha,项目名称:NETMF-LPC,代码行数:50,代码来源:bnspeed.cpp


示例13: bn_mul_mont_fixed_top

int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                          BN_MONT_CTX *mont, BN_CTX *ctx)
{
    BIGNUM *tmp;
    int ret = 0;
    int num = mont->N.top;

#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)
    if (num > 1 && a->top == num && b->top == num) {
        if (bn_wexpand(r, num) == NULL)
            return (0);
        if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
            r->neg = a->neg ^ b->neg;
            r->top = num;
            r->flags |= BN_FLG_FIXED_TOP;
            return (1);
        }
    }
#endif

    if ((a->top + b->top) > 2 * num)
        return 0;

    BN_CTX_start(ctx);
    tmp = BN_CTX_get(ctx);
    if (tmp == NULL)
        goto err;

    bn_check_top(tmp);
    if (a == b) {
        if (!BN_sqr(tmp, a, ctx))
            goto err;
    } else {
        if (!BN_mul(tmp, a, b, ctx))
            goto err;
    }
    /* reduce from aRR to aR */
#ifdef MONT_WORD
    if (!bn_from_montgomery_word(r, tmp, mont))
        goto err;
#else
    if (!BN_from_montgomery(r, tmp, mont, ctx))
        goto err;
#endif
    ret = 1;
 err:
    BN_CTX_end(ctx);
    return (ret);
}
开发者ID:AlexanderPankiv,项目名称:node,代码行数:49,代码来源:bn_mont.c


示例14: BN_mod_mul_montgomery

int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                          const BN_MONT_CTX *mont, BN_CTX *ctx) {
  BIGNUM *tmp;
  int ret = 0;

#if defined(OPENSSL_BN_ASM_MONT)
  int num = mont->N.top;

  if (num > 1 && a->top == num && b->top == num) {
    if (bn_wexpand(r, num) == NULL) {
      return 0;
    }
    if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
      r->neg = a->neg ^ b->neg;
      r->top = num;
      bn_correct_top(r);
      return 1;
    }
  }
#endif

  BN_CTX_start(ctx);
  tmp = BN_CTX_get(ctx);
  if (tmp == NULL) {
    goto err;
  }

  if (a == b) {
    if (!BN_sqr(tmp, a, ctx)) {
      goto err;
    }
  } else {
    if (!BN_mul(tmp, a, b, ctx)) {
      goto err;
    }
  }

  /* reduce from aRR to aR */
  if (!BN_from_montgomery_word(r, tmp, mont)) {
    goto err;
  }

  ret = 1;

err:
  BN_CTX_end(ctx);
  return ret;
}
开发者ID:LiTianjue,项目名称:etls,代码行数:48,代码来源:montgomery.c


示例15: BN_mod_mul_montgomery

int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                          const BN_MONT_CTX *mont, BN_CTX *ctx) {
  BIGNUM *tmp;
  int ret = 0;

  int num = mont->N.top;

  /* bn_mul_mont requires at least four limbs, at least for x86. */
  if (num >= 4 && a->top == num && b->top == num) {
    if (bn_wexpand(r, num) == NULL) {
      return 0;
    }
    bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num);
    r->neg = a->neg ^ b->neg;
    r->top = num;
    bn_correct_top(r);
    return 1;
  }

  BN_CTX_start(ctx);
  tmp = BN_CTX_get(ctx);
  if (tmp == NULL) {
    goto err;
  }

  if (a == b) {
    if (!BN_sqr(tmp, a, ctx)) {
      goto err;
    }
  } else {
    if (!BN_mul(tmp, a, b, ctx)) {
      goto err;
    }
  }

  /* reduce from aRR to aR */
  if (!BN_from_montgomery_word(r, tmp, mont)) {
    goto err;
  }

  ret = 1;

err:
  BN_CTX_end(ctx);
  return ret;
}
开发者ID:tarcieri,项目名称:ring,代码行数:46,代码来源:montgomery.c


示例16: test_sqr

int test_sqr(BIO *bp, BN_CTX *ctx)
	{
	BIGNUM a,c,d,e;
	int i;

	BN_init(&a);
	BN_init(&c);
	BN_init(&d);
	BN_init(&e);

	for (i=0; i<num0; i++)
		{
		BN_bntest_rand(&a,40+i*10,0,0);
		a.neg=rand_neg();
		BN_sqr(&c,&a,ctx);
		if (bp != NULL)
			{
			if (!results)
				{
				BN_print(bp,&a);
				BIO_puts(bp," * ");
				BN_print(bp,&a);
				BIO_puts(bp," - ");
				}
			BN_print(bp,&c);
			BIO_puts(bp,"\n");
			}
		BN_div(&d,&e,&c,&a,ctx);
		BN_sub(&d,&d,&a);
		if(!BN_is_zero(&d) || !BN_is_zero(&e))
		    {
		    fprintf(stderr,"Square test failed!\n");
		    return 0;
		    }
		}
	BN_free(&a);
	BN_free(&c);
	BN_free(&d);
	BN_free(&e);
	return(1);
	}
开发者ID:froggatt,项目名称:edimax-br-6528n,代码行数:41,代码来源:bntest.c


示例17: BN_mod_mul_reciprocal

int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *x, BIGNUM *y, BN_RECP_CTX *recp,
	     BN_CTX *ctx)
	{
	int ret=0;
	BIGNUM *a;

	BN_CTX_start(ctx);
	if ((a = BN_CTX_get(ctx)) == NULL) goto err;
	if (y != NULL)
		{
		if (x == y)
			{ if (!BN_sqr(a,x,ctx)) goto err; }
		else
			{ if (!BN_mul(a,x,y,ctx)) goto err; }
		}
	else
		a=x; /* Just do the mod */

	BN_div_recp(NULL,r,a,recp,ctx);
	ret=1;
err:
	BN_CTX_end(ctx);
	return(ret);
	}
开发者ID:robacklin,项目名称:uclinux-linux,代码行数:24,代码来源:bn_recp.c


示例18: pollard_rho

static void
pollard_rho(BIGNUM *val)
{
	BIGNUM *x, *y, *tmp, *num;
	BN_ULONG a;
	unsigned int steps_taken, steps_limit;

	x = BN_new();
	y = BN_new();
	tmp = BN_new();
	num = BN_new();
	a = 1;
restart:
	steps_taken = 0;
	steps_limit = 2;
	BN_set_word(x, 1);
	BN_copy(y, x);

	for (;;) {
		BN_sqr(tmp, x, ctx);
		BN_add_word(tmp, a);
		BN_mod(x, tmp, val, ctx);
		BN_sub(tmp, x, y);
		if (BN_is_zero(tmp)) {
#ifdef DEBUG
			printf(" (loop)");
#endif
			a++;
			goto restart;
		}
		BN_gcd(tmp, tmp, val, ctx);

		if (!BN_is_one(tmp)) {
			if (BN_is_prime(tmp, PRIME_CHECKS, NULL, NULL,
			    NULL) == 1) {
				putchar(' ');
				BN_print_dec_fp(stdout, tmp);
			} else {
#ifdef DEBUG
				printf(" (recurse for ");
				BN_print_dec_fp(stdout, tmp);
				putchar(')');
#endif
				pollard_rho(BN_dup(tmp));
#ifdef DEBUG
				printf(" (back)");
#endif
			}
			fflush(stdout);

			BN_div(num, NULL, val, tmp, ctx);
			if (BN_is_one(num))
				return;
			if (BN_is_prime(num, PRIME_CHECKS, NULL, NULL,
			    NULL) == 1) {
				putchar(' ');
				BN_print_dec_fp(stdout, num);
				fflush(stdout);
				return;
			}
			BN_copy(val, num);
			goto restart;
		}
		steps_taken++;
		if (steps_taken == steps_limit) {
			BN_copy(y, x); /* teleport the turtle */
			steps_taken = 0;
			steps_limit *= 2;
			if (steps_limit == 0) {
#ifdef DEBUG
				printf(" (overflow)");
#endif
				a++;
				goto restart;
			}
		}
	}
}
开发者ID:Hooman3,项目名称:minix,代码行数:78,代码来源:factor.c


示例19: BN_mod_sqr

int
BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
{
    if (!BN_sqr(r, (BIGNUM*)a, ctx)) return 0;
    return BN_mod(r, r, m, ctx);
}
开发者ID:4nkh,项目名称:rhodes,代码行数:6,代码来源:openssl_missing.c


示例20: main


//.........这里部分代码省略.........

    char out[80];
    char fileNameT[80];
    strftime (out, 80, "%Y-%m-%d %H:%M:%S", now_tm);
    strftime (fileNameT, 80, "%Y.%m.%d.%H.%M.%S", now_tm);

    char filename[20];
    strcpy (filename,"Bignumber_");
    strcat (filename,fileNameT);
    strcat (filename,".txt");

    FILE *f = fopen(filename, "a+");
    //setbuf(f, NULL);
    if (f == NULL)
    {
        printf("Error opening file!\n");
        exit(1);
    }
    
    fprintf(f, "\nTested started on %s\n", out);
    fprintf(f, "Normal iteration = %d\n", _ITERATION_NOR);
    fprintf(f, "Exp    iteration = %d\n", _ITERATION_EXP);
    fprintf(f, "MOD    iteration = %d\n", _ITERATION_NOR*10);
    fprintf(f, "===================================\n");


for(j=0;j<100;j++){ 
    printf("Iteration\t#%d\n", j+1);
    fprintf(f, "Iteration #%d\n", j+1);
    fflush(f);
BN_rand(a, 1024, 0, 0);
BN_rand(d, 1024, 0, 0);

// d^2
begin = clock();
    for(i=0;i<_ITERATION_NOR;i++){
        BN_sqr(c, d, ctx);
    }
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Square %f\n", time_spent);
fprintf(f, "Square\t%f\n", time_spent);
fflush(f);

BN_rand(b, 1024, 0, 0);
BN_rand(c, 1024, 0, 0);
// b+c
begin = clock();
    for(i=0;i<_ITERATION_NOR;i++){
        BN_mod_add(b, b, c, prime_mod, ctx);
    }
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Addition %f\n", time_spent);
fprintf(f, "Addition\t%f\n", time_spent);
fflush(f);

BN_rand(b, 1024, 0, 0);
// b mod p
begin = clock();
    for(i=0;i<_ITERATION_NOR*10;i++){
        BN_mod(d, b, prime_mod, ctx);
    }
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Modulus %f\n", time_spent);
fprintf(f, "Modulus\t%f\n", time_spent);
fflush(f);

BN_rand(c, 1024, 0, 0);
BN_rand(d, 1024, 0, 0);
// c ^ d mod p
begin = clock();
    for(i=0;i<_ITERATION_EXP;i++){
        BN_mod_exp(b, c, d, prime_mod, ctx); // b = c^d mod p
    }
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("exponetial %f\n", time_spent);
fprintf(f, "exponetial\t%f\n", time_spent);
fflush(f);
}
    time(&now);
    now_tm = localtime(&now);
    strftime (out, 80, "%Y-%m-%d %H:%M:%S", now_tm);
    fprintf(f, "\nTested ended on %s\n", out);








    BN_CTX_free(ctx);
    printf("====================================================================\n");
    fclose(f);

    return 0;
}
开发者ID:jw-spark,项目名称:myossl,代码行数:101,代码来源:whatever.c



注:本文中的BN_sqr函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ BN_sub_word函数代码示例发布时间:2022-05-30
下一篇:
C++ BN_set_word函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap