Mailing List Archive

Modular add/sub/mul incorrect result if result and modulus pointer are equal
In the following program, the results of addm, subm or mulm are all zero,
but should be 6, 5 and 1, respectively.

#include <gcrypt.h>

#define CF_CHECK_EQ(expr, res) if ( (expr) != (res) ) { goto end; }

int main(void)
{
gcry_mpi_t A;
gcry_mpi_t B;
gcry_mpi_t C;
gcry_error_t err;
char *buf;

CF_CHECK_EQ(err = gcry_mpi_scan(&A, GCRYMPI_FMT_HEX, "2", 0, NULL), 0);
CF_CHECK_EQ(err = gcry_mpi_scan(&B, GCRYMPI_FMT_HEX, "4", 0, NULL), 0);
CF_CHECK_EQ(err = gcry_mpi_scan(&C, GCRYMPI_FMT_HEX, "7", 0, NULL), 0);
gcry_mpi_addm(C, A, B, C);
//gcry_mpi_subm(C, A, B, C);
//gcry_mpi_mulm(C, A, B, C);
CF_CHECK_EQ(err = gcry_mpi_aprint(GCRYMPI_FMT_HEX, (unsigned
char**)&buf, NULL, C), 0);
printf("%s\n", buf);
gcry_mpi_release(A);
gcry_mpi_release(B);
gcry_mpi_release(C);
gcry_free(buf);
end:
return 0;
}
Re: Modular add/sub/mul incorrect result if result and modulus pointer are equal [ In reply to ]
On Fri, 2 Jun 2023 03:20, Guido Vranken said:
> In the following program, the results of addm, subm or mulm are all zero,
> but should be 6, 5 and 1, respectively.

I modified the included mpicalc program (patch against 1.10 attached) to
allow for addm and subm but can't replicate your findings.:

$ mpicalc
2
4
7
M+pc
06
2
4
7
M-pc
05
2
4
7
mpc
01

I have not checked your code, though.


Shalom-Salam,

Werner

--
The pioneers of a warless world are the youth that
refuse military service. - A. Einstein
Re: Modular add/sub/mul incorrect result if result and modulus pointer are equal [ In reply to ]
On 2.6.2023 12.27, Werner Koch via Gcrypt-devel wrote:
> On Fri, 2 Jun 2023 03:20, Guido Vranken said:
>> In the following program, the results of addm, subm or mulm are all zero,
>> but should be 6, 5 and 1, respectively.
>
> I modified the included mpicalc program (patch against 1.10 attached) to
> allow for addm and subm but can't replicate your findings.:
>
> $ mpicalc
> 2
> 4
> 7
> M+pc
> 06
> 2
> 4
> 7
> M-pc
> 05
> 2
> 4
> 7
> mpc
> 01
>
> I have not checked your code, though.

Below is code for mpi_addm. If W and M are the same (as is in Guido's example),
then it looks that W and M gets replaced by mpi_add result. After that mpi_mod
gives zero as result (have not tested, just inspecting code).

void
_gcry_mpi_addm( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m)
{
mpi_add (w, u, v);
mpi_mod (w, w, m);
}

-Jussi

>
>
> Shalom-Salam,
>
> Werner
>
>
> _______________________________________________
> Gcrypt-devel mailing list
> Gcrypt-devel@gnupg.org
> https://lists.gnupg.org/mailman/listinfo/gcrypt-devel


_______________________________________________
Gcrypt-devel mailing list
Gcrypt-devel@gnupg.org
https://lists.gnupg.org/mailman/listinfo/gcrypt-devel