Hi,
I try to implement a point addition in affine coordinate and to benchmark it.
Compilation are ok:
gcc -o test2.exe -O2 -I secp256k1/src/ -I secp256k1/ test2.c -lgmp
but i’ve sometimes an error message on program execution (core dumped). Not on every execution.
this is my code :
0#include "libsecp256k1-config.h"
1
2
3#include <stdio.h>
4#include <stdlib.h>
5
6#include <time.h>
7
8#include "include/secp256k1.h"
9#include "assumptions.h"
10#include "group.h"
11#include "secp256k1.c"
12
13
14#define ROUNDS 100000000`
15
16static void light_add(secp256k1_fe *xr,secp256k1_fe *yr, const secp256k1_fe *x1, const secp256k1_fe *y1, const secp256k1_fe *x2, const secp256k1_fe *y2){
17
18 secp256k1_fe xtmp,ytmp,x1neg,x2neg,y2neg,xinv,m;
19 xtmp = *x1;
20 ytmp = *y1;
21 int f;
22
23 secp256k1_fe_negate(&x2neg,x2,f); //x2neg=-x2
24 secp256k1_fe_negate(&y2neg,y2,f);//y2neg=-y2
25 secp256k1_fe_negate(&x1neg,x1,f);//x1neg=-x1
26
27 secp256k1_fe_add(&xtmp,&x2neg); //(x1-x2)
28 secp256k1_fe_add(&ytmp,&y2neg);//(y1-y2)
29
30 secp256k1_fe_inv_var(&xinv,&xtmp); //inverse_mod(x1-x2,P)
31
32 secp256k1_fe_mul(&m,&ytmp,&xinv); //m=(y1-y2)*(x1-x2)^-1
33 secp256k1_fe_sqr(xr,&m);//m^2
34
35 secp256k1_fe_add(xr,&x1neg); //xr=m^2-x1
36 secp256k1_fe_add(xr,&x2neg); //xr=m^2-x1-x2
37
38 xtmp = *xr;
39 secp256k1_fe_add(&xtmp,&x1neg); //(xr-x1)
40 secp256k1_fe_mul(&xtmp,&m,&xtmp); //m*(xr-x1)
41 secp256k1_fe_add(&xtmp,y1); //y1+(xr-x1)
42 secp256k1_fe_negate(yr,&xtmp,f); //yr=-(y1+(xr-x1))
43
44}
45
46int main(int argc, char** argv) {
47
48
49 secp256k1_fe xr,yr;
50 secp256k1_ge pt1,pt2;
51 secp256k1_gej pt1j,pt2j;
52 time_t now;
53
54
55 secp256k1_gej_set_ge(&pt2j, &secp256k1_ge_const_g);
56
57 secp256k1_gej_double_var(&pt2j, &pt2j, NULL);
58 secp256k1_ge_set_gej(&pt2, &pt2j); //pt2=2*G
59
60
61 time(&now);
62 printf("Today is : %s", ctime(&now));
63
64 for (int i=0;i<ROUNDS;i++){
65
66 light_add(&pt2.x,&pt2.y,&pt2.x,&pt2.y,&secp256k1_ge_const_g.x,&secp256k1_ge_const_g.y);
67 }
68 time(&now);
69 printf("Today is : %s", ctime(&now));
70}
The issue is difficult to track on debug mode because it’s happens maybe 1 per 1 billion times additions but it seems to comes from secp256k1_fe_inv_var.
Do you know why there can have is this issue? Regards Crunchy