Menu

[r23]: / trunk / common / ap.c  Maximize  Restore  History

Download this file

751 lines (615 with data), 17.6 kB

  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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*--------------------------------------------------------------------
This source distribution is placed in the public domain by its author,
Jason Papadopoulos. You may use it for any purpose, free of charge,
without having to notify anyone. I disclaim any responsibility for any
errors.
Optionally, please be nice and tell me if you find this source to be
useful. Again optionally, if you add to the functionality present here
please consider making those additions public too, so that others may
benefit from your work.
$Id$
--------------------------------------------------------------------*/
#include <mp_int.h>
#include <ap.h>
/*---------------------------------------------------------------*/
static void ap_check_nwords(ap_t *a, uint32 nwords) {
if (a->num_alloc < nwords) {
a->num_alloc = nwords + 100;
a->val = (uint32 *)xrealloc(a->val, a->num_alloc *
sizeof(uint32));
}
}
/*---------------------------------------------------------------*/
void ap_copy(ap_t *src, ap_t *dest) {
if (src == dest)
return;
ap_check_nwords(dest, src->nwords);
memcpy(dest->val, src->val, src->nwords * sizeof(uint32));
dest->nwords = src->nwords;
dest->sign = src->sign;
}
/*---------------------------------------------------------------*/
void ap_mp2ap(mp_t *src, uint32 sign, ap_t *dest) {
if (mp_is_zero(src)) {
dest->nwords = 0;
dest->sign = POSITIVE;
return;
}
ap_check_nwords(dest, src->nwords);
memcpy(dest->val, src->val, src->nwords * sizeof(uint32));
dest->nwords = src->nwords;
dest->sign = sign;
}
/*---------------------------------------------------------------*/
void ap_si2ap(uint32 i, uint32 sign, ap_t *dest) {
if (i == 0) {
dest->nwords = 0;
dest->sign = POSITIVE;
return;
}
ap_check_nwords(dest, 1);
dest->val[0] = i;
dest->nwords = 1;
dest->sign = sign;
}
/*---------------------------------------------------------------*/
uint32 ap_bits(ap_t *a) {
uint32 i, bits, mask, top_word;
if (ap_is_zero(a))
return 0;
i = a->nwords;
bits = 32 * i;
top_word = a->val[i - 1];
#if defined(GCC_ASM32X) || defined(GCC_ASM64X)
ASM_G("bsrl %1, %0": "=r"(mask) : "rm"(top_word) : "cc");
bits -= 31 - mask;
#else
mask = 0x80000000;
if ((top_word >> 16) == 0) {
mask = 0x8000;
bits -= 16;
}
while ( !(top_word & mask) ) {
bits--;
mask >>= 1;
}
#endif
return bits;
}
/*---------------------------------------------------------------*/
static void ap_add_abs(ap_t *a, ap_t *b, ap_t *sum) {
/* a->nwords is assumed >= b->nwords */
uint32 min_words, max_words;
uint32 i;
uint32 carry = 0;
uint32 acc;
max_words = a->nwords;
min_words = b->nwords;
ap_check_nwords(sum, max_words + 1);
for (i = 0; i < min_words; i++) {
acc = a->val[i] + carry;
carry = (acc < a->val[i]);
sum->val[i] = acc + b->val[i];
carry += (sum->val[i] < acc);
}
for (; i < max_words; i++) {
acc = a->val[i] + carry;
carry = (acc < a->val[i]);
sum->val[i] = acc;
}
if (carry)
sum->val[i++] = carry;
sum->nwords = num_nonzero_words(sum->val, i);
}
/*---------------------------------------------------------------*/
static void ap_sub_abs(ap_t *a, ap_t *b, ap_t *diff) {
/* a->nwords is assumed >= b->nwords */
uint32 min_words, max_words;
uint32 i;
uint32 borrow = 0;
uint32 acc;
max_words = a->nwords;
min_words = b->nwords;
ap_check_nwords(diff, max_words);
for (i = 0; i < min_words; i++) {
acc = a->val[i] - borrow;
borrow = (acc > a->val[i]);
diff->val[i] = acc - b->val[i];
borrow += (diff->val[i] > acc);
}
for (; i < max_words; i++) {
acc = a->val[i] - borrow;
borrow = (acc > a->val[i]);
diff->val[i] = acc;
}
diff->nwords = num_nonzero_words(diff->val, max_words);
}
/*---------------------------------------------------------------*/
void ap_add(ap_t *a, ap_t *b, ap_t *sum) {
if (ap_is_zero(a)) {
ap_copy(b, sum);
return;
}
if (ap_is_zero(b)) {
ap_copy(a, sum);
return;
}
switch(2 * a->sign + b->sign) {
case 2*POSITIVE + POSITIVE:
case 2*NEGATIVE + NEGATIVE:
if (ap_cmp_abs(a, b) >= 0)
ap_add_abs(a, b, sum);
else
ap_add_abs(b, a, sum);
sum->sign = a->sign;
break;
case 2*POSITIVE + NEGATIVE:
if (ap_cmp_abs(a, b) >= 0) {
ap_sub_abs(a, b, sum);
sum->sign = POSITIVE;
}
else {
ap_sub_abs(b, a, sum);
sum->sign = NEGATIVE;
}
break;
case 2*NEGATIVE + POSITIVE:
if (ap_cmp_abs(a, b) > 0) {
ap_sub_abs(a, b, sum);
sum->sign = NEGATIVE;
}
else {
ap_sub_abs(b, a, sum);
sum->sign = POSITIVE;
}
break;
}
}
/*---------------------------------------------------------------*/
void ap_sub(ap_t *a, ap_t *b, ap_t *diff) {
if (ap_is_zero(a)) {
ap_copy(b, diff);
diff->sign = b->sign ^ 1;
return;
}
if (ap_is_zero(b)) {
ap_copy(a, diff);
return;
}
switch(2 * a->sign + b->sign) {
case 2*POSITIVE + POSITIVE:
if (ap_cmp_abs(a, b) >= 0) {
ap_sub_abs(a, b, diff);
diff->sign = POSITIVE;
}
else {
ap_sub_abs(b, a, diff);
diff->sign = NEGATIVE;
}
break;
case 2*NEGATIVE + NEGATIVE:
if (ap_cmp_abs(a, b) > 0) {
ap_sub_abs(a, b, diff);
diff->sign = NEGATIVE;
}
else {
ap_sub_abs(b, a, diff);
diff->sign = POSITIVE;
}
break;
case 2*POSITIVE + NEGATIVE:
case 2*NEGATIVE + POSITIVE:
if (ap_cmp_abs(a, b) >= 0)
ap_add_abs(a, b, diff);
else
ap_add_abs(b, a, diff);
diff->sign = a->sign;
break;
}
}
/*---------------------------------------------------------------*/
void ap_rshift(ap_t *a, uint32 shift, ap_t *res) {
int32 i;
int32 words = a->nwords;
int32 start_word = shift / 32;
uint32 word_shift = shift & 31;
uint32 comp_word_shift = 32 - word_shift;
if (start_word > words) {
res->nwords = 0;
res->sign = POSITIVE;
return;
}
ap_check_nwords(res, (uint32)(words - start_word));
if (word_shift == 0) {
for (i = 0; i < (words-start_word); i++)
res->val[i] = a->val[start_word+i];
}
else {
for (i = 0; i < (words-start_word-1); i++) {
res->val[i] = a->val[start_word+i] >> word_shift |
a->val[start_word+i+1] << comp_word_shift;
}
res->val[i] = a->val[start_word+i] >> word_shift;
}
res->nwords = num_nonzero_words(res->val, (uint32)(words - start_word));
res->sign = a->sign;
}
/*---------------------------------------------------------------*/
void ap_lshift(ap_t *a, uint32 shift, ap_t *res) {
int32 i;
uint32 words = a->nwords;
uint32 start_word = shift / 32;
uint32 word_shift = shift & 31;
uint32 comp_word_shift = 32 - word_shift;
if (ap_is_zero(a)) {
res->nwords = 0;
res->sign = POSITIVE;
return;
}
ap_check_nwords(res, (uint32)(words + start_word + 1));
if (word_shift == 0) {
res->val[words + start_word] = 0;
for (i = words - 1; (int32)i >= 0; i--)
res->val[start_word + i] = a->val[i];
}
else {
res->val[words + start_word] =
a->val[words - 1] >> comp_word_shift;
for (i = words - 1; i; i--) {
res->val[start_word + i] = a->val[i] << word_shift |
a->val[i-1] >> comp_word_shift;
}
res->val[start_word + i] = a->val[i] << word_shift;
}
memset(res->val, 0, start_word * sizeof(uint32));
res->nwords = num_nonzero_words(res->val, words + start_word + 1);
res->sign = a->sign;
}
/*---------------------------------------------------------------*/
static void ap_addmul_1(uint32 *a, uint32 awords, uint32 b, uint32 *x) {
uint32 carry = 0;
#if defined(GCC_ASM32A)
uint32 tmp = awords;
ASM_G(
"negl %0 \n\t"
"jz 1f \n\t"
"0: \n\t"
"movl (%2,%0,4), %%eax \n\t"
"mull %4 \n\t"
"addl %1, %%eax \n\t"
"adcl $0, %%edx \n\t"
"addl %%eax, (%3,%0,4) \n\t"
"movl %%edx, %1 \n\t"
"adcl $0, %1 \n\t"
"addl $1, %0 \n\t"
"jnz 0b \n\t"
"1: \n\t"
: "+r"(tmp), "+r"(carry)
: "r"(a + awords), "r"(x + awords), "m"(b)
: "%eax", "%edx", "cc", "memory");
#elif defined(MSC_ASM32A)
ASM_M
{
push ebx
xor ebx,ebx ; carry
mov ecx,awords ; negative loop count
mov esi,a ; pointer to source
mov edi,x ; pointer to destination
lea esi,[esi+ecx*4]
lea edi,[edi+ecx*4]
neg ecx
jz L1
L0: mov eax,[esi+ecx*4]
mul b
add eax,ebx
adc edx,0
add [edi+ecx*4],eax
mov ebx,edx
adc ebx,0
add ecx,1
jnz L0
mov carry,ebx
L1: pop ebx
}
#elif defined(MSC_ASM64X)
/* TODO: use 64-bit operations (but
check if one 32-bit op is needed) */
ASM_M
{
mov r10,rcx ; entry rcx = *a, rdx = awoords
mov r11,r9 ; r8 = b, r9 = *x
mov rcx,rdx
xor r9,r9 ; carry
lea r10,[r10+rcx*4] ; pointer to source
lea r11,[r11+rcx*4] ; pointer to destination
neg rcx ; note b is in r8 already
jz L1
L0: mov eax,[r10+rcx*4]
mul r8d
add eax,r9d
adc edx,0
add [r11+rcx*4],eax
mov r9d,edx
adc r9d,0
add ecx,1
jnz L0
mov carry,r9d
L1:
}
#else
uint32 i;
uint64 acc;
for (i = 0; i < awords; i++) {
acc = (uint64)a[i] * (uint64)b +
(uint64)carry +
(uint64)x[i];
x[i] = (uint32)acc;
carry = (uint32)(acc >> 32);
}
#endif
x[awords] = carry;
}
/*---------------------------------------------------------------*/
static void ap_addmul(uint32 *a, uint32 awords,
uint32 *b, uint32 bwords,
uint32 *prod) {
/* awords assumed >= bwords */
uint32 i;
for (i = 0; i < bwords; i++)
ap_addmul_1(a, awords, b[i], prod + i);
}
/*---------------------------------------------------------------*/
void ap_mul(ap_t *a, ap_t *b, ap_t *prod, fastmult_info_t *info) {
ap_t *c, *d;
uint32 cwords, dwords, prod_words;
if (ap_is_zero(a) || ap_is_zero(b)) {
prod->nwords = 0;
prod->sign = POSITIVE;
return;
}
if (a->nwords > b->nwords) {
c = a; d = b;
}
else {
c = b; d = a;
}
cwords = c->nwords;
dwords = d->nwords;
prod_words = cwords + dwords;
ap_check_nwords(prod, prod_words);
if (cwords <= FFT_MIN_WORDS) {
uint32 tmp[2 * FFT_MIN_WORDS];
memset(tmp, 0, prod_words * sizeof(uint32));
ap_addmul(c->val, cwords, d->val, dwords, tmp);
memcpy(prod->val, tmp, prod_words * sizeof(uint32));
}
else if (dwords <= FFT_MIN_WORDS) {
uint32 i, j;
uint32 tmp[2 * FFT_MIN_WORDS] = {0};
uint32 tmp_d_val[FFT_MIN_WORDS];
ap_t tmp_d;
uint32 mul_words = MIN(cwords, 2 * FFT_MIN_WORDS - dwords);
if (prod == d) {
tmp_d = *d;
memcpy(tmp_d_val, d->val, dwords * sizeof(uint32));
d = &tmp_d;
d->val = tmp_d_val;
}
for (i = 0; i < cwords - mul_words; i += mul_words) {
ap_addmul(c->val + i, mul_words, d->val, dwords, tmp);
memcpy(prod->val + i, tmp, mul_words * sizeof(uint32));
for (j = 0; j < dwords; j++)
tmp[j] = tmp[j + mul_words];
for (; j < 2 * FFT_MIN_WORDS; j++)
tmp[j] = 0;
}
if (cwords - i < dwords)
ap_addmul(d->val, dwords, c->val + i, cwords - i, tmp);
else
ap_addmul(c->val + i, cwords - i, d->val, dwords, tmp);
memcpy(prod->val + i, tmp,
(cwords - i + dwords) * sizeof(uint32));
}
else {
fastmult(c->val, cwords, d->val, dwords, prod->val, info);
}
prod->nwords = num_nonzero_words(prod->val, prod_words);
prod->sign = c->sign ^ d->sign;
}
/*---------------------------------------------------------------*/
static void ap_mod_1(ap_t *num, ap_t *den, ap_t *res) {
uint32 nwords = num->nwords;
uint32 dwords = den->nwords;
big_mp_t n;
mp_t d, q, r;
ap_check_nwords(res, dwords);
if (dwords == 1) {
res->val[0] = mp_mod_1_core(num->val, nwords, den->val[0]);
res->nwords = (res->val[0]) ? 1 : 0;
res->sign = num->sign;
return;
}
mp_clear(&r);
mp_clear(&d);
d.nwords = dwords;
memcpy(d.val, den->val, dwords * sizeof(uint32));
while (nwords > 0) {
uint32 i;
uint32 chunk = MIN(nwords, MAX_MP_WORDS);
for (i = 0; i < chunk; i++)
n.val[i] = num->val[nwords - chunk + i];
for (i = 0; i < r.nwords; i++)
n.val[chunk + i] = r.val[i];
for (i = chunk + r.nwords; i < 2 * MAX_MP_WORDS; i++)
n.val[i] = 0;
n.nwords = chunk + r.nwords;
mp_divrem_core(&n, &d, &q, &r);
nwords -= chunk;
}
ap_check_nwords(res, dwords);
memcpy(res->val, r.val, dwords * sizeof(uint32));
res->sign = num->sign;
res->nwords = num_nonzero_words(res->val, dwords);
}
/*---------------------------------------------------------------*/
#define NUM_GUARD_BITS 32
void ap_recip(ap_t *a, ap_t *res, uint32 div_bits, fastmult_info_t *info) {
uint32 curr_bits, new_bits;
ap_t r2, a2;
big_mp_t n;
mp_t d, init_q, init_r;
uint32 abits = ap_bits(a);
uint32 prod_bits = abits + div_bits;
/* this is a heavily modified version of the generalized
reciprocal algorithm from Crandall and Pomerance. In
particular:
- the precision is controlled adaptively, so that
the entire reciprocal process has an asymptotic
latency of <= 4 full-precision multiplies
- the iteration process can produce a reciprocal
large enough to divide numbers with up to
(div_bits+bits(a)) bits in one step. C&P specialize
to the case case of prod_bits = 2*bits(a)
*/
memset(&n, 0, sizeof(big_mp_t));
mp_clear(&d);
/* to get the initial approximation for use in the
Newton step, calculate 2^x / (a >> y), where the
numerator and denominator are chosen to be small
enough for the quotient to be computed directly */
curr_bits = MIN(abits, 32 * (MAX_MP_WORDS - 1));
ap_rshift(a, abits - curr_bits, res);
d.nwords = res->nwords;
memcpy(d.val, res->val, d.nwords * sizeof(uint32));
new_bits = MIN(prod_bits, curr_bits + 32 * (MAX_MP_WORDS - 1));
n.nwords = new_bits / 32 + 1;
n.val[n.nwords - 1] = 1 << (new_bits % 32);
/* if x is large enough and y is zero, we have the
answer already */
mp_divrem_core(&n, &d, &init_q, &init_r);
ap_mp2ap(&init_q, POSITIVE, res);
if (new_bits == prod_bits && curr_bits == abits)
return;
/* iterate until log2(answer) == div_bits */
ap_init(&r2);
ap_init(&a2);
while (1) {
/* each iteration will double the number of correct
bits in res. If doubling the precision will produce
more than div_bits correct bits, then reduce the
precision of the answer until doubling will provide
slightly more correct bits than we need */
curr_bits = ap_bits(res);
if (div_bits < 2 * curr_bits - NUM_GUARD_BITS) {
ap_rshift(res, curr_bits -
(div_bits + NUM_GUARD_BITS) / 2, res);
curr_bits = ap_bits(res);
}
/* square the previous answer. The number of bits
in the product will be the new precision level */
ap_mul(res, res, &r2, info);
new_bits = ap_bits(&r2);
/* we have to get (a*res^2 >> new_bits) and
(2 * previous_answer) to the current precision level.
The latter is easy, and just needs a left shift.
The former needs only the high-order bits of 'a'
if the precision is low, or all of 'a' if it is high. */
ap_lshift(res, new_bits - curr_bits + 1, res);
if (abits <= new_bits) {
ap_mul(&r2, a, &r2, info);
}
else {
ap_rshift(a, abits - new_bits, &a2);
ap_mul(&r2, &a2, &r2, info);
}
ap_rshift(&r2, ap_bits(&r2) - new_bits, &r2);
/* compute the next approximation, and if it has
enough bits then we're done */
ap_sub(res, &r2, res);
new_bits = ap_bits(res);
if (div_bits < new_bits) {
ap_rshift(res, new_bits - div_bits - 1, res);
break;
}
}
ap_clear(&r2);
ap_clear(&a2);
}
/*---------------------------------------------------------------*/
void ap_mod(ap_t *num, ap_t *den, ap_t *recip,
ap_t *res, fastmult_info_t *info) {
/* the algorithm is from Crandall and Pomerance, who
cite the Handbook of Applied Cryptography. Note that
we generalize the algorithm from C&P so that
- recip can be *any* size; in particular num can
exceed den*den in size
- the division takes place log2(recip) bits at a time.
This lets calling code deal with huge operands in
chunks of more manageable size
*/
uint32 dbits;
uint32 nbits1, nbits2;
uint32 rbits;
ap_t tmp;
ap_t *curr_num;
if (ap_is_zero(num) || ap_cmp_abs(num, den) == 0) {
res->nwords = 0;
res->sign = POSITIVE;
return;
}
if (ap_cmp_abs(num, den) < 0) {
ap_copy(num, res);
return;
}
if (den->nwords <= MAX_MP_WORDS) {
ap_mod_1(num, den, res);
return;
}
/* perform as many reduction steps as are needed
to get the remainder to the neighborhood of
the correct result */
ap_init(&tmp);
dbits = ap_bits(den);
rbits = ap_bits(recip);
curr_num = num;
nbits1 = ap_bits(curr_num);
do {
/* each iteration removes at most rbits bits from the
numerator. First compute an approximation to
the quotient, using MIN(rbits, bits(curr_num)) bits
of recip and curr_num */
if (nbits1 > rbits) {
ap_rshift(curr_num, nbits1 - rbits, &tmp);
ap_mul(&tmp, recip, &tmp, info);
}
else {
ap_rshift(recip, rbits - nbits1, &tmp);
ap_mul(curr_num, &tmp, &tmp, info);
}
/* compute the high-order bits of the quotient
and multiply by den */
nbits2 = ap_bits(&tmp);
if (nbits2 > nbits1 - dbits) {
ap_rshift(&tmp, nbits2 - (nbits1 - dbits), &tmp);
}
ap_mul(&tmp, den, &tmp, info);
/* compute num - quotient * den. Equalize the
precision before subtracting */
nbits2 = ap_bits(&tmp);
if (nbits2 > nbits1)
ap_rshift(&tmp, nbits2 - nbits1, &tmp);
else
ap_lshift(&tmp, nbits1 - nbits2, &tmp);
ap_sub(curr_num, &tmp, res);
curr_num = res;
nbits1 = ap_bits(curr_num);
} while (nbits1 > dbits + 1);
/* compute the correct result from the approximation */
while (ap_cmp_abs(res, den) >= 0) {
if (res->sign == POSITIVE)
ap_sub(res, den, res);
else
ap_add(res, den, res);
}
ap_clear(&tmp);
}