0

I get the elliptic curve parameters a, b, prime, order, convert the parameters into mp_int format, and then use ‘‘wc_ecc_gen_k()’ to generate a random number num. Use‘ wc_ecc_get_generator()’ to get the generator, ‘use wc_ecc_is_point()’ to judge whether the generator is on the elliptic curve, the returned value is -214 not 0, the generator must be on the curve, why return -214, I use ‘wc_ecc_mulmod ()’ Get the product point_res of num and the generator, judge whether it is on the curve, the return value obtained is also -214, this point should also be on the curve, why return -214, is there something wrong with my code, or the compiler , or something else?

#include <iostream>
#include <string>
#include <unistd.h>
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/ssl.h>
#include <wolfssl/wolfcrypt/types.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/sp_int.h>
#include <wolfssl/wolfcrypt/integer.h>
#include <wolfssl/wolfcrypt/wolfmath.h>
#include <wolfssl/wolfcrypt/sha.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/hash.h>
#include <wolfssl/wolfcrypt/asn.h>
#include <wolfssl/wolfcrypt/aes.h>
//#include <wolfssl/wolfcrypt/sp.h>
using namespace std
int main(){

ecc_key key;
int ret;
WC_RNG rng;
wc_ecc_init(&key);
wc_InitRng(&rng);
int curveId = ECC_SECP256R1;
int keySize = wc_ecc_get_curve_size_from_id(curveId);
ret = wc_ecc_make_key_ex(&rng, keySize, &key, curveId);
if (ret != MP_OKAY) {
// error handling
}

mp_int a,b,prime,order,num;
mp_init_multi(&a,&b,&prime,&order,&num,NULL);
ret = mp_read_radix(&a,key.dp->Af,16);
cout<<"get mp_int af: "<<ret<<endl;
ret = mp_read_radix(&b,key.dp->Bf,16);
cout<<"get mp_int bf: "<<ret<<endl;
ret = mp_read_radix(&prime,key.dp->prime,16);
cout<<"get mp_int prime: "<<ret<<endl;
ret = mp_read_radix(&order,key.dp->order,16);
cout<<"get mp_int order: "<<ret<<endl;
ret = wc_ecc_gen_k(&rng,32,&num,&order);
cout<<"get mp_int num: "<<ret<<endl;

ecc_point* point = wc_ecc_new_point();
ecc_point* point_res = wc_ecc_new_point();
ret = wc_ecc_get_generator(point,ECC_SECP256R1);
cout<<"get ecc_point point: "<<ret<<endl;
ret = wc_ecc_is_point(point,&a,&b,&prime);
cout<<"point is on curve: "<<ret<<endl;
ret = wc_ecc_mulmod(&num,point,point_res,&a,&prime,1);
cout<<"get ecc_point point_res: "<<ret<<endl;
ret = wc_ecc_is_point(point_res,&a,&b,&prime);
cout<<"point_res is on curve: "<<ret<<endl;
return 0;
}

enter image description here

zihao wang
  • 17
  • 2

1 Answers1

1

You'll want to pass in the the curve ID to wc_ecc_get_generator:

ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));

There is an example in test_wc_ecc_get_generator() in /tests/api.c

  • Using @Eric's suggestion (Pseudo patch): ` --- original.c 2023-04-11 14:25:56.000000000 -0600 +++ test.c 2023-04-11 14:23:40.000000000 -0600 @@ -16,7 +16,7 @@ -using namespace std +using namespace std; @@ -46,7 +46,7 @@ - ret = wc_ecc_get_generator(point,ECC_SECP256R1); + ret = wc_ecc_get_generator(point,wc_ecc_get_curve_idx(ECC_SECP256R1)); ` OUTPUT: ` get mp_int af: 0 get mp_int bf: 0 get mp_int prime: 0 get mp_int order: 0 get mp_int num: 0 get ecc_point point: 0 point is on curve: 0 get ecc_point point_res: 0 point_res is on curve: 0 ` – Kaleb Apr 11 '23 at 20:28
  • (Sorry comments don't' keep code syntax and I don't want to post a second answer just confirming Eric's solution checks out) – Kaleb Apr 11 '23 at 20:31
  • The method is useful, thank you very much, sorry to bother you because of this stupid question, I will read the example in /tests/api.c, thank you again – zihao wang Apr 12 '23 at 01:28