xref: /freebsd/crypto/libecc/src/nn/nn_mul.c (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1*f0865ec9SKyle Evans /*
2*f0865ec9SKyle Evans  *  Copyright (C) 2017 - This file is part of libecc project
3*f0865ec9SKyle Evans  *
4*f0865ec9SKyle Evans  *  Authors:
5*f0865ec9SKyle Evans  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6*f0865ec9SKyle Evans  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7*f0865ec9SKyle Evans  *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8*f0865ec9SKyle Evans  *
9*f0865ec9SKyle Evans  *  Contributors:
10*f0865ec9SKyle Evans  *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11*f0865ec9SKyle Evans  *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12*f0865ec9SKyle Evans  *
13*f0865ec9SKyle Evans  *  This software is licensed under a dual BSD and GPL v2 license.
14*f0865ec9SKyle Evans  *  See LICENSE file at the root folder of the project.
15*f0865ec9SKyle Evans  */
16*f0865ec9SKyle Evans #include <libecc/nn/nn_add.h>
17*f0865ec9SKyle Evans #include <libecc/nn/nn.h>
18*f0865ec9SKyle Evans /* Use internal API header */
19*f0865ec9SKyle Evans #include "nn_mul.h"
20*f0865ec9SKyle Evans 
21*f0865ec9SKyle Evans /*
22*f0865ec9SKyle Evans  * Compute out = (in1 * in2) & (2^(WORD_BYTES * wlimits) - 1).
23*f0865ec9SKyle Evans  *
24*f0865ec9SKyle Evans  * The function is constant time for all sets of parameters of given
25*f0865ec9SKyle Evans  * lengths.
26*f0865ec9SKyle Evans  *
27*f0865ec9SKyle Evans  * Implementation: while most generic library implement some advanced
28*f0865ec9SKyle Evans  * algorithm (Karatsuba, Toom-Cook, or FFT based algorithms)
29*f0865ec9SKyle Evans  * which provide a performance advantage for large numbers, the code
30*f0865ec9SKyle Evans  * below is mainly oriented towards simplicity and readibility. It is
31*f0865ec9SKyle Evans  * a direct writing of the naive multiplication algorithm one has
32*f0865ec9SKyle Evans  * learned in school.
33*f0865ec9SKyle Evans  *
34*f0865ec9SKyle Evans  * Portability: in order for the code to be portable, all word by
35*f0865ec9SKyle Evans  * word multiplication are actually performed by an helper macro
36*f0865ec9SKyle Evans  * on half words.
37*f0865ec9SKyle Evans  *
38*f0865ec9SKyle Evans  * Note: 'out' is initialized by the function (caller can omit it)
39*f0865ec9SKyle Evans  *
40*f0865ec9SKyle Evans  * Internal use only. Check on input nn left to the caller.
41*f0865ec9SKyle Evans  *
42*f0865ec9SKyle Evans  * The function returns 0 on succes, -1 on error.
43*f0865ec9SKyle Evans  */
_nn_mul_low(nn_t out,nn_src_t in1,nn_src_t in2,u8 wlimit)44*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET static int _nn_mul_low(nn_t out, nn_src_t in1, nn_src_t in2,
45*f0865ec9SKyle Evans 			u8 wlimit)
46*f0865ec9SKyle Evans {
47*f0865ec9SKyle Evans 	word_t carry, prod_high, prod_low;
48*f0865ec9SKyle Evans 	u8 i, j, pos;
49*f0865ec9SKyle Evans 	int ret;
50*f0865ec9SKyle Evans 
51*f0865ec9SKyle Evans 	/* We have to check that wlimit does not exceed our NN_MAX_WORD_LEN */
52*f0865ec9SKyle Evans 	MUST_HAVE(((wlimit * WORD_BYTES) <= NN_MAX_BYTE_LEN), ret, err);
53*f0865ec9SKyle Evans 
54*f0865ec9SKyle Evans 	ret = nn_init(out, (u16)(wlimit * WORD_BYTES)); EG(ret, err);
55*f0865ec9SKyle Evans 
56*f0865ec9SKyle Evans 	for (i = 0; i < in1->wlen; i++) {
57*f0865ec9SKyle Evans 		carry = 0;
58*f0865ec9SKyle Evans 		pos = 0;
59*f0865ec9SKyle Evans 
60*f0865ec9SKyle Evans 		for (j = 0; j < in2->wlen; j++) {
61*f0865ec9SKyle Evans 			pos = (u8)(i + j);
62*f0865ec9SKyle Evans 
63*f0865ec9SKyle Evans 			/*
64*f0865ec9SKyle Evans 			 * size of the result provided by the caller may not
65*f0865ec9SKyle Evans 			 * be large enough for what multiplication may
66*f0865ec9SKyle Evans 			 * generate.
67*f0865ec9SKyle Evans 			 */
68*f0865ec9SKyle Evans 			if (pos >= wlimit) {
69*f0865ec9SKyle Evans 				continue;
70*f0865ec9SKyle Evans 			}
71*f0865ec9SKyle Evans 
72*f0865ec9SKyle Evans 			/*
73*f0865ec9SKyle Evans 			 * Compute the result of the multiplication of
74*f0865ec9SKyle Evans 			 * two words.
75*f0865ec9SKyle Evans 			 */
76*f0865ec9SKyle Evans 			WORD_MUL(prod_high, prod_low,
77*f0865ec9SKyle Evans 				 in1->val[i], in2->val[j]);
78*f0865ec9SKyle Evans 			/*
79*f0865ec9SKyle Evans 			 * And add previous carry.
80*f0865ec9SKyle Evans 			 */
81*f0865ec9SKyle Evans 			prod_low  = (word_t)(prod_low + carry);
82*f0865ec9SKyle Evans 			prod_high = (word_t)(prod_high + (prod_low < carry));
83*f0865ec9SKyle Evans 
84*f0865ec9SKyle Evans 			/*
85*f0865ec9SKyle Evans 			 * Add computed word to what we can currently
86*f0865ec9SKyle Evans 			 * find at current position in result.
87*f0865ec9SKyle Evans 			 */
88*f0865ec9SKyle Evans 			out->val[pos] = (word_t)(out->val[pos] + prod_low);
89*f0865ec9SKyle Evans 			carry = (word_t)(prod_high + (out->val[pos] < prod_low));
90*f0865ec9SKyle Evans 		}
91*f0865ec9SKyle Evans 
92*f0865ec9SKyle Evans 		/*
93*f0865ec9SKyle Evans 		 * What remains in acc_high at end of previous loop should
94*f0865ec9SKyle Evans 		 * be added to next word after pos in result.
95*f0865ec9SKyle Evans 		 */
96*f0865ec9SKyle Evans 		if ((pos + 1) < wlimit) {
97*f0865ec9SKyle Evans 			out->val[pos + 1] = (word_t)(out->val[pos + 1] + carry);
98*f0865ec9SKyle Evans 		}
99*f0865ec9SKyle Evans 	}
100*f0865ec9SKyle Evans 
101*f0865ec9SKyle Evans err:
102*f0865ec9SKyle Evans 	return ret;
103*f0865ec9SKyle Evans }
104*f0865ec9SKyle Evans 
105*f0865ec9SKyle Evans /* Aliased version. Internal use only. Check on input nn left to the caller */
_nn_mul_low_aliased(nn_t out,nn_src_t in1,nn_src_t in2,u8 wlimit)106*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET static int _nn_mul_low_aliased(nn_t out, nn_src_t in1, nn_src_t in2,
107*f0865ec9SKyle Evans 			       u8 wlimit)
108*f0865ec9SKyle Evans {
109*f0865ec9SKyle Evans 	nn out_cpy;
110*f0865ec9SKyle Evans 	int ret;
111*f0865ec9SKyle Evans 	out_cpy.magic = WORD(0);
112*f0865ec9SKyle Evans 
113*f0865ec9SKyle Evans 	ret = _nn_mul_low(&out_cpy, in1, in2, wlimit); EG(ret, err);
114*f0865ec9SKyle Evans 	ret = nn_init(out, out_cpy.wlen); EG(ret, err);
115*f0865ec9SKyle Evans 	ret = nn_copy(out, &out_cpy);
116*f0865ec9SKyle Evans 
117*f0865ec9SKyle Evans err:
118*f0865ec9SKyle Evans 	nn_uninit(&out_cpy);
119*f0865ec9SKyle Evans 
120*f0865ec9SKyle Evans 	return ret;
121*f0865ec9SKyle Evans }
122*f0865ec9SKyle Evans 
123*f0865ec9SKyle Evans /* Public version supporting aliasing. */
nn_mul_low(nn_t out,nn_src_t in1,nn_src_t in2,u8 wlimit)124*f0865ec9SKyle Evans int nn_mul_low(nn_t out, nn_src_t in1, nn_src_t in2, u8 wlimit)
125*f0865ec9SKyle Evans {
126*f0865ec9SKyle Evans 	int ret;
127*f0865ec9SKyle Evans 
128*f0865ec9SKyle Evans 	ret = nn_check_initialized(in1); EG(ret, err);
129*f0865ec9SKyle Evans 	ret = nn_check_initialized(in2); EG(ret, err);
130*f0865ec9SKyle Evans 
131*f0865ec9SKyle Evans 	/* Handle output aliasing */
132*f0865ec9SKyle Evans 	if ((out == in1) || (out == in2)) {
133*f0865ec9SKyle Evans 		ret = _nn_mul_low_aliased(out, in1, in2, wlimit);
134*f0865ec9SKyle Evans 	} else {
135*f0865ec9SKyle Evans 		ret = _nn_mul_low(out, in1, in2, wlimit);
136*f0865ec9SKyle Evans 	}
137*f0865ec9SKyle Evans 
138*f0865ec9SKyle Evans err:
139*f0865ec9SKyle Evans 	return ret;
140*f0865ec9SKyle Evans }
141*f0865ec9SKyle Evans 
142*f0865ec9SKyle Evans /*
143*f0865ec9SKyle Evans  * Compute out = in1 * in2. 'out' is initialized by the function.
144*f0865ec9SKyle Evans  * The function returns 0 on success, -1 on error.
145*f0865ec9SKyle Evans  *
146*f0865ec9SKyle Evans  * Aliasing supported.
147*f0865ec9SKyle Evans  */
nn_mul(nn_t out,nn_src_t in1,nn_src_t in2)148*f0865ec9SKyle Evans int nn_mul(nn_t out, nn_src_t in1, nn_src_t in2)
149*f0865ec9SKyle Evans {
150*f0865ec9SKyle Evans 	int ret;
151*f0865ec9SKyle Evans 
152*f0865ec9SKyle Evans 	ret = nn_check_initialized(in1); EG(ret, err);
153*f0865ec9SKyle Evans 	ret = nn_check_initialized(in2); EG(ret, err);
154*f0865ec9SKyle Evans 	ret = nn_mul_low(out, in1, in2, (u8)(in1->wlen + in2->wlen));
155*f0865ec9SKyle Evans 
156*f0865ec9SKyle Evans err:
157*f0865ec9SKyle Evans 	return ret;
158*f0865ec9SKyle Evans }
159*f0865ec9SKyle Evans 
nn_sqr_low(nn_t out,nn_src_t in,u8 wlimit)160*f0865ec9SKyle Evans int nn_sqr_low(nn_t out, nn_src_t in, u8 wlimit)
161*f0865ec9SKyle Evans {
162*f0865ec9SKyle Evans 	return nn_mul_low(out, in, in, wlimit);
163*f0865ec9SKyle Evans }
164*f0865ec9SKyle Evans 
165*f0865ec9SKyle Evans /*
166*f0865ec9SKyle Evans  * Compute out = in * in. 'out' is initialized by the function.
167*f0865ec9SKyle Evans  * The function returns 0 on success, -1 on error.
168*f0865ec9SKyle Evans  *
169*f0865ec9SKyle Evans  * Aliasing supported.
170*f0865ec9SKyle Evans  */
nn_sqr(nn_t out,nn_src_t in)171*f0865ec9SKyle Evans int nn_sqr(nn_t out, nn_src_t in)
172*f0865ec9SKyle Evans {
173*f0865ec9SKyle Evans 	return nn_mul(out, in, in);
174*f0865ec9SKyle Evans }
175*f0865ec9SKyle Evans 
176*f0865ec9SKyle Evans /*
177*f0865ec9SKyle Evans  * Multiply a multiprecision number by a word, i.e. out = in * w. The function
178*f0865ec9SKyle Evans  * returns 0 on success, -1 on error.
179*f0865ec9SKyle Evans  *
180*f0865ec9SKyle Evans  * Aliasing supported.
181*f0865ec9SKyle Evans  */
nn_mul_word(nn_t out,nn_src_t in,word_t w)182*f0865ec9SKyle Evans int nn_mul_word(nn_t out, nn_src_t in, word_t w)
183*f0865ec9SKyle Evans {
184*f0865ec9SKyle Evans 	nn w_nn;
185*f0865ec9SKyle Evans 	int ret;
186*f0865ec9SKyle Evans 	w_nn.magic = WORD(0);
187*f0865ec9SKyle Evans 
188*f0865ec9SKyle Evans 	ret = nn_check_initialized(in); EG(ret, err);
189*f0865ec9SKyle Evans 	ret = nn_init(&w_nn, WORD_BYTES); EG(ret, err);
190*f0865ec9SKyle Evans 	w_nn.val[0] = w;
191*f0865ec9SKyle Evans 	ret = nn_mul(out, in, &w_nn);
192*f0865ec9SKyle Evans 
193*f0865ec9SKyle Evans err:
194*f0865ec9SKyle Evans 	nn_uninit(&w_nn);
195*f0865ec9SKyle Evans 
196*f0865ec9SKyle Evans 	return ret;
197*f0865ec9SKyle Evans }
198