]> andersk Git - moira.git/blob - util/gdss/lib/crypto/bignum/c/testKerN.c
initial import of gdss from the Athena source tree
[moira.git] / util / gdss / lib / crypto / bignum / c / testKerN.c
1 /* Copyright     Digital Equipment Corporation & INRIA     1988, 1989 */
2 /* testKerN.c: tests des primitives de KerN                           */
3 /* Last modified_on Mon Mar 26 20:37:00 GMT+2:00 1990 by shand        */
4 /*      modified_on Wed Feb 14 16:14:04 GMT+1:00 1990 by herve        */
5 /*      modified_on 17-OCT-1989 20:35:55.91 by Jim Lawton             */
6
7 /* You can comment the line below if you want to test the C macro Package
8    instead of C or Assembly functions. */
9
10 #define BNNMACROS_OFF 1
11
12
13 #include "BigNum.h"
14 #include "BntoBnn.h"
15
16
17                         /* structure d'un test */
18
19 struct testenv {
20         char    *name;          /* Le nom de la fonction teste'e.        */
21         int     flag;           /* Pour savoir si l'on continue le Test. */
22         char    hist[2048];     /* L'expression qui provoque l'erreur.   */
23         char    *depend;        /* De quoi depend le Test.               */
24 };
25
26
27                         /* Les nombres pre'de'finies. */
28
29 static BigNum           NumbVect[5][2];
30 static BigNum           NumbProto, Ntmp2, NtmpBig;
31
32 #define RN(n)           NumbVect[n][0]
33 #define SN(n)           NumbVect[n][1]
34
35                         /* Taille des nombres utilise's. */
36                         /* de la forme 4(n + 1) */
37 #define TESTLENGTH      16
38 #define DTL             TESTLENGTH/2
39 #define QTL             TESTLENGTH/4
40
41 /* Nombre de test. */
42 int TestCount;
43
44 ResetTest(n) int n; {
45         /* Remet le nieme nombre a` la valeur prototype. */
46         BnAssign(RN(n), 0, NumbProto, 0, TESTLENGTH);
47         BnAssign(SN(n), 0, NumbProto, 0, TESTLENGTH);
48 }
49
50 Check(n) int n; {
51         int i;
52         /* Verifie que les n nombres calcules correspondent aux simule's. */
53         for(i = 0; i < n; i++)
54                 if(CheckSubRange(i, 0, TESTLENGTH)) return(1);
55         return(FALSE);
56 }
57
58 CheckSubRange(x, nd, nl) int x, nd, nl; {
59         /* Verifie l'e'galite' des sous-nombres
60            (RN(x), nd, nl) et (SN(x), nd, nl) */
61         while(nl) {
62                 nl--;
63                 if(BnCompareDigits(RN(x), nd, SN(x), nd)) return(nd + 1);
64                 nd++;
65         }
66         return(FALSE);
67 }
68
69 ShowDiff0(e, r1, r2) struct testenv *e; int r1,r2; {
70         ErrorPrint(e);
71         if(r1 != r2)
72                 printf("---- Result is %d and should be %d----\n", r1, r2);
73         return(e->flag);
74 }
75
76 ShowDiff1(e, r1, r2, n, nd, nl)
77                 struct testenv *e; char *n; int r1, r2, nd, nl; {
78         ErrorPrint(e);
79         if(r1 != r2)
80                 printf("---- Result is %d and should be %d----\n", r1, r2);
81         ShowOutRange(0, n, nd, nl);
82         ShowSubNumber(0, n, nd, nl);
83         return(e->flag);
84 }
85
86 ShowDiff2(e, r1, r2, n, nd, nl, m, md, ml)
87                 struct testenv *e; char *n, *m; int r1, r2, nd, nl, md, ml; {
88         ErrorPrint(e);
89         if(r1 != r2)
90                 printf("---- Result is %d and should be %d----\n", r1, r2);
91         ShowOutRange(0, n, nd, nl);
92         ShowOutRange(1, m, md, ml);
93         ShowSubNumber(0, n, nd, nl);
94         ShowSubNumber(1, m, md, ml);
95         return(e->flag);
96 }
97
98 ShowDiff3(e, r1, r2, n, nd, nl, m, md, ml, o, od, ol)
99                 struct testenv *e; char *n, *m, *o;
100                 int r1, r2, nd, nl, md, ml, od, ol; {
101         ErrorPrint(e);
102         if(r1 != r2)
103                 printf("---- Result is %d and should be %d----\n", r1, r2);
104         ShowOutRange(0, n, nd, nl);
105         ShowOutRange(1, m, md, ml);
106         ShowOutRange(2, o, od, ol);
107         ShowSubNumber(0, n, nd, nl);
108         ShowSubNumber(1, m, md, ml);
109         ShowSubNumber(2, o, od, ol);
110         return(e->flag);
111 }
112
113 ShowDiff4(e, r1, r2, n, nd, nl, m, md, ml, o, od, ol, p, pd, pl)
114                 struct testenv *e; char *n, *m, *o, *p;
115                 int r1, r2, nd, nl, md, ml, od, ol, pd, pl; {
116         ErrorPrint(e);
117         if(r1 != r2)
118                 printf("---- Result is %d and should be %d----\n", r1, r2);
119         ShowOutRange(0, n, nd, nl);
120         ShowOutRange(1, m, md, ml);
121         ShowOutRange(2, o, od, ol);
122         ShowOutRange(3, p, pd, pl);
123         ShowSubNumber(0, n, nd, nl);
124         ShowSubNumber(1, m, md, ml);
125         ShowSubNumber(2, o, od, ol);
126         ShowSubNumber(3, p, pd, pl);
127         return(e->flag);
128 }
129
130 ShowSubNumber(x, n, nd, nl) char *n; int x, nd, nl; {
131         printf("[%s, %d, %d] =  ", n, nd, nl);
132         RangeNumberPrint("", RN(x), nd, nl);
133         if(CheckSubRange(x, nd, nl)) {
134                 RangeNumberPrint(" Before:      ", NumbProto, nd, nl);
135                 RangeNumberPrint(" Simulated:   ", SN(x), nd, nl);
136 }       }
137
138 RangeNumberPrint(s, n, nd, nl) char *s; BigNum n; int nd, nl; {
139         int first = 1;
140
141         /* Ne marche que si BnGetDigit est garanti!!! */
142         printf("%s {", s);
143         while(nl) {
144                 nl--;
145                 if(!first) printf(", "); else first = 0;
146                 if(BN_DIGIT_SIZE <= 16)
147                         printf("%.4X", BnGetDigit(n, nd + nl));
148                 else    printf("%.8X", BnGetDigit(n, nd + nl));
149         }
150         printf("}\n");
151 }
152
153 char *msg = "---- Modification Out of Range of number ";
154 ShowOutRange(x, n, nd, nl) char *n; int x, nd, nl; {
155         int i = 0, bol = 0;
156
157         while(i = CheckSubRange(x, i, TESTLENGTH - i)) {
158                 if((i <= nd) || (i > nd + nl)) {
159                         if(!bol) {
160                                 bol = 1;
161                                 printf("%s %s at index: (%d", msg, n, i - 1);
162                         } else {
163                                 printf(" %d", i - 1);
164         }       }       }
165         if(bol) printf(").\n");
166 }               
167
168 ErrorPrint(e) struct testenv *e; {
169         printf("*** Error in compute : %s\n", e->hist);
170         printf("  Depends on %s\n", e->depend);
171 }
172
173 /*
174  *      Tests des fonctions non redefinisables
175  */
176
177 int genlengthvec[] = {9, 8, 1, 0, 2000, 32000,};
178 BigNumType gentypevec[] = {0, 1, 2, 3, 4, 5,};
179
180 Generique(e) struct testenv *e; {
181         int i;
182         int length, length2;
183         BigNumType type, type2;
184         int fix;
185         BigNum n;
186
187         
188    for(i=0; i < 6; i++) {
189         type = gentypevec[i];
190         length = genlengthvec[i];
191         n = BnCreate(type, length);
192         if((type2 = BnGetType(n)) != type) {
193                 sprintf(e->hist,"BnGetType(BnCreate(%d, %d));", type, length);
194                 if(ShowDiff0(e, type, type2)) return(TRUE);
195         }
196         if((length2 = BnGetSize(n)) != length) {
197                 sprintf(e->hist,"BnGetSize(BnCreate(%d, %d));", type, length);
198                 if(ShowDiff0(e, length, length2)) return(TRUE);
199         }
200         if(BnFree(n) == 0) {
201                 sprintf(e->hist, "BnFree(BnCreate(%d, %d));", type, length);
202                 if(ShowDiff0(e, 1, 0)) return(TRUE);
203         }
204         BnSetType((n = BnAlloc(length)), type);
205         if((type2 = BnGetType(n)) != type) {
206                 sprintf(e->hist,"BnGetType(BnAlloc(%d, %d));", type, length);
207                 if(ShowDiff0(e, type, type2)) return(TRUE);
208         }
209         if((length2 = BnGetSize(n)) != length) {
210                 sprintf(e->hist,"BnGetSize(BnAlloc(%d, %d));", type, length);
211                 if(ShowDiff0(e, length, length2)) return(TRUE);
212         }
213         if(BnFree(n) == 0) {
214                 sprintf(e->hist, "BnFree(BnAlloc(%d, %d));", type, length);
215                 if(ShowDiff0(e, 1, 0)) return(TRUE);
216         }
217    }
218    return(FALSE);
219 }
220
221 /*
222  *      BnSetToZero
223  */
224 ___BnSetToZero___(n, nd, nl) register BigNum n; register int nd, nl; {
225         register int i;
226         for(i=0; i<nl; i++)
227                 BnSetDigit(n, nd + i, 0);
228 }
229
230 TestBnSetToZero(e) struct testenv *e; {
231         int nd, nl;
232
233         e->depend = "()";
234         for(nd = 0; nd <= TESTLENGTH; nd++)
235            for(nl = 0; nl <= TESTLENGTH - nd; nl++) {
236                 TestCount++;
237                 ResetTest(0);
238                    BnSetToZero   (RN(0), nd, nl);
239                 ___BnSetToZero___(SN(0), nd, nl);
240                 if(Check(1)) {
241                         sprintf(e->hist, "%s(n, %d, %d)", e->name, nd, nl);
242                         if(ShowDiff1(e, 0, 0, "n", nd, nl)) return(1);
243         }       }
244         return(FALSE);
245 }
246
247 /*
248  *      BnAssign
249  */
250 ___BnAssign___(m, md, n, nd, nl) BigNum m, n; int md, nd, nl; {
251         BnSetToZero(NtmpBig, 0, nl);
252         BnAdd(NtmpBig, 0, nl, n, nd, nl, 0);
253         BnSetToZero(m, md, nl);
254         BnAdd(m, md, nl, NtmpBig, 0, nl, 0);
255 }
256
257 TestBnAssign(e) struct testenv *e; {
258         int md, nd, nl;
259
260         e->depend = "(BnSetToZero, BnAdd)";
261         for(md = 0; md <= TESTLENGTH; md++)
262           for(nd = 0; nd <= TESTLENGTH; nd++)
263             for(nl=0; ((nl<=TESTLENGTH-nd) && (nl<=TESTLENGTH-md)); nl++) {
264                 TestCount++;
265                 ResetTest(0);
266                    BnAssign   (RN(0), md, RN(0), nd, nl);
267                 ___BnAssign___(SN(0), md, SN(0), nd, nl);
268                 if(Check(1)) {
269                         sprintf(e->hist, "%s(n, %d, n, %d, %d)", e->name,
270                                                 md, nd, nl);
271                         if(ShowDiff1(e, 0, 0, "n", md, nl)) return(1);
272         }       }
273         return(FALSE);
274 }
275
276
277 /*
278  *      BnNumDigits
279  */
280 ___BnNumDigits___(n, nd, nl) register BigNum n; register int nd, nl; {
281
282         while(nl != 0) {
283                 nl--;
284                 if(!BnIsDigitZero(n, nd + nl)) break;
285         }
286         return(nl + 1);
287 }
288
289 TestBnNumDigits(e) struct testenv *e; {
290         int nd0, nl0, nd, nl, l1, l2;
291
292         e->depend = "(BnIsDigitZero)";
293         for(nd0 = 0; nd0 <= TESTLENGTH; nd0++)
294           for(nl0 = 0; nl0 <= TESTLENGTH - nd0; nl0++)
295             for(nd = 0; nd <= TESTLENGTH; nd++)
296               for(nl = 0; nl <= TESTLENGTH - nd; nl++) {
297                 TestCount++;
298                 ResetTest(0);
299                 BnSetToZero(RN(0), nd0, nl0);
300                 BnSetToZero(SN(0), nd0, nl0);
301                 l1 =    BnNumDigits   (RN(0), nd, nl);
302                 l2 = ___BnNumDigits___(SN(0), nd, nl);
303                 if(Check(1) || l1 != l2) {
304                         sprintf(e->hist, "%s(n, %d, %d)", e->name, nd, nl);
305                         if(ShowDiff1(e, l1, l2, "n", nd, nl)) return(1);
306         }       }
307         return(FALSE);
308 }
309
310 /*
311  *      BnNumLeadingZeroBitsInDigit
312  */
313 __BnNumLeadingZeroBitsInDigit__(n, nd) BigNum n; int nd; {
314         int p = 0;
315
316         if(BnIsDigitZero(n, nd)) return(BN_DIGIT_SIZE);
317         BnAssign(Ntmp2, 0, n, nd, 1);
318         BnShiftLeft(Ntmp2, 0, 1, Ntmp2, 1, 1);
319         while(BnIsDigitZero(Ntmp2, 1)) {
320                 BnShiftLeft(Ntmp2, 0, 1, Ntmp2, 1, 1);
321                 p++;
322         }
323         return(p);
324 }
325
326 TestBnNumLeadingZeroBitsInDigit(e) struct testenv *e; {
327         int nd; int l1, l2;
328
329
330         e->depend = "(BnShiftLeft, BnIsDigitZero)";
331         ResetTest(0);
332         for(nd = 0; nd < TESTLENGTH; nd++) {
333                 TestCount++;
334                 l1 =    BnNumLeadingZeroBitsInDigit   (RN(0), nd);
335                 l2 = __BnNumLeadingZeroBitsInDigit__(SN(0), nd);
336                 if(Check(1) || l1 != l2) {
337                         sprintf(e->hist, "%s(n, %d)", e->name, nd);
338                         if(ShowDiff1(e, l1, l2, "n", nd, 1)) return(1);
339         }       }
340         return(FALSE);
341 }
342
343 /*
344  *      BnIsDigitZero
345  */
346 ___BnIsDigitZero___(n, nd) BigNum n; int nd; {
347         if(BnGetDigit(n, nd) == 0) return(1);
348         return(0);
349 }
350
351 TestBnIsDigitZero(e) struct testenv *e; {
352         int nd; int l1, l2;
353
354         e->depend = "()";
355         ResetTest(0);
356         for(nd = 0; nd < TESTLENGTH; nd++) {
357                 TestCount++;
358                 l1 =    BnIsDigitZero   (RN(0), nd);
359                 l2 = ___BnIsDigitZero___(SN(0), nd);
360                 if(Check(1) || ((l1 == 0) && (l2 != 0)) ||
361                                ((l1 != 0) && (l2 == 0))) {
362                         sprintf(e->hist, "%s(n, %d)", e->name, nd);
363                         if(ShowDiff1(e, l1, l2, "n", nd, 1)) return(1);
364         }       }
365         return(FALSE);
366 }
367
368 /*
369  *      BnIsDigitNormalized
370  */
371 ___BnIsDigitNormalized___(n, nd) BigNum n; int nd; {
372         BnAssign(Ntmp2, 0, n, nd, 1);
373         BnShiftLeft(Ntmp2, 0, 1, Ntmp2, 1, 1);
374         if(BnIsDigitZero(Ntmp2, 1)) return(0);
375         return(1);
376 }
377
378 TestBnIsDigitNormalized(e) struct testenv *e; {
379         int nd; int l1, l2;
380
381         e->depend = "(BnShiftLeft, BnIsDigitZero)";
382         ResetTest(0);
383         for(nd = 0; nd < TESTLENGTH; nd++) {
384                 TestCount++;
385                 l1 =    BnIsDigitNormalized   (RN(0), nd);
386                 l2 = ___BnIsDigitNormalized___(SN(0), nd);
387                 if(Check(1) || ((l1 == 0) && (l2 != 0)) ||
388                                ((l1 != 0) && (l2 == 0))) {
389                         sprintf(e->hist, "%s(n, %d)", e->name, nd);
390                         if(ShowDiff1(e, l1, l2, "n", nd, 1)) return(1);
391         }       }
392         return(FALSE);
393 }
394
395 /*
396  *      BnIsDigitOdd
397  */
398 ___BnIsDigitOdd___(n, nd) BigNum n; int nd; {
399         BnAssign(Ntmp2, 0, n, nd, 1);
400         BnShiftRight(Ntmp2, 0, 1, Ntmp2, 1, 1);
401         if(BnIsDigitZero(Ntmp2, 1)) return(0);
402         return(1);
403 }
404
405 TestBnIsDigitOdd(e) struct testenv *e; {
406         int nd; int l1, l2;
407
408         e->depend = "(BnShiftRight, BnIsDigitZero)";
409         ResetTest(0);
410         for(nd = 0; nd < TESTLENGTH; nd++) {
411                 TestCount++;
412                 l1 =    BnIsDigitOdd   (RN(0), nd);
413                 l2 = ___BnIsDigitOdd___(SN(0), nd);
414                 if(Check(1) || ((l1 == 0) && (l2 != 0)) ||
415                                ((l1 != 0) && (l2 == 0))) {
416                         sprintf(e->hist, "%s(n, %d)", e->name, nd);
417                         if(ShowDiff1(e, l1, l2, "n", nd, 1)) return(1);
418         }       }
419         return(FALSE);
420 }
421
422 /*
423  *      BnCompareDigits
424  */
425 ___BnCompareDigits___(n, nd, m, md) BigNum n, m; int nd, md; {
426         BnAssign(Ntmp2, 0, n, nd, 1);
427         BnComplement(Ntmp2, 0, 1);
428         if(BnAdd(Ntmp2, 0, 1, m, md, 1, 0)) return(-1);
429         BnComplement(Ntmp2, 0, 1);
430         if(BnIsDigitZero(Ntmp2, 0)) return(0);
431         return(1);
432 }
433
434 TestBnCompareDigits(e) struct testenv *e; {
435         int nd, md; int l1, l2;
436
437         e->depend = "(BnComplement, BnAdd, BnIsDigitZero)";
438         ResetTest(0);
439         ResetTest(1);
440         for(nd = 0; nd < TESTLENGTH; nd++)
441            for(md = 0; md < TESTLENGTH; md++) {
442                 TestCount++;
443                 l1 =    BnCompareDigits   (RN(0), nd, RN(1), md);
444                 l2 = ___BnCompareDigits___(SN(0), nd, SN(1), md);
445                 if(Check(2) || l1 != l2) {
446                         sprintf(e->hist, "%s(n, %d, m, %d)", e->name, nd, md);
447                         if(ShowDiff2(e, l1, l2, "n", nd, 1, "m", md, 1))
448                                 return(1);
449         }       }
450         return(FALSE);
451 }
452
453 /*
454  *      BnComplement
455  */
456 ___BnComplement___(n, nd, nl) BigNum n; int nd, nl; {
457         int i;
458
459         BnSetDigit(Ntmp2, 0, 0);
460         BnSubtractBorrow(Ntmp2, 0, 1, 0);
461         for(i = 0; i < nl; i++)
462                 BnXorDigits(n, nd + i, Ntmp2, 0);
463 }
464
465 TestBnComplement(e) struct testenv *e; {
466         int nd, nl;
467
468         e->depend = "(BnSubtractBorrow, BnXorDigits)";
469         for(nd = 0; nd <= TESTLENGTH; nd++)
470            for(nl = 0; nl <= TESTLENGTH - nd; nl++) {
471                 TestCount++;
472                 ResetTest(0);
473                    BnComplement   (RN(0), nd, nl);
474                 ___BnComplement___(SN(0), nd, nl);
475                 if(Check(1)) {
476                         sprintf(e->hist, "%s(n, %d, %d)", e->name, nd, nl);
477                         if(ShowDiff1(e, 0, 0, "n", nd, nl)) return(1);
478         }       }
479         return(FALSE);
480 }
481
482 /*
483  *      BnAndDigits
484  */
485 ___BnAndDigits___(n, nd, m, md) BigNum n, m; int nd, md; {
486         BnAssign(Ntmp2, 0, n, nd, 1);
487         BnOrDigits(Ntmp2, 0, m, md);
488         BnXorDigits(Ntmp2, 0, m, md);
489         BnXorDigits(n, nd, Ntmp2, 0);
490 }
491
492 TestBnAndDigits(e) struct testenv *e; {
493         int nd, md;
494
495         e->depend = "(BnOrDigits, BnXorDigits)";
496         ResetTest(1);
497         for(nd = 0; nd < TESTLENGTH; nd++)
498            for(md = 0; md < TESTLENGTH; md++) {
499                 TestCount++;
500                 ResetTest(0);
501                    BnAndDigits   (RN(0), nd, RN(1), md);
502                 ___BnAndDigits___(SN(0), nd, SN(1), md);
503                 if(Check(2)) {
504                         sprintf(e->hist, "%s(n, %d, m, %d)", e->name, nd, md);
505                         if(ShowDiff2(e, 0, 0, "n", nd, 1, "m", md, 1))
506                                 return(1);
507         }       }
508         return(FALSE);
509 }
510
511 /*
512  *      BnOrDigits
513  */
514 ___BnOrDigits___(n, nd, m, md) BigNum n, m; int nd, md; {
515         BnAssign(Ntmp2, 0, n, nd, 1);
516         BnAndDigits(Ntmp2, 0, m, md);
517         BnXorDigits(Ntmp2, 0, m, md);
518         BnXorDigits(n, nd, Ntmp2, 0);
519 }
520
521 TestBnOrDigits(e) struct testenv *e; {
522         int nd, md;
523
524         e->depend = "(BnAndDigits, BnXorDigits)";
525         ResetTest(1);
526         for(nd = 0; nd < TESTLENGTH; nd++)
527            for(md = 0; md < TESTLENGTH; md++) {
528                 TestCount++;
529                 ResetTest(0);
530                    BnOrDigits   (RN(0), nd, RN(1), md);
531                 ___BnOrDigits___(SN(0), nd, SN(1), md);
532                 if(Check(2)) {
533                         sprintf(e->hist, "%s(n, %d, m, %d)", e->name, nd, md);
534                         if(ShowDiff2(e, 0, 0, "n", nd, 1, "m", md, 1))
535                                 return(1);
536         }       }
537         return(FALSE);
538 }
539
540 /*
541  *      BnXorDigits
542  */
543 ___BnXorDigits___(n, nd, m, md) BigNum n, m; int nd, md; {
544         BnAssign(Ntmp2, 0, n, nd, 1);
545         BnAndDigits(Ntmp2, 0, m, md);
546         BnComplement(Ntmp2, 0, 1);
547         BnOrDigits(n, nd, m, md);
548         BnAndDigits(n, nd, Ntmp2, 0);
549 }
550
551 TestBnXorDigits(e) struct testenv *e; {
552         int nd, md;
553
554         e->depend = "(BnAndDigits, BnComplement, BnOrDigits)";
555         ResetTest(1);
556         for(nd = 0; nd < TESTLENGTH; nd++)
557            for(md = 0; md < TESTLENGTH; md++) {
558                 TestCount++;
559                 ResetTest(0);
560                    BnXorDigits   (RN(0), nd, RN(1), md);
561                 ___BnXorDigits___(SN(0), nd, SN(1), md);
562                 if(Check(2)) {
563                         sprintf(e->hist, "%s(n, %d, m, %d)", e->name, nd, md);
564                         if(ShowDiff2(e, 0, 0, "n", nd, 1, "m", md, 1))
565                                 return(1);
566         }       }
567         return(FALSE);
568 }
569
570 /*
571  *      BnShiftLeft
572  */
573 ___BnShiftLeft___(n, nd, nl, m, md, s) BigNum n, m; int nd, nl, md; int s; {
574         BnSetDigit(m, md, 2);
575         BnSetDigit(Ntmp2, 0, 1);
576         while(s--) {
577                 BnSetToZero(NtmpBig, 0, 2);
578                 BnMultiplyDigit(NtmpBig, 0, 2, Ntmp2, 0, 1, m, md);
579                 BnAssign(Ntmp2, 0, NtmpBig, 0, 1);
580         }
581         BnSetToZero(NtmpBig, 0, nl + 1);
582         BnMultiplyDigit(NtmpBig, 0, nl + 1, n, nd, nl, Ntmp2, 0);
583         BnAssign(n, nd, NtmpBig, 0, nl);
584         BnAssign(m, md, NtmpBig, nl, 1);
585 }
586
587 TestBnShiftLeft(e) struct testenv *e; {
588         int nd, nl, md; int s;
589
590         e->depend = "(BnSetToZero, BnMultiplyDigit)";
591         ResetTest(1);
592         for(nd = 0; nd <= TESTLENGTH; nd++)
593           for(nl = 0; nl <= TESTLENGTH - nd; nl++)
594             for(md = 0; md < 2; md++)
595               for(s = 0; s < BN_DIGIT_SIZE; s++) {
596                 TestCount++;
597                 ResetTest(0);
598                    BnShiftLeft   (RN(0), nd, nl, RN(1), md, s);
599                 ___BnShiftLeft___(SN(0), nd, nl, SN(1), md, s);
600                 if(Check(2)) {
601                         sprintf(e->hist, "%s(n, %d, %d, m, %d, %d)",
602                                         e->name, nd, nl, md, s);
603                         if(ShowDiff2(e, 0, 0, "n", nd, nl, "m", md, 1))
604                                 return(1);
605         }       }
606         return(FALSE);
607 }
608
609 /*
610  *      BnShiftRight
611  */
612 ___BnShiftRight___(n, nd, nl, m, md, s) BigNum n, m; int nd, nl, md; int s; {
613         if((nl == 0) || (s == 0)) {
614                 BnSetDigit(m, md, 0);
615                 return;
616         }
617         BnAssign(NtmpBig, 0, n, nd, nl);
618         BnShiftLeft(NtmpBig, 0, nl, NtmpBig, nl, BN_DIGIT_SIZE - s);
619         BnAssign(n, nd, NtmpBig, 1, nl);
620         BnAssign(m, md, NtmpBig, 0, 1);
621 }
622
623 TestBnShiftRight(e) struct testenv *e; {
624         int nd, nl, md; int s;
625
626         e->depend = "(BnShiftLeft)";
627         ResetTest(1);
628         for(nd = 0; nd <= TESTLENGTH; nd++)
629           for(nl = 0; nl <= TESTLENGTH - nd; nl++)
630             for(md = 0; md < 2; md++)
631               for(s = 0; s < BN_DIGIT_SIZE; s++) {
632                 TestCount++;
633                 ResetTest(0);
634                    BnShiftRight   (RN(0), nd, nl, RN(1), md, s);
635                 ___BnShiftRight___(SN(0), nd, nl, SN(1), md, s);
636                 if(Check(2)) {
637                         sprintf(e->hist, "%s(n, %d, %d, m, %d, %d)",
638                                         e->name, nd, nl, md, s);
639                         if(ShowDiff2(e, 0, 0, "n", nd, nl, "m", md, 1))
640                                 return(1);
641         }       }
642         return(FALSE);
643 }
644
645 /*
646  *      BnAddCarry
647  */
648 ___BnAddCarry___(n, nd, nl, r) BigNum n; int nd, nl; int r;{
649         if(r == 0) return(0);
650         BnComplement(n, nd, nl);
651         r = BnSubtractBorrow(n, nd, nl, 0);
652         BnComplement(n, nd, nl);
653         if(r == 0) return(1);
654         return(0);
655 }
656
657 TestBnAddCarry(e) struct testenv *e; {
658         int nd, nl; int r, l1, l2;
659
660         e->depend = "(BnComplement, BnSubtractBorrow)";
661         for(nd = 0; nd <= TESTLENGTH; nd++)
662           for(nl = 0; nl <= TESTLENGTH - nd; nl++)
663             for(r = 0; r < 2; r++) {
664                 TestCount++;
665                 ResetTest(0);
666                 l1 =    BnAddCarry   (RN(0), nd, nl, r);
667                 l2 = ___BnAddCarry___(SN(0), nd, nl, r);
668                 if(Check(1) || l1 != l2) {
669                         sprintf(e->hist, "%s(n, %d, %d, %d)",
670                                         e->name, nd, nl, r);
671                         if(ShowDiff1(e, l1, l2, "n", nd, nl)) return(1);
672         }       }
673         return(FALSE);
674 }
675
676 /*
677  *      BnAdd
678  */
679 ___BnAdd___(n, nd, nl, m, md, ml, r) BigNum n, m; int nd, nl, md, ml; int r;{
680         BnComplement(m, md, ml);
681         r = BnSubtract(n, nd, ml, m, md, ml, r);
682         BnComplement(m, md, ml);
683         return(BnAddCarry(n, nd + ml, nl - ml, r));
684 }
685
686 TestBnAdd(e) struct testenv *e; {
687         int nd, nl, md, ml; int r, l1, l2;
688
689         e->depend = "(BnComplement, BnSubtract, BnAddCarry)";
690         ResetTest(1);
691         for(nd = 0; nd <= TESTLENGTH; nd++)
692          for(nl = 0; nl <= TESTLENGTH - nd; nl++)
693           for(md = 0; md <= TESTLENGTH - nl; md++)
694            for(ml = 0; ml <= nl ; ml++)
695             for(r = 0; r < 2; r++) {
696                 TestCount++;
697                 ResetTest(0);
698                 l1 =    BnAdd   (RN(0), nd, nl, RN(1), md, ml, r);
699                 l2 = ___BnAdd___(SN(0), nd, nl, SN(1), md, ml, r);
700                 if(Check(2) || l1 != l2) {
701                         sprintf(e->hist, "%s(n, %d, %d, m, %d, %d, %d)",
702                                         e->name, nd, nl, md, ml, r);
703                         if(ShowDiff2(e, l1, l2, "n", nd, nl, "m", md, ml))
704                                 return(1);
705         }       }
706         return(FALSE);
707 }
708
709 /*
710  *      BnSubtractBorrow
711  */
712 ___BnSubtractBorrow___(n, nd, nl, r) BigNum n; int nd, nl; int r;{
713         if(r == 1) return(1);
714         BnComplement(n, nd, nl);
715         r = BnAddCarry(n, nd, nl, 1);
716         BnComplement(n, nd, nl);
717         if(r == 0) return(1);
718         return(0);
719 }
720
721 TestBnSubtractBorrow(e) struct testenv *e; {
722         int nd, nl; int r, l1, l2;
723
724         e->depend = "(BnComplement, BnAddCarry)";
725         for(nd = 0; nd <= TESTLENGTH; nd++)
726           for(nl = 0; nl <= TESTLENGTH - nd; nl++)
727             for(r = 0; r < 2; r++) {
728                 TestCount++;
729                 ResetTest(0);
730                 l1 =    BnSubtractBorrow   (RN(0), nd, nl, r);
731                 l2 = ___BnSubtractBorrow___(SN(0), nd, nl, r);
732                 if(Check(1) || l1 != l2) {
733                         sprintf(e->hist, "%s(n, %d, %d, %d)",
734                                         e->name, nd, nl, r);
735                         if(ShowDiff1(e, l1, l2, "n", nd, nl)) return(1);
736         }       }
737         return(FALSE);
738 }
739
740 /*
741  *      BnSubtract
742  */
743 ___BnSubtract___(n, nd, nl, m, md, ml, r) BigNum n, m; int nd, nl, md, ml; int r;{
744         BnComplement(m, md, ml);
745         r = BnAdd(n, nd, ml, m, md, ml, r);
746         BnComplement(m, md, ml);
747         return(BnSubtractBorrow(n, nd + ml, nl - ml, r));
748 }
749
750 TestBnSubtract(e) struct testenv *e; {
751         int nd, nl, md, ml; int r, l1, l2;
752
753         e->depend = "(BnComplement, BnAdd, BnSubtractBorrow)";
754         ResetTest(1);
755         for(nd = 0; nd <= TESTLENGTH; nd++)
756          for(nl = 0; nl <= TESTLENGTH - nd; nl++)
757           for(md = 0; md <= TESTLENGTH - nl; md++)
758            for(ml = 0; ml <= nl ; ml++)
759             for(r = 0; r < 2; r++) {    
760                 TestCount++;
761                 ResetTest(0);
762                 l1 =    BnSubtract   (RN(0), nd, nl, RN(1), md, ml, r);
763                 l2 = ___BnSubtract___(SN(0), nd, nl, SN(1), md, ml, r);
764                 if(Check(2) || l1 != l2) {
765                         sprintf(e->hist, "%s(n, %d, %d, m, %d, %d, %d)",
766                                         e->name, nd, nl, md, ml, r);
767                         if(ShowDiff2(e, l1, l2, "n", nd, nl, "m", md, ml))
768                                 return(1);
769         }       }
770         return(FALSE);
771 }
772
773 /*
774  *      BnMultiplyDigit
775  */
776 ___BnMultiplyDigit___(p, pd, pl, n, nd, nl, m, md) BigNum p, n, m; int pd, pl, nd, nl, md; {
777         int r = 0, ret = 0;
778
779         BnAssign(Ntmp2, 0, m, md, 1);
780         BnAssign(NtmpBig, 0, n, nd, nl);
781         BnSetToZero(NtmpBig, nl, 1);
782         while(!BnIsDigitZero(Ntmp2, 0)) {
783                 if(BnIsDigitOdd(Ntmp2, 0)) {
784                         r = BnAdd(p, pd, pl, NtmpBig, 0, nl + 1, 0);
785                         if((ret == 0) && (r == 1)) ret = 1;
786                         else if((ret == 1) && (r == 1)) ret = 2;
787                 }
788                 BnShiftRight(Ntmp2, 0, 1, Ntmp2, 1, 1);
789                 BnShiftLeft(NtmpBig, 0, nl + 1, Ntmp2, 1, 1);
790                 if(!BnIsDigitZero(Ntmp2, 1)) ret = 3;
791         }
792         return(ret);
793 }
794
795 TestBnMultiplyDigit(e) struct testenv *e; {
796         int pd, pl, nd, nl, md; int l1, l2;
797
798         e->depend = "(BnSetToZero, BnIsDigitZero, BnIsDigitOdd, BnAdd, BnShiftRight, BnShiftLeft)";
799         ResetTest(1);
800         ResetTest(2);
801         for(pd = 0; pd <= TESTLENGTH; pd++)
802          for(pl = 0; pl <= TESTLENGTH - pd; pl++)
803           for(nd = 0; nd <= TESTLENGTH - pl; nd++)
804            for(nl = 0; nl < pl ; nl++)
805             for(md = 0; md < TESTLENGTH; md++) {
806                 TestCount++;
807                 ResetTest(0);
808                 l1 =    BnMultiplyDigit   (RN(0),pd,pl,RN(1),nd,nl,RN(2),md);
809                 l2 = ___BnMultiplyDigit___(SN(0),pd,pl,SN(1),nd,nl,SN(2),md);
810                 if(Check(3) || l1 != l2) {
811                         sprintf(e->hist,
812                            "BnMultiplyDigit(p, %d, %d, n, %d, %d, m, %d)",
813                                                 pd, pl, nd, nl, md);
814                         if(ShowDiff3(e,l1,l2,"p",pd,pl,"n",nd,nl,"m",md,1))
815                                  return(1);
816         }       }
817         return(FALSE);
818 }
819
820 /*
821  *      BnDivideDigit
822  */
823 TestBnDivideDigit(e) struct testenv *e; {
824         int nd, nl, md, qd, rd, l2;
825
826         e->depend = "(BnSetToZero, BnMultiplyDigit, BnCompareDigits)";
827         ResetTest(2);
828         ResetTest(3);
829         for(nd = 0; nd <= TESTLENGTH - 2; nd++)
830          for(nl = 2; nl <= TESTLENGTH - nd; nl++)
831           for(md = 0; md < TESTLENGTH; md++)
832            for(qd = 0; qd < TESTLENGTH - nl + 1 ; qd++)
833             for(rd = 0; rd < 2; rd++)
834              if((!BnIsDigitZero(RN(3), md)) &&
835                         (BnCompareDigits(RN(2), nd+nl-1, RN(3), md) == -1)) {
836                 TestCount++;
837                 ResetTest(0);
838                 ResetTest(1);
839                 BnDivideDigit(RN(0), qd, RN(1), rd, RN(2), nd, nl, RN(3), md);
840                 BnAssign(SN(0), qd, RN(0), qd, nl - 1);
841                 BnAssign(SN(1), rd, RN(1), rd, 1);
842                 BnSetToZero(SN(2), nd, nl);
843                 BnAssign(SN(2), nd, SN(1), rd, 1);
844                 l2 = BnMultiplyDigit(SN(2),nd,nl, SN(0),qd,nl - 1, SN(3), md);
845                 if(Check(4) || l2 != 0) {
846                         sprintf(e->hist,
847                            "BnDivideDigit(q, %d, r, %d, n, %d, %d, m, %d)",
848                                                 qd, rd, nd, nl, md);
849                         if(ShowDiff4(e, 0, l2, "q", qd, nl - 1, "r", rd, 1,
850                                         "n", nd, nl, "m", md, 1))
851                                 return(TRUE);
852         }       }
853         return(FALSE);
854 }
855
856 /*
857  *      BnMultiply
858  */
859 ___BnMultiply___(p, pd, pl, m, md, ml, n, nd, nl) BigNum p, m, n; int pd, pl, md, ml, nd, nl; {
860         int ret;
861
862         for (ret = 0; nl-- > 0; pd++, nd++, pl--)
863            ret += BnMultiplyDigit (p, pd, pl, m, md, ml, n, nd);
864         return(ret);
865 }
866
867 TestBnMultiply(e) struct testenv *e; {
868         int pd, pl, nd, nl, md, ml; int l1, l2;
869
870         e->depend = "(BnSetToZero, BnMultiplyDigit)";
871         ResetTest(1);
872         ResetTest(2);
873         for(pd = 0; pd <= TESTLENGTH; pd++)
874          for(pl = 0; pl <= TESTLENGTH - pd && pl <= TESTLENGTH/2; pl++)
875           for(nd = 0; nd <= TESTLENGTH - pl; nd++)
876            for(nl = 0; nl < pl && nl <= TESTLENGTH/3; nl++)
877            {
878             if (nl <= pl-nl)
879             {
880                 /* Test squaring */
881                 TestCount++;
882                 ResetTest(0);
883                 l1 =    BnMultiply   (RN(0),pd,pl,RN(1),nd,nl,RN(1),nd,nl);
884                 l2 = ___BnMultiply___(SN(0),pd,pl,SN(1),nd,nl,SN(1),nd,nl);
885                 if(Check(3) || l1 != l2) {
886                         sprintf(e->hist,
887                            "BnMultiply(p, %d, %d, n, %d, %d, n, %d, %d)",
888                                                 pd, pl, nd, nl, nd, nl);
889                         if(ShowDiff3(e,l1,l2,"p",pd,pl,"n",nd,nl,"n",nd,nl))
890                                  return(1);
891                 }
892
893             }
894             for(md = 0; md <= TESTLENGTH; md++) 
895              for (ml = 0; ml <= pl-nl && ml <= TESTLENGTH/3 && md+ml <= TESTLENGTH; ml++) {
896                 TestCount++;
897                 ResetTest(0);
898                 l1 =    BnMultiply   (RN(0),pd,pl,RN(1),nd,nl,RN(2),md,ml);
899                 l2 = ___BnMultiply___(SN(0),pd,pl,SN(1),nd,nl,SN(2),md,ml);
900                 if(Check(3) || l1 != l2) {
901                         sprintf(e->hist,
902                            "BnMultiply(p, %d, %d, n, %d, %d, m, %d, %d)",
903                                                 pd, pl, nd, nl, md, ml);
904                         if(ShowDiff3(e,l1,l2,"p",pd,pl,"n",nd,nl,"m",md,ml))
905                                  return(1);
906         }  }    }
907         return(FALSE);
908 }
909
910 /*
911  *      Main
912  */
913 typedef struct {
914         int     (*TestFnt)();
915         char    *NameFnt;
916 } TESTONE;
917 TESTONE AllTest[] = {
918         Generique,                              "Generic Functions",
919         TestBnSetToZero,                        "BnSetToZero",
920         TestBnAssign,                           "BnAssign",
921         TestBnNumDigits,                        "BnNumDigits",
922         TestBnNumLeadingZeroBitsInDigit,        "BnNumLeadingZeroBitsInDigit",
923         TestBnIsDigitZero,                      "BnIsDigitZero",
924         TestBnIsDigitNormalized,                "BnIsDigitNormalized",
925         TestBnIsDigitOdd,                       "BnIsDigitOdd",
926         TestBnCompareDigits,                    "BnCompareDigits",
927         TestBnComplement,                       "BnComplement",
928         TestBnAndDigits,                        "BnAndDigits",
929         TestBnOrDigits,                         "BnOrDigits",
930         TestBnXorDigits,                        "BnXorDigits",
931         TestBnShiftLeft,                        "BnShiftLeft",
932         TestBnShiftRight,                       "BnShiftRight",
933         TestBnAddCarry,                         "BnAddCarry",
934         TestBnAdd,                              "BnAdd",
935         TestBnSubtractBorrow,                   "BnSubtractBorrow",
936         TestBnSubtract,                         "BnSubtract",
937         TestBnMultiplyDigit,                    "BnMultiplyDigit",
938         TestBnDivideDigit,                      "BnDivideDigit",
939         TestBnMultiply,                         "BnMultiply",
940 };
941
942 main(n, s) int n; char **s; {
943         struct testenv realenv, *e = &realenv;
944         int i, j, nbtest, SizeAllTest;
945
946         /* Initialisations de l'environnement de test. */
947         e->flag = 1;
948         e->depend = "()";
949         /* Allocation des 2 nombres globaux. */
950         Ntmp2 = BnAlloc(2);
951         NtmpBig = BnAlloc(2 * TESTLENGTH);
952         NumbProto = BnAlloc(TESTLENGTH);
953         /* Creation du nombre prototype. */
954         BnSetDigit(NumbProto, 0, 0);            /* Les 2 premiers a` ze'ro. */
955         BnSetDigit(NumbProto, 1, 0);
956         for(i=0; i < TESTLENGTH/4 - 1; i++)     /* Le premier quart est la */
957                 BnSetDigit(NumbProto, i + 2, i + 1);    /* suite 1, 2, 3, ...      */
958         /* Le 2nd quart est le 1er shifte de BN_DIGIT_SIZE - 2. 0x4000 0x8000 ...*/
959         BnAssign(NumbProto, QTL + 1, NumbProto, 2, QTL - 1);
960         BnShiftLeft(NumbProto, QTL + 1, QTL - 1, NumbProto, 0, BN_DIGIT_SIZE - 2);
961         /* La 2nd moitie est l'inverse logique de la 1ere */
962         BnAssign(NumbProto, DTL, NumbProto, 0, DTL);
963         BnComplement(NumbProto, DTL, DTL);
964         /* Allocation des nombres utilise's */
965         for(i=0; i < 5; i++) {
966                 RN(i) = BnAlloc(TESTLENGTH);
967                 SN(i) = BnAlloc(TESTLENGTH);
968         }
969         if(n == 1) {
970                 printf("%s [v|a|TestNum]\n", s[0]);
971         }
972         /* On y va */
973         SizeAllTest = (sizeof(AllTest)/sizeof(AllTest[0]));
974         for(i = 1; i < n; i++) {
975                 if(s[i][0] == 'm') {
976                         /* 0 = No skip; 1 = skip to next; else STOP */
977                         e->flag = atoi(&s[i][1]);
978                 } else if(s[i][0] == 'a') {
979                         for(i = 0; i < SizeAllTest; i++)
980                                 dotest(e, i);
981                 } else if(s[i][0] == 'v') {
982                         for(j = 0; j < SizeAllTest; j++)
983                                 seetest(j);
984                 } else {
985                         nbtest = atoi(s[i]);
986                         if((nbtest < 0) || (nbtest >= SizeAllTest))
987                                 printf("Test %d is invalid\n", nbtest);
988                         else    dotest(e, nbtest);
989 }       }       }
990
991 dotest(e, n) struct testenv *e; int n; {
992         seetest(n);
993         TestCount = 0;
994         e->name = AllTest[n].NameFnt;
995         if(((*(AllTest[n].TestFnt)) (e)) && e->flag > 1) exit(0);
996         printf("%d tests were performed\n", TestCount);
997 }
998
999 seetest(n) int n; {
1000         printf("%d.     Testing %s\n", n, AllTest[n].NameFnt);
1001 }
1002
This page took 0.164467 seconds and 5 git commands to generate.