1 /*
2  *   Test program for fast computation of xG for fixed G using precomputation.
3  *   This idea can be used to substantially speed up certain phases
4  *   of the EC Digital Signature Standard (ECDSA).
5  *
6  *   See "Handbook of Applied Cryptography"
7  *
8  */
9 
10 #include <stdio.h>
11 #include "miracl.h"
12 
main()13 int main()
14 {
15     FILE *fp;
16     big e,n,a,b,x,y,r;
17     epoint *g;
18     ebrick binst;
19     int nb,bits,window,len,bptr,m,i,j;
20     miracl *mip=mirsys(50,0);
21     n=mirvar(0);
22     e=mirvar(0);
23     a=mirvar(0);
24     b=mirvar(0);
25     x=mirvar(0);
26     y=mirvar(0);
27     r=mirvar(0);
28 #ifndef MR_EDWARDS
29     fp=fopen("common.ecs","rt");
30 #else
31     fp=fopen("edwards.ecs","rt");
32 #endif
33 	fscanf(fp,"%d\n",&bits);
34     mip->IOBASE=16;
35     cinnum(n,fp);
36     cinnum(a,fp);
37     cinnum(b,fp);
38     cinnum(r,fp);
39     cinnum(x,fp);
40     cinnum(y,fp);
41     mip->IOBASE=10;
42 
43     printf("modulus is %d bits in length\n",logb2(n));
44     printf("Enter max. size of exponent in bits = ");
45     scanf("%d",&nb);
46     getchar();
47     printf("Enter window size in bits (1-10)= ");
48     scanf("%d",&window);
49     getchar();
50 
51     ebrick_init(&binst,x,y,a,b,n,window,nb);
52 
53 /* Print out the precomputed table (for use in ecdhp.c ?)
54    In which case make sure that MR_SPECIAL is defined and
55    active in the build of this program, so MR_COMBA must
56    also be defined as the number of words in the modulus *
57 
58 len=MR_ROUNDUP(bits,MIRACL);
59 bptr=0;
60 for (i=0;i<2*(1<<window);i++)
61 {
62     for (j=0;j<len;j++)
63     {
64         printf("0x%x,",binst.table[bptr++]);
65     }
66     printf("\n");
67 }
68 
69 */
70 
71     printf("%d elliptic curve points have been precomputed and stored\n",(1<< window));
72 
73     bigbits(nb,e);  /* random exponent */
74 
75     printf("naive method\n");
76     ecurve_init(a,b,n,MR_PROJECTIVE);
77     g=epoint_init();
78     epoint_set(x,y,0,g);
79     ecurve_mult(e,g,g);
80     epoint_get(g,x,y);
81     cotnum(x,stdout);
82     cotnum(y,stdout);
83 
84     printf("Comb method\n");
85     mul_brick(&binst,e,x,y);
86 
87     ebrick_end(&binst);
88 
89     cotnum(x,stdout);
90     cotnum(y,stdout);
91 
92     return 0;
93 }
94 
95 
96