aboutsummaryrefslogtreecommitdiff
path: root/stm32/modexpng_util.c
blob: 27afb191a506b712599acd6a23edc123ed251dc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// helper precomputation routines for the "modexpng" core
//
#include "modexpng_util.h"


//
// internal buffers
//
static uint32_t MOD_FACTOR_N[BUF_NUM_WORDS];
static uint32_t MOD_NN[BUF_NUM_WORDS+1];
static uint32_t MOD_T[BUF_NUM_WORDS+1];

static void _add32(uint32_t, uint32_t, uint32_t, uint32_t *, uint32_t *);
static void _sub32(uint32_t, uint32_t, uint32_t, uint32_t *, uint32_t *);
static void _mul32(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t *, uint32_t *);


//
// calculation of the Montgomery factor
//
void _calc_montgomery_factor(uint32_t num_words, const uint32_t *N, uint32_t *N_FACTOR)
{
		// counters
		uint32_t i, j;		

		// flag
		uint32_t flag_keep;

		// carry and borrow
		uint32_t cry_in, cry_out;
		uint32_t brw_in, brw_out;

		// initially set N_FACTOR = 1
		for (i=0; i<num_words; i++)
				N_FACTOR[i] = i ? 0 : 1;
		
		// do the math
		for (i=0; i<2*(num_words * UINT32_BITS + UINT16_BITS); i++)
		{	
				// clear carry and borrow
				cry_in = 0, brw_in = 0;
		
				// calculate N_FACTOR = N_FACTOR << 1, MOD_FACTOR_N = N_FACTOR - N
				for (j=0; j<num_words; j++)
				{
						cry_out = N_FACTOR[j] >> (UINT32_BITS - 1);		// | N_FACTOR <<= 1
						N_FACTOR[j] <<= 1; N_FACTOR[j] |= cry_in;			// |

						_sub32(N_FACTOR[j], N[j], brw_in, &MOD_FACTOR_N[j], &brw_out);	// MOD_FACTOR_N = N_FACTOR - N
	
						// propagate carry & borrow
						cry_in = cry_out, brw_in = brw_out;
				}

				// obtain flag
				flag_keep = brw_out && !cry_out;

				// now select the right value
				for (j=0; j<num_words; j++)
						N_FACTOR[j] = flag_keep ? N_FACTOR[j] : MOD_FACTOR_N[j];
		}
}


//
// calculation of the modulus-dependent speed-up coefficient
//
void _calc_modulus_coeff(uint32_t num_words, const uint32_t *N, uint32_t *N_COEFF)
{
		// counters
		uint32_t i, j, k, jk;

		// indices
		uint32_t word_index, bit_index;

		// flag
		uint32_t flag_update;
	
		// carries
		uint32_t cry_in, cry_out;
	
		// temporary variables
		uint32_t mod_p, add_s, b_word;
	
		// initially set N_COEFF to 1
		for (i=0; i<=num_words; i++)
				N_COEFF[i] = i ? 0 : 1;

		// also set NN to ~N+1
		// note that since N must be odd, ~N is even, so adding 1 to it doesn't need
		// any carry propagation
		for (i=0; i<num_words; i++) MOD_NN[i] = ~N[i];
		MOD_NN[0] += 1;
		MOD_NN[num_words] = 0xffffffff;
        
		// do the math
		for (i=1; i<(num_words * UINT32_BITS + UINT16_BITS); i++)
		{
				word_index = i / UINT32_BITS;
			  bit_index = i & (UINT32_BITS - 1);

				// clear T
				for (j=0; j<=num_words; j++) MOD_T[j] = 0;
				
				// T = N_COEFF * NN mod 2 ** (modulus_length + 16)
				/*
			   * Note, that we only need the lower half of the product T, so in
			   * the outer loop we always scan entire N_COEFF, but the inner
				 * loop only scans entire NN during the first iteration, and then
			   * keeps skipping one more word every iteration, during the last
				 * iteration we only scan one word of NN.
			   *
			   */
				for (j=0; j<=num_words; j++)
				{   cry_in = 0;
						for (k=0; k<=(num_words-j); k++)
						{		jk = j + k;
								_mul32(N_COEFF[j], MOD_NN[k], MOD_T[jk], cry_in, &mod_p, &cry_out);
								MOD_T[jk] = mod_p;
								cry_in = cry_out;
								if (word_index == jk)
										flag_update = MOD_T[jk] & (1 << bit_index) ? 1 : 0;
						}
				}
				if (flag_update)
				{		cry_in = 0;
						for (j=0; j<=num_words; j++)
						{		b_word = (j == word_index) ? (1 << bit_index) : 0;
								_add32(b_word, N_COEFF[j], cry_in, &add_s, &cry_out);
								N_COEFF[j] = add_s;
								cry_in = cry_out;
						}
				}
		}
}


//
// low-level addition w/ carry
//
static void _add32(uint32_t a, uint32_t b, uint32_t c_in, uint32_t *s, uint32_t *c_out)
{
		uint64_t t;															// intermediate var
	
		t = (uint64_t)a + (uint64_t)b;					// obtain "wide" difference
		t += (uint64_t)(c_in & 1);							// take borrow into account
	
		*s = (uint32_t)t;												// return the lower part of result
		*c_out = (uint32_t)(t >> UINT32_BITS);	// return the higher part of result, ...
		*c_out &= (uint32_t)1;									// ...but truncate it to 1 bit
}


//
// low-level subtraction w/ borrow
//
static void _sub32(uint32_t a, uint32_t b, uint32_t b_in, uint32_t *d, uint32_t *b_out)
{
		uint64_t t;															// intermediate var
	
		t = (uint64_t)a - (uint64_t)b;					// obtain "wide" difference
		t -= (uint64_t)(b_in & 1);							// take borrow into account
	
		*d = (uint32_t)t;												// return the lower part of result
		*b_out = (uint32_t)(t >> UINT32_BITS);	// return the higher part of result, ...
		*b_out &= (uint32_t)1;									// ...but truncate it to 1 bit
}


//
// low-level multiplication w/ carry and pre-adder
//
static void _mul32(uint32_t a, uint32_t b, uint32_t t, uint32_t c_in, uint32_t *p, uint32_t *c_out)
{
		uint64_t r;															// intermediate result
	
		r = (uint64_t)a * (uint64_t)b;					// obtain wide product
		r += (uint64_t)t;												// handle pre-addition
		r += (uint64_t)c_in;										// take carry into account
	
		*p = (uint32_t)r;												// return the lower part of result
		*c_out = (uint32_t)(r >> UINT32_BITS);	// return the higher part of result, ...
}


//
// end-of-file
//