[GMP mpz_t函数的返回类型

问题描述 投票:0回答:1

我正在尝试使用一个递归函数,该函数最终将返回mpz_t数据类型。我尝试使用mpz_get_ui()来实现这一点,但是它不能给出非常大的数字(15位+)的正确结果。该程序的目的是加密。

[我也尝试过使类型为void的函数并添加一个mpz_t参数作为答案占位符,如下所示:void fme(mpz_t y, mpz_t g, mpz_t x, mpz_t p),但这消除了递归调用该函数的可能性,因为它不再返回任何内容。

我想知道是否可以使用mpz_t指针数据类型mpz_t*进行任何操作?

这是我到目前为止的内容:

unsigned long int fme(mpz_t g, mpz_t x, mpz_t p) {

  mpz_t result, temp;
  mpz_init(result);
  mpz_init(temp);

  if (mpz_cmp_d(x, 0) == 0) {

    mpz_clear(result);
    mpz_clear(temp);
    return 1;
  } else if (mpz_even_p(x) != 0) {

    mpz_fdiv_q_ui(temp, x, 2);
    mpz_set_ui(result, fme(g, temp, p));
    mpz_mul(result, result, result);
    mpz_mod(result, result, p);

    unsigned long int answer = mpz_get_ui(result);
    mpz_clear(result);
    mpz_clear(temp);
    return answer;
  }
c return-type gmp
1个回答
0
投票

我想是:

// note: places result in the first argument, that has to initialized
void fme(mpz_t result, mpz_t g, mpz_t x, mpz_t p) {
    if (mpz_cmp_d(x, 0) == 0) {
         mpz_set_ui(result, 1);
         return;
    } else if (mpz_even_p(x) != 0) {
         mpz_t temp;
         mpz_init(temp);
         mpz_fdiv_q_ui(temp, x, 2);
         fme(result, g, temp, p);
         mpz_clear(temp);

         mpz_mul(result, result, result);
         mpz_mod(result, result, p);
    }
}

// should work the same as original
unsigned long int fme_old(mpz_t g, mpz_t x, mpz_t p) {
    mpz_t result;
    mpz_init(result);
    fme(result, g, x, p);
    unsigned long int ret = mpz_get_ui(result);
    mpz_clear(result);
    return ret;
}
© www.soinside.com 2019 - 2024. All rights reserved.