a081430a61514d42215c43db9fad35c7450d4b9f
[trex.git] /
1
2 #include "api.h"
3 #include "crypto_scalarmult.h"
4 #include "fe.h"
5
6 #ifndef HAVE_TI_MODE
7
8 int crypto_scalarmult(unsigned char *q,
9   const unsigned char *n,
10   const unsigned char *p)
11 {
12   unsigned char e[32];
13   unsigned int i;
14   fe x1;
15   fe x2;
16   fe z2;
17   fe x3;
18   fe z3;
19   fe tmp0;
20   fe tmp1;
21   int pos;
22   unsigned int swap;
23   unsigned int b;
24
25   for (i = 0;i < 32;++i) e[i] = n[i];
26   e[0] &= 248;
27   e[31] &= 127;
28   e[31] |= 64;
29   fe_frombytes(x1,p);
30   fe_1(x2);
31   fe_0(z2);
32   fe_copy(x3,x1);
33   fe_1(z3);
34
35   swap = 0;
36   for (pos = 254;pos >= 0;--pos) {
37     b = e[pos / 8] >> (pos & 7);
38     b &= 1;
39     swap ^= b;
40     fe_cswap(x2,x3,swap);
41     fe_cswap(z2,z3,swap);
42     swap = b;
43 #include "montgomery.h"
44   }
45   fe_cswap(x2,x3,swap);
46   fe_cswap(z2,z3,swap);
47
48   fe_invert(z2,z2);
49   fe_mul(x2,x2,z2);
50   fe_tobytes(q,x2);
51   return 0;
52 }
53
54 #endif