Debian Codegraph @

curve/ed25519/additions/ge_scalarmult.c

raw
1 #include "crypto_uint32.h"
2 #include "ge.h"
3 #include "crypto_additions.h"
4
5 static unsigned char equal(signed char b,signed char c)
6 {
7 unsigned char ub = b;
8 unsigned char uc = c;
9 unsigned char x = ub ^ uc; /* 0: yes; 1..255: no */
10 crypto_uint32 y = x; /* 0: yes; 1..255: no */
11 y -= 1; /* 4294967295: yes; 0..254: no */
12 y >>= 31; /* 1: yes; 0: no */
13 return y;
14 }
15
16 static unsigned char negative(signed char b)
17 {
18 unsigned long long x = b; /* 18446744073709551361..18446744073709551615: yes; 0..255: no */
19 x >>= 63; /* 1: yes; 0: no */
20 return x;
21 }
22
23 static void cmov(ge_cached *t,const ge_cached *u,unsigned char b)
24 {
25 fe_cmov(t->YplusX,u->YplusX,b);
26 fe_cmov(t->YminusX,u->YminusX,b);
27 fe_cmov(t->Z,u->Z,b);
28 fe_cmov(t->T2d,u->T2d,b);
29 }
30
31 static void select(ge_cached *t,const ge_cached *pre, signed char b)
32 {
33 ge_cached minust;
34 unsigned char bnegative = negative(b);
35 unsigned char babs = b - (((-bnegative) & b) << 1);
36
37 fe_1(t->YplusX);
38 fe_1(t->YminusX);
39 fe_1(t->Z);
40 fe_0(t->T2d);
41
42 cmov(t,pre+0,equal(babs,1));
43 cmov(t,pre+1,equal(babs,2));
44 cmov(t,pre+2,equal(babs,3));
45 cmov(t,pre+3,equal(babs,4));
46 cmov(t,pre+4,equal(babs,5));
47 cmov(t,pre+5,equal(babs,6));
48 cmov(t,pre+6,equal(babs,7));
49 cmov(t,pre+7,equal(babs,8));
50 fe_copy(minust.YplusX,t->YminusX);
51 fe_copy(minust.YminusX,t->YplusX);
52 fe_copy(minust.Z,t->Z);
53 fe_neg(minust.T2d,t->T2d);
54 cmov(t,&minust,bnegative);
55 }
56
57 /*
58 h = a * B
59 where a = a[0]+256*a[1]+...+256^31 a[31]
60 B is the Ed25519 base point (x,4/5) with x positive.
61
62 Preconditions:
63 a[31] <= 127
64 */
65
66 void ge_scalarmult(ge_p3 *h, const unsigned char *a, const ge_p3 *A)
67 {
68 signed char e[64];
69 signed char carry;
70 ge_p1p1 r;
71 ge_p2 s;
72 ge_p3 t0, t1, t2;
73 ge_cached t, pre[8];
74 int i;
75
76 for (i = 0;i < 32;++i) {
77 e[2 * i + 0] = (a[i] >> 0) & 15;
78 e[2 * i + 1] = (a[i] >> 4) & 15;
79 }
80 /* each e[i] is between 0 and 15 */
81 /* e[63] is between 0 and 7 */
82
83 carry = 0;
84 for (i = 0;i < 63;++i) {
85 e[i] += carry;
86 carry = e[i] + 8;
87 carry >>= 4;
88 e[i] -= carry << 4;
89 }
90 e[63] += carry;
91 /* each e[i] is between -8 and 8 */
92
93 // Precomputation:
94 ge_p3_to_cached(pre+0, A); // A
95
96 ge_p3_dbl(&r, A);
97 ge_p1p1_to_p3(&t0, &r);
98 ge_p3_to_cached(pre+1, &t0); // 2A
99
100 ge_add(&r, A, pre+1);
101 ge_p1p1_to_p3(&t1, &r);
102 ge_p3_to_cached(pre+2, &t1); // 3A
103
104 ge_p3_dbl(&r, &t0);
105 ge_p1p1_to_p3(&t0, &r);
106 ge_p3_to_cached(pre+3, &t0); // 4A
107
108 ge_add(&r, A, pre+3);
109 ge_p1p1_to_p3(&t2, &r);
110 ge_p3_to_cached(pre+4, &t2); // 5A
111
112 ge_p3_dbl(&r, &t1);
113 ge_p1p1_to_p3(&t1, &r);
114 ge_p3_to_cached(pre+5, &t1); // 6A
115
116 ge_add(&r, A, pre+5);
117 ge_p1p1_to_p3(&t1, &r);
118 ge_p3_to_cached(pre+6, &t1); // 7A
119
120 ge_p3_dbl(&r, &t0);
121 ge_p1p1_to_p3(&t0, &r);
122 ge_p3_to_cached(pre+7, &t0); // 8A
123
124 ge_p3_0(h);
125
126 for (i = 63;i > 0; i--) {
127 select(&t,pre,e[i]);
128 ge_add(&r, h, &t);
129 ge_p1p1_to_p2(&s,&r);
130
131 ge_p2_dbl(&r,&s); ge_p1p1_to_p2(&s,&r);
132 ge_p2_dbl(&r,&s); ge_p1p1_to_p2(&s,&r);
133 ge_p2_dbl(&r,&s); ge_p1p1_to_p2(&s,&r);
134 ge_p2_dbl(&r,&s); ge_p1p1_to_p3(h,&r);
135
136 }
137 select(&t,pre,e[0]);
138 ge_add(&r, h, &t);
139 ge_p1p1_to_p3(h,&r);
140 }