dce9941e2ceaf54a3b744f06f240387ce888f2f1
2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
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
)
34 uint16_t *t1
, *t2
, *base
;
42 mwlen
= (m
[0] + 31) >> 4;
43 mlen
= mwlen
* sizeof m
[0];
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.
53 if (twlen
< (mwlen
<< 1)) {
56 for (win_len
= 5; win_len
> 1; win_len
--) {
57 if ((((uint32_t)1 << win_len
) + 1) * mwlen
<= twlen
) {
63 * Everything is done in Montgomery representation.
65 br_i15_to_monty(x
, m
);
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).
75 memcpy(t2
+ mwlen
, x
, mlen
);
77 for (u
= 2; u
< ((unsigned)1 << win_len
); u
++) {
78 br_i15_montymul(base
+ mwlen
, base
, x
, m
, m0i
);
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.
90 br_i15_muladd_small(x
, 0, m
);
93 * We process bits from most to least significant. At each
94 * loop iteration, we have acc_len bits in acc.
98 while (acc_len
> 0 || elen
> 0) {
106 if (acc_len
< win_len
) {
108 acc
= (acc
<< 8) | *e
++;
115 bits
= (acc
>> (acc_len
- k
)) & (((uint32_t)1 << k
) - 1);
119 * We could get exactly k bits. Compute k squarings.
121 for (i
= 0; i
< k
; i
++) {
122 br_i15_montymul(t1
, x
, x
, m
, m0i
);
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.
133 br_i15_zero(t2
, m
[0]);
135 for (u
= 1; u
< ((uint32_t)1 << k
); u
++) {
139 for (v
= 1; v
< mwlen
; v
++) {
140 t2
[v
] |= m
& base
[v
];
147 * Multiply with the looked-up value. We keep the
148 * product only if the exponent bits are not all-zero.
150 br_i15_montymul(t1
, x
, t2
, m
, m0i
);
151 CCOPY(NEQ(bits
, 0), x
, t1
, mlen
);
155 * Convert back from Montgomery representation, and exit.
157 br_i15_from_monty(x
, m
, m0i
);