Mailing List Archive

gcry_mpi_sub_ui result is positive when it should be negative
In the program below, the result of the computation -5 - 2 should be -7,
but it is 7.

Compare with the behavior of gcry_mpi_add_ui and gcry_mpi_mul_ui
(commented), which do produce a negative result, as they should.

This was tested on the latest repository checkout.

#include <gcrypt.h>

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

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

CF_CHECK_EQ(err = gcry_mpi_scan(&A, GCRYMPI_FMT_HEX, "-5", 0, NULL), 0);
CF_CHECK_EQ(err = gcry_mpi_scan(&res, GCRYMPI_FMT_HEX, "0", 0, NULL),
0);
gcry_mpi_sub_ui(res, A, 2);
//gcry_mpi_add_ui(res, A, 2);
//gcry_mpi_mul_ui(res, A, 2);
CF_CHECK_EQ(err = gcry_mpi_aprint(GCRYMPI_FMT_HEX, (unsigned
char**)&buf, NULL, res), 0);
printf("%s\n", buf);
end:

return 0;
}
Re: gcry_mpi_sub_ui result is positive when it should be negative [ In reply to ]
Hello,

On 30.11.2021 15.21, Guido Vranken via Gcrypt-devel wrote:
> In the program below, the result of the computation -5 - 2 should be -7, but it is 7.
>
> Compare with the behavior of gcry_mpi_add_ui and gcry_mpi_mul_ui (commented), which do produce a negative result, as they should.
>
> This was tested on the latest repository checkout.

Thanks for report.

This looks to go all the way back to code from 1997, commit "initial checkin" (4b5e71ca4e84e61e595dec19e1c7cab0c0a73f24).

-Jussi

>
> #include <gcrypt.h>
>
> #define CF_CHECK_EQ(expr, res) if ( (expr) != (res) ) { goto end; }
>
> int main(void)
> {
>     gcry_mpi_t A;
>     gcry_mpi_t res;
>     gcry_error_t err;
>     char *buf;
>
>     CF_CHECK_EQ(err = gcry_mpi_scan(&A, GCRYMPI_FMT_HEX, "-5", 0, NULL), 0);
>     CF_CHECK_EQ(err = gcry_mpi_scan(&res, GCRYMPI_FMT_HEX, "0", 0, NULL), 0);
>     gcry_mpi_sub_ui(res, A, 2);
>     //gcry_mpi_add_ui(res, A, 2);
>     //gcry_mpi_mul_ui(res, A, 2);
>     CF_CHECK_EQ(err = gcry_mpi_aprint(GCRYMPI_FMT_HEX, (unsigned char**)&buf, NULL, res), 0);
>     printf("%s\n", buf);
> end:
>
>     return 0;
> }
>
> _______________________________________________
> Gcrypt-devel mailing list
> Gcrypt-devel@gnupg.org
> http://lists.gnupg.org/mailman/listinfo/gcrypt-devel
>


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