I want to implement an elliptic curve point multiplication (Q=dG) operation with libgcrypt (gcrypt). I write the following code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <gcrypt.h> //select and insert key and curve parameters char *key521 = "(key-data\n" " (public-key\n" " (ecc\n" " (curve NIST P-521)\n" " (q #00F2271E305679EBF9D7673FBF75EEA22E90D100A9E0B5DE30C500411E91710D05826F17F2C33A9527CC6799553C626C015ED666F63A4D5CEB27CDBE61F34DB0AF#)\n" " )\n" " )\n" " (private-key\n" " (ecc\n" " (curve NIST P-521)\n" " (d #00BC9D8FD5D0AC1C91C04A1E0A5B6A89229924AAD20E23C5F5E3FE702C3C4633E325D2084DC0CE2005A88FF0512E0CACC271DF3279865DC2C33FCF573F7788278E#)\n" " )\n" " )\n" "\n )"; // workflow for calculating of Q=dG int main() { gcry_check_version(NULL); gcry_mpi_point_t Q,G; //define and hold two elliptic curve point gcry_mpi_point_release(G); gcry_mpi_t d; //define the scalar (d) gcry_sexp_t dsex; //define an S-expression variable gcry_sexp_sscan(&dsex, NULL, key521, strlen(key521)); //set dsex = d in key521 data structure gcry_sexp_release(dsex); d= gcry_sexp_nth_mpi(dsex, 1, GCRYMPI_FMT_USG); // convert from S-exp to MPI format char curvename= "NIST P-521"; //define curve name gcry_ctx_t context; //define a context (ctx) gcry_mpi_ec_new(context, dsex, curvename); //set context gcry_ctx_release(context); gcry_mpi_ec_mul(Q,d,G,context); // calculate Q=dG }
first, I select and insert the values of scalar (d) and curve (NIST P521) in S-expression format. then I write the main program (above code with comments).
my program doesn’t work and it has many errors and problems. my question is how do you implement Q=dG in C with libgcrypt library? any idea about my errors?
the below link is Libgcrypt Reference Manual:
https://www.gnupg.org/documentation/manuals/gcrypt