0
votes

i am working with GMP lib for the first time and it looks like this code does some dirty memory cheats:

void addition (const point p,const point q,const mpz_t fp, point* result){
    mpz_t ld;
    mpz_init(ld);
    lambda_add(p,q,fp,ld);

    gmp_printf( "WTFCHECK1:  %Zx, %Zx\n", p.x ,p.y);
    mpz_mul(result->x,ld,ld);
    gmp_printf( "WTFCHECK2:  %Zx, %Zx\n", p.x ,p.y);
    mpz_sub(result->x,result->x,p.x);
    gmp_printf( "WTFCHECK3:  %Zx, %Zx\n", p.x ,p.y);
...
}

where lambda looks like

void lambda_add (const point p,const point q,const mpz_t fp, mpz_t result){
    mpz_t ydiff,xdiff;
    mpz_init (ydiff);       
    mpz_init (xdiff);

    mpz_sub (ydiff,q.y,p.y);
    mpz_sub (xdiff,q.x,p.x);
...//no more usage of p or q
}

But program output still looks like this:

WTFCHECK1:  7cf27b188d034f7e8a52380304b51ac3c08969e277f21b35a60b48fc47669978, 7775510db8ed040293d9ac69f7430dbba7dade63ce982299e04b79d227873d1
WTFCHECK2:  de8a6886d284cb85d49a93b707475ed3d277242444ba9e860207f9a59a9ef351, 7775510db8ed040293d9ac69f7430dbba7dade63ce982299e04b79d227873d1

As you can see, variable p.x of type mpz_t (x and y are mpz_t type in typedef structure point) has changed without me assigning anything into it. What is the reason of this behavior? Thanks.

1
You may need to post the setup of p, q, and result as well as the call to addition. What happens if you comment out the call to lambda_add?Craig Estey
Can't say much without a MCVE. Note that you would likely save a lot of trouble if you used the header <gmpxx.h> that comes with GMP, and the type mpz_class it provides.Marc Glisse

1 Answers

0
votes

Okay, :D problem was a bit on my side i was calling addition (const point p,const point q,const mpz_t fp, point* result) with same structure given as a point p and as a point* result, so along with modifying result i was also modifying point p. I appreciate your help anyway.