xref: /illumos-gate/usr/src/common/mpi/mp_gf2m.c (revision c40a6cd7)
1f9fbec18Smcpowers /*
2f9fbec18Smcpowers  * ***** BEGIN LICENSE BLOCK *****
3f9fbec18Smcpowers  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4f9fbec18Smcpowers  *
5f9fbec18Smcpowers  * The contents of this file are subject to the Mozilla Public License Version
6f9fbec18Smcpowers  * 1.1 (the "License"); you may not use this file except in compliance with
7f9fbec18Smcpowers  * the License. You may obtain a copy of the License at
8f9fbec18Smcpowers  * http://www.mozilla.org/MPL/
9f9fbec18Smcpowers  *
10f9fbec18Smcpowers  * Software distributed under the License is distributed on an "AS IS" basis,
11f9fbec18Smcpowers  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12f9fbec18Smcpowers  * for the specific language governing rights and limitations under the
13f9fbec18Smcpowers  * License.
14f9fbec18Smcpowers  *
15f9fbec18Smcpowers  * The Original Code is the Multi-precision Binary Polynomial Arithmetic Library.
16f9fbec18Smcpowers  *
17f9fbec18Smcpowers  * The Initial Developer of the Original Code is
18f9fbec18Smcpowers  * Sun Microsystems, Inc.
19f9fbec18Smcpowers  * Portions created by the Initial Developer are Copyright (C) 2003
20f9fbec18Smcpowers  * the Initial Developer. All Rights Reserved.
21f9fbec18Smcpowers  *
22f9fbec18Smcpowers  * Contributor(s):
23f9fbec18Smcpowers  *   Sheueling Chang Shantz <sheueling.chang@sun.com> and
24f9fbec18Smcpowers  *   Douglas Stebila <douglas@stebila.ca> of Sun Laboratories.
25f9fbec18Smcpowers  *
26f9fbec18Smcpowers  * Alternatively, the contents of this file may be used under the terms of
27f9fbec18Smcpowers  * either the GNU General Public License Version 2 or later (the "GPL"), or
28f9fbec18Smcpowers  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29f9fbec18Smcpowers  * in which case the provisions of the GPL or the LGPL are applicable instead
30f9fbec18Smcpowers  * of those above. If you wish to allow use of your version of this file only
31f9fbec18Smcpowers  * under the terms of either the GPL or the LGPL, and not to allow others to
32f9fbec18Smcpowers  * use your version of this file under the terms of the MPL, indicate your
33f9fbec18Smcpowers  * decision by deleting the provisions above and replace them with the notice
34f9fbec18Smcpowers  * and other provisions required by the GPL or the LGPL. If you do not delete
35f9fbec18Smcpowers  * the provisions above, a recipient may use your version of this file under
36f9fbec18Smcpowers  * the terms of any one of the MPL, the GPL or the LGPL.
37f9fbec18Smcpowers  *
38f9fbec18Smcpowers  * ***** END LICENSE BLOCK ***** */
39f9fbec18Smcpowers /*
40f9fbec18Smcpowers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
41f9fbec18Smcpowers  * Use is subject to license terms.
42f9fbec18Smcpowers  *
43f9fbec18Smcpowers  * Sun elects to use this software under the MPL license.
44f9fbec18Smcpowers  */
45f9fbec18Smcpowers 
46f9fbec18Smcpowers #include "mp_gf2m.h"
47f9fbec18Smcpowers #include "mp_gf2m-priv.h"
48f9fbec18Smcpowers #include "mplogic.h"
49f9fbec18Smcpowers #include "mpi-priv.h"
50f9fbec18Smcpowers 
51f9fbec18Smcpowers const mp_digit mp_gf2m_sqr_tb[16] =
52f9fbec18Smcpowers {
53f9fbec18Smcpowers       0,     1,     4,     5,    16,    17,    20,    21,
54f9fbec18Smcpowers      64,    65,    68,    69,    80,    81,    84,    85
55f9fbec18Smcpowers };
56f9fbec18Smcpowers 
57f9fbec18Smcpowers /* Multiply two binary polynomials mp_digits a, b.
58f9fbec18Smcpowers  * Result is a polynomial with degree < 2 * MP_DIGIT_BITS - 1.
59f9fbec18Smcpowers  * Output in two mp_digits rh, rl.
60f9fbec18Smcpowers  */
61f9fbec18Smcpowers #if MP_DIGIT_BITS == 32
62*c40a6cd7SToomas Soome void
s_bmul_1x1(mp_digit * rh,mp_digit * rl,const mp_digit a,const mp_digit b)63f9fbec18Smcpowers s_bmul_1x1(mp_digit *rh, mp_digit *rl, const mp_digit a, const mp_digit b)
64f9fbec18Smcpowers {
65f9fbec18Smcpowers     register mp_digit h, l, s;
66*c40a6cd7SToomas Soome     mp_digit tab[8], top2b = a >> 30;
67f9fbec18Smcpowers     register mp_digit a1, a2, a4;
68f9fbec18Smcpowers 
69f9fbec18Smcpowers     a1 = a & (0x3FFFFFFF); a2 = a1 << 1; a4 = a2 << 1;
70f9fbec18Smcpowers 
71f9fbec18Smcpowers     tab[0] =  0; tab[1] = a1;    tab[2] = a2;    tab[3] = a1^a2;
72f9fbec18Smcpowers     tab[4] = a4; tab[5] = a1^a4; tab[6] = a2^a4; tab[7] = a1^a2^a4;
73f9fbec18Smcpowers 
74f9fbec18Smcpowers     s = tab[b       & 0x7]; l  = s;
75f9fbec18Smcpowers     s = tab[b >>  3 & 0x7]; l ^= s <<  3; h  = s >> 29;
76f9fbec18Smcpowers     s = tab[b >>  6 & 0x7]; l ^= s <<  6; h ^= s >> 26;
77f9fbec18Smcpowers     s = tab[b >>  9 & 0x7]; l ^= s <<  9; h ^= s >> 23;
78f9fbec18Smcpowers     s = tab[b >> 12 & 0x7]; l ^= s << 12; h ^= s >> 20;
79f9fbec18Smcpowers     s = tab[b >> 15 & 0x7]; l ^= s << 15; h ^= s >> 17;
80f9fbec18Smcpowers     s = tab[b >> 18 & 0x7]; l ^= s << 18; h ^= s >> 14;
81f9fbec18Smcpowers     s = tab[b >> 21 & 0x7]; l ^= s << 21; h ^= s >> 11;
82f9fbec18Smcpowers     s = tab[b >> 24 & 0x7]; l ^= s << 24; h ^= s >>  8;
83f9fbec18Smcpowers     s = tab[b >> 27 & 0x7]; l ^= s << 27; h ^= s >>  5;
84f9fbec18Smcpowers     s = tab[b >> 30      ]; l ^= s << 30; h ^= s >>  2;
85f9fbec18Smcpowers 
86f9fbec18Smcpowers     /* compensate for the top two bits of a */
87f9fbec18Smcpowers 
88*c40a6cd7SToomas Soome     if (top2b & 01) { l ^= b << 30; h ^= b >> 2; }
89*c40a6cd7SToomas Soome     if (top2b & 02) { l ^= b << 31; h ^= b >> 1; }
90f9fbec18Smcpowers 
91f9fbec18Smcpowers     *rh = h; *rl = l;
92*c40a6cd7SToomas Soome }
93f9fbec18Smcpowers #else
94*c40a6cd7SToomas Soome void
s_bmul_1x1(mp_digit * rh,mp_digit * rl,const mp_digit a,const mp_digit b)95f9fbec18Smcpowers s_bmul_1x1(mp_digit *rh, mp_digit *rl, const mp_digit a, const mp_digit b)
96f9fbec18Smcpowers {
97f9fbec18Smcpowers     register mp_digit h, l, s;
98f9fbec18Smcpowers     mp_digit tab[16], top3b = a >> 61;
99f9fbec18Smcpowers     register mp_digit a1, a2, a4, a8;
100f9fbec18Smcpowers 
101*c40a6cd7SToomas Soome     a1 = a & (0x1FFFFFFFFFFFFFFFULL); a2 = a1 << 1;
102f9fbec18Smcpowers     a4 = a2 << 1; a8 = a4 << 1;
103f9fbec18Smcpowers     tab[ 0] = 0;     tab[ 1] = a1;       tab[ 2] = a2;       tab[ 3] = a1^a2;
104f9fbec18Smcpowers     tab[ 4] = a4;    tab[ 5] = a1^a4;    tab[ 6] = a2^a4;    tab[ 7] = a1^a2^a4;
105f9fbec18Smcpowers     tab[ 8] = a8;    tab[ 9] = a1^a8;    tab[10] = a2^a8;    tab[11] = a1^a2^a8;
106f9fbec18Smcpowers     tab[12] = a4^a8; tab[13] = a1^a4^a8; tab[14] = a2^a4^a8; tab[15] = a1^a2^a4^a8;
107f9fbec18Smcpowers 
108f9fbec18Smcpowers     s = tab[b       & 0xF]; l  = s;
109f9fbec18Smcpowers     s = tab[b >>  4 & 0xF]; l ^= s <<  4; h  = s >> 60;
110f9fbec18Smcpowers     s = tab[b >>  8 & 0xF]; l ^= s <<  8; h ^= s >> 56;
111f9fbec18Smcpowers     s = tab[b >> 12 & 0xF]; l ^= s << 12; h ^= s >> 52;
112f9fbec18Smcpowers     s = tab[b >> 16 & 0xF]; l ^= s << 16; h ^= s >> 48;
113f9fbec18Smcpowers     s = tab[b >> 20 & 0xF]; l ^= s << 20; h ^= s >> 44;
114f9fbec18Smcpowers     s = tab[b >> 24 & 0xF]; l ^= s << 24; h ^= s >> 40;
115f9fbec18Smcpowers     s = tab[b >> 28 & 0xF]; l ^= s << 28; h ^= s >> 36;
116f9fbec18Smcpowers     s = tab[b >> 32 & 0xF]; l ^= s << 32; h ^= s >> 32;
117f9fbec18Smcpowers     s = tab[b >> 36 & 0xF]; l ^= s << 36; h ^= s >> 28;
118f9fbec18Smcpowers     s = tab[b >> 40 & 0xF]; l ^= s << 40; h ^= s >> 24;
119f9fbec18Smcpowers     s = tab[b >> 44 & 0xF]; l ^= s << 44; h ^= s >> 20;
120f9fbec18Smcpowers     s = tab[b >> 48 & 0xF]; l ^= s << 48; h ^= s >> 16;
121f9fbec18Smcpowers     s = tab[b >> 52 & 0xF]; l ^= s << 52; h ^= s >> 12;
122f9fbec18Smcpowers     s = tab[b >> 56 & 0xF]; l ^= s << 56; h ^= s >>  8;
123f9fbec18Smcpowers     s = tab[b >> 60      ]; l ^= s << 60; h ^= s >>  4;
124f9fbec18Smcpowers 
125f9fbec18Smcpowers     /* compensate for the top three bits of a */
126f9fbec18Smcpowers 
127*c40a6cd7SToomas Soome     if (top3b & 01) { l ^= b << 61; h ^= b >> 3; }
128*c40a6cd7SToomas Soome     if (top3b & 02) { l ^= b << 62; h ^= b >> 2; }
129*c40a6cd7SToomas Soome     if (top3b & 04) { l ^= b << 63; h ^= b >> 1; }
130f9fbec18Smcpowers 
131f9fbec18Smcpowers     *rh = h; *rl = l;
132*c40a6cd7SToomas Soome }
133f9fbec18Smcpowers #endif
134f9fbec18Smcpowers 
135*c40a6cd7SToomas Soome /* Compute xor-multiply of two binary polynomials  (a1, a0) x (b1, b0)
136f9fbec18Smcpowers  * result is a binary polynomial in 4 mp_digits r[4].
137f9fbec18Smcpowers  * The caller MUST ensure that r has the right amount of space allocated.
138f9fbec18Smcpowers  */
139*c40a6cd7SToomas Soome void
s_bmul_2x2(mp_digit * r,const mp_digit a1,const mp_digit a0,const mp_digit b1,const mp_digit b0)140f9fbec18Smcpowers s_bmul_2x2(mp_digit *r, const mp_digit a1, const mp_digit a0, const mp_digit b1,
141f9fbec18Smcpowers            const mp_digit b0)
142f9fbec18Smcpowers {
143f9fbec18Smcpowers     mp_digit m1, m0;
144f9fbec18Smcpowers     /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */
145f9fbec18Smcpowers     s_bmul_1x1(r+3, r+2, a1, b1);
146f9fbec18Smcpowers     s_bmul_1x1(r+1, r, a0, b0);
147f9fbec18Smcpowers     s_bmul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1);
148f9fbec18Smcpowers     /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */
149f9fbec18Smcpowers     r[2] ^= m1 ^ r[1] ^ r[3];  /* h0 ^= m1 ^ l1 ^ h1; */
150f9fbec18Smcpowers     r[1]  = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0;  /* l1 ^= l0 ^ h0 ^ m0; */
151f9fbec18Smcpowers }
152f9fbec18Smcpowers 
153*c40a6cd7SToomas Soome /* Compute xor-multiply of two binary polynomials  (a2, a1, a0) x (b2, b1, b0)
154f9fbec18Smcpowers  * result is a binary polynomial in 6 mp_digits r[6].
155f9fbec18Smcpowers  * The caller MUST ensure that r has the right amount of space allocated.
156f9fbec18Smcpowers  */
157*c40a6cd7SToomas Soome void
s_bmul_3x3(mp_digit * r,const mp_digit a2,const mp_digit a1,const mp_digit a0,const mp_digit b2,const mp_digit b1,const mp_digit b0)158*c40a6cd7SToomas Soome s_bmul_3x3(mp_digit *r, const mp_digit a2, const mp_digit a1, const mp_digit a0,
159f9fbec18Smcpowers 	const mp_digit b2, const mp_digit b1, const mp_digit b0)
160f9fbec18Smcpowers {
161f9fbec18Smcpowers 	mp_digit zm[4];
162f9fbec18Smcpowers 
163f9fbec18Smcpowers 	s_bmul_1x1(r+5, r+4, a2, b2);         /* fill top 2 words */
164f9fbec18Smcpowers 	s_bmul_2x2(zm, a1, a2^a0, b1, b2^b0); /* fill middle 4 words */
165f9fbec18Smcpowers 	s_bmul_2x2(r, a1, a0, b1, b0);        /* fill bottom 4 words */
166f9fbec18Smcpowers 
167f9fbec18Smcpowers 	zm[3] ^= r[3];
168*c40a6cd7SToomas Soome 	zm[2] ^= r[2];
169f9fbec18Smcpowers 	zm[1] ^= r[1] ^ r[5];
170f9fbec18Smcpowers 	zm[0] ^= r[0] ^ r[4];
171f9fbec18Smcpowers 
172f9fbec18Smcpowers 	r[5]  ^= zm[3];
173f9fbec18Smcpowers 	r[4]  ^= zm[2];
174f9fbec18Smcpowers 	r[3]  ^= zm[1];
175f9fbec18Smcpowers 	r[2]  ^= zm[0];
176f9fbec18Smcpowers }
177f9fbec18Smcpowers 
178*c40a6cd7SToomas Soome /* Compute xor-multiply of two binary polynomials  (a3, a2, a1, a0) x (b3, b2, b1, b0)
179f9fbec18Smcpowers  * result is a binary polynomial in 8 mp_digits r[8].
180f9fbec18Smcpowers  * The caller MUST ensure that r has the right amount of space allocated.
181f9fbec18Smcpowers  */
s_bmul_4x4(mp_digit * r,const mp_digit a3,const mp_digit a2,const mp_digit a1,const mp_digit a0,const mp_digit b3,const mp_digit b2,const mp_digit b1,const mp_digit b0)182*c40a6cd7SToomas Soome void s_bmul_4x4(mp_digit *r, const mp_digit a3, const mp_digit a2, const mp_digit a1,
183*c40a6cd7SToomas Soome 	const mp_digit a0, const mp_digit b3, const mp_digit b2, const mp_digit b1,
184f9fbec18Smcpowers 	const mp_digit b0)
185f9fbec18Smcpowers {
186f9fbec18Smcpowers 	mp_digit zm[4];
187f9fbec18Smcpowers 
188f9fbec18Smcpowers 	s_bmul_2x2(r+4, a3, a2, b3, b2);            /* fill top 4 words */
189f9fbec18Smcpowers 	s_bmul_2x2(zm, a3^a1, a2^a0, b3^b1, b2^b0); /* fill middle 4 words */
190f9fbec18Smcpowers 	s_bmul_2x2(r, a1, a0, b1, b0);              /* fill bottom 4 words */
191f9fbec18Smcpowers 
192*c40a6cd7SToomas Soome 	zm[3] ^= r[3] ^ r[7];
193*c40a6cd7SToomas Soome 	zm[2] ^= r[2] ^ r[6];
194*c40a6cd7SToomas Soome 	zm[1] ^= r[1] ^ r[5];
195*c40a6cd7SToomas Soome 	zm[0] ^= r[0] ^ r[4];
196f9fbec18Smcpowers 
197*c40a6cd7SToomas Soome 	r[5]  ^= zm[3];
198f9fbec18Smcpowers 	r[4]  ^= zm[2];
199*c40a6cd7SToomas Soome 	r[3]  ^= zm[1];
200f9fbec18Smcpowers 	r[2]  ^= zm[0];
201f9fbec18Smcpowers }
202f9fbec18Smcpowers 
203f9fbec18Smcpowers /* Compute addition of two binary polynomials a and b,
204*c40a6cd7SToomas Soome  * store result in c; c could be a or b, a and b could be equal;
205f9fbec18Smcpowers  * c is the bitwise XOR of a and b.
206f9fbec18Smcpowers  */
207f9fbec18Smcpowers mp_err
mp_badd(const mp_int * a,const mp_int * b,mp_int * c)208f9fbec18Smcpowers mp_badd(const mp_int *a, const mp_int *b, mp_int *c)
209f9fbec18Smcpowers {
210f9fbec18Smcpowers     mp_digit *pa, *pb, *pc;
211f9fbec18Smcpowers     mp_size ix;
212f9fbec18Smcpowers     mp_size used_pa, used_pb;
213f9fbec18Smcpowers     mp_err res = MP_OKAY;
214f9fbec18Smcpowers 
215f9fbec18Smcpowers     /* Add all digits up to the precision of b.  If b had more
216f9fbec18Smcpowers      * precision than a initially, swap a, b first
217f9fbec18Smcpowers      */
218f9fbec18Smcpowers     if (MP_USED(a) >= MP_USED(b)) {
219f9fbec18Smcpowers         pa = MP_DIGITS(a);
220f9fbec18Smcpowers         pb = MP_DIGITS(b);
221f9fbec18Smcpowers         used_pa = MP_USED(a);
222f9fbec18Smcpowers         used_pb = MP_USED(b);
223f9fbec18Smcpowers     } else {
224f9fbec18Smcpowers         pa = MP_DIGITS(b);
225f9fbec18Smcpowers         pb = MP_DIGITS(a);
226f9fbec18Smcpowers         used_pa = MP_USED(b);
227f9fbec18Smcpowers         used_pb = MP_USED(a);
228f9fbec18Smcpowers     }
229f9fbec18Smcpowers 
230f9fbec18Smcpowers     /* Make sure c has enough precision for the output value */
231f9fbec18Smcpowers     MP_CHECKOK( s_mp_pad(c, used_pa) );
232f9fbec18Smcpowers 
233f9fbec18Smcpowers     /* Do word-by-word xor */
234f9fbec18Smcpowers     pc = MP_DIGITS(c);
235f9fbec18Smcpowers     for (ix = 0; ix < used_pb; ix++) {
236f9fbec18Smcpowers         (*pc++) = (*pa++) ^ (*pb++);
237f9fbec18Smcpowers     }
238f9fbec18Smcpowers 
239f9fbec18Smcpowers     /* Finish the rest of digits until we're actually done */
240f9fbec18Smcpowers     for (; ix < used_pa; ++ix) {
241f9fbec18Smcpowers         *pc++ = *pa++;
242f9fbec18Smcpowers     }
243f9fbec18Smcpowers 
244f9fbec18Smcpowers     MP_USED(c) = used_pa;
245f9fbec18Smcpowers     MP_SIGN(c) = ZPOS;
246f9fbec18Smcpowers     s_mp_clamp(c);
247f9fbec18Smcpowers 
248f9fbec18Smcpowers CLEANUP:
249f9fbec18Smcpowers     return res;
250*c40a6cd7SToomas Soome }
251f9fbec18Smcpowers 
252f9fbec18Smcpowers #define s_mp_div2(a) MP_CHECKOK( mpl_rsh((a), (a), 1) );
253f9fbec18Smcpowers 
254f9fbec18Smcpowers /* Compute binary polynomial multiply d = a * b */
255*c40a6cd7SToomas Soome static void
s_bmul_d(const mp_digit * a,mp_size a_len,mp_digit b,mp_digit * d)256f9fbec18Smcpowers s_bmul_d(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *d)
257f9fbec18Smcpowers {
258f9fbec18Smcpowers     mp_digit a_i, a0b0, a1b1, carry = 0;
259f9fbec18Smcpowers     while (a_len--) {
260f9fbec18Smcpowers         a_i = *a++;
261f9fbec18Smcpowers         s_bmul_1x1(&a1b1, &a0b0, a_i, b);
262f9fbec18Smcpowers         *d++ = a0b0 ^ carry;
263f9fbec18Smcpowers         carry = a1b1;
264f9fbec18Smcpowers     }
265f9fbec18Smcpowers     *d = carry;
266f9fbec18Smcpowers }
267f9fbec18Smcpowers 
268f9fbec18Smcpowers /* Compute binary polynomial xor multiply accumulate d ^= a * b */
269*c40a6cd7SToomas Soome static void
s_bmul_d_add(const mp_digit * a,mp_size a_len,mp_digit b,mp_digit * d)270f9fbec18Smcpowers s_bmul_d_add(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *d)
271f9fbec18Smcpowers {
272f9fbec18Smcpowers     mp_digit a_i, a0b0, a1b1, carry = 0;
273f9fbec18Smcpowers     while (a_len--) {
274f9fbec18Smcpowers         a_i = *a++;
275f9fbec18Smcpowers         s_bmul_1x1(&a1b1, &a0b0, a_i, b);
276f9fbec18Smcpowers         *d++ ^= a0b0 ^ carry;
277f9fbec18Smcpowers         carry = a1b1;
278f9fbec18Smcpowers     }
279f9fbec18Smcpowers     *d ^= carry;
280f9fbec18Smcpowers }
281f9fbec18Smcpowers 
282*c40a6cd7SToomas Soome /* Compute binary polynomial xor multiply c = a * b.
283f9fbec18Smcpowers  * All parameters may be identical.
284f9fbec18Smcpowers  */
285*c40a6cd7SToomas Soome mp_err
mp_bmul(const mp_int * a,const mp_int * b,mp_int * c)286f9fbec18Smcpowers mp_bmul(const mp_int *a, const mp_int *b, mp_int *c)
287f9fbec18Smcpowers {
288f9fbec18Smcpowers     mp_digit *pb, b_i;
289f9fbec18Smcpowers     mp_int tmp;
290f9fbec18Smcpowers     mp_size ib, a_used, b_used;
291f9fbec18Smcpowers     mp_err res = MP_OKAY;
292f9fbec18Smcpowers 
293f9fbec18Smcpowers     MP_DIGITS(&tmp) = 0;
294f9fbec18Smcpowers 
295f9fbec18Smcpowers     ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
296f9fbec18Smcpowers 
297f9fbec18Smcpowers     if (a == c) {
298f9fbec18Smcpowers         MP_CHECKOK( mp_init_copy(&tmp, a) );
299f9fbec18Smcpowers         if (a == b)
300f9fbec18Smcpowers             b = &tmp;
301f9fbec18Smcpowers         a = &tmp;
302f9fbec18Smcpowers     } else if (b == c) {
303f9fbec18Smcpowers         MP_CHECKOK( mp_init_copy(&tmp, b) );
304f9fbec18Smcpowers         b = &tmp;
305f9fbec18Smcpowers     }
306f9fbec18Smcpowers 
307f9fbec18Smcpowers     if (MP_USED(a) < MP_USED(b)) {
308f9fbec18Smcpowers         const mp_int *xch = b;      /* switch a and b if b longer */
309f9fbec18Smcpowers         b = a;
310f9fbec18Smcpowers         a = xch;
311f9fbec18Smcpowers     }
312f9fbec18Smcpowers 
313f9fbec18Smcpowers     MP_USED(c) = 1; MP_DIGIT(c, 0) = 0;
314f9fbec18Smcpowers     MP_CHECKOK( s_mp_pad(c, USED(a) + USED(b)) );
315f9fbec18Smcpowers 
316f9fbec18Smcpowers     pb = MP_DIGITS(b);
317f9fbec18Smcpowers     s_bmul_d(MP_DIGITS(a), MP_USED(a), *pb++, MP_DIGITS(c));
318f9fbec18Smcpowers 
319f9fbec18Smcpowers     /* Outer loop:  Digits of b */
320f9fbec18Smcpowers     a_used = MP_USED(a);
321f9fbec18Smcpowers     b_used = MP_USED(b);
322f9fbec18Smcpowers 	MP_USED(c) = a_used + b_used;
323f9fbec18Smcpowers     for (ib = 1; ib < b_used; ib++) {
324f9fbec18Smcpowers         b_i = *pb++;
325f9fbec18Smcpowers 
326f9fbec18Smcpowers         /* Inner product:  Digits of a */
327f9fbec18Smcpowers         if (b_i)
328f9fbec18Smcpowers             s_bmul_d_add(MP_DIGITS(a), a_used, b_i, MP_DIGITS(c) + ib);
329f9fbec18Smcpowers         else
330f9fbec18Smcpowers             MP_DIGIT(c, ib + a_used) = b_i;
331f9fbec18Smcpowers     }
332f9fbec18Smcpowers 
333f9fbec18Smcpowers     s_mp_clamp(c);
334f9fbec18Smcpowers 
335f9fbec18Smcpowers     SIGN(c) = ZPOS;
336f9fbec18Smcpowers 
337f9fbec18Smcpowers CLEANUP:
338f9fbec18Smcpowers     mp_clear(&tmp);
339f9fbec18Smcpowers     return res;
340f9fbec18Smcpowers }
341f9fbec18Smcpowers 
342f9fbec18Smcpowers 
343*c40a6cd7SToomas Soome /* Compute modular reduction of a and store result in r.
344*c40a6cd7SToomas Soome  * r could be a.
345*c40a6cd7SToomas Soome  * For modular arithmetic, the irreducible polynomial f(t) is represented
346*c40a6cd7SToomas Soome  * as an array of int[], where f(t) is of the form:
347f9fbec18Smcpowers  *     f(t) = t^p[0] + t^p[1] + ... + t^p[k]
348f9fbec18Smcpowers  * where m = p[0] > p[1] > ... > p[k] = 0.
349f9fbec18Smcpowers  */
350f9fbec18Smcpowers mp_err
mp_bmod(const mp_int * a,const unsigned int p[],mp_int * r)351f9fbec18Smcpowers mp_bmod(const mp_int *a, const unsigned int p[], mp_int *r)
352f9fbec18Smcpowers {
353f9fbec18Smcpowers     int j, k;
354f9fbec18Smcpowers     int n, dN, d0, d1;
355f9fbec18Smcpowers     mp_digit zz, *z, tmp;
356f9fbec18Smcpowers     mp_size used;
357f9fbec18Smcpowers     mp_err res = MP_OKAY;
358f9fbec18Smcpowers 
359*c40a6cd7SToomas Soome     /* The algorithm does the reduction in place in r,
360f9fbec18Smcpowers      * if a != r, copy a into r first so reduction can be done in r
361f9fbec18Smcpowers      */
362f9fbec18Smcpowers     if (a != r) {
363f9fbec18Smcpowers         MP_CHECKOK( mp_copy(a, r) );
364f9fbec18Smcpowers     }
365f9fbec18Smcpowers     z = MP_DIGITS(r);
366f9fbec18Smcpowers 
367f9fbec18Smcpowers     /* start reduction */
368f9fbec18Smcpowers     dN = p[0] / MP_DIGIT_BITS;
369f9fbec18Smcpowers     used = MP_USED(r);
370f9fbec18Smcpowers 
371f9fbec18Smcpowers     for (j = used - 1; j > dN;) {
372f9fbec18Smcpowers 
373f9fbec18Smcpowers         zz = z[j];
374f9fbec18Smcpowers         if (zz == 0) {
375f9fbec18Smcpowers             j--; continue;
376f9fbec18Smcpowers         }
377f9fbec18Smcpowers         z[j] = 0;
378f9fbec18Smcpowers 
379f9fbec18Smcpowers         for (k = 1; p[k] > 0; k++) {
380f9fbec18Smcpowers             /* reducing component t^p[k] */
381f9fbec18Smcpowers             n = p[0] - p[k];
382*c40a6cd7SToomas Soome             d0 = n % MP_DIGIT_BITS;
383f9fbec18Smcpowers             d1 = MP_DIGIT_BITS - d0;
384f9fbec18Smcpowers             n /= MP_DIGIT_BITS;
385f9fbec18Smcpowers             z[j-n] ^= (zz>>d0);
386*c40a6cd7SToomas Soome             if (d0)
387f9fbec18Smcpowers                 z[j-n-1] ^= (zz<<d1);
388f9fbec18Smcpowers         }
389f9fbec18Smcpowers 
390f9fbec18Smcpowers         /* reducing component t^0 */
391*c40a6cd7SToomas Soome         n = dN;
392f9fbec18Smcpowers         d0 = p[0] % MP_DIGIT_BITS;
393f9fbec18Smcpowers         d1 = MP_DIGIT_BITS - d0;
394f9fbec18Smcpowers         z[j-n] ^= (zz >> d0);
395*c40a6cd7SToomas Soome         if (d0)
396f9fbec18Smcpowers             z[j-n-1] ^= (zz << d1);
397f9fbec18Smcpowers 
398f9fbec18Smcpowers     }
399f9fbec18Smcpowers 
400f9fbec18Smcpowers     /* final round of reduction */
401f9fbec18Smcpowers     while (j == dN) {
402f9fbec18Smcpowers 
403f9fbec18Smcpowers         d0 = p[0] % MP_DIGIT_BITS;
404*c40a6cd7SToomas Soome         zz = z[dN] >> d0;
405f9fbec18Smcpowers         if (zz == 0) break;
406f9fbec18Smcpowers         d1 = MP_DIGIT_BITS - d0;
407f9fbec18Smcpowers 
408f9fbec18Smcpowers         /* clear up the top d1 bits */
409*c40a6cd7SToomas Soome         if (d0) z[dN] = (z[dN] << d1) >> d1;
410f9fbec18Smcpowers         *z ^= zz; /* reduction t^0 component */
411f9fbec18Smcpowers 
412f9fbec18Smcpowers         for (k = 1; p[k] > 0; k++) {
413f9fbec18Smcpowers             /* reducing component t^p[k]*/
414f9fbec18Smcpowers             n = p[k] / MP_DIGIT_BITS;
415f9fbec18Smcpowers             d0 = p[k] % MP_DIGIT_BITS;
416f9fbec18Smcpowers             d1 = MP_DIGIT_BITS - d0;
417f9fbec18Smcpowers             z[n] ^= (zz << d0);
418f9fbec18Smcpowers             tmp = zz >> d1;
419f9fbec18Smcpowers             if (d0 && tmp)
420f9fbec18Smcpowers                 z[n+1] ^= tmp;
421f9fbec18Smcpowers         }
422f9fbec18Smcpowers     }
423f9fbec18Smcpowers 
424f9fbec18Smcpowers     s_mp_clamp(r);
425f9fbec18Smcpowers CLEANUP:
426f9fbec18Smcpowers     return res;
427f9fbec18Smcpowers }
428f9fbec18Smcpowers 
429*c40a6cd7SToomas Soome /* Compute the product of two polynomials a and b, reduce modulo p,
430f9fbec18Smcpowers  * Store the result in r.  r could be a or b; a could be b.
431f9fbec18Smcpowers  */
432*c40a6cd7SToomas Soome mp_err
mp_bmulmod(const mp_int * a,const mp_int * b,const unsigned int p[],mp_int * r)433f9fbec18Smcpowers mp_bmulmod(const mp_int *a, const mp_int *b, const unsigned int p[], mp_int *r)
434f9fbec18Smcpowers {
435f9fbec18Smcpowers     mp_err res;
436*c40a6cd7SToomas Soome 
437f9fbec18Smcpowers     if (a == b) return mp_bsqrmod(a, p, r);
438f9fbec18Smcpowers     if ((res = mp_bmul(a, b, r) ) != MP_OKAY)
439f9fbec18Smcpowers 	return res;
440f9fbec18Smcpowers     return mp_bmod(r, p, r);
441f9fbec18Smcpowers }
442f9fbec18Smcpowers 
443*c40a6cd7SToomas Soome /* Compute binary polynomial squaring c = a*a mod p .
444f9fbec18Smcpowers  * Parameter r and a can be identical.
445f9fbec18Smcpowers  */
446f9fbec18Smcpowers 
447*c40a6cd7SToomas Soome mp_err
mp_bsqrmod(const mp_int * a,const unsigned int p[],mp_int * r)448f9fbec18Smcpowers mp_bsqrmod(const mp_int *a, const unsigned int p[], mp_int *r)
449f9fbec18Smcpowers {
450f9fbec18Smcpowers     mp_digit *pa, *pr, a_i;
451f9fbec18Smcpowers     mp_int tmp;
452f9fbec18Smcpowers     mp_size ia, a_used;
453f9fbec18Smcpowers     mp_err res;
454f9fbec18Smcpowers 
455f9fbec18Smcpowers     ARGCHK(a != NULL && r != NULL, MP_BADARG);
456f9fbec18Smcpowers     MP_DIGITS(&tmp) = 0;
457f9fbec18Smcpowers 
458f9fbec18Smcpowers     if (a == r) {
459f9fbec18Smcpowers         MP_CHECKOK( mp_init_copy(&tmp, a) );
460f9fbec18Smcpowers         a = &tmp;
461f9fbec18Smcpowers     }
462f9fbec18Smcpowers 
463f9fbec18Smcpowers     MP_USED(r) = 1; MP_DIGIT(r, 0) = 0;
464f9fbec18Smcpowers     MP_CHECKOK( s_mp_pad(r, 2*USED(a)) );
465f9fbec18Smcpowers 
466f9fbec18Smcpowers     pa = MP_DIGITS(a);
467f9fbec18Smcpowers     pr = MP_DIGITS(r);
468f9fbec18Smcpowers     a_used = MP_USED(a);
469f9fbec18Smcpowers 	MP_USED(r) = 2 * a_used;
470f9fbec18Smcpowers 
471f9fbec18Smcpowers     for (ia = 0; ia < a_used; ia++) {
472f9fbec18Smcpowers         a_i = *pa++;
473f9fbec18Smcpowers         *pr++ = gf2m_SQR0(a_i);
474f9fbec18Smcpowers         *pr++ = gf2m_SQR1(a_i);
475f9fbec18Smcpowers     }
476f9fbec18Smcpowers 
477f9fbec18Smcpowers     MP_CHECKOK( mp_bmod(r, p, r) );
478f9fbec18Smcpowers     s_mp_clamp(r);
479f9fbec18Smcpowers     SIGN(r) = ZPOS;
480f9fbec18Smcpowers 
481f9fbec18Smcpowers CLEANUP:
482f9fbec18Smcpowers     mp_clear(&tmp);
483f9fbec18Smcpowers     return res;
484f9fbec18Smcpowers }
485f9fbec18Smcpowers 
486f9fbec18Smcpowers /* Compute binary polynomial y/x mod p, y divided by x, reduce modulo p.
487f9fbec18Smcpowers  * Store the result in r. r could be x or y, and x could equal y.
488*c40a6cd7SToomas Soome  * Uses algorithm Modular_Division_GF(2^m) from
489*c40a6cd7SToomas Soome  *     Chang-Shantz, S.  "From Euclid's GCD to Montgomery Multiplication to
490f9fbec18Smcpowers  *     the Great Divide".
491f9fbec18Smcpowers  */
492*c40a6cd7SToomas Soome int
mp_bdivmod(const mp_int * y,const mp_int * x,const mp_int * pp,const unsigned int p[],mp_int * r)493*c40a6cd7SToomas Soome mp_bdivmod(const mp_int *y, const mp_int *x, const mp_int *pp,
494f9fbec18Smcpowers     const unsigned int p[], mp_int *r)
495f9fbec18Smcpowers {
496f9fbec18Smcpowers     mp_int aa, bb, uu;
497f9fbec18Smcpowers     mp_int *a, *b, *u, *v;
498f9fbec18Smcpowers     mp_err res = MP_OKAY;
499f9fbec18Smcpowers 
500f9fbec18Smcpowers     MP_DIGITS(&aa) = 0;
501f9fbec18Smcpowers     MP_DIGITS(&bb) = 0;
502f9fbec18Smcpowers     MP_DIGITS(&uu) = 0;
503f9fbec18Smcpowers 
504f9fbec18Smcpowers     MP_CHECKOK( mp_init_copy(&aa, x) );
505f9fbec18Smcpowers     MP_CHECKOK( mp_init_copy(&uu, y) );
506f9fbec18Smcpowers     MP_CHECKOK( mp_init_copy(&bb, pp) );
507f9fbec18Smcpowers     MP_CHECKOK( s_mp_pad(r, USED(pp)) );
508f9fbec18Smcpowers     MP_USED(r) = 1; MP_DIGIT(r, 0) = 0;
509f9fbec18Smcpowers 
510f9fbec18Smcpowers     a = &aa; b= &bb; u=&uu; v=r;
511f9fbec18Smcpowers     /* reduce x and y mod p */
512f9fbec18Smcpowers     MP_CHECKOK( mp_bmod(a, p, a) );
513f9fbec18Smcpowers     MP_CHECKOK( mp_bmod(u, p, u) );
514f9fbec18Smcpowers 
515f9fbec18Smcpowers     while (!mp_isodd(a)) {
516f9fbec18Smcpowers         s_mp_div2(a);
517f9fbec18Smcpowers         if (mp_isodd(u)) {
518f9fbec18Smcpowers             MP_CHECKOK( mp_badd(u, pp, u) );
519f9fbec18Smcpowers         }
520f9fbec18Smcpowers         s_mp_div2(u);
521f9fbec18Smcpowers     }
522f9fbec18Smcpowers 
523f9fbec18Smcpowers     do {
524f9fbec18Smcpowers         if (mp_cmp_mag(b, a) > 0) {
525f9fbec18Smcpowers             MP_CHECKOK( mp_badd(b, a, b) );
526f9fbec18Smcpowers             MP_CHECKOK( mp_badd(v, u, v) );
527f9fbec18Smcpowers             do {
528f9fbec18Smcpowers                 s_mp_div2(b);
529f9fbec18Smcpowers                 if (mp_isodd(v)) {
530f9fbec18Smcpowers                     MP_CHECKOK( mp_badd(v, pp, v) );
531f9fbec18Smcpowers                 }
532f9fbec18Smcpowers                 s_mp_div2(v);
533f9fbec18Smcpowers             } while (!mp_isodd(b));
534f9fbec18Smcpowers         }
535f9fbec18Smcpowers         else if ((MP_DIGIT(a,0) == 1) && (MP_USED(a) == 1))
536f9fbec18Smcpowers             break;
537f9fbec18Smcpowers         else {
538f9fbec18Smcpowers             MP_CHECKOK( mp_badd(a, b, a) );
539f9fbec18Smcpowers             MP_CHECKOK( mp_badd(u, v, u) );
540f9fbec18Smcpowers             do {
541f9fbec18Smcpowers                 s_mp_div2(a);
542f9fbec18Smcpowers                 if (mp_isodd(u)) {
543f9fbec18Smcpowers                     MP_CHECKOK( mp_badd(u, pp, u) );
544f9fbec18Smcpowers                 }
545f9fbec18Smcpowers                 s_mp_div2(u);
546f9fbec18Smcpowers             } while (!mp_isodd(a));
547f9fbec18Smcpowers         }
548f9fbec18Smcpowers     } while (1);
549f9fbec18Smcpowers 
550f9fbec18Smcpowers     MP_CHECKOK( mp_copy(u, r) );
551f9fbec18Smcpowers 
552f9fbec18Smcpowers CLEANUP:
553f9fbec18Smcpowers     /* XXX this appears to be a memory leak in the NSS code */
554f9fbec18Smcpowers     mp_clear(&aa);
555f9fbec18Smcpowers     mp_clear(&bb);
556f9fbec18Smcpowers     mp_clear(&uu);
557f9fbec18Smcpowers     return res;
558f9fbec18Smcpowers 
559f9fbec18Smcpowers }
560f9fbec18Smcpowers 
561f9fbec18Smcpowers /* Convert the bit-string representation of a polynomial a into an array
562f9fbec18Smcpowers  * of integers corresponding to the bits with non-zero coefficient.
563f9fbec18Smcpowers  * Up to max elements of the array will be filled.  Return value is total
564f9fbec18Smcpowers  * number of coefficients that would be extracted if array was large enough.
565f9fbec18Smcpowers  */
566f9fbec18Smcpowers int
mp_bpoly2arr(const mp_int * a,unsigned int p[],int max)567f9fbec18Smcpowers mp_bpoly2arr(const mp_int *a, unsigned int p[], int max)
568f9fbec18Smcpowers {
569f9fbec18Smcpowers     int i, j, k;
570f9fbec18Smcpowers     mp_digit top_bit, mask;
571f9fbec18Smcpowers 
572f9fbec18Smcpowers     top_bit = 1;
573f9fbec18Smcpowers     top_bit <<= MP_DIGIT_BIT - 1;
574f9fbec18Smcpowers 
575f9fbec18Smcpowers     for (k = 0; k < max; k++) p[k] = 0;
576f9fbec18Smcpowers     k = 0;
577f9fbec18Smcpowers 
578f9fbec18Smcpowers     for (i = MP_USED(a) - 1; i >= 0; i--) {
579f9fbec18Smcpowers         mask = top_bit;
580f9fbec18Smcpowers         for (j = MP_DIGIT_BIT - 1; j >= 0; j--) {
581f9fbec18Smcpowers             if (MP_DIGITS(a)[i] & mask) {
582f9fbec18Smcpowers                 if (k < max) p[k] = MP_DIGIT_BIT * i + j;
583f9fbec18Smcpowers                 k++;
584f9fbec18Smcpowers             }
585f9fbec18Smcpowers             mask >>= 1;
586f9fbec18Smcpowers         }
587f9fbec18Smcpowers     }
588f9fbec18Smcpowers 
589f9fbec18Smcpowers     return k;
590f9fbec18Smcpowers }
591f9fbec18Smcpowers 
592*c40a6cd7SToomas Soome /* Convert the coefficient array representation of a polynomial to a
593f9fbec18Smcpowers  * bit-string.  The array must be terminated by 0.
594f9fbec18Smcpowers  */
595f9fbec18Smcpowers mp_err
mp_barr2poly(const unsigned int p[],mp_int * a)596f9fbec18Smcpowers mp_barr2poly(const unsigned int p[], mp_int *a)
597f9fbec18Smcpowers {
598f9fbec18Smcpowers 
599f9fbec18Smcpowers     mp_err res = MP_OKAY;
600f9fbec18Smcpowers     int i;
601f9fbec18Smcpowers 
602f9fbec18Smcpowers     mp_zero(a);
603f9fbec18Smcpowers     for (i = 0; p[i] > 0; i++) {
604f9fbec18Smcpowers 	MP_CHECKOK( mpl_set_bit(a, p[i], 1) );
605f9fbec18Smcpowers     }
606f9fbec18Smcpowers     MP_CHECKOK( mpl_set_bit(a, 0, 1) );
607*c40a6cd7SToomas Soome 
608f9fbec18Smcpowers CLEANUP:
609f9fbec18Smcpowers     return res;
610f9fbec18Smcpowers }
611