首页 > 解决方案 > 使用 GMP 获取函数

问题描述

我有点(x,y),我想添加另一个点。我只写公式,但我想获得函数:

    struct point {
        mpz_t x;
        mpz_t y;
    };

    point my_func(mpz_t x, mpz_t y) {
   /..../
        //lambda
        mpz_sub(a, Y, y);
        mpz_sub(b, X, x);
        mpz_invert(c, b, P);
        mpz_mul(b, a, c);
        mpz_mod(f, b, P);

        //point x
        mpz_mul(a, f, f);
        mpz_sub(b, a, x);
        mpz_sub(c, b, X);
        mpz_mod(d, c, P);

        //point y
        mpz_sub(a, x, d);
        mpz_mul(b, f, a);
        mpz_sub(c, b, y);
        mpz_mod(e, c, P);

        return (x, y);
    }

在我看来,这应该不难,但我一整天都在开车。点 gp = {x, y}; 错误:数组必须用大括号括起来的初始化程序初始化

我只想有一个点 (x, y) 来获得输出的下一个点。它不一定是一个结构,我尝试了对和元组,它仍然没有帮助。

标签: c++funcgmp

解决方案


真的,如果我只需要更改 x 和 y 的值 void 函数通过引用解决了问题,为什么还要麻烦,我感谢有用的建议这个函数工作并且需要 RAM 600kb

void add(mpz_t& x, mpz_t& y, mpz_t& X, mpz_t& Y,  mpz_t& P) {
    mpz_t a, b, c, d, f;
    mpz_inits(a, b, c, d, f, 0);
        //lambda
       mpz_sub(a, Y, y);
       mpz_sub(b, X, x);
    mpz_invert(c, b, P);
       mpz_mul(b, a, c);
       mpz_mod(f, b, P);
        //point x
    mpz_mul(a, f, f);
    mpz_sub(b, a, x);
    mpz_sub(c, b, X);
    mpz_mod(d, c, P);
        //point y
    mpz_sub(a, x, d);
    mpz_mul(b, f, a);
    mpz_sub(c, b, y);
    mpz_mod(f, c, P);
        //set x, y
    mpz_set(x, d);
    mpz_set(y, f);

    mpz_clears(a, b, c, d, f, nullptr);
}

推荐阅读