コード例 #1
0
ファイル: rsa-gmp.c プロジェクト: Kendra123/heimdal
static int
rsa_private_calculate(mpz_t in, mpz_t p,  mpz_t q,
		      mpz_t dmp1, mpz_t dmq1, mpz_t iqmp,
		      mpz_t out)
{
    mpz_t vp, vq, u;
    mpz_init(vp); mpz_init(vq); mpz_init(u);

    /* vq = c ^ (d mod (q - 1)) mod q */
    /* vp = c ^ (d mod (p - 1)) mod p */
    mpz_fdiv_r(vp, in, p);
    mpz_powm(vp, vp, dmp1, p);
    mpz_fdiv_r(vq, in, q);
    mpz_powm(vq, vq, dmq1, q);

    /* C2 = 1/q mod p  (iqmp) */
    /* u = (vp - vq)C2 mod p. */
    mpz_sub(u, vp, vq);
#if 0
    if (mp_int_compare_zero(&u) < 0)
	mp_int_add(&u, p, &u);
#endif
    mpz_mul(u, iqmp, u);
    mpz_fdiv_r(u, u, p);

    /* c ^ d mod n = vq + u q */
    mpz_mul(u, q, u);
    mpz_add(out, u, vq);

    mpz_clear(vp);
    mpz_clear(vq);
    mpz_clear(u);

    return 0;
}
コード例 #2
0
int test_add(testspec_t *t, FILE *ofp)
{
  mp_int in[3], out[1];
  int    v;
  mp_result res, expect;

  if(!parse_int_values(t, in, out, &expect))
    return imath_errno = MP_BADARG, 0;

  if(strcmp(t->code, "addv") == 0) {
    if((res = mp_int_to_int(in[1], &v)) != MP_OK)
      return imath_errno = res, 0;
    if((res = mp_int_add_value(in[0], v, in[2])) != expect)
      return imath_errno = res, 0;
  } else {
    if((res = mp_int_add(in[0], in[1], in[2])) != expect)
      return imath_errno = res, 0;
  }

  if(expect == MP_OK && mp_int_compare(in[2], out[0]) != 0) {
    mp_int_to_string(in[2], 10, g_output, OUTPUT_LIMIT);
    return imath_errno = OTHER_ERROR, 0;
  }

  return 1;
}
コード例 #3
0
char* IP2Location_read128(FILE *handle, uint32_t position) 
{
	uint32_t b96_127 = IP2Location_read32(handle, position);
	uint32_t b64_95 = IP2Location_read32(handle, position + 4); 
	uint32_t b32_63 = IP2Location_read32(handle, position + 8);
	uint32_t b1_31 = IP2Location_read32(handle, position + 12);

	mpz_t result, multiplier, mp96_127, mp64_95, mp32_63, mp1_31;
	mp_int_init(&result);
	mp_int_init(&multiplier);
	mp_int_init(&mp96_127);
	mp_int_init(&mp64_95);
	mp_int_init(&mp32_63);
	mp_int_init(&mp1_31);
	
	mp_int_init_value(&multiplier, 65536);
	mp_int_mul(&multiplier, &multiplier, &multiplier);
	mp_int_init_value(&mp96_127, b96_127);
	mp_int_init_value(&mp64_95, b64_95);
	mp_int_init_value(&mp32_63, b32_63);
	mp_int_init_value(&mp1_31, b1_31);

	mp_int_mul(&mp1_31, &multiplier, &mp1_31);
	mp_int_mul(&mp1_31, &multiplier, &mp1_31);
	mp_int_mul(&mp1_31, &multiplier, &mp1_31);

	mp_int_mul(&mp32_63, &multiplier, &mp32_63);
	mp_int_mul(&mp32_63, &multiplier, &mp32_63);

	mp_int_mul(&mp64_95, &multiplier, &mp64_95);
	
	mp_int_add(&mp1_31, &mp32_63, &result);
	mp_int_add(&result, &mp64_95, &result);
	mp_int_add(&result, &mp96_127, &result);
	return IP2Location_mp2string(result);
}
コード例 #4
0
ファイル: pi.c プロジェクト: creachadair/imath
/*
  Compute mul * atan(1/x) to prec digits of precision, and store the
  result in sum.

  Computes atan(1/x) using the formula:

               1     1      1      1
  atan(1/x) = --- - ---- + ---- - ---- + ...
               x    3x^3   5x^5   7x^7

 */
mp_result arctan(mp_small radix, mp_small mul, mp_small x, mp_small prec,
                 mp_int sum) {
  mpz_t t, v;
  mp_result res;
  mp_small rem, sign = 1, coeff = 1;

  mp_int_init(&t);
  mp_int_init(&v);
  ++prec;

  /* Compute mul * radix^prec * x
     The initial multiplication by x saves a special case in the loop for
     the first term of the series.
   */
  if ((res = mp_int_expt_value(radix, prec, &t)) != MP_OK ||
      (res = mp_int_mul_value(&t, mul, &t)) != MP_OK ||
      (res = mp_int_mul_value(&t, x, &t)) != MP_OK)
    goto CLEANUP;

  x *= x; /* assumes x <= sqrt(MP_SMALL_MAX) */
  mp_int_zero(sum);

  do {
    if ((res = mp_int_div_value(&t, x, &t, &rem)) != MP_OK) goto CLEANUP;

    if ((res = mp_int_div_value(&t, coeff, &v, &rem)) != MP_OK) goto CLEANUP;

    /* Add or subtract the result depending on the current sign (1 = add) */
    if (sign > 0)
      res = mp_int_add(sum, &v, sum);
    else
      res = mp_int_sub(sum, &v, sum);

    if (res != MP_OK) goto CLEANUP;
    sign = -sign;
    coeff += 2;

  } while (mp_int_compare_zero(&t) != 0);

  res = mp_int_div_value(sum, radix, sum, NULL);

CLEANUP:
  mp_int_clear(&v);
  mp_int_clear(&t);

  return res;
}
コード例 #5
0
int test_egcd(testspec_t *t, FILE *ofp)
{
  mp_int in[5], out[3], t1 = g_zreg + 8, t2 = g_zreg + 9;
  mp_result res, expect;

  if(!parse_int_values(t, in, out, &expect))
    return imath_errno = MP_BADARG, 0;

  if((res = mp_int_egcd(in[0], in[1], in[2], in[3], in[4])) != expect)
    return imath_errno = res, 0;

  /* If we got an error we expected, return success immediately */
  if(expect != MP_OK)
    return 1;

  if((mp_int_compare(in[2], out[0]) != 0) ||
     (mp_int_compare(in[3], out[1]) != 0) ||
     (mp_int_compare(in[4], out[2]) != 0)) {
    int len, len2;
    char *str;

    /* Failure might occur because the tester computed x and y in a different
       way than we did.  Verify that the results are correct before reporting
       an error. */
    mp_int_mul(in[3], in[0], t1);
    mp_int_mul(in[4], in[1], t2);
    mp_int_add(t1, t2, t2);
    if(mp_int_compare(t2, in[2]) == 0) 
      return 1;

    mp_int_to_string(in[2], 10, g_output, OUTPUT_LIMIT);
    str = g_output + (len = strlen(g_output));
    *str++ = ',';
    mp_int_to_string(in[3], 10, str, OUTPUT_LIMIT - (len + 1));
    str = str + (len2 = strlen(str));
    *str++ = ',';
    mp_int_to_string(in[4], 10, str, OUTPUT_LIMIT - (len + len2 + 2));
    return imath_errno = OTHER_ERROR, 0;
  }

  return 1;
}