There is a bug in GMP [1] that in SSIKE software results in accessing invalid (previously freed) memory and subsequently in failing to successfully calculate the same shared key on Alice's and Bob's side.
According to the following comment in gfp2.c,
/*
There seems to be a bug in GMP 4.2.1 that makes mpz_mod give
unpredictable results when the mpz_t holding the result is the same
as one of the operands.
*/
you already suspected something, but what you describe there is just a symptom. The underlying problem is that when input and output mpz_t variables are copies of one another (if they are the same, then there is no problem), invalid memory accesses occur and you get unpredictable behavior - sometimes the calculated value will be correct despite accessing invalid memory, sometimes it will be wrong.
In my testing (using a modified version of you SSIKE software) this bug manifested itself in sporadically wrong results from neg_GF() or more specifically mpz_sub() inside it. In your testing bug might manifest itself somewhere else. So the correct way to fix this bug is to not use multiple copies of the same mpz_t object unless they are all read-only.
See [1] for code that ilustrates the bug. There pair_t is similar to GF in SSIKE.
[1] https://gmplib.org/list-archives/gmp-bugs/2016-April/003939.html
There is a bug in GMP [1] that in SSIKE software results in accessing invalid (previously freed) memory and subsequently in failing to successfully calculate the same shared key on Alice's and Bob's side.
According to the following comment in gfp2.c,
you already suspected something, but what you describe there is just a symptom. The underlying problem is that when input and output
mpz_tvariables are copies of one another (if they are the same, then there is no problem), invalid memory accesses occur and you get unpredictable behavior - sometimes the calculated value will be correct despite accessing invalid memory, sometimes it will be wrong.In my testing (using a modified version of you SSIKE software) this bug manifested itself in sporadically wrong results from
neg_GF()or more specificallympz_sub()inside it. In your testing bug might manifest itself somewhere else. So the correct way to fix this bug is to not use multiple copies of the samempz_tobject unless they are all read-only.See [1] for code that ilustrates the bug. There
pair_tis similar toGFin SSIKE.[1] https://gmplib.org/list-archives/gmp-bugs/2016-April/003939.html