xref: /freebsd/contrib/bearssl/src/int/i31_decmod.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty  * Copyright (c) 2016 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_i31_decode_mod(uint32_t * x,const void * src,size_t len,const uint32_t * m)29*0957b409SSimon J. Gerraty br_i31_decode_mod(uint32_t *x, const void *src, size_t len, const uint32_t *m)
30*0957b409SSimon J. Gerraty {
31*0957b409SSimon J. Gerraty 	/*
32*0957b409SSimon J. Gerraty 	 * Two-pass algorithm: in the first pass, we determine whether the
33*0957b409SSimon J. Gerraty 	 * value fits; in the second pass, we do the actual write.
34*0957b409SSimon J. Gerraty 	 *
35*0957b409SSimon J. Gerraty 	 * During the first pass, 'r' contains the comparison result so
36*0957b409SSimon J. Gerraty 	 * far:
37*0957b409SSimon J. Gerraty 	 *  0x00000000   value is equal to the modulus
38*0957b409SSimon J. Gerraty 	 *  0x00000001   value is greater than the modulus
39*0957b409SSimon J. Gerraty 	 *  0xFFFFFFFF   value is lower than the modulus
40*0957b409SSimon J. Gerraty 	 *
41*0957b409SSimon J. Gerraty 	 * Since we iterate starting with the least significant bytes (at
42*0957b409SSimon J. Gerraty 	 * the end of src[]), each new comparison overrides the previous
43*0957b409SSimon J. Gerraty 	 * except when the comparison yields 0 (equal).
44*0957b409SSimon J. Gerraty 	 *
45*0957b409SSimon J. Gerraty 	 * During the second pass, 'r' is either 0xFFFFFFFF (value fits)
46*0957b409SSimon J. Gerraty 	 * or 0x00000000 (value does not fit).
47*0957b409SSimon J. Gerraty 	 *
48*0957b409SSimon J. Gerraty 	 * We must iterate over all bytes of the source, _and_ possibly
49*0957b409SSimon J. Gerraty 	 * some extra virtual bytes (with value 0) so as to cover the
50*0957b409SSimon J. Gerraty 	 * complete modulus as well. We also add 4 such extra bytes beyond
51*0957b409SSimon J. Gerraty 	 * the modulus length because it then guarantees that no accumulated
52*0957b409SSimon J. Gerraty 	 * partial word remains to be processed.
53*0957b409SSimon J. Gerraty 	 */
54*0957b409SSimon J. Gerraty 	const unsigned char *buf;
55*0957b409SSimon J. Gerraty 	size_t mlen, tlen;
56*0957b409SSimon J. Gerraty 	int pass;
57*0957b409SSimon J. Gerraty 	uint32_t r;
58*0957b409SSimon J. Gerraty 
59*0957b409SSimon J. Gerraty 	buf = src;
60*0957b409SSimon J. Gerraty 	mlen = (m[0] + 31) >> 5;
61*0957b409SSimon J. Gerraty 	tlen = (mlen << 2);
62*0957b409SSimon J. Gerraty 	if (tlen < len) {
63*0957b409SSimon J. Gerraty 		tlen = len;
64*0957b409SSimon J. Gerraty 	}
65*0957b409SSimon J. Gerraty 	tlen += 4;
66*0957b409SSimon J. Gerraty 	r = 0;
67*0957b409SSimon J. Gerraty 	for (pass = 0; pass < 2; pass ++) {
68*0957b409SSimon J. Gerraty 		size_t u, v;
69*0957b409SSimon J. Gerraty 		uint32_t acc;
70*0957b409SSimon J. Gerraty 		int acc_len;
71*0957b409SSimon J. Gerraty 
72*0957b409SSimon J. Gerraty 		v = 1;
73*0957b409SSimon J. Gerraty 		acc = 0;
74*0957b409SSimon J. Gerraty 		acc_len = 0;
75*0957b409SSimon J. Gerraty 		for (u = 0; u < tlen; u ++) {
76*0957b409SSimon J. Gerraty 			uint32_t b;
77*0957b409SSimon J. Gerraty 
78*0957b409SSimon J. Gerraty 			if (u < len) {
79*0957b409SSimon J. Gerraty 				b = buf[len - 1 - u];
80*0957b409SSimon J. Gerraty 			} else {
81*0957b409SSimon J. Gerraty 				b = 0;
82*0957b409SSimon J. Gerraty 			}
83*0957b409SSimon J. Gerraty 			acc |= (b << acc_len);
84*0957b409SSimon J. Gerraty 			acc_len += 8;
85*0957b409SSimon J. Gerraty 			if (acc_len >= 31) {
86*0957b409SSimon J. Gerraty 				uint32_t xw;
87*0957b409SSimon J. Gerraty 
88*0957b409SSimon J. Gerraty 				xw = acc & (uint32_t)0x7FFFFFFF;
89*0957b409SSimon J. Gerraty 				acc_len -= 31;
90*0957b409SSimon J. Gerraty 				acc = b >> (8 - acc_len);
91*0957b409SSimon J. Gerraty 				if (v <= mlen) {
92*0957b409SSimon J. Gerraty 					if (pass) {
93*0957b409SSimon J. Gerraty 						x[v] = r & xw;
94*0957b409SSimon J. Gerraty 					} else {
95*0957b409SSimon J. Gerraty 						uint32_t cc;
96*0957b409SSimon J. Gerraty 
97*0957b409SSimon J. Gerraty 						cc = (uint32_t)CMP(xw, m[v]);
98*0957b409SSimon J. Gerraty 						r = MUX(EQ(cc, 0), r, cc);
99*0957b409SSimon J. Gerraty 					}
100*0957b409SSimon J. Gerraty 				} else {
101*0957b409SSimon J. Gerraty 					if (!pass) {
102*0957b409SSimon J. Gerraty 						r = MUX(EQ(xw, 0), r, 1);
103*0957b409SSimon J. Gerraty 					}
104*0957b409SSimon J. Gerraty 				}
105*0957b409SSimon J. Gerraty 				v ++;
106*0957b409SSimon J. Gerraty 			}
107*0957b409SSimon J. Gerraty 		}
108*0957b409SSimon J. Gerraty 
109*0957b409SSimon J. Gerraty 		/*
110*0957b409SSimon J. Gerraty 		 * When we reach this point at the end of the first pass:
111*0957b409SSimon J. Gerraty 		 * r is either 0, 1 or -1; we want to set r to 0 if it
112*0957b409SSimon J. Gerraty 		 * is equal to 0 or 1, and leave it to -1 otherwise.
113*0957b409SSimon J. Gerraty 		 *
114*0957b409SSimon J. Gerraty 		 * When we reach this point at the end of the second pass:
115*0957b409SSimon J. Gerraty 		 * r is either 0 or -1; we want to leave that value
116*0957b409SSimon J. Gerraty 		 * untouched. This is a subcase of the previous.
117*0957b409SSimon J. Gerraty 		 */
118*0957b409SSimon J. Gerraty 		r >>= 1;
119*0957b409SSimon J. Gerraty 		r |= (r << 1);
120*0957b409SSimon J. Gerraty 	}
121*0957b409SSimon J. Gerraty 
122*0957b409SSimon J. Gerraty 	x[0] = m[0];
123*0957b409SSimon J. Gerraty 	return r & (uint32_t)1;
124*0957b409SSimon J. Gerraty }
125