File: mp32barrett.c

package info (click to toggle)
beecrypt 2.2.0-pre1-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,820 kB
  • ctags: 1,325
  • sloc: ansic: 12,215; sh: 9,073; asm: 2,715; makefile: 80
file content (996 lines) | stat: -rw-r--r-- 23,085 bytes parent folder | download | duplicates (2)
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/*
 * mp32barrett.c
 *
 * Barrett modular reduction, code
 *
 * For more information on this algorithm, see:
 * "Handbook of Applied Cryptography", Chapter 14.3.3
 *  Menezes, van Oorschot, Vanstone
 *  CRC Press
 *
 * Copyright (c) 1997, 1998, 1999, 2000, 2001 Virtual Unlimited B.V.
 *
 * Author: Bob Deblier <bob@virtualunlimited.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#define BEECRYPT_DLL_EXPORT

#include "mp32.h"
#include "mp32prime.h"
#include "mp32barrett.h"

#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if HAVE_MALLOC_H
# include <malloc.h>
#endif

#include <stdio.h>

/**
 * mp32bzero
 */
void mp32bzero(mp32barrett* b)
{
	b->size = 0;
	b->modl = b->mu = (uint32*) 0;
}

/**
 * mp32binit
 *  allocates the data words for an mp32barrett structure
 *  will allocate 2*size+1 words
 */
void mp32binit(mp32barrett* b, uint32 size)
{
	b->size	= size;
	b->modl	= (uint32*) calloc(2*size+1, sizeof(uint32));

	if (b->modl != (uint32*) 0)
		b->mu = b->modl+size;
	else
		b->mu = (uint32*) 0;
}

/**
 * mp32bfree
 */
void mp32bfree(mp32barrett* b)
{
	if (b->modl != (uint32*) 0)
	{
		free(b->modl);
		b->modl = b->mu = (uint32*) 0;
	}
	b->size = 0;
}

void mp32bcopy(mp32barrett* b, const mp32barrett* copy)
{
	register uint32 size = copy->size;

	if (size)
	{
		if (b->modl)
		{
			if (b->size != size)
				b->modl = (uint32*) realloc(b->modl, (2*size+1) * sizeof(uint32));
		}
		else
			b->modl = (uint32*) malloc((2*size+1) * sizeof(uint32));

		if (b->modl)
		{
			b->size = size;
			b->mu = b->modl+copy->size;
			mp32copy(2*size+1, b->modl, copy->modl);
		}
		else
		{
			b->size = 0;
			b->mu = (uint32*) 0;
		}
	}
	else if (b->modl)
	{
		free(b->modl);
		b->size = 0;
		b->modl = b->mu = (uint32*) 0;
	}
}

/**
 * mp32bset
 */
void mp32bset(mp32barrett* b, uint32 size, const uint32 *data)
{
	if (size > 0)
	{
		if (b->modl)
		{
			if (b->size != size)
				b->modl = (uint32*) realloc(b->modl, (2*size+1) * sizeof(uint32));
		}
		else
			b->modl = (uint32*) malloc((2*size+1) * sizeof(uint32));

		if (b->modl)
		{
			uint32* temp = (uint32*) malloc((6*size+4) * sizeof(uint32));

			b->size = size;
			b->mu = b->modl+size;
			mp32copy(size, b->modl, data);
			mp32bmu_w(b, temp);

			free(temp);
		}
		else
		{
			b->size = 0;
			b->mu = (uint32*) 0;
		}
	}
}

void mp32bsethex(mp32barrett* b, const char* hex)
{
	uint32 length = strlen(hex);
	uint32 size = (length+7) >> 3;
	uint8 rem = (uint8)(length & 0x7);

	if (b->modl)
	{
		if (b->size != size)
			b->modl = (uint32*) realloc(b->modl, (2*size+1) * sizeof(uint32));
	}
	else
		b->modl = (uint32*) malloc((2*size+1) * sizeof(uint32));

	if (b->modl != (uint32*) 0)
	{
		register uint32  val = 0;
		register uint32* dst = b->modl;
		register uint32* temp = (uint32*) malloc((6*size+4) * sizeof(uint32));
		register char ch;

		b->size = size;
		b->mu = b->modl+size;

		while (length-- > 0)
		{
			ch = *(hex++);
			val <<= 4;
			if (ch >= '0' && ch <= '9')
				val += (ch - '0');
			else if (ch >= 'A' && ch <= 'F')
				val += (ch - 'A') + 10;
			else if (ch >= 'a' && ch <= 'f')
				val += (ch - 'a') + 10;

			if ((length & 0x7) == 0)
			{
				*(dst++) = val;
				val = 0;
			}
		}
		if (rem)
			*dst = val;

		mp32bmu_w(b, temp);

		free(temp);
	}
	else
	{
		b->size = 0;
		b->mu = 0;
	}
}

/**
 * mp32bmu_w
 *  computes the Barrett 'mu' coefficient
 *  needs workspace of (6*size+4) words
 */
void mp32bmu_w(mp32barrett* b, uint32* wksp)
{
	register uint32  size = b->size;
	register uint32* divmod = wksp;
	register uint32* dividend = divmod+(size*2+2);
	register uint32* workspace = dividend+(size*2+1);
	register uint32  shift;

	/* normalize modulus before division */
	shift = mp32norm(size, b->modl);
	/* make the dividend, initialize first word to 1 (shifted); the rest is zero */
	*dividend = (uint32) (1 << shift);
	mp32zero(size*2, dividend+1);
	mp32ndivmod(divmod, size*2+1, dividend, size, b->modl, workspace);
	mp32copy(size+1, b->mu, divmod+1);
	/* de-normalize */
	mp32rshift(size, b->modl, shift);
}

/**
 * mp32brnd_w
 *  generates a random number in the range 1 < r < b-1
 *  need workspace of (size) words
 */
void mp32brnd_w(const mp32barrett* b, randomGeneratorContext* rc, uint32* result, uint32* wksp)
{
	uint32 msz = mp32mszcnt(b->size, b->modl);

	mp32copy(b->size, wksp, b->modl);
	mp32subw(b->size, wksp, 1);

	do
	{
		rc->rng->next(rc->param, result, b->size);

		result[0] &= (0xffffffff >> msz);

		while (mp32ge(b->size, result, wksp))
			mp32sub(b->size, result, wksp);
	} while (mp32leone(b->size, result));
}

/**
 * mp32brndodd_w
 *  generates a random odd number in the range 1 < r < b-1
 *  needs workspace of (size) words
 */
void mp32brndodd_w(const mp32barrett* b, randomGeneratorContext* rc, uint32* result, uint32* wksp)
{
	uint32 msz = mp32mszcnt(b->size, b->modl);

	mp32copy(b->size, wksp, b->modl);
	mp32subw(b->size, wksp, 1);

	do
	{
		rc->rng->next(rc->param, result, b->size);

		result[0] &= (0xffffffff >> msz);
		mp32setlsb(b->size, result);

		while (mp32ge(b->size, result, wksp))
		{
			mp32sub(b->size, result, wksp);
			mp32setlsb(b->size, result);
		}
	} while (mp32leone(b->size, result));
}

/**
 * mp32brndinv_w
 *  generates a random invertible (modulo b) in the range 1 < r < b-1
 *  needs workspace of (6*size+6) words
 */
void mp32brndinv_w(const mp32barrett* b, randomGeneratorContext* rc, uint32* result, uint32* inverse, uint32* wksp)
{
	register uint32  size = b->size;

	do
	{
		if (mp32even(size, b->modl))
			mp32brndodd_w(b, rc, result, wksp);
		else
			mp32brnd_w(b, rc, result, wksp);

	} while (mp32binv_w(b, size, result, inverse, wksp) == 0);
}

/**
 * mp32bmod_w
 *  computes the barrett modular reduction of a number x, which has twice the size of b
 *  needs workspace of (2*size+2) words
 */
void mp32bmod_w(const mp32barrett* b, const uint32* xdata, uint32* result, uint32* wksp)
{
	register uint32 rc;
	register uint32 sp = 2;
	register const uint32* src = xdata+b->size+1;
	register       uint32* dst = wksp +b->size+1;

	rc = mp32setmul(sp, dst, b->mu, *(--src));
	*(--dst) = rc;

	while (sp <= b->size)
	{
		sp++;
		if ((rc = *(--src)))
		{
			rc = mp32addmul(sp, dst, b->mu, rc);
			*(--dst) = rc;
		}
		else
			*(--dst) = 0;
	}
	if ((rc = *(--src)))
	{
		rc = mp32addmul(sp, dst, b->mu, rc);
		*(--dst) = rc;
	}
	else
		*(--dst) = 0;

	/* q3 is one word larger than b->modl */
	/* r2 is (2*size+1) words, of which we only needs the (size+1) lsw's */

	sp = b->size;
	rc = 0;

	dst = wksp+b->size+1;
	src = dst;

	*dst = mp32setmul(sp, dst+1, b->modl, *(--src));

	while (sp > 0)
	{
		mp32addmul(sp--, dst, b->modl+(rc++), *(--src));
	}

	mp32setx(b->size+1, wksp, b->size*2, xdata);
	mp32sub(b->size+1, wksp, wksp+b->size+1);

	while (mp32gex(b->size+1, wksp, b->size, b->modl))
	{
		mp32subx(b->size+1, wksp, b->size, b->modl);
	}
	mp32copy(b->size, result, wksp+1);
}

/**
 * mp32bsubone
 *  copies (b-1) into result
 */
void mp32bsubone(const mp32barrett* b, uint32* result)
{
	register uint32 size = b->size;

	mp32copy(size, result, b->modl);
	mp32subw(size, result, 1);
}

/**
 * mp32bneg
 *  computes the negative (modulo b) of x, where x must contain a value between 0 and b-1
 */
void mp32bneg(const mp32barrett* b, const uint32* xdata, uint32* result)
{
	register uint32  size = b->size;

	mp32copy(size, result, xdata);
	mp32neg(size, result);
	mp32add(size, result, b->modl);
}

/**
 * mp32baddmod_w
 *  computes the sum (modulo b) of x and y
 *  needs a workspace of (4*size+2) words
 */
void mp32baddmod_w(const mp32barrett* b, uint32 xsize, const uint32* xdata, uint32 ysize, const uint32* ydata, uint32* result, uint32* wksp)
{
	/* xsize and ysize must be less than or equal to b->size */
	register uint32  size = b->size;
	register uint32* temp = wksp + size*2+2;

	mp32setx(2*size, temp, xsize, xdata);
	mp32addx(2*size, temp, ysize, ydata);

	mp32bmod_w(b, temp, result, wksp);
}

/**
 * mp32bsubmod_w
 *  computes the difference (modulo b) of x and y
 *  needs a workspace of (4*size+2) words
 */
void mp32bsubmod_w(const mp32barrett* b, uint32 xsize, const uint32* xdata, uint32 ysize, const uint32* ydata, uint32* result, uint32* wksp)
{
	/* xsize and ysize must be less than or equal to b->size */
	register uint32  size = b->size;
	register uint32* temp = wksp + size*2+2;
	
	mp32setx(2*size, temp, xsize, xdata);
	if (mp32subx(2*size, temp, ysize, ydata)) /* if there's carry, i.e. the result would be negative, add the modulus */
		mp32addx(2*size, temp, size, b->modl);

	mp32bmod_w(b, temp, result, wksp);
}

/**
 * mp32mulmod_w
 *  computes the product (modulo b) of x and y
 *  needs a workspace of (4*size+2) words
 */
void mp32bmulmod_w(const mp32barrett* b, uint32 xsize, const uint32* xdata, uint32 ysize, const uint32* ydata, uint32* result, uint32* wksp)
{
	/* xsize and ysize must be <= b->size */
	register uint32  size = b->size;
	register uint32* temp = wksp + size*2+2;
	register uint32  fill = size*2-xsize-ysize;

	if (fill)
		mp32zero(fill, temp);

	mp32mul(temp+fill, xsize, xdata, ysize, ydata);
	mp32bmod_w(b, temp, result, wksp);
}

/**
 * mp32bsqrmod_w
 *  computes the square (modulo b) of x
 *  needs a workspace of (4*size+2) words
 */
void mp32bsqrmod_w(const mp32barrett* b, uint32 xsize, const uint32* xdata, uint32* result, uint32* wksp)
{
	/* xsize must be <= b->size */
	register uint32  size = b->size;
	register uint32* temp = wksp + size*2+2;
	register uint32  fill = 2*(size-xsize);

	if (fill)
		mp32zero(fill, temp);

	mp32sqr(temp+fill, xsize, xdata);
	mp32bmod_w(b, temp, result, wksp);
}

/*
 * Sliding Window Exponentiation technique, slightly altered from the method Applied Cryptography:
 *
 * First of all, the table with the powers of g can be reduced by about half; the even powers don't
 * need to be accessed or stored.
 *
 * Get up to K bits starting with a one, if we have that many still available
 *
 * Do the number of squarings of A in the first column, the multiply by the value in column two,
 * and finally do the number of squarings in column three.
 *
 * This table can be used for K=2,3,4 and can be extended
 *  
 *     0 : - | -       | -
 *     1 : 1 |  g1 @ 0 | 0
 *    10 : 1 |  g1 @ 0 | 1
 *    11 : 2 |  g3 @ 1 | 0
 *   100 : 1 |  g1 @ 0 | 2
 *   101 : 3 |  g5 @ 2 | 0
 *   110 : 2 |  g3 @ 1 | 1
 *   111 : 3 |  g7 @ 3 | 0
 *  1000 : 1 |  g1 @ 0 | 3
 *  1001 : 4 |  g9 @ 4 | 0
 *  1010 : 3 |  g5 @ 2 | 1
 *  1011 : 4 | g11 @ 5 | 0
 *  1100 : 2 |  g3 @ 1 | 2
 *  1101 : 4 | g13 @ 6 | 0
 *  1110 : 3 |  g7 @ 3 | 1
 *  1111 : 4 | g15 @ 7 | 0
 *
 */

/**
 * mp32bslide_w
 *  precomputes the sliding window table for computing powers of x modulo b
 *  needs workspace (4*size+2)
 */
void mp32bslide_w(const mp32barrett* b, const uint32 xsize, const uint32* xdata, uint32* slide, uint32* wksp)
{
	register uint32 size = b->size;
	mp32bsqrmod_w(b, xsize, xdata,                     slide       , wksp); /* x^2 mod b, temp */
	mp32bmulmod_w(b, xsize, xdata, size, slide       , slide+size  , wksp); /* x^3 mod b */
	mp32bmulmod_w(b,  size, slide, size, slide+size  , slide+2*size, wksp); /* x^5 mod b */
	mp32bmulmod_w(b,  size, slide, size, slide+2*size, slide+3*size, wksp); /* x^7 mod b */
	mp32bmulmod_w(b,  size, slide, size, slide+3*size, slide+4*size, wksp); /* x^9 mod b */
	mp32bmulmod_w(b,  size, slide, size, slide+4*size, slide+5*size, wksp); /* x^11 mod b */
	mp32bmulmod_w(b,  size, slide, size, slide+5*size, slide+6*size, wksp); /* x^13 mod b */
	mp32bmulmod_w(b,  size, slide, size, slide+6*size, slide+7*size, wksp); /* x^15 mod b */
	mp32setx(size, slide, xsize, xdata);                                    /* x^1 mod b */
}

static byte mp32bslide_presq[16] = 
{ 0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 4, 2, 4, 3, 4 };

static byte mp32bslide_mulg[16] =
{ 0, 0, 0, 1, 0, 2, 1, 3, 0, 4, 2, 5, 1, 6, 3, 7 };

static byte mp32bslide_postsq[16] =
{ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 };

/**
 * needs workspace of 4*size+2 words
 */
void mp32bpowmod_w(const mp32barrett* b, uint32 xsize, const uint32* xdata, uint32 psize, const uint32* pdata, uint32* result, uint32* wksp)
{
	/*
	 * Modular exponention
	 *
	 * Uses sliding window exponentiation; needs extra storage: if K=3, needs 8*size, if K=4, needs 16*size
	 *
	 */

	/* K == 4 for the first try */
	
	uint32  size = b->size;
	uint32  temp;

	while (psize)
	{
		if ((temp = *(pdata++))) /* break when first non-zero word found */
			break;
		psize--;
	}

	/* if temp is still zero, then we're trying to raise x to power zero, and result stays one */
	if (temp)
	{
		uint32* slide = (uint32*) malloc((8*size)*sizeof(uint32));

		mp32bslide_w(b, xsize, xdata, slide, wksp);

		mp32bpowmodsld_w(b, slide, psize, pdata-1, result, wksp);

		free(slide);
	}
}

void mp32bpowmodsld_w(const mp32barrett* b, const uint32* slide, uint32 psize, const uint32* pdata, uint32* result, uint32* wksp)
{
	/*
	 * Modular exponentiation with precomputed sliding window table, so no x is required
	 *
	 */

	uint32 size = b->size;
	uint32 temp;

	mp32setw(size, result, 1);

	while (psize)
	{
		if ((temp = *(pdata++))) /* break when first non-zero word found in power */
			break;
		psize--;
	}

	/* if temp is still zero, then we're trying to raise x to power zero, and result stays one */
	if (temp)
	{
		uint8 l = 0, n = 0, count = 32;

		/* first skip bits until we reach a one */
		while (count)
		{
			if (temp & 0x80000000)
				break;
			temp <<= 1;
			count--;
		}

		while (psize)
		{
			while (count)
			{
				uint8 bit = (temp & 0x80000000) != 0;

				n <<= 1;
				n += bit;
				
				if (n)
				{
					if (l)
						l++;
					else if (bit)
						l = 1;

					if (l == 4)
					{
						uint8 s = mp32bslide_presq[n];
						
						while (s--)
							mp32bsqrmod_w(b, size, result, result, wksp);
						
						mp32bmulmod_w(b, size, result, size, slide+mp32bslide_mulg[n]*size, result, wksp);
						
						s = mp32bslide_postsq[n];
						
						while (s--)
							mp32bsqrmod_w(b, size, result, result, wksp);

						l = n = 0;
					}
				}
				else
					mp32bsqrmod_w(b, size, result, result, wksp);

				temp <<= 1;
				count--;
			}
			if (--psize)
			{
				count = 32;
				temp = *(pdata++);
			}
		}

		if (n)
		{
			uint8 s = mp32bslide_presq[n];
			while (s--)
				mp32bsqrmod_w(b, size, result, result, wksp);
				
			mp32bmulmod_w(b, size, result, size, slide+mp32bslide_mulg[n]*size, result, wksp);
			
			s = mp32bslide_postsq[n];
			
			while (s--)
				mp32bsqrmod_w(b, size, result, result, wksp);
		}
	}	
}

/**
 * mp32btwopowmod_w
 *  needs workspace of (4*size+2) words
 */
void mp32btwopowmod_w(const mp32barrett* b, uint32 psize, const uint32* pdata, uint32* result, uint32* wksp)
{
	/*
	 * Modular exponention, 2^p mod modulus, special optimization
	 *
	 * Uses left-to-right exponentiation; needs no extra storage
	 *
	 */

	/* this routine calls mp32bmod, which needs (size*2+2), this routine needs (size*2) for sdata */

	register uint32 size = b->size;
	register uint32 temp;

	mp32setw(size, result, 1);

	while (psize)
	{
		if ((temp = *(pdata++))) /* break when first non-zero word found */
			break;
		psize--;
	}

	/* if temp is still zero, then we're trying to raise x to power zero, and result stays one */
	if (temp)
	{
		register int count = 32;

		/* first skip bits until we reach a one */
		while (count)
		{
			if (temp & 0x80000000)
				break;
			temp <<= 1;
			count--;
		}

		while (psize--)
		{
			while (count)
			{
				/* always square */
				mp32bsqrmod_w(b, size, result, result, wksp);
				
				/* multiply by two if bit is 1 */
				if (temp & 0x80000000)
				{
					if (mp32add(size, result, result) || mp32ge(size, result, b->modl))
					{
						/* there was carry, or the result is greater than the modulus, so we need to adjust */
						mp32sub(size, result, b->modl);
					}
				}

				temp <<= 1;
				count--;
			}
			count = 32;
			temp = *(pdata++);
		}
	}
}

/**
 * mp32binv_w
 *  computes the inverse (modulo b) of x, and returns 1 if x was invertible
 *  needs workspace of (6*size+6) words
 *  note: xdata and result cannot point to the same area
 */
int mp32binv_w(const mp32barrett* b, uint32 xsize, const uint32* xdata, uint32* result, uint32* wksp)
{
	/*
	 * Fact: if a element of Zn, then a is invertible if and only if gcd(a,n) = 1
	 * Hence: if b->modl is even, then x must be odd, otherwise the gcd(x,n) >= 2
	 *
	 * The calling routine must guarantee this condition.
	 */

	register uint32  size = b->size;

	uint32* udata = wksp;
	uint32* vdata = udata+size+1;
	uint32* adata = vdata+size+1;
	uint32* bdata = adata+size+1;
	uint32* cdata = bdata+size+1;
	uint32* ddata = cdata+size+1;

	if (mp32odd(b->size, b->modl))
	{
		/* use simplified binary extended gcd algorithm */
		mp32setx(size+1, udata, size, b->modl);
		mp32setx(size+1, vdata, xsize, xdata);
		mp32zero(size+1, bdata);
		mp32setw(size+1, ddata, 1);

		while (1)
		{
			while (mp32even(size+1, udata))
			{
				mp32divtwo(size+1, udata);

				if (mp32odd(size+1, bdata))
					mp32subx(size+1, bdata, size, b->modl);

				mp32sdivtwo(size+1, bdata);
			}
			while (mp32even(size+1, vdata))
			{
				mp32divtwo(size+1, vdata);

				if (mp32odd(size+1, ddata))
					mp32subx(size+1, ddata, size, b->modl);

				mp32sdivtwo(size+1, ddata);
			}
			if (mp32ge(size+1, udata, vdata))
			{
				mp32sub(size+1, udata, vdata);
				mp32sub(size+1, bdata, ddata);
			}
			else
			{
				mp32sub(size+1, vdata, udata);
				mp32sub(size+1, ddata, bdata);
			}

			if (mp32z(size+1, udata))
			{
				if (mp32isone(size+1, vdata))
				{
					if (result)
					{
						mp32setx(size, result, size+1, ddata);
						if (*ddata & 0x80000000)
						{
							/* keep adding the modulus until we get a carry */
							while (!mp32add(size, result, b->modl));
						}
					}
					return 1;
				}
				return 0;
			}
		}
	}
	else
	{
		/* use full binary extended gcd algorithm */
		mp32setx(size+1, udata, size, b->modl);
		mp32setx(size+1, vdata, xsize, xdata);
		mp32setw(size+1, adata, 1);
		mp32zero(size+1, bdata);
		mp32zero(size+1, cdata);
		mp32setw(size+1, ddata, 1);

		while (1)
		{
			while (mp32even(size+1, udata))
			{
				mp32divtwo(size+1, udata);

				if (mp32odd(size+1, adata) || mp32odd(size+1, bdata))
				{
					mp32addx(size+1, adata, xsize, xdata);
					mp32subx(size+1, bdata, size, b->modl);
				}

				mp32sdivtwo(size+1, adata);
				mp32sdivtwo(size+1, bdata);
			}
			while (mp32even(size+1, vdata))
			{
				mp32divtwo(size+1, vdata);

				if (mp32odd(size+1, cdata) || mp32odd(size+1, ddata))
				{
					mp32addx(size+1, cdata, xsize, xdata);
					mp32subx(size+1, ddata, size, b->modl);
				}

				mp32sdivtwo(size+1, cdata);
				mp32sdivtwo(size+1, ddata);
			}
			if (mp32ge(size+1, udata, vdata))
			{
				mp32sub(size+1, udata, vdata);
				mp32sub(size+1, adata, cdata);
				mp32sub(size+1, bdata, ddata);
			}
			else
			{
				mp32sub(size+1, vdata, udata);
				mp32sub(size+1, cdata, adata);
				mp32sub(size+1, ddata, bdata);
			}

			if (mp32z(size+1, udata))
			{
				if (mp32isone(size+1, vdata))
				{
					if (result)
					{
						mp32setx(size, result, size+1, ddata);
						if (*ddata & 0x80000000)
						{
							/* keep adding the modulus until we get a carry */
							while (!mp32add(size, result, b->modl));
						}
					}
					return 1;
				}
				return 0;
			}
		}
	}
}

/**
 * needs workspace of (7*size+2) words
 */
int mp32bpprime_w(const mp32barrett* b, randomGeneratorContext* r, int t, uint32* wksp)
{
	/*
	 * This test works for candidate probable primes >= 3, which are also not small primes.
	 *
	 * It assumes that b->modl contains the candidate prime
	 *
	 */

	uint32 size = b->size;

	/* first test if modl is odd */

	if (mp32odd(b->size, b->modl))
	{
		/*
		 * Small prime factor test:
		 * 
		 * Tables in mp32spprod contain multi-precision integers with products of small primes
		 * If the greatest common divisor of this product and the candidate is not one, then
		 * the candidate has small prime factors, or is a small prime. Neither is acceptable when
		 * we are looking for large probable primes =)
		 *
		 */
		
		if (size > SMALL_PRIMES_PRODUCT_MAX)
		{
			mp32setx(size, wksp+size, SMALL_PRIMES_PRODUCT_MAX, mp32spprod[SMALL_PRIMES_PRODUCT_MAX-1]);
			mp32gcd_w(size, b->modl, wksp+size, wksp, wksp+2*size);
		}
		else
		{
			mp32gcd_w(size, b->modl, mp32spprod[size-1], wksp, wksp+2*size);
		}

		if (mp32isone(size, wksp))
		{
			return mp32pmilrab_w(b, r, t, wksp);
		}
	}

	return 0;
}

void mp32bnrnd(const mp32barrett* b, randomGeneratorContext* rc, mp32number* result)
{
	register uint32  size = b->size;
	register uint32* temp = (uint32*) malloc(size * sizeof(uint32));

	mp32nfree(result);
	mp32nsize(result, size);
	mp32brnd_w(b, rc, result->data, temp);

	free(temp);
}

void mp32bnmulmod(const mp32barrett* b, const mp32number* x, const mp32number* y, mp32number* result)
{
	register uint32  size = b->size;
	register uint32* temp = (uint32*) malloc((4*size+2) * sizeof(uint32));

	/* xsize and ysize must be <= b->size */
	register uint32  fill = 2*size-x->size-y->size;
	register uint32* opnd = temp+size*2+2;

	mp32nfree(result);
	mp32nsize(result, size);

	if (fill)
		mp32zero(fill, opnd);

	mp32mul(opnd+fill, x->size, x->data, y->size, y->data);
	mp32bmod_w(b, opnd, result->data, temp);

	free(temp);
}

void mp32bnsqrmod(const mp32barrett* b, const mp32number* x, mp32number* result)
{
	register uint32  size = b->size;
	register uint32* temp = (uint32*) malloc(size * sizeof(uint32));

	/* xsize must be <= b->size */
	register uint32  fill = 2*(size-x->size);
	register uint32* opnd = temp + size*2+2;

	mp32nfree(result);
	mp32nsize(result, size);

	if (fill)
		mp32zero(fill, opnd);

	mp32sqr(opnd+fill, x->size, x->data);
	mp32bmod_w(b, opnd, result->data, temp);

	free(temp);
}

void mp32bnpowmod(const mp32barrett* b, const mp32number* x, const mp32number* pow, mp32number* y)
{
	register uint32  size = b->size;
	register uint32* temp = (uint32*) malloc((4*size+2) * sizeof(uint32));

	mp32nfree(y);
	mp32nsize(y, size);

	mp32bpowmod_w(b, x->size, x->data, pow->size, pow->data, y->data, temp);

	free(temp);
}

void mp32bnpowmodsld(const mp32barrett* b, const uint32* slide, const mp32number* pow, mp32number* y)
{
	register uint32  size = b->size;
	register uint32* temp = (uint32*) malloc((4*size+2) * sizeof(uint32));

	mp32nfree(y);
	mp32nsize(y, size);

	mp32bpowmodsld_w(b, slide, pow->size, pow->data, y->data, temp);

	free(temp);
}