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

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

  if((res = mp_int_to_int(in[1], &v)) != MP_OK)
    return imath_errno = res, 0;
  if((res = mp_int_to_int(out[1], &orem)) != MP_OK)
    return imath_errno = res, 0;

  if((res = mp_int_div_value(in[0], v, in[2], &rem)) != expect)
    return imath_errno = res, 0;

  if(expect == MP_OK &&
     ((mp_int_compare(in[2], out[0]) != 0) || (rem != orem))) {
    char *str;
    
    mp_int_to_string(in[2], 10, g_output, OUTPUT_LIMIT);
    str = g_output + strlen(g_output);
    *str++ = ',';
    sprintf(str, "%d", rem);
    return imath_errno = OTHER_ERROR, 0;
  }

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

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

  if((res = mp_int_to_int(in[1], &p2)) != MP_OK)
    return imath_errno = res, 0;

  if((res = mp_int_div_pow2(in[0], p2, in[2], in[3])) != expect)
    return imath_errno = res, 0;

  if(expect == MP_OK &&
     ((mp_int_compare(in[2], out[0]) != 0) ||
      (mp_int_compare(in[3], out[1]) != 0))) {
    int len;
    char *str;
    
    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));
    return imath_errno = OTHER_ERROR, 0;
  }

  return 1;
}
コード例 #3
0
int test_sub(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, "subv") == 0) {
    if((res = mp_int_to_int(in[1], &v)) != MP_OK)
      return imath_errno = res, 0;
    if((res = mp_int_sub_value(in[0], v, in[2])) != expect)
      return imath_errno = res, 0;
  } else {
    if((res = mp_int_sub(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;
}
コード例 #4
0
int test_qexpt(testspec_t *t, FILE *ofp)
{
  mp_rat in[3], out[1];
  mp_result res, expect;
  int power;

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

  if(!mp_rat_is_integer(in[1])) {
    fprintf(stderr, "Line %d: Second argument must be an integer (test_qexpt)\n",
	    t->line);
    return imath_errno = MP_BADARG, 0;
  }

  if((res = mp_int_to_int(MP_NUMER_P(in[1]), &power)) != MP_OK)
    return imath_errno = res, 0;
  
  if((res = mp_rat_expt(in[0], power, in[2])) != expect)
    return imath_errno = res, 0;
  
  if(expect == MP_OK && mp_rat_compare(in[2], out[0]) != 0) {
    mp_rat_to_string(in[2], 10, g_output, OUTPUT_LIMIT);
    return imath_errno = OTHER_ERROR, 0;
  }

  return 1;
}
コード例 #5
0
int test_tostr(testspec_t *t, FILE *ofp)
{
  mp_int    in[2];
  int       radix;
  mp_result res, len;

  if(!parse_int_values(t, in, NULL, NULL))
    return imath_errno = MP_BADARG, 0;

  if((res = mp_int_to_int(in[1], &radix)) != MP_OK)
    return imath_errno = MP_BADARG, 0;

  if(radix < MP_MIN_RADIX || radix > MP_MAX_RADIX)
    return imath_errno = MP_RANGE, 0;

  trim_line(t->output[0]);

  len = mp_int_string_len(in[0], radix);

  if((res = mp_int_to_string(in[0], radix, g_output, len)) != MP_OK)
    return imath_errno = res, 0;

  if(strcmp(t->output[0], g_output) != 0) 
    return imath_errno = OTHER_ERROR, 0;

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

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

  if((res = mp_int_to_int(in[0], &a)) != MP_OK) 
    return imath_errno = res, 0;
  if((res = mp_int_to_int(in[1], &b)) != MP_OK)
    return imath_errno = res, 0;

  if((res = mp_int_expt_value(a, b, 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;
}
コード例 #7
0
ファイル: isl_imath.c プロジェクト: abduld/isl
/* Try a standard conversion that fits into a long.
 */
int isl_imath_fits_slong_p(mp_int op)
{
	unsigned long out;
	mp_result res = mp_int_to_int(op, &out);
	return res == MP_OK;
}