summaryrefslogtreecommitdiff
path: root/crypto/bn/comba.pl
blob: 211a8b45c786890dd2bcb97a8a68840cab76af72 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/local/bin/perl

$num=8;
$num2=8/2;

print <<"EOF";
/* crypto/bn/bn_comba.c */
#include <stdio.h>
#include "bn_lcl.h"
/* Auto generated from crypto/bn/comba.pl
 */

#undef bn_mul_comba8
#undef bn_mul_comba4
#undef bn_sqr_comba8
#undef bn_sqr_comba4

#ifdef BN_LLONG
#define mul_add_c(a,b,c0,c1,c2) \\
	t=(BN_ULLONG)a*b; \\
	t1=(BN_ULONG)Lw(t); \\
	t2=(BN_ULONG)Hw(t); \\
	c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \\
	c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;

#define mul_add_c2(a,b,c0,c1,c2) \\
	t=(BN_ULLONG)a*b; \\
	tt=(t+t)&BN_MASK; \\
	if (tt < t) c2++; \\
	t1=(BN_ULONG)Lw(tt); \\
	t2=(BN_ULONG)Hw(tt); \\
	c0=(c0+t1)&BN_MASK2;  \\
	if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \\
	c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;

#define sqr_add_c(a,i,c0,c1,c2) \\
	t=(BN_ULLONG)a[i]*a[i]; \\
	t1=(BN_ULONG)Lw(t); \\
	t2=(BN_ULONG)Hw(t); \\
	c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \\
	c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;

#define sqr_add_c2(a,i,j,c0,c1,c2) \\
	mul_add_c2((a)[i],(a)[j],c0,c1,c2)
#else
#define mul_add_c(a,b,c0,c1,c2) \\
	t1=LBITS(a); t2=HBITS(a); \\
	bl=LBITS(b); bh=HBITS(b); \\
	mul64(t1,t2,bl,bh); \\
	c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \\
	c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;

#define mul_add_c2(a,b,c0,c1,c2) \\
	t1=LBITS(a); t2=HBITS(a); \\
	bl=LBITS(b); bh=HBITS(b); \\
	mul64(t1,t2,bl,bh); \\
	if (t2 & BN_TBIT) c2++; \\
	t2=(t2+t2)&BN_MASK2; \\
	if (t1 & BN_TBIT) t2++; \\
	t1=(t1+t1)&BN_MASK2; \\
	c0=(c0+t1)&BN_MASK2;  \\
	if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \\
	c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;

#define sqr_add_c(a,i,c0,c1,c2) \\
	sqr64(t1,t2,(a)[i]); \\
	c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \\
	c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;

#define sqr_add_c2(a,i,j,c0,c1,c2) \\
	mul_add_c2((a)[i],(a)[j],c0,c1,c2)
#endif

void bn_mul_comba${num}(r,a,b)
BN_ULONG *r,*a,*b;
	{
#ifdef BN_LLONG
	BN_ULLONG t;
#else
	BN_ULONG bl,bh;
#endif
	BN_ULONG t1,t2;
	BN_ULONG c1,c2,c3;

EOF
$ret=&combas_mul("r","a","b",$num,"c1","c2","c3");
printf <<"EOF";
	}

void bn_mul_comba${num2}(r,a,b)
BN_ULONG *r,*a,*b;
	{
#ifdef BN_LLONG
	BN_ULLONG t;
#else
	BN_ULONG bl,bh;
#endif
	BN_ULONG t1,t2;
	BN_ULONG c1,c2,c3;

EOF
$ret=&combas_mul("r","a","b",$num2,"c1","c2","c3");
printf <<"EOF";
	}

void bn_sqr_comba${num}(r,a)
BN_ULONG *r,*a;
	{
#ifdef BN_LLONG
	BN_ULLONG t,tt;
#else
	BN_ULONG bl,bh;
#endif
	BN_ULONG t1,t2;
	BN_ULONG c1,c2,c3;

EOF
$ret=&combas_sqr("r","a",$num,"c1","c2","c3");
printf <<"EOF";
	}

void bn_sqr_comba${num2}(r,a)
BN_ULONG *r,*a;
	{
#ifdef BN_LLONG
	BN_ULLONG t,tt;
#else
	BN_ULONG bl,bh;
#endif
	BN_ULONG t1,t2;
	BN_ULONG c1,c2,c3;

EOF
$ret=&combas_sqr("r","a",$num2,"c1","c2","c3");
printf <<"EOF";
	}
EOF

sub bn_str
	{
	local($var,$val)=@_;
	print "\t$var=$val;\n";
	}

sub bn_ary
	{
	local($var,$idx)=@_;
	return("${var}[$idx]");
	}

sub bn_clr
	{
	local($var)=@_;

	print "\t$var=0;\n";
	}

sub bn_mad
	{
	local($a,$b,$c0,$c1,$c2,$num)=@_;

	if ($num == 2)
		{ printf("\tmul_add_c2($a,$b,$c0,$c1,$c2);\n"); }
	else
		{ printf("\tmul_add_c($a,$b,$c0,$c1,$c2);\n"); }
	}

sub bn_sad
	{
	local($a,$i,$j,$c0,$c1,$c2,$num)=@_;

	if ($num == 2)
		{ printf("\tsqr_add_c2($a,$i,$j,$c0,$c1,$c2);\n"); }
	else
		{ printf("\tsqr_add_c($a,$i,$c0,$c1,$c2);\n"); }
	}

sub combas_mul
	{
	local($r,$a,$b,$num,$c0,$c1,$c2)=@_;
	local($i,$as,$ae,$bs,$be,$ai,$bi);
	local($tot,$end);

	$as=0;
	$ae=0;
	$bs=0;
	$be=0;
	$tot=$num+$num-1;
	&bn_clr($c0);
	&bn_clr($c1);
	for ($i=0; $i<$tot; $i++)
		{
		$ai=$as;
		$bi=$bs;
		$end=$be+1;
		@numa=@numb=();

#print "($as $ae) ($bs $be) $bs -> $end [$i $num]\n";
		for ($j=$bs; $j<$end; $j++)
			{
			push(@numa,$ai);
			push(@numb,$bi);
			$ai--;
			$bi++;
			}

		if ($i & 1)
			{
			@numa=reverse(@numa);
			@numb=reverse(@numb);
			}

		&bn_clr($c2);
		for ($j=0; $j<=$#numa; $j++)
			{
			&bn_mad(&bn_ary($a,$numa[$j]),
				&bn_ary($b,$numb[$j]),$c0,$c1,$c2,1);
			}
		&bn_str(&bn_ary($r,$i),$c0);
		($c0,$c1,$c2)=($c1,$c2,$c0);

		$as++ if ($i < ($num-1));
		$ae++ if ($i >= ($num-1));

		$bs++ if ($i >= ($num-1));
		$be++ if ($i < ($num-1));
		}
	&bn_str(&bn_ary($r,$i),$c0);
	}

sub combas_sqr
	{
	local($r,$a,$num,$c0,$c1,$c2)=@_;
	local($i,$as,$ae,$bs,$be,$ai,$bi);
	local($b,$tot,$end,$half);

	$b=$a;
	$as=0;
	$ae=0;
	$bs=0;
	$be=0;
	$tot=$num+$num-1;
	&bn_clr($c0);
	&bn_clr($c1);
	for ($i=0; $i<$tot; $i++)
		{
		$ai=$as;
		$bi=$bs;
		$end=$be+1;
		@numa=@numb=();

#print "($as $ae) ($bs $be) $bs -> $end [$i $num]\n";
		for ($j=$bs; $j<$end; $j++)
			{
			push(@numa,$ai);
			push(@numb,$bi);
			$ai--;
			$bi++;
			last if ($ai < $bi);
			}
		if (!($i & 1))
			{
			@numa=reverse(@numa);
			@numb=reverse(@numb);
			}

		&bn_clr($c2);
		for ($j=0; $j <= $#numa; $j++)
			{
			if ($numa[$j] == $numb[$j])
				{&bn_sad($a,$numa[$j],$numb[$j],$c0,$c1,$c2,1);}
			else
				{&bn_sad($a,$numa[$j],$numb[$j],$c0,$c1,$c2,2);}
			}
		&bn_str(&bn_ary($r,$i),$c0);
		($c0,$c1,$c2)=($c1,$c2,$c0);

		$as++ if ($i < ($num-1));
		$ae++ if ($i >= ($num-1));

		$bs++ if ($i >= ($num-1));
		$be++ if ($i < ($num-1));
		}
	&bn_str(&bn_ary($r,$i),$c0);
	}