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