Hello anthony,
Thank you very much for your enthusiastic help recently, my problem has been solved, due to my lack of mathematical knowledge and carelessness, the calculation of ra3 = ra1*ra2 mod prime is problematic, ra3 = ra1*ra2 mod order is correct
Reference: https://stackoverflow.com/questions/762 … ome-errors
Thank you again for your concern and help recently.
Wang Zihao

Hello anthony,
      The result of my program is the same as yours, I sent you an email, thank you very much.
Wang Zihao

When I use the wc_ecc_mulmod() function to perform calculations, some errors will occur. Ra1 and ra2 are two 32-bit random numbers, and G is the generator of the elliptic curve. The calculation (ra1ra2)G is not equal to ra1(ra2G). I noticed that when using wc_ecc_mulmod, the number cannot exceed the number of digits of the large prime number modulus, but I just need the product of two large prime numbers (ra1a2) , and then multiplied by the generator G. I did a test, ra3 = ra1ra2 mod prime, ra3G!=ra1(ra2G), is there any other way for wolfssl to achieve (ra1ra2)G= ra1(ra2*G) ? when ra1 and ra2 are relatively small numbers, there is no problem. For example, point A and point C in the following code are equal.thank you very much

#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>

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);

//get param of ecc curve
mp_int a,b,prime,order,ra,s;
mp_init_multi(&a,&b,&prime,&order,&ra,&s);
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,&ra,&order);
cout<<"get mp_int ra: "<<ret<<endl;
ret = mp_copy(&key.k,&s);
cout<<"get mp_int s: "<<ret<<endl;

ecc_point* pointG = wc_ecc_new_point();
ret = wc_ecc_get_generator(pointG,wc_ecc_get_curve_idx(ECC_SECP256R1));
cout<<"get ecc_point pointG: "<<ret<<endl;
ret = wc_ecc_is_point(pointG,&a,&b,&prime);
cout<<"point is on curve: "<<ret<<endl;

//tes1:n1 = 10,n2 = 33,n3 = n1*n2, A = n3*G,B = n2*G,C = n1*B=> A == C
mp_int n1,n2,n3;
mp_init_multi(&n1,&n2,&n3,NULL,NULL,NULL);
mp_set_int(&n1,10);
mp_set_int(&n2,33);
ret = mp_mulmod(&n1,&n2,&prime,&n3);
ecc_point* A = wc_ecc_new_point();
ecc_point* B =wc_ecc_new_point();
ecc_point* C =wc_ecc_new_point();
ret = wc_ecc_mulmod(&n3,pointG,A,&a,&prime,1);
cout<<"n3*G: "<<ret<<endl;
ret = wc_ecc_mulmod(&n1,pointG,B,&a,&prime,1);
cout<<"n1*G: "<<ret<<endl;
ret = wc_ecc_mulmod(&n2,B,C,&a,&prime,1);
cout<<"n2*B: "<<ret<<endl;
ret = wc_ecc_cmp_point(A,C);
cout<<"A is equal to C: "<<ret<<endl;

//test2:ra1,ra2 are big number ra3 = ra1*ra2, D = ra3*G,E = ra2*G,F = ra1*E=> D != F
mp_int ra1,ra2,ra3;
mp_init_multi(&ra1,&ra2,&ra3,NULL,NULL,NULL);
ret = wc_ecc_gen_k(&rng,32,&ra1,&order);
cout<<"get mp_int ra1: "<<ret<<endl;
ret = wc_ecc_gen_k(&rng,32,&ra2,&order);
cout<<"get mp_int ra2: "<<ret<<endl;
ret = mp_mulmod(&ra1,&ra2,&prime,&ra3);
ecc_point* D = wc_ecc_new_point();
ecc_point* E =wc_ecc_new_point();
ecc_point* F =wc_ecc_new_point();
ret = wc_ecc_mulmod(&ra3,pointG,D,&a,&prime,1);
cout<<"ra3*G: "<<ret<<endl;
ret = wc_ecc_mulmod(&ra1,pointG,E,&a,&prime,1);
cout<<"ra1*G: "<<ret<<endl;
ret = wc_ecc_mulmod(&ra2,E,F,&a,&prime,1);
cout<<"ra2*E: "<<ret<<endl;
ret = wc_ecc_cmp_point(D,F);
cout<<"D is equal to F: "<<ret<<endl;
return 0;

}

4

(3 replies, posted in wolfCrypt)

Hello Eric,
    It worked, thank you very much for your patient answer, I also want to know which file or document or source code should I check when I encounter the problem of undefined reference to 'xxx' to know what content should be added to compile, I  saw the ifdef 'yyy' in front of the 'xxx' function in the source code before, and then I know that 'yyy' should be added when compiling, but it seems that this method does not always work, so I want to know if you have any way to know  What is added at compile time.

Thanks again,
Wangzihao

5

(3 replies, posted in wolfCrypt)

I checked the original code and found that FP_ECC needs to be compiled.I have added --enable-ecclshamir and --enable-fpecc when compiling, but it still prompts undefined reference to 'ecc_mul2add()' when compiling.And I did not find an example of 'ecc_mul2add()' in the test file.I'm so sorry to bother you with this stupid question.Here is my compile command:‘./configure --enable-eccshamir --enable-fpecc --enable-opensslextra --enable-ecc --enable-ecccustcurves CFLAGS="-DWOLFSSL_TEST_CERT -DWOLFSSL_DER_TO_PEM -DHAVE_ECC_KOBLITZ -DWOLFSSL_PUBLIC_MP -DUSE_ECC_B_PARAM"&&make&&sudo make install’

#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
    }
 
    //ecc_projective_add_point();
    ecc_point *point_a = wc_ecc_new_point();
    ecc_point *point_b = wc_ecc_new_point();
 
    mp_int num1,num2,num3;
    mp_init(&num1);
    mp_init(&num2);
    mp_init(&num3);
    mp_set_int(&num1,23);
    mp_set_int(&num2,45);
    mp_add(&num1,&num2,&num3);
 
    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,wc_ecc_get_curve_idx(ECC_SECP256R1));
    cout<<"get ecc_point point: "<<ret<<endl;
 
    ret = wc_ecc_mulmod(&num1,point,point_a,&a,&prime,1);
    cout<<"num1 * point: "<<ret<<endl;
    ret = wc_ecc_mulmod(&num2,point,point_b,&a,&prime,1);
    cout<<"num2 * point: "<<ret<<endl;
 
    mp_int ka,kb;
    mp_init(&ka);
    mp_init(&kb);
    mp_set_int(&ka,1);
    mp_set_int(&kb,1);
 
    ecc_point* point_c = wc_ecc_new_point();
 
    ret = ecc_mul2add(point_a,&ka,point_b,&kb,point_c,&a,&prime,NULL);
    cout<<"ecc_mul2add: "<<ret<<endl;
 
 
    return 0;
    }

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

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;
    }

Hi,David Garske,
        I used this command

./configure  --enable-opensslextra --enable-ecc --enable-ecccustcurves CFLAGS="-DWOLFSSL_TEST_CERT -DWOLFSSL_DER_TO_PEM -DHAVE_ECC_KOBLITZ -DWOLFSSL_PUBLIC_MP " && make && sudo make install

        Thank you very much.
        Wangzihao

Hi,David Garske,
    This method is useful, but when compiling the wc_ecc_curve_load() function, a warning"no previous procrytotype" is given.Is this normal? I will pay attention to the compilation problem next time, thank you very much

I downloaded and installed wolfssl-5.5.4 according to the document. I want to use the wc_ecc_get_generator() function in ecc.h. When compiling, I found undefined reference to 'wc_ecc_get_generator()'. I use command 'nm libwolfssl.so | grep wc_ecc_get_generator' in the /usr/local/lib ,found that there is no such function in the .so file.Some other functions in ecc.h can be used, but this function cannot, what is the solution? i just want to get the elliptic curve generator of ecc_point type.Is there any way to replace this function to achieve it?Thank you very much

#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>
using namespace std;
int main(){
ecc_key key;
WC_RNG rng;
wc_ecc_init(&key);
wc_InitRng(&rng);
int curveId = ECC_SECP521R1;
int keySize = wc_ecc_get_curve_size_from_id(curveId);
int ret = wc_ecc_make_key_ex(&rng, keySize, &key, curveId);
if (ret != MP_OKAY) {
// error handling
}
ecc_point point = key.pubkey;
ecc_point point1 ;
int err = 1;
err = wc_ecc_get_generator(&point1,curveId);
printf("%d\n",err);
err = 1;
err = wc_ecc_cmp_point(&point,&point1);
printf("%d\n",err);
return 0
}

11

(4 replies, posted in wolfCrypt)

i just used mp_int

12

(4 replies, posted in wolfCrypt)

i just used mp_int

13

(4 replies, posted in wolfCrypt)

i am sorry to bother you,I tried my best but couldn't solve it. I have already configured,i have include <wolfssl/wolfcrypt/sp_int.h>,but it told me undefined reference to 'sp_init',one solution maybe work according tohttps://github.com/wolfSSL/wolfssl/pull/5328,but i don't quite understand .i could not include <wolfssl/wolfcrypt/sp.h>,
I don't know if it's normal.thanks for answering me

14

(1 replies, posted in wolfCrypt)

i used g++ to compile one cpp code included wolfssl,but it told me :undefined reference to 'sp_init',i had include <wolfssl/wolfcrypt/sp_int.h>,i used wolfssl5.5.3,and i found sp_int.h in /usr/local/wolfssl/wolfcrypt,the following code just wants to initalize a mp_int varible and use wc_ecc_gen_k to genetate a random number k between 0 and order of ecc
#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>
using namespace std;
int main(){
    ecc_key key;
    WC_RNG rng;
    wc_ecc_init(&key);
    wc_InitGng(&rng);
    int curveId = ECC_SECP521R1;

    const ecc_set_type* ecc_params;
    ecc_params = wc_ecc_get_curve_params(curveId);

    mp_int ord;     //order of ecc
    mp_int priv;    //privatekey
    int err;
    err = mp_init(&ord);
    cout<<err<<endl;
    err = mp_init(&priv);
    cout<<err<<endl;
    //err = mp_read_radix(&ord,ecc_params->order,MP_RADIX_HEX);   //
    //cout<<err<<endl;
    //err = wc_ecc_gen_k(&rng,120,&priv,&ord)
    return 0 ;
}