1 /* A simple static test program. */
2 #include <tfm.h>
3
4 #ifdef GBA_MODE
5 #include <gba.h>
6 #define DISPLAY(x) modetxt_puts(vfb, x, 1)
7 #endif
8
9 #ifndef DISPLAY
10 #define DISPLAY(x) printf(x)
11 #define DISPLAY_P(...) printf(__VA_ARGS__)
12 #else
13 #define DISPLAY_P(...) (void)0
14 #define fp_dump(n,p) do{}while(0)
15 #endif
16
17 #ifndef fp_dump
fp_dump(const char * n,fp_int * p)18 void fp_dump(const char* n, fp_int* p)
19 {
20 int sz;
21 if (fp_radix_size(p, 2, &sz) != FP_OKAY)
22 return;
23 char* str = malloc(sz);
24 if (!str)
25 return;
26 #ifdef STEST_VERBOSE
27 fp_toradix(p, str, 2);
28 DISPLAY_P("%s = 0b%s\n", n, str);
29 fp_toradix(p, str, 16);
30 DISPLAY_P("%s = 0x%s\n", n, str);
31 #endif
32 fp_toradix(p, str, 10);
33 DISPLAY_P("%s = %s\n", n, str);
34 free(str);
35 }
36 #endif
37
38 #ifdef GBA_MODE
c_main(void)39 int c_main(void)
40 #else
41 int main(void)
42 #endif
43 {
44 fp_int a,b,c,d,e,f;
45 fp_digit dp;
46
47 fp_init(&a);
48 fp_init(&b);
49 fp_init(&c);
50 fp_init(&d);
51 fp_init(&e);
52 fp_init(&f);
53
54 #ifdef GBA_MODE
55 install_common();
56 modetxt_init();
57 modetxt_gotoxy(0,0);
58 #endif
59
60 DISPLAY_P("TFM Ident string:\n%s\n\n", fp_ident());
61
62 /* test multiplication */
63 fp_read_radix(&a, "3453534534535345345341230891273", 10);
64 fp_read_radix(&b, "2394873294871238934718923" , 10);
65 fp_read_radix(&c, "8270777629674273015508507050766235312931312159028658979", 10);
66 fp_mul(&a, &b, &d);
67 if (fp_cmp(&c, &d)) {
68 DISPLAY("mul failed\n");
69 return -1;
70 } else {
71 DISPLAY("mul passed\n");
72 }
73
74 /* test multiplication */
75 fp_read_radix(&a, "30481290320498235987349712308523652378643912563478232907782361237864278207235782364578264891274789264278634289739", 10);
76 fp_read_radix(&b, "48761478126387263782638276327836287632836278362837627838736278362923698724823749238732" , 10);
77 fp_read_radix(&c, "1486312771227034563307950634490737985563993459700941115664257275795366623795590136120579100118233580357115074068815507257715906295105536107921754177810976863679300283932188006885811950341132768970948", 10);
78 fp_mul(&a, &b, &d);
79 if (fp_cmp(&c, &d)) {
80 DISPLAY("mul failed\n");
81 return -1;
82 } else {
83 DISPLAY("mul passed\n");
84 }
85
86 /* test multiplication */
87 fp_read_radix(&a, "115792089237316195423570985008687907853269984665640564039457584007913129639935", 10);
88 fp_read_radix(&b, "174224571863520493293247799005065324265471" , 10);
89 fp_read_radix(&c, "20173827172553973356686868531273530268200710714389071377794102651988800859098544338487575161443744102709980552583184385", 10);
90 fp_mul(&a, &b, &d);
91 if (fp_cmp(&c, &d)) {
92 DISPLAY("mul failed\n");
93 return -1;
94 } else {
95 DISPLAY("mul passed\n");
96 }
97
98 /* test squaring */
99 fp_read_radix(&a, "298723982748923478923473927489237289347238947238947238947238972893", 10);
100 fp_read_radix(&b, "89236017869379132235512787068367546521309689412262624434964313994127411682542855190667724226920696163962644836740110835385588789449" , 10);
101 fp_sqr(&a, &c);
102 if (fp_cmp(&c, &b)) {
103 DISPLAY("sqr failed\n");
104 return -1;
105 } else {
106 DISPLAY("sqr passed\n");
107 }
108
109 fp_read_radix(&a, "397823894238973128942895123894327123941724927848927349274897238978927593487012378490184789429812734982738972389", 10);
110 fp_read_radix(&b, "158263850827461677491961439999264901067636282938352531932899298293270945997930087353471903166601507321298827087008336951419604640736464667188494668962822678461626245753696845719301945679092882499787869509090904187704367321" , 10);
111 fp_sqr(&a, &c);
112 if (fp_cmp(&c, &b)) {
113 DISPLAY("sqr failed\n");
114 return -1;
115 } else {
116 DISPLAY("sqr passed\n");
117 }
118
119 fp_read_radix(&a, "13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084095", 10);
120 fp_read_radix(&b, "179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474097562152033539671286128252223189553839160721441767298250321715263238814402734379959506792230903356495130620869925267845538430714092411695463462326211969025" , 10);
121 fp_sqr(&a, &c);
122 if (fp_cmp(&c, &b)) {
123 DISPLAY("sqr failed\n");
124 return -1;
125 } else {
126 DISPLAY("sqr passed\n");
127 }
128
129
130 /* montgomery reductions */
131 fp_read_radix(&a, "234892374892374893489123428937892781237863278637826327367637836278362783627836783678363", 10);
132 fp_read_radix(&b, "4447823492749823749234123489273987393983289319382762756425425425642727352327452374521", 10);
133 #ifdef FP_64BIT
134 fp_read_radix(&c, "942974496560863503657226741422301598807235487941674147660989764036913926327577165648", 10);
135 #else
136 fp_read_radix(&c, "2396271882990732698083317035605836523697277786556053771759862552557086442129695099100", 10);
137 #endif
138 if (fp_montgomery_setup(&b, &dp) != FP_OKAY)
139 DISPLAY("mont setup failed\n");
140 fp_montgomery_reduce(&a, &b, dp);
141 if (fp_cmp(&a, &c)) {
142 DISPLAY("mont failed\n");
143 fp_dump("a (is )", &a);
144 fp_dump("c (should)", &c);
145 return -1;
146 } else {
147 DISPLAY("mont passed\n");
148 }
149
150 fp_read_radix(&a, "2348923748923748934891234456645654645645684576353428937892781237863278637826327367637836278362783627836783678363", 10);
151 fp_read_radix(&b, "444782349274982374923412348927398739398328931938276275642542542564272735232745237452123424324324444121111119", 10);
152 fp_read_radix(&c, "45642613844554582908652603086180267403823312390990082328515008314514368668691233331246183943400359349283420", 10);
153 if (fp_montgomery_setup(&b, &dp) != FP_OKAY)
154 DISPLAY("mont setup failed\n");
155 fp_montgomery_reduce(&a, &b, dp);
156 if (fp_cmp(&a, &c)) {
157 DISPLAY("mont failed\n");
158 fp_dump("a (is )", &a);
159 fp_dump("c (should)", &c);
160 return -1;
161 } else {
162 DISPLAY("mont passed\n");
163 }
164
165 fp_read_radix(&a, "234823424242342923748923748934891234456645654645645684576353424972378234762378623891236834132352375235378462378489378927812378632786378263273676378362783627555555555539568389052478124618461834763837685723645827529034853490580134568947341278498542893481762349723907847892983627836783678363", 10);
166 fp_read_radix(&b, "44478234927456563455982374923412348927398739398328931938276275642485623481638279025465891276312903262837562349056234783648712314678120389173890128905425242424239784256427", 10);
167 fp_read_radix(&c, "33160865265453361650564031464519042126185632333462754084489985719613480783282357410514898819797738034600484519472656152351777186694609218202276509271061460265488348645081", 10);
168 if (fp_montgomery_setup(&b, &dp) != FP_OKAY)
169 DISPLAY("mont setup failed\n");
170 fp_montgomery_reduce(&a, &b, dp);
171 if (fp_cmp(&a, &c)) {
172 DISPLAY("mont failed\n");
173 fp_dump("a (is )", &a);
174 fp_dump("c (should)", &c);
175 return -1;
176 } else {
177 DISPLAY("mont passed\n");
178 }
179
180
181 return 0;
182 }
183
184
185 /* $Source$ */
186 /* $Revision$ */
187 /* $Date$ */
188