mpz_powm在GMP中的复杂度是什么?

huangapple go评论61阅读模式
英文:

What complexity does mpz_powm in GMP have?

问题

GMP函数mpz_powm的复杂度是多少?

有许多实现模指数运算的方法。它是否使用“从右到左的二进制方法”,其时间复杂度为O(log e)?

我想将其用于大指数,这对于O(e)不可扩展。

英文:

What complexity does the GMP function mpz_powm have?

There are many ways to implement modular exponentiation. Does it use "Right-to-left binary method" which runs in O(log e)?

I would like to use it for large exponents, which doesn't scale for O(e).

答案1

得分: 0

我只是基于 GitHub 上的另一个实现来实现了我的自己。这是一个简单的实现,具有类似的运行时间,我也知道它是 O(log e) 的,允许我在它冻结的地方记录并停止。这证实了官方实现和我所期望的一样快,但遗憾的是对我的目的来说还不够快。

#include <gmp.h>

// https://github.com/csknk/fast-modular-exponentiation/blob/master/cpp/main.cpp
void mod_exp_fast(mpz_t result, const mpz_t& b_in, const mpz_t& e_in, const mpz_t& m) {
    mpz_t b, e;
    MpzRaii(b, e); // 只是为我调用 mpz_init/free()。
    mpz_set(b, b_in);
    mpz_set(e, e_in);

    if (mpz_odd_p(e) != 0) {
        mpz_set(result, b);
    } else {
        mpz_set_ui(result, 1);
    }

    while (mpz_cmp_ui(e, 0) > 0) {
        // gmp_printf("mod_exp e=%d\n", mpz_sizeinbase(e, 2));
        mpz_powm_ui(b, b, 2, m);
        mpz_fdiv_q_2exp(e, e, 1);
        if (mpz_odd_p(e) != 0) {
            mpz_mul(result, result, b);
            mpz_mod(result, result, m);
        }
    }
}
英文:

I just implemented my own based on another implementation in github. It's a simple implementation with similar run-time and which I also know is O(log e), and allowing me to log and stop where it freezes. This confirms the official implementation is as fast as I hoped, which sadly turned out to be not fast enough for my purposes.

#include &lt;gmp.h&gt;

// https://github.com/csknk/fast-modular-exponentiation/blob/master/cpp/main.cpp
void mod_exp_fast(mpz_t result, const mpz_t&amp; b_in, const mpz_t&amp; e_in, const mpz_t&amp; m) {
    mpz_t b, e;
    MpzRaii(b, e); // Just calling mpz_init/free() for me.
    mpz_set(b, b_in);
    mpz_set(e, e_in);

    if (mpz_odd_p(e) != 0) {
        mpz_set(result, b);
    } else {
        mpz_set_ui(result, 1);
    }

    while (mpz_cmp_ui(e, 0) &gt; 0) {
        // gmp_printf(&quot;mod_exp e=%d\n&quot;, mpz_sizeinbase(e, 2));
        mpz_powm_ui(b, b, 2, m);
        mpz_fdiv_q_2exp(e, e, 1);
        if (mpz_odd_p(e) != 0) {
            mpz_mul(result, result, b);
            mpz_mod(result, result, m);
        }
    }
}

huangapple
  • 本文由 发表于 2023年8月10日 18:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定