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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
#include <stdio.h>
#include <stdint.h>
#include <string.h>
typedef uint8_t leg_t;
typedef uint16_t dleg_t;
enum { g_legs = 256 };
#define dump_int(A,B,C) dump_int2(A,B,C)
static void dump_int2( uint8_t const * p, int l, int nl )
{
while( l-- )
printf( "%02X", *(p++) );
if( nl ) putchar( 10 );
}
//#define printf(...)
//#define putchar(A)
static int mp_cmp( leg_t const *a, leg_t const *b, int legs )
{
int leg;
for( leg=0; leg<legs; ++leg )
{
if( a[leg] < b[leg] ) return -1;
if( a[leg] > b[leg] ) return 1;
}
return 0;
}
static void add( leg_t *oper, leg_t const *oper2, int legs )
{
dleg_t acc = 0;
while( legs-- > 0 )
{
acc += oper[legs] + oper2[legs];
oper[legs] = (leg_t)acc;
acc >>= 8*sizeof(leg_t);
}
while( acc )
{
acc += oper[legs];
oper[legs--] = (leg_t)acc;
acc >>= 8*sizeof(leg_t);
}
}
static void adm( leg_t *oper, leg_t const *oper2, int legs, int flegs )
{
dleg_t acc = 0;
while( legs-- > 0 )
{
acc += oper[legs] + oper2[legs];
oper[legs] = (leg_t)acc;
acc >>= 8*sizeof(leg_t);
}
while( acc && flegs-- )
{
acc += oper[legs];
oper[legs--] = (leg_t)acc;
acc >>= 8*sizeof(leg_t);
}
}
static void sub( leg_t *oper, leg_t const *oper2, int legs )
{
int borrow = 0, borrow_temp;
while( legs-- )
{
leg_t temp = oper[legs] - oper2[legs];
borrow_temp = temp > oper[legs];
oper[legs] = temp - borrow;
borrow = borrow_temp | ( oper[legs] > temp );
}
while( borrow )
{
leg_t temp = oper[legs] - borrow;
borrow = temp > oper[legs];
oper[legs--] = temp;
}
printf( "sub: %d\n", legs );
}
/* result is guaranteed to be initialized with enough legs prepended to take the carry */
static void mp_mul_uint_add( leg_t *result, leg_t const *oper, leg_t fac, int legs )
{
dleg_t acc = 0;
while( legs-- )
{
acc += (dleg_t)result[legs] + (dleg_t)oper[legs] * (dleg_t)fac;
result[legs] = (leg_t)acc;
acc >>= 8*sizeof(leg_t);
}
while( acc )
{
acc += result[legs];
result[legs--] = (leg_t)acc;
acc >>= 8*sizeof(leg_t);
}
}
static void mul( leg_t * result, leg_t const *oper, leg_t const *oper2, int legs )
{
int leg = legs;
while( leg-- )
mp_mul_uint_add( result + 1 + leg, oper, oper2[leg], legs );
}
static int sub_mod( leg_t * result, leg_t const *a, leg_t const * b, int legs )
{
int borrow = 0, borrow_temp;
while( legs-- )
{
leg_t temp = a[legs] - b[legs];
borrow_temp = temp > a[legs];
result[legs] = temp - borrow;
borrow = borrow_temp | (result[legs] > temp );
}
return borrow;
}
static void negate( leg_t * p, int legs )
{
while( legs && !p[legs-1] ) p[legs--]^=(leg_t)-1;
if( !legs ) return;
p[legs-1]--;
while( legs--) p[legs]^=(leg_t)-1;
}
void kara2( leg_t* p, leg_t *a, leg_t *b, int len )
{
printf( "L%d karaloop: a*b ", len ); dump_int( a, len, 0 ); putchar('*'); dump_int( b, len, 1 );
memset( p, 0, 2 * len * sizeof( leg_t ));
if( len == 2 )
{
mul( p, a, b, len );
printf( "L%d p:", len ); dump_int( p, len*2, 1 );
}
else
{
leg_t scratch[len];
int sign = 0, n = len / 2;
if( mp_cmp( a, a + n, n ) > 0 )
sub_mod( scratch, a, a + n, n );
else
{
sub_mod( scratch, a + n, a, n );
sign = 1;
}
if( mp_cmp( b, b + n, n ) > 0 )
sub_mod( scratch + n, b, b + n, n );
else
{
sub_mod( scratch + n, b + n, b, n );
sign ^= 1;
}
printf( "L%d (x0-x1)*(y1-y0)=", len ); dump_int( scratch, n, 0 ); putchar('*'); dump_int( scratch + n, n, 0 ); printf( "(sign = %d)\n", sign );
kara2( p + n, scratch, scratch + n, n );
if( !sign )
negate( p, len + n );
printf( "L%d psub:", len ); dump_int( p, len*2, 1 );
kara2( scratch, a + n, b + n, n );
adm( p + len, scratch, len, len );
adm( p + n, scratch, len, n );
kara2( scratch, a, b, n );
adm( p + n, scratch, len, n );
adm( p, scratch, len, 0 );
printf( "L%d p:", len ); dump_int( p, len*2, 1 );
}
}
void kara( uint8_t* p, uint8_t *a, uint8_t *b, int len )
{
int i;
memset( p, 0, 2 * len * sizeof(leg_t ) );
if( len <= 3 )
{
printf( "L%d karaend: a*b ", len ); dump_int( a, len, 0 ); putchar('*'); dump_int( b, len, 1 );
mul( p, a, b, len );
printf( "L%d result:\n", len ); dump_int( p, len*2, 1 );
}
else {
int n = len / 2;
int l = ( len + 1 ) / 2;
int o = l - n;
int sh = l + 1;
uint8_t scratch[2*sh];
uint8_t *midpoint = p + len * 2 - n;
printf( "L%d karaloop: a*b ", len ); dump_int( a, len, 0 ); putchar('*'); dump_int( b, len, 1 );
printf( "Values n: %d l: %d o: %d sh: %d\n", n, l, o, sh );
memset( scratch, 0, sizeof(scratch));
memcpy( scratch + 1 , a, l );
memcpy( scratch + 1 + sh , b, l );
/* P3 = (a1+a2)*(b1+b2) */
add( scratch + 1 + o, a + l, n );
add( scratch + sh + 1 + o, b + l, n );
printf( "L%d a1+a2: \n", len ); dump_int( scratch, sh, 1 );
printf( "L%d b1+b2: \n", len ); dump_int( scratch+sh, sh, 1 );
kara( midpoint - 2 * sh, scratch, scratch + sh, sh );
printf( "L%d (a1+a2)*(b1+b2)<<n: \n", len ); dump_int( p, len*2, 1 );
/* P2 = a2*b2 */
kara( scratch, a + l, b + l, n );
printf( "L%d a2*b2: \n", len ); dump_int( scratch, n*2, 1 );
/* P = ( P3 - P2 ) * 2^n + P2 ... and later: + P1 * 2^(2*n) - P1 * 2^n*/
add( midpoint - n, scratch, n * 2 );
printf( "L%d p + p2: \n", len ); dump_int( p, len*2, 1 );
sub( midpoint - n * 2, scratch, n * 2 );
printf( "L%d p - p2<<n: \n", len ); dump_int( p, len*2, 1 );
/* now + P1 * 2^(2*n) - P1 * 2^n*/
kara( scratch, a, b, l );
printf( "L%d a1*b1: \n", len ); dump_int( scratch, l*2, 1 );
sub( midpoint - l * 2, scratch, l * 2 );
printf( "L%d p - p1<<n: \n", len ); dump_int( p, len*2, 1 );
add( midpoint - n - l * 2, scratch, l * 2 );
printf( "L%d p + p1<<n^2: \n", len ); dump_int( p, len*2, 1 );
}
}
#undef putchar
int main() {
uint8_t a[] = { 0x99,0xf3,0x0a,0x3d,0x8a,0xdd,0x2d,0x1f };
uint8_t b[] = { 0xf1,0x48,0x71,0x4b,0xf7,0xb1,0x9a,0x30 };
// uint8_t a[g_legs], b[g_legs];
// uint8_t p[2*g_legs];
/*
static const leg_t b[] = {
0x3D, 0xA9, 0x76, 0x59, 0xE2, 0x80, 0xDB, 0x0B, 0xE6, 0x5B, 0xCC, 0x3A, 0xB7, 0x8F, 0xDA, 0xA9, 0xB7, 0xB7, 0x68, 0xC8, 0x99, 0x31, 0xD7, 0x8D, 0xF8, 0xB1, 0x17, 0x25, 0x33, 0x9E, 0xBC, 0x93,
0xAA, 0x7F, 0xBD, 0x95, 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, 0x67, 0x53, 0x7D, 0x4A, 0x61, 0x2A, 0x18, 0x51, 0xE7, 0x5D, 0x99, 0x98, 0xF0, 0x01, 0xDB,
0xC9, 0xC7, 0x7F, 0x0C, 0x35, 0x2D, 0x40, 0x8C, 0xA7, 0x96, 0xD1, 0x82, 0x04, 0xA6, 0x36, 0xF7, 0xE4, 0x40, 0x40, 0x92, 0x1C, 0x1E, 0x46, 0x7C, 0x52, 0x4E, 0x7C, 0x7A, 0x7E, 0xD3, 0x6C, 0x41,
0x2A, 0x43, 0x4C, 0xEB, 0x23, 0x0B, 0x2D, 0xFE, 0x35, 0x49, 0xC5, 0x77, 0x7A, 0x17, 0xFB, 0x04, 0xB8, 0x50, 0xDE, 0x95, 0xD9, 0x7A, 0xC4, 0x0A, 0x55, 0xEA, 0x6F, 0x75, 0x41, 0xC4, 0xF8, 0x2B,
0x37, 0xBF, 0x90, 0xFE, 0x52, 0x07, 0x4F, 0x19, 0xFA, 0x8F, 0x75, 0xF0, 0x06, 0x7E, 0x82, 0xB1, 0x8A, 0x1A, 0xC0, 0x24, 0xB3, 0x0E, 0x9B, 0x12, 0xC1, 0x4A, 0xB0, 0xDD, 0xCC, 0x03, 0xAA, 0x20
};
uint8_t a[sizeof(b)];
*/
uint8_t p[2*sizeof(b)];
int i;
srandomdev();
// while( 1 )
{
// memset( a, 0, sizeof(a) );
// a[sizeof(b)-1]=2;
/*
memset( b, 0, sizeof(b) );
a[g_legs-1] = random() & 0xff;
for( i=g_legs/2; i<sizeof(a); ++i )
{
b[i] = random() & 0xff;
}
*/
kara2( p, a, b, sizeof(b));
dump_int2( a, sizeof(a), 0 );
putchar( '*' );
dump_int2( b, sizeof(b), 0 );
putchar( '-' );
dump_int2( p, sizeof(p), 1 );
}
return 0;
}
|