Improved modular exponentiation (automatic window optimisation if there is enough...
[BearSSL] / src / int / i15_modpow2.c
1 /*
2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "inner.h"
26
27 /* see inner.h */
28 uint32_t
29 br_i15_modpow_opt(uint16_t *x,
30 const unsigned char *e, size_t elen,
31 const uint16_t *m, uint16_t m0i, uint16_t *tmp, size_t twlen)
32 {
33 size_t mlen, mwlen;
34 uint16_t *t1, *t2, *base;
35 size_t u, v;
36 uint32_t acc;
37 int acc_len, win_len;
38
39 /*
40 * Get modulus size.
41 */
42 mwlen = (m[0] + 31) >> 4;
43 mlen = mwlen * sizeof m[0];
44 t1 = tmp;
45 t2 = tmp + mwlen;
46
47 /*
48 * Compute possible window size, with a maximum of 5 bits.
49 * When the window has size 1 bit, we use a specific code
50 * that requires only two temporaries. Otherwise, for a
51 * window of k bits, we need 2^k+1 temporaries.
52 */
53 if (twlen < (mwlen << 1)) {
54 return 0;
55 }
56 for (win_len = 5; win_len > 1; win_len --) {
57 if ((((uint32_t)1 << win_len) + 1) * mwlen <= twlen) {
58 break;
59 }
60 }
61
62 /*
63 * Everything is done in Montgomery representation.
64 */
65 br_i15_to_monty(x, m);
66
67 /*
68 * Compute window contents. If the window has size one bit only,
69 * then t2 is set to x; otherwise, t2[0] is left untouched, and
70 * t2[k] is set to x^k (for k >= 1).
71 */
72 if (win_len == 1) {
73 memcpy(t2, x, mlen);
74 } else {
75 memcpy(t2 + mwlen, x, mlen);
76 base = t2 + mwlen;
77 for (u = 2; u < ((unsigned)1 << win_len); u ++) {
78 br_i15_montymul(base + mwlen, base, x, m, m0i);
79 base += mwlen;
80 }
81 }
82
83 /*
84 * We need to set x to 1, in Montgomery representation. This can
85 * be done efficiently by setting the high word to 1, then doing
86 * one word-sized shift.
87 */
88 br_i15_zero(x, m[0]);
89 x[mwlen - 1] = 1;
90 br_i15_muladd_small(x, 0, m);
91
92 /*
93 * We process bits from most to least significant. At each
94 * loop iteration, we have acc_len bits in acc.
95 */
96 acc = 0;
97 acc_len = 0;
98 while (acc_len > 0 || elen > 0) {
99 int i, k;
100 uint32_t bits;
101
102 /*
103 * Get the next bits.
104 */
105 k = win_len;
106 if (acc_len < win_len) {
107 if (elen > 0) {
108 acc = (acc << 8) | *e ++;
109 elen --;
110 acc_len += 8;
111 } else {
112 k = acc_len;
113 }
114 }
115 bits = (acc >> (acc_len - k)) & (((uint32_t)1 << k) - 1);
116 acc_len -= k;
117
118 /*
119 * We could get exactly k bits. Compute k squarings.
120 */
121 for (i = 0; i < k; i ++) {
122 br_i15_montymul(t1, x, x, m, m0i);
123 memcpy(x, t1, mlen);
124 }
125
126 /*
127 * Window lookup: we want to set t2 to the window
128 * lookup value, assuming the bits are non-zero. If
129 * the window length is 1 bit only, then t2 is
130 * already set; otherwise, we do a constant-time lookup.
131 */
132 if (win_len > 1) {
133 br_i15_zero(t2, m[0]);
134 base = t2 + mwlen;
135 for (u = 1; u < ((uint32_t)1 << k); u ++) {
136 uint32_t m;
137
138 m = -EQ(u, bits);
139 for (v = 1; v < mwlen; v ++) {
140 t2[v] |= m & base[v];
141 }
142 base += mwlen;
143 }
144 }
145
146 /*
147 * Multiply with the looked-up value. We keep the
148 * product only if the exponent bits are not all-zero.
149 */
150 br_i15_montymul(t1, x, t2, m, m0i);
151 CCOPY(NEQ(bits, 0), x, t1, mlen);
152 }
153
154 /*
155 * Convert back from Montgomery representation, and exit.
156 */
157 br_i15_from_monty(x, m, m0i);
158 return 1;
159 }