gcry_mpi_sub_ui result is positive when it should be negative

Guido Vranken guidovranken at gmail.com
Tue Nov 30 14:21:43 CET 2021


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;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.gnupg.org/pipermail/gcrypt-devel/attachments/20211130/d6bfcb3c/attachment.html>


More information about the Gcrypt-devel mailing list